Facade.php 5.0KB

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