LegacyProxy.php
100 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 \Psr\Container\ContainerInterface; |
| 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 | * @param string $class_name The name of the class to get an instance for. |
| 31 | * @param mixed ...$args Parameters to be passed to the class constructor or to the appropriate internal 'get_instance_of_' method. |
| 32 | * |
| 33 | * @return object The instance of the class. |
| 34 | * @throws \Exception The requested class belongs to the `src` directory, or there was an error creating an instance of the class. |
| 35 | */ |
| 36 | public function get_instance_of( string $class_name, ...$args ) { |
| 37 | if ( false !== strpos( $class_name, '\\' ) ) { |
| 38 | throw new \Exception( |
| 39 | 'The LegacyProxy class is not intended for getting instances of classes in the src directory, please use ' . |
| 40 | Definition::INJECTION_METHOD . ' method injection or the instance of ' . ContainerInterface::class . ' for that.' |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | // If a class has a dedicated method to obtain a instance, use it. |
| 45 | $method = 'get_instance_of_' . strtolower( $class_name ); |
| 46 | if ( method_exists( __CLASS__, $method ) ) { |
| 47 | return $this->$method( ...$args ); |
| 48 | } |
| 49 | |
| 50 | // If the class is a singleton, use the "instance" method. |
| 51 | if ( method_exists( $class_name, 'instance' ) ) { |
| 52 | return $class_name::instance( ...$args ); |
| 53 | } |
| 54 | |
| 55 | // If the class has a "load" method, use it. |
| 56 | if ( method_exists( $class_name, 'load' ) ) { |
| 57 | return $class_name::load( ...$args ); |
| 58 | } |
| 59 | |
| 60 | // Fallback to simply creating a new instance of the class. |
| 61 | return new $class_name( ...$args ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get an instance of a class implementing WC_Queue_Interface. |
| 66 | * |
| 67 | * @return \WC_Queue_Interface The instance. |
| 68 | */ |
| 69 | private function get_instance_of_wc_queue_interface() { |
| 70 | return \WC_Queue::instance(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Call a user function. This should be used to execute any non-idempotent function, especially |
| 75 | * those in the `includes` directory or provided by WordPress. |
| 76 | * |
| 77 | * @param string $function_name The function to execute. |
| 78 | * @param mixed ...$parameters The parameters to pass to the function. |
| 79 | * |
| 80 | * @return mixed The result from the function. |
| 81 | */ |
| 82 | public function call_function( $function_name, ...$parameters ) { |
| 83 | return call_user_func_array( $function_name, $parameters ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Call a static method in a class. This should be used to execute any non-idempotent method in classes |
| 88 | * from the `includes` directory. |
| 89 | * |
| 90 | * @param string $class_name The name of the class containing the method. |
| 91 | * @param string $method_name The name of the method. |
| 92 | * @param mixed ...$parameters The parameters to pass to the method. |
| 93 | * |
| 94 | * @return mixed The result from the method. |
| 95 | */ |
| 96 | public function call_static( $class_name, $method_name, ...$parameters ) { |
| 97 | return call_user_func_array( "$class_name::$method_name", $parameters ); |
| 98 | } |
| 99 | } |
| 100 |