1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,239 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\Support\Facades; |
|
4 |
- |
|
5 |
-use Closure; |
|
6 |
-use Mockery; |
|
7 |
-use RuntimeException; |
|
8 |
-use Mockery\MockInterface; |
|
9 |
- |
|
10 |
-abstract class Facade |
|
11 |
-{ |
|
12 |
- /** |
|
13 |
- * The application instance being facaded. |
|
14 |
- * |
|
15 |
- * @var \Illuminate\Contracts\Foundation\Application |
|
16 |
- */ |
|
17 |
- protected static $app; |
|
18 |
- |
|
19 |
- /** |
|
20 |
- * The resolved object instances. |
|
21 |
- * |
|
22 |
- * @var array |
|
23 |
- */ |
|
24 |
- protected static $resolvedInstance; |
|
25 |
- |
|
26 |
- /** |
|
27 |
- * Run a Closure when the facade has been resolved. |
|
28 |
- * |
|
29 |
- * @param \Closure $callback |
|
30 |
- * @return void |
|
31 |
- */ |
|
32 |
- public static function resolved(Closure $callback) |
|
33 |
- { |
|
34 |
- static::$app->afterResolving(static::getFacadeAccessor(), function ($service) use ($callback) { |
|
35 |
- $callback($service); |
|
36 |
- }); |
|
37 |
- } |
|
38 |
- |
|
39 |
- /** |
|
40 |
- * Convert the facade into a Mockery spy. |
|
41 |
- * |
|
42 |
- * @return \Mockery\MockInterface |
|
43 |
- */ |
|
44 |
- public static function spy() |
|
45 |
- { |
|
46 |
- if (! static::isMock()) { |
|
47 |
- $class = static::getMockableClass(); |
|
48 |
- |
|
49 |
- return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) { |
|
50 |
- static::swap($spy); |
|
51 |
- }); |
|
52 |
- } |
|
53 |
- } |
|
54 |
- |
|
55 |
- /** |
|
56 |
- * Initiate a mock expectation on the facade. |
|
57 |
- * |
|
58 |
- * @return \Mockery\Expectation |
|
59 |
- */ |
|
60 |
- public static function shouldReceive() |
|
61 |
- { |
|
62 |
- $name = static::getFacadeAccessor(); |
|
63 |
- |
|
64 |
- $mock = static::isMock() |
|
65 |
- ? static::$resolvedInstance[$name] |
|
66 |
- : static::createFreshMockInstance(); |
|
67 |
- |
|
68 |
- return $mock->shouldReceive(...func_get_args()); |
|
69 |
- } |
|
70 |
- |
|
71 |
- /** |
|
72 |
- * Create a fresh mock instance for the given class. |
|
73 |
- * |
|
74 |
- * @return \Mockery\Expectation |
|
75 |
- */ |
|
76 |
- protected static function createFreshMockInstance() |
|
77 |
- { |
|
78 |
- return tap(static::createMock(), function ($mock) { |
|
79 |
- static::swap($mock); |
|
80 |
- |
|
81 |
- $mock->shouldAllowMockingProtectedMethods(); |
|
82 |
- }); |
|
83 |
- } |
|
84 |
- |
|
85 |
- /** |
|
86 |
- * Create a fresh mock instance for the given class. |
|
87 |
- * |
|
88 |
- * @return \Mockery\MockInterface |
|
89 |
- */ |
|
90 |
- protected static function createMock() |
|
91 |
- { |
|
92 |
- $class = static::getMockableClass(); |
|
93 |
- |
|
94 |
- return $class ? Mockery::mock($class) : Mockery::mock(); |
|
95 |
- } |
|
96 |
- |
|
97 |
- /** |
|
98 |
- * Determines whether a mock is set as the instance of the facade. |
|
99 |
- * |
|
100 |
- * @return bool |
|
101 |
- */ |
|
102 |
- protected static function isMock() |
|
103 |
- { |
|
104 |
- $name = static::getFacadeAccessor(); |
|
105 |
- |
|
106 |
- return isset(static::$resolvedInstance[$name]) && |
|
107 |
- static::$resolvedInstance[$name] instanceof MockInterface; |
|
108 |
- } |
|
109 |
- |
|
110 |
- /** |
|
111 |
- * Get the mockable class for the bound instance. |
|
112 |
- * |
|
113 |
- * @return string|null |
|
114 |
- */ |
|
115 |
- protected static function getMockableClass() |
|
116 |
- { |
|
117 |
- if ($root = static::getFacadeRoot()) { |
|
118 |
- return get_class($root); |
|
119 |
- } |
|
120 |
- } |
|
121 |
- |
|
122 |
- /** |
|
123 |
- * Hotswap the underlying instance behind the facade. |
|
124 |
- * |
|
125 |
- * @param mixed $instance |
|
126 |
- * @return void |
|
127 |
- */ |
|
128 |
- public static function swap($instance) |
|
129 |
- { |
|
130 |
- static::$resolvedInstance[static::getFacadeAccessor()] = $instance; |
|
131 |
- |
|
132 |
- if (isset(static::$app)) { |
|
133 |
- static::$app->instance(static::getFacadeAccessor(), $instance); |
|
134 |
- } |
|
135 |
- } |
|
136 |
- |
|
137 |
- /** |
|
138 |
- * Get the root object behind the facade. |
|
139 |
- * |
|
140 |
- * @return mixed |
|
141 |
- */ |
|
142 |
- public static function getFacadeRoot() |
|
143 |
- { |
|
144 |
- return static::resolveFacadeInstance(static::getFacadeAccessor()); |
|
145 |
- } |
|
146 |
- |
|
147 |
- /** |
|
148 |
- * Get the registered name of the component. |
|
149 |
- * |
|
150 |
- * @return string |
|
151 |
- * |
|
152 |
- * @throws \RuntimeException |
|
153 |
- */ |
|
154 |
- protected static function getFacadeAccessor() |
|
155 |
- { |
|
156 |
- throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); |
|
157 |
- } |
|
158 |
- |
|
159 |
- /** |
|
160 |
- * Resolve the facade root instance from the container. |
|
161 |
- * |
|
162 |
- * @param object|string $name |
|
163 |
- * @return mixed |
|
164 |
- */ |
|
165 |
- protected static function resolveFacadeInstance($name) |
|
166 |
- { |
|
167 |
- if (is_object($name)) { |
|
168 |
- return $name; |
|
169 |
- } |
|
170 |
- |
|
171 |
- if (isset(static::$resolvedInstance[$name])) { |
|
172 |
- return static::$resolvedInstance[$name]; |
|
173 |
- } |
|
174 |
- |
|
175 |
- return static::$resolvedInstance[$name] = static::$app[$name]; |
|
176 |
- } |
|
177 |
- |
|
178 |
- /** |
|
179 |
- * Clear a resolved facade instance. |
|
180 |
- * |
|
181 |
- * @param string $name |
|
182 |
- * @return void |
|
183 |
- */ |
|
184 |
- public static function clearResolvedInstance($name) |
|
185 |
- { |
|
186 |
- unset(static::$resolvedInstance[$name]); |
|
187 |
- } |
|
188 |
- |
|
189 |
- /** |
|
190 |
- * Clear all of the resolved instances. |
|
191 |
- * |
|
192 |
- * @return void |
|
193 |
- */ |
|
194 |
- public static function clearResolvedInstances() |
|
195 |
- { |
|
196 |
- static::$resolvedInstance = []; |
|
197 |
- } |
|
198 |
- |
|
199 |
- /** |
|
200 |
- * Get the application instance behind the facade. |
|
201 |
- * |
|
202 |
- * @return \Illuminate\Contracts\Foundation\Application |
|
203 |
- */ |
|
204 |
- public static function getFacadeApplication() |
|
205 |
- { |
|
206 |
- return static::$app; |
|
207 |
- } |
|
208 |
- |
|
209 |
- /** |
|
210 |
- * Set the application instance. |
|
211 |
- * |
|
212 |
- * @param \Illuminate\Contracts\Foundation\Application $app |
|
213 |
- * @return void |
|
214 |
- */ |
|
215 |
- public static function setFacadeApplication($app) |
|
216 |
- { |
|
217 |
- static::$app = $app; |
|
218 |
- } |
|
219 |
- |
|
220 |
- /** |
|
221 |
- * Handle dynamic, static calls to the object. |
|
222 |
- * |
|
223 |
- * @param string $method |
|
224 |
- * @param array $args |
|
225 |
- * @return mixed |
|
226 |
- * |
|
227 |
- * @throws \RuntimeException |
|
228 |
- */ |
|
229 |
- public static function __callStatic($method, $args) |
|
230 |
- { |
|
231 |
- $instance = static::getFacadeRoot(); |
|
232 |
- |
|
233 |
- if (! $instance) { |
|
234 |
- throw new RuntimeException('A facade root has not been set.'); |
|
235 |
- } |
|
236 |
- |
|
237 |
- return $instance->$method(...$args); |
|
238 |
- } |
|
239 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,239 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\Support\Facades; |
|
4 |
+ |
|
5 |
+use Closure; |
|
6 |
+use Mockery; |
|
7 |
+use RuntimeException; |
|
8 |
+use Mockery\MockInterface; |
|
9 |
+ |
|
10 |
+abstract class Facade |
|
11 |
+{ |
|
12 |
+ /** |
|
13 |
+ * The application instance being facaded. |
|
14 |
+ * |
|
15 |
+ * @var \Illuminate\Contracts\Foundation\Application |
|
16 |
+ */ |
|
17 |
+ protected static $app; |
|
18 |
+ |
|
19 |
+ /** |
|
20 |
+ * The resolved object instances. |
|
21 |
+ * |
|
22 |
+ * @var array |
|
23 |
+ */ |
|
24 |
+ protected static $resolvedInstance; |
|
25 |
+ |
|
26 |
+ /** |
|
27 |
+ * Run a Closure when the facade has been resolved. |
|
28 |
+ * |
|
29 |
+ * @param \Closure $callback |
|
30 |
+ * @return void |
|
31 |
+ */ |
|
32 |
+ public static function resolved(Closure $callback) |
|
33 |
+ { |
|
34 |
+ static::$app->afterResolving(static::getFacadeAccessor(), function ($service) use ($callback) { |
|
35 |
+ $callback($service); |
|
36 |
+ }); |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ /** |
|
40 |
+ * Convert the facade into a Mockery spy. |
|
41 |
+ * |
|
42 |
+ * @return \Mockery\MockInterface |
|
43 |
+ */ |
|
44 |
+ public static function spy() |
|
45 |
+ { |
|
46 |
+ if (! static::isMock()) { |
|
47 |
+ $class = static::getMockableClass(); |
|
48 |
+ |
|
49 |
+ return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) { |
|
50 |
+ static::swap($spy); |
|
51 |
+ }); |
|
52 |
+ } |
|
53 |
+ } |
|
54 |
+ |
|
55 |
+ /** |
|
56 |
+ * Initiate a mock expectation on the facade. |
|
57 |
+ * |
|
58 |
+ * @return \Mockery\Expectation |
|
59 |
+ */ |
|
60 |
+ public static function shouldReceive() |
|
61 |
+ { |
|
62 |
+ $name = static::getFacadeAccessor(); |
|
63 |
+ |
|
64 |
+ $mock = static::isMock() |
|
65 |
+ ? static::$resolvedInstance[$name] |
|
66 |
+ : static::createFreshMockInstance(); |
|
67 |
+ |
|
68 |
+ return $mock->shouldReceive(...func_get_args()); |
|
69 |
+ } |
|
70 |
+ |
|
71 |
+ /** |
|
72 |
+ * Create a fresh mock instance for the given class. |
|
73 |
+ * |
|
74 |
+ * @return \Mockery\Expectation |
|
75 |
+ */ |
|
76 |
+ protected static function createFreshMockInstance() |
|
77 |
+ { |
|
78 |
+ return tap(static::createMock(), function ($mock) { |
|
79 |
+ static::swap($mock); |
|
80 |
+ |
|
81 |
+ $mock->shouldAllowMockingProtectedMethods(); |
|
82 |
+ }); |
|
83 |
+ } |
|
84 |
+ |
|
85 |
+ /** |
|
86 |
+ * Create a fresh mock instance for the given class. |
|
87 |
+ * |
|
88 |
+ * @return \Mockery\MockInterface |
|
89 |
+ */ |
|
90 |
+ protected static function createMock() |
|
91 |
+ { |
|
92 |
+ $class = static::getMockableClass(); |
|
93 |
+ |
|
94 |
+ return $class ? Mockery::mock($class) : Mockery::mock(); |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ /** |
|
98 |
+ * Determines whether a mock is set as the instance of the facade. |
|
99 |
+ * |
|
100 |
+ * @return bool |
|
101 |
+ */ |
|
102 |
+ protected static function isMock() |
|
103 |
+ { |
|
104 |
+ $name = static::getFacadeAccessor(); |
|
105 |
+ |
|
106 |
+ return isset(static::$resolvedInstance[$name]) && |
|
107 |
+ static::$resolvedInstance[$name] instanceof MockInterface; |
|
108 |
+ } |
|
109 |
+ |
|
110 |
+ /** |
|
111 |
+ * Get the mockable class for the bound instance. |
|
112 |
+ * |
|
113 |
+ * @return string|null |
|
114 |
+ */ |
|
115 |
+ protected static function getMockableClass() |
|
116 |
+ { |
|
117 |
+ if ($root = static::getFacadeRoot()) { |
|
118 |
+ return get_class($root); |
|
119 |
+ } |
|
120 |
+ } |
|
121 |
+ |
|
122 |
+ /** |
|
123 |
+ * Hotswap the underlying instance behind the facade. |
|
124 |
+ * |
|
125 |
+ * @param mixed $instance |
|
126 |
+ * @return void |
|
127 |
+ */ |
|
128 |
+ public static function swap($instance) |
|
129 |
+ { |
|
130 |
+ static::$resolvedInstance[static::getFacadeAccessor()] = $instance; |
|
131 |
+ |
|
132 |
+ if (isset(static::$app)) { |
|
133 |
+ static::$app->instance(static::getFacadeAccessor(), $instance); |
|
134 |
+ } |
|
135 |
+ } |
|
136 |
+ |
|
137 |
+ /** |
|
138 |
+ * Get the root object behind the facade. |
|
139 |
+ * |
|
140 |
+ * @return mixed |
|
141 |
+ */ |
|
142 |
+ public static function getFacadeRoot() |
|
143 |
+ { |
|
144 |
+ return static::resolveFacadeInstance(static::getFacadeAccessor()); |
|
145 |
+ } |
|
146 |
+ |
|
147 |
+ /** |
|
148 |
+ * Get the registered name of the component. |
|
149 |
+ * |
|
150 |
+ * @return string |
|
151 |
+ * |
|
152 |
+ * @throws \RuntimeException |
|
153 |
+ */ |
|
154 |
+ protected static function getFacadeAccessor() |
|
155 |
+ { |
|
156 |
+ throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); |
|
157 |
+ } |
|
158 |
+ |
|
159 |
+ /** |
|
160 |
+ * Resolve the facade root instance from the container. |
|
161 |
+ * |
|
162 |
+ * @param object|string $name |
|
163 |
+ * @return mixed |
|
164 |
+ */ |
|
165 |
+ protected static function resolveFacadeInstance($name) |
|
166 |
+ { |
|
167 |
+ if (is_object($name)) { |
|
168 |
+ return $name; |
|
169 |
+ } |
|
170 |
+ |
|
171 |
+ if (isset(static::$resolvedInstance[$name])) { |
|
172 |
+ return static::$resolvedInstance[$name]; |
|
173 |
+ } |
|
174 |
+ |
|
175 |
+ return static::$resolvedInstance[$name] = static::$app[$name]; |
|
176 |
+ } |
|
177 |
+ |
|
178 |
+ /** |
|
179 |
+ * Clear a resolved facade instance. |
|
180 |
+ * |
|
181 |
+ * @param string $name |
|
182 |
+ * @return void |
|
183 |
+ */ |
|
184 |
+ public static function clearResolvedInstance($name) |
|
185 |
+ { |
|
186 |
+ unset(static::$resolvedInstance[$name]); |
|
187 |
+ } |
|
188 |
+ |
|
189 |
+ /** |
|
190 |
+ * Clear all of the resolved instances. |
|
191 |
+ * |
|
192 |
+ * @return void |
|
193 |
+ */ |
|
194 |
+ public static function clearResolvedInstances() |
|
195 |
+ { |
|
196 |
+ static::$resolvedInstance = []; |
|
197 |
+ } |
|
198 |
+ |
|
199 |
+ /** |
|
200 |
+ * Get the application instance behind the facade. |
|
201 |
+ * |
|
202 |
+ * @return \Illuminate\Contracts\Foundation\Application |
|
203 |
+ */ |
|
204 |
+ public static function getFacadeApplication() |
|
205 |
+ { |
|
206 |
+ return static::$app; |
|
207 |
+ } |
|
208 |
+ |
|
209 |
+ /** |
|
210 |
+ * Set the application instance. |
|
211 |
+ * |
|
212 |
+ * @param \Illuminate\Contracts\Foundation\Application $app |
|
213 |
+ * @return void |
|
214 |
+ */ |
|
215 |
+ public static function setFacadeApplication($app) |
|
216 |
+ { |
|
217 |
+ static::$app = $app; |
|
218 |
+ } |
|
219 |
+ |
|
220 |
+ /** |
|
221 |
+ * Handle dynamic, static calls to the object. |
|
222 |
+ * |
|
223 |
+ * @param string $method |
|
224 |
+ * @param array $args |
|
225 |
+ * @return mixed |
|
226 |
+ * |
|
227 |
+ * @throws \RuntimeException |
|
228 |
+ */ |
|
229 |
+ public static function __callStatic($method, $args) |
|
230 |
+ { |
|
231 |
+ $instance = static::getFacadeRoot(); |
|
232 |
+ |
|
233 |
+ if (! $instance) { |
|
234 |
+ throw new RuntimeException('A facade root has not been set.'); |
|
235 |
+ } |
|
236 |
+ |
|
237 |
+ return $instance->$method(...$args); |
|
238 |
+ } |
|
239 |
+} |