PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Facades / Facade.php
wp-staging / Framework / Facades Last commit date
Escape.php 3 years ago Facade.php 3 years ago Hooks.php 2 years ago Sanitize.php 3 years ago
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