/laradock/vendor/illuminate/support/Facades/Facade.php

https://gitlab.com/hoangduys4k5/laravelproject · PHP · 339 lines · 177 code · 41 blank · 121 comment · 8 complexity · 952d3b43e3c2d4e9fcd830b6077c4921 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Support\Facades;
  3. use Closure;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Js;
  7. use Illuminate\Support\Str;
  8. use Mockery;
  9. use Mockery\LegacyMockInterface;
  10. use RuntimeException;
  11. abstract class Facade
  12. {
  13. /**
  14. * The application instance being facaded.
  15. *
  16. * @var \Illuminate\Contracts\Foundation\Application
  17. */
  18. protected static $app;
  19. /**
  20. * The resolved object instances.
  21. *
  22. * @var array
  23. */
  24. protected static $resolvedInstance;
  25. /**
  26. * Indicates if the resolved instance should be cached.
  27. *
  28. * @var bool
  29. */
  30. protected static $cached = true;
  31. /**
  32. * Run a Closure when the facade has been resolved.
  33. *
  34. * @param \Closure $callback
  35. * @return void
  36. */
  37. public static function resolved(Closure $callback)
  38. {
  39. $accessor = static::getFacadeAccessor();
  40. if (static::$app->resolved($accessor) === true) {
  41. $callback(static::getFacadeRoot());
  42. }
  43. static::$app->afterResolving($accessor, function ($service) use ($callback) {
  44. $callback($service);
  45. });
  46. }
  47. /**
  48. * Convert the facade into a Mockery spy.
  49. *
  50. * @return \Mockery\MockInterface
  51. */
  52. public static function spy()
  53. {
  54. if (! static::isMock()) {
  55. $class = static::getMockableClass();
  56. return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) {
  57. static::swap($spy);
  58. });
  59. }
  60. }
  61. /**
  62. * Initiate a partial mock on the facade.
  63. *
  64. * @return \Mockery\MockInterface
  65. */
  66. public static function partialMock()
  67. {
  68. $name = static::getFacadeAccessor();
  69. $mock = static::isMock()
  70. ? static::$resolvedInstance[$name]
  71. : static::createFreshMockInstance();
  72. return $mock->makePartial();
  73. }
  74. /**
  75. * Initiate a mock expectation on the facade.
  76. *
  77. * @return \Mockery\Expectation
  78. */
  79. public static function shouldReceive()
  80. {
  81. $name = static::getFacadeAccessor();
  82. $mock = static::isMock()
  83. ? static::$resolvedInstance[$name]
  84. : static::createFreshMockInstance();
  85. return $mock->shouldReceive(...func_get_args());
  86. }
  87. /**
  88. * Initiate a mock expectation on the facade.
  89. *
  90. * @return \Mockery\Expectation
  91. */
  92. public static function expects()
  93. {
  94. $name = static::getFacadeAccessor();
  95. $mock = static::isMock()
  96. ? static::$resolvedInstance[$name]
  97. : static::createFreshMockInstance();
  98. return $mock->expects(...func_get_args());
  99. }
  100. /**
  101. * Create a fresh mock instance for the given class.
  102. *
  103. * @return \Mockery\MockInterface
  104. */
  105. protected static function createFreshMockInstance()
  106. {
  107. return tap(static::createMock(), function ($mock) {
  108. static::swap($mock);
  109. $mock->shouldAllowMockingProtectedMethods();
  110. });
  111. }
  112. /**
  113. * Create a fresh mock instance for the given class.
  114. *
  115. * @return \Mockery\MockInterface
  116. */
  117. protected static function createMock()
  118. {
  119. $class = static::getMockableClass();
  120. return $class ? Mockery::mock($class) : Mockery::mock();
  121. }
  122. /**
  123. * Determines whether a mock is set as the instance of the facade.
  124. *
  125. * @return bool
  126. */
  127. protected static function isMock()
  128. {
  129. $name = static::getFacadeAccessor();
  130. return isset(static::$resolvedInstance[$name]) &&
  131. static::$resolvedInstance[$name] instanceof LegacyMockInterface;
  132. }
  133. /**
  134. * Get the mockable class for the bound instance.
  135. *
  136. * @return string|null
  137. */
  138. protected static function getMockableClass()
  139. {
  140. if ($root = static::getFacadeRoot()) {
  141. return get_class($root);
  142. }
  143. }
  144. /**
  145. * Hotswap the underlying instance behind the facade.
  146. *
  147. * @param mixed $instance
  148. * @return void
  149. */
  150. public static function swap($instance)
  151. {
  152. static::$resolvedInstance[static::getFacadeAccessor()] = $instance;
  153. if (isset(static::$app)) {
  154. static::$app->instance(static::getFacadeAccessor(), $instance);
  155. }
  156. }
  157. /**
  158. * Get the root object behind the facade.
  159. *
  160. * @return mixed
  161. */
  162. public static function getFacadeRoot()
  163. {
  164. return static::resolveFacadeInstance(static::getFacadeAccessor());
  165. }
  166. /**
  167. * Get the registered name of the component.
  168. *
  169. * @return string
  170. *
  171. * @throws \RuntimeException
  172. */
  173. protected static function getFacadeAccessor()
  174. {
  175. throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
  176. }
  177. /**
  178. * Resolve the facade root instance from the container.
  179. *
  180. * @param string $name
  181. * @return mixed
  182. */
  183. protected static function resolveFacadeInstance($name)
  184. {
  185. if (isset(static::$resolvedInstance[$name])) {
  186. return static::$resolvedInstance[$name];
  187. }
  188. if (static::$app) {
  189. if (static::$cached) {
  190. return static::$resolvedInstance[$name] = static::$app[$name];
  191. }
  192. return static::$app[$name];
  193. }
  194. }
  195. /**
  196. * Clear a resolved facade instance.
  197. *
  198. * @param string $name
  199. * @return void
  200. */
  201. public static function clearResolvedInstance($name)
  202. {
  203. unset(static::$resolvedInstance[$name]);
  204. }
  205. /**
  206. * Clear all of the resolved instances.
  207. *
  208. * @return void
  209. */
  210. public static function clearResolvedInstances()
  211. {
  212. static::$resolvedInstance = [];
  213. }
  214. /**
  215. * Get the application default aliases.
  216. *
  217. * @return \Illuminate\Support\Collection
  218. */
  219. public static function defaultAliases()
  220. {
  221. return collect([
  222. 'App' => App::class,
  223. 'Arr' => Arr::class,
  224. 'Artisan' => Artisan::class,
  225. 'Auth' => Auth::class,
  226. 'Blade' => Blade::class,
  227. 'Broadcast' => Broadcast::class,
  228. 'Bus' => Bus::class,
  229. 'Cache' => Cache::class,
  230. 'Config' => Config::class,
  231. 'Cookie' => Cookie::class,
  232. 'Crypt' => Crypt::class,
  233. 'Date' => Date::class,
  234. 'DB' => DB::class,
  235. 'Eloquent' => Model::class,
  236. 'Event' => Event::class,
  237. 'File' => File::class,
  238. 'Gate' => Gate::class,
  239. 'Hash' => Hash::class,
  240. 'Http' => Http::class,
  241. 'Js' => Js::class,
  242. 'Lang' => Lang::class,
  243. 'Log' => Log::class,
  244. 'Mail' => Mail::class,
  245. 'Notification' => Notification::class,
  246. 'Password' => Password::class,
  247. 'Queue' => Queue::class,
  248. 'RateLimiter' => RateLimiter::class,
  249. 'Redirect' => Redirect::class,
  250. 'Request' => Request::class,
  251. 'Response' => Response::class,
  252. 'Route' => Route::class,
  253. 'Schema' => Schema::class,
  254. 'Session' => Session::class,
  255. 'Storage' => Storage::class,
  256. 'Str' => Str::class,
  257. 'URL' => URL::class,
  258. 'Validator' => Validator::class,
  259. 'View' => View::class,
  260. ]);
  261. }
  262. /**
  263. * Get the application instance behind the facade.
  264. *
  265. * @return \Illuminate\Contracts\Foundation\Application
  266. */
  267. public static function getFacadeApplication()
  268. {
  269. return static::$app;
  270. }
  271. /**
  272. * Set the application instance.
  273. *
  274. * @param \Illuminate\Contracts\Foundation\Application $app
  275. * @return void
  276. */
  277. public static function setFacadeApplication($app)
  278. {
  279. static::$app = $app;
  280. }
  281. /**
  282. * Handle dynamic, static calls to the object.
  283. *
  284. * @param string $method
  285. * @param array $args
  286. * @return mixed
  287. *
  288. * @throws \RuntimeException
  289. */
  290. public static function __callStatic($method, $args)
  291. {
  292. $instance = static::getFacadeRoot();
  293. if (! $instance) {
  294. throw new RuntimeException('A facade root has not been set.');
  295. }
  296. return $instance->$method(...$args);
  297. }
  298. }