LegacyProxy.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * LegacyProxy class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Proxies; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\DependencyManagement\Definition; |
| 9 | use Automattic\WooCommerce\Utilities\StringUtil; |
| 10 | use Automattic\WooCommerce\Vendor\Psr\Container\ContainerInterface; |
| 11 | |
| 12 | /** |
| 13 | * Proxy class to access legacy WooCommerce functionality. |
| 14 | * |
| 15 | * This class should be used to interact with code outside the `src` directory, especially functions and classes |
| 16 | * in the `includes` directory, unless a more specific proxy exists for the functionality at hand (e.g. `ActionsProxy`). |
| 17 | * Idempotent functions can be executed directly. |
| 18 | */ |
| 19 | class LegacyProxy { |
| 20 | |
| 21 | /** |
| 22 | * Gets an instance of a given legacy class. |
| 23 | * This must not be used to get instances of classes in the `src` directory. |
| 24 | * |
| 25 | * If a given class needs a special procedure to get an instance of it, |
| 26 | * please add a private get_instance_of_(lowercased_class_name) and it will be |
| 27 | * automatically invoked. See also how objects of classes having a static `instance` |
| 28 | * method are retrieved, similar approaches can be used as needed to make use |
| 29 | * of existing factory methods such as e.g. 'load'. |
| 30 | * |
| 31 | * @param string $class_name The name of the class to get an instance for. |
| 32 | * @param mixed ...$args Parameters to be passed to the class constructor or to the appropriate internal 'get_instance_of_' method. |
| 33 | * |
| 34 | * @return object The instance of the class. |
| 35 | * @throws \Exception The requested class has a namespace starting with ' Automattic\WooCommerce', or there was an error creating an instance of the class. |
| 36 | */ |
| 37 | public function get_instance_of( string $class_name, ...$args ) { |
| 38 | if ( StringUtil::starts_with( $class_name, 'Automattic\\WooCommerce\\' ) ) { |
| 39 | throw new \Exception( |
| 40 | 'The LegacyProxy class is not intended for getting instances of classes whose namespace starts with \'Automattic\\WooCommerce\', please use ' . |
| 41 | Definition::INJECTION_METHOD . ' method injection or the instance of ' . ContainerInterface::class . ' for that.' |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | // If a class has a dedicated method to obtain a instance, use it. |
| 46 | $method = 'get_instance_of_' . strtolower( $class_name ); |
| 47 | if ( method_exists( __CLASS__, $method ) ) { |
| 48 | return $this->$method( ...$args ); |
| 49 | } |
| 50 | |
| 51 | // If the class is a singleton, use the "instance" method. |
| 52 | if ( method_exists( $class_name, 'instance' ) ) { |
| 53 | return $class_name::instance( ...$args ); |
| 54 | } |
| 55 | |
| 56 | // If the class has a "load" method, use it. |
| 57 | if ( method_exists( $class_name, 'load' ) ) { |
| 58 | return $class_name::load( ...$args ); |
| 59 | } |
| 60 | |
| 61 | // Fallback to simply creating a new instance of the class. |
| 62 | return new $class_name( ...$args ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get an instance of a class implementing WC_Queue_Interface. |
| 67 | * |
| 68 | * @return \WC_Queue_Interface The instance. |
| 69 | */ |
| 70 | private function get_instance_of_wc_queue_interface() { |
| 71 | return \WC_Queue::instance(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Call a user function. This should be used to execute any non-idempotent function, especially |
| 76 | * those in the `includes` directory or provided by WordPress. |
| 77 | * |
| 78 | * @param string $function_name The function to execute. |
| 79 | * @param mixed ...$parameters The parameters to pass to the function. |
| 80 | * |
| 81 | * @return mixed The result from the function. |
| 82 | */ |
| 83 | public function call_function( $function_name, ...$parameters ) { |
| 84 | return call_user_func_array( $function_name, $parameters ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Call a static method in a class. This should be used to execute any non-idempotent method in classes |
| 89 | * from the `includes` directory. |
| 90 | * |
| 91 | * @param string $class_name The name of the class containing the method. |
| 92 | * @param string $method_name The name of the method. |
| 93 | * @param mixed ...$parameters The parameters to pass to the method. |
| 94 | * |
| 95 | * @return mixed The result from the method. |
| 96 | */ |
| 97 | public function call_static( $class_name, $method_name, ...$parameters ) { |
| 98 | return call_user_func_array( "$class_name::$method_name", $parameters ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the value of a global. |
| 103 | * |
| 104 | * @param string $global_name The name of the global to get the value for. |
| 105 | * @return mixed The value of the global. |
| 106 | */ |
| 107 | public function get_global( string $global_name ) { |
| 108 | return $GLOBALS[ $global_name ]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Terminates execution of the script. |
| 113 | * |
| 114 | * @param int|string $status An error code to be returned, or an error message to be shown. |
| 115 | * @return void |
| 116 | */ |
| 117 | public function exit( $status = '' ) { |
| 118 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 119 | exit( $status ); |
| 120 | } |
| 121 | } |
| 122 |