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