Facade.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Facades; |
| 4 | |
| 5 | use Exception; |
| 6 | use ReflectionMethod; |
| 7 | use RuntimeException; |
| 8 | use WPStaging\Core\WPStaging; |
| 9 | |
| 10 | /** |
| 11 | * Class Facade |
| 12 | * |
| 13 | * As the name suggest it works and behaves as a Laravel Facade but without the mockery |
| 14 | * It still has swapInstance static method to make mocking easy |
| 15 | * |
| 16 | * @package WPStaging\Framework\Facades |
| 17 | */ |
| 18 | abstract class Facade |
| 19 | { |
| 20 | protected static $facadeInstances = []; |
| 21 | |
| 22 | /** |
| 23 | * Caution: Use in testing Only |
| 24 | * It replace the current instance with the given instance and return old instance |
| 25 | * @param self $instance |
| 26 | * @return self |
| 27 | * @throws RuntimeException |
| 28 | */ |
| 29 | public static function swapInstance($instance) |
| 30 | { |
| 31 | $oldInstance = static::$facadeInstances[static::getFacadeAccessor()]; |
| 32 | static::setInstance($instance); |
| 33 | return $oldInstance; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Caution: Use in testing Only |
| 38 | * @param self $instance |
| 39 | * @throws RuntimeException |
| 40 | */ |
| 41 | public static function setInstance($instance) |
| 42 | { |
| 43 | $class = static::getFacadeAccessor(); |
| 44 | if ($instance instanceof $class) { |
| 45 | static::$facadeInstances[static::getFacadeAccessor()] = $instance; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | throw new RuntimeException('Given instance is not an instance of ' . $class); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param string $method |
| 54 | * @param array $args |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public static function __callStatic($method, $args) |
| 58 | { |
| 59 | $instance = static::getInstance(); |
| 60 | |
| 61 | if ($instance === null) { |
| 62 | throw new RuntimeException('A facade instance cannot be created!'); |
| 63 | } |
| 64 | |
| 65 | if (!method_exists($instance, $method)) { |
| 66 | throw new RuntimeException('Method does not exists!'); |
| 67 | } |
| 68 | |
| 69 | $reflection = new ReflectionMethod($instance, $method); |
| 70 | if (!$reflection->isPublic()) { |
| 71 | throw new RuntimeException('Can only call a public method!'); |
| 72 | } |
| 73 | |
| 74 | return $instance->$method(...$args); |
| 75 | } |
| 76 | |
| 77 | protected static function createInstance() |
| 78 | { |
| 79 | try { |
| 80 | static::$facadeInstances[static::getFacadeAccessor()] = WPStaging::make(static::getFacadeAccessor()); |
| 81 | } catch (Exception $ex) { |
| 82 | static::$facadeInstances[static::getFacadeAccessor()] = null; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** @return self */ |
| 87 | protected static function getInstance() |
| 88 | { |
| 89 | if (!isset(static::$facadeInstances[static::getFacadeAccessor()]) || static::$facadeInstances[static::getFacadeAccessor()] === null) { |
| 90 | static::createInstance(); |
| 91 | } |
| 92 | |
| 93 | return static::$facadeInstances[static::getFacadeAccessor()]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the registered name of the component. |
| 98 | * |
| 99 | * @return string |
| 100 | * |
| 101 | * @throws RuntimeException |
| 102 | */ |
| 103 | protected static function getFacadeAccessor() |
| 104 | { |
| 105 | throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); |
| 106 | } |
| 107 | } |
| 108 |