ServiceProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace Illuminate\Support;
  3. use Closure;
  4. use Illuminate\Console\Application as Artisan;
  5. use Illuminate\Contracts\Foundation\CachesConfiguration;
  6. use Illuminate\Contracts\Foundation\CachesRoutes;
  7. use Illuminate\Contracts\Support\DeferrableProvider;
  8. use Illuminate\Database\Eloquent\Factory as ModelFactory;
  9. use Illuminate\View\Compilers\BladeCompiler;
  10. abstract class ServiceProvider
  11. {
  12. /**
  13. * The application instance.
  14. *
  15. * @var \Illuminate\Contracts\Foundation\Application
  16. */
  17. protected $app;
  18. /**
  19. * All of the registered booting callbacks.
  20. *
  21. * @var array
  22. */
  23. protected $bootingCallbacks = [];
  24. /**
  25. * All of the registered booted callbacks.
  26. *
  27. * @var array
  28. */
  29. protected $bootedCallbacks = [];
  30. /**
  31. * The paths that should be published.
  32. *
  33. * @var array
  34. */
  35. public static $publishes = [];
  36. /**
  37. * The paths that should be published by group.
  38. *
  39. * @var array
  40. */
  41. public static $publishGroups = [];
  42. /**
  43. * Create a new service provider instance.
  44. *
  45. * @param \Illuminate\Contracts\Foundation\Application $app
  46. * @return void
  47. */
  48. public function __construct($app)
  49. {
  50. $this->app = $app;
  51. }
  52. /**
  53. * Register any application services.
  54. *
  55. * @return void
  56. */
  57. public function register()
  58. {
  59. //
  60. }
  61. /**
  62. * Register a booting callback to be run before the "boot" method is called.
  63. *
  64. * @param \Closure $callback
  65. * @return void
  66. */
  67. public function booting(Closure $callback)
  68. {
  69. $this->bootingCallbacks[] = $callback;
  70. }
  71. /**
  72. * Register a booted callback to be run after the "boot" method is called.
  73. *
  74. * @param \Closure $callback
  75. * @return void
  76. */
  77. public function booted(Closure $callback)
  78. {
  79. $this->bootedCallbacks[] = $callback;
  80. }
  81. /**
  82. * Call the registered booting callbacks.
  83. *
  84. * @return void
  85. */
  86. public function callBootingCallbacks()
  87. {
  88. $index = 0;
  89. while ($index < count($this->bootingCallbacks)) {
  90. $this->app->call($this->bootingCallbacks[$index]);
  91. $index++;
  92. }
  93. }
  94. /**
  95. * Call the registered booted callbacks.
  96. *
  97. * @return void
  98. */
  99. public function callBootedCallbacks()
  100. {
  101. $index = 0;
  102. while ($index < count($this->bootedCallbacks)) {
  103. $this->app->call($this->bootedCallbacks[$index]);
  104. $index++;
  105. }
  106. }
  107. /**
  108. * Merge the given configuration with the existing configuration.
  109. *
  110. * @param string $path
  111. * @param string $key
  112. * @return void
  113. */
  114. protected function mergeConfigFrom($path, $key)
  115. {
  116. if (! ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) {
  117. $config = $this->app->make('config');
  118. $config->set($key, array_merge(
  119. require $path, $config->get($key, [])
  120. ));
  121. }
  122. }
  123. /**
  124. * Load the given routes file if routes are not already cached.
  125. *
  126. * @param string $path
  127. * @return void
  128. */
  129. protected function loadRoutesFrom($path)
  130. {
  131. if (! ($this->app instanceof CachesRoutes && $this->app->routesAreCached())) {
  132. require $path;
  133. }
  134. }
  135. /**
  136. * Register a view file namespace.
  137. *
  138. * @param string|array $path
  139. * @param string $namespace
  140. * @return void
  141. */
  142. protected function loadViewsFrom($path, $namespace)
  143. {
  144. $this->callAfterResolving('view', function ($view) use ($path, $namespace) {
  145. if (isset($this->app->config['view']['paths']) &&
  146. is_array($this->app->config['view']['paths'])) {
  147. foreach ($this->app->config['view']['paths'] as $viewPath) {
  148. if (is_dir($appPath = $viewPath.'/vendor/'.$namespace)) {
  149. $view->addNamespace($namespace, $appPath);
  150. }
  151. }
  152. }
  153. $view->addNamespace($namespace, $path);
  154. });
  155. }
  156. /**
  157. * Register the given view components with a custom prefix.
  158. *
  159. * @param string $prefix
  160. * @param array $components
  161. * @return void
  162. */
  163. protected function loadViewComponentsAs($prefix, array $components)
  164. {
  165. $this->callAfterResolving(BladeCompiler::class, function ($blade) use ($prefix, $components) {
  166. foreach ($components as $alias => $component) {
  167. $blade->component($component, is_string($alias) ? $alias : null, $prefix);
  168. }
  169. });
  170. }
  171. /**
  172. * Register a translation file namespace.
  173. *
  174. * @param string $path
  175. * @param string $namespace
  176. * @return void
  177. */
  178. protected function loadTranslationsFrom($path, $namespace)
  179. {
  180. $this->callAfterResolving('translator', function ($translator) use ($path, $namespace) {
  181. $translator->addNamespace($namespace, $path);
  182. });
  183. }
  184. /**
  185. * Register a JSON translation file path.
  186. *
  187. * @param string $path
  188. * @return void
  189. */
  190. protected function loadJsonTranslationsFrom($path)
  191. {
  192. $this->callAfterResolving('translator', function ($translator) use ($path) {
  193. $translator->addJsonPath($path);
  194. });
  195. }
  196. /**
  197. * Register database migration paths.
  198. *
  199. * @param array|string $paths
  200. * @return void
  201. */
  202. protected function loadMigrationsFrom($paths)
  203. {
  204. $this->callAfterResolving('migrator', function ($migrator) use ($paths) {
  205. foreach ((array) $paths as $path) {
  206. $migrator->path($path);
  207. }
  208. });
  209. }
  210. /**
  211. * Register Eloquent model factory paths.
  212. *
  213. * @deprecated Will be removed in a future Laravel version.
  214. *
  215. * @param array|string $paths
  216. * @return void
  217. */
  218. protected function loadFactoriesFrom($paths)
  219. {
  220. $this->callAfterResolving(ModelFactory::class, function ($factory) use ($paths) {
  221. foreach ((array) $paths as $path) {
  222. $factory->load($path);
  223. }
  224. });
  225. }
  226. /**
  227. * Setup an after resolving listener, or fire immediately if already resolved.
  228. *
  229. * @param string $name
  230. * @param callable $callback
  231. * @return void
  232. */
  233. protected function callAfterResolving($name, $callback)
  234. {
  235. $this->app->afterResolving($name, $callback);
  236. if ($this->app->resolved($name)) {
  237. $callback($this->app->make($name), $this->app);
  238. }
  239. }
  240. /**
  241. * Register paths to be published by the publish command.
  242. *
  243. * @param array $paths
  244. * @param mixed $groups
  245. * @return void
  246. */
  247. protected function publishes(array $paths, $groups = null)
  248. {
  249. $this->ensurePublishArrayInitialized($class = static::class);
  250. static::$publishes[$class] = array_merge(static::$publishes[$class], $paths);
  251. foreach ((array) $groups as $group) {
  252. $this->addPublishGroup($group, $paths);
  253. }
  254. }
  255. /**
  256. * Ensure the publish array for the service provider is initialized.
  257. *
  258. * @param string $class
  259. * @return void
  260. */
  261. protected function ensurePublishArrayInitialized($class)
  262. {
  263. if (! array_key_exists($class, static::$publishes)) {
  264. static::$publishes[$class] = [];
  265. }
  266. }
  267. /**
  268. * Add a publish group / tag to the service provider.
  269. *
  270. * @param string $group
  271. * @param array $paths
  272. * @return void
  273. */
  274. protected function addPublishGroup($group, $paths)
  275. {
  276. if (! array_key_exists($group, static::$publishGroups)) {
  277. static::$publishGroups[$group] = [];
  278. }
  279. static::$publishGroups[$group] = array_merge(
  280. static::$publishGroups[$group], $paths
  281. );
  282. }
  283. /**
  284. * Get the paths to publish.
  285. *
  286. * @param string|null $provider
  287. * @param string|null $group
  288. * @return array
  289. */
  290. public static function pathsToPublish($provider = null, $group = null)
  291. {
  292. if (! is_null($paths = static::pathsForProviderOrGroup($provider, $group))) {
  293. return $paths;
  294. }
  295. return collect(static::$publishes)->reduce(function ($paths, $p) {
  296. return array_merge($paths, $p);
  297. }, []);
  298. }
  299. /**
  300. * Get the paths for the provider or group (or both).
  301. *
  302. * @param string|null $provider
  303. * @param string|null $group
  304. * @return array
  305. */
  306. protected static function pathsForProviderOrGroup($provider, $group)
  307. {
  308. if ($provider && $group) {
  309. return static::pathsForProviderAndGroup($provider, $group);
  310. } elseif ($group && array_key_exists($group, static::$publishGroups)) {
  311. return static::$publishGroups[$group];
  312. } elseif ($provider && array_key_exists($provider, static::$publishes)) {
  313. return static::$publishes[$provider];
  314. } elseif ($group || $provider) {
  315. return [];
  316. }
  317. }
  318. /**
  319. * Get the paths for the provider and group.
  320. *
  321. * @param string $provider
  322. * @param string $group
  323. * @return array
  324. */
  325. protected static function pathsForProviderAndGroup($provider, $group)
  326. {
  327. if (! empty(static::$publishes[$provider]) && ! empty(static::$publishGroups[$group])) {
  328. return array_intersect_key(static::$publishes[$provider], static::$publishGroups[$group]);
  329. }
  330. return [];
  331. }
  332. /**
  333. * Get the service providers available for publishing.
  334. *
  335. * @return array
  336. */
  337. public static function publishableProviders()
  338. {
  339. return array_keys(static::$publishes);
  340. }
  341. /**
  342. * Get the groups available for publishing.
  343. *
  344. * @return array
  345. */
  346. public static function publishableGroups()
  347. {
  348. return array_keys(static::$publishGroups);
  349. }
  350. /**
  351. * Register the package's custom Artisan commands.
  352. *
  353. * @param array|mixed $commands
  354. * @return void
  355. */
  356. public function commands($commands)
  357. {
  358. $commands = is_array($commands) ? $commands : func_get_args();
  359. Artisan::starting(function ($artisan) use ($commands) {
  360. $artisan->resolveCommands($commands);
  361. });
  362. }
  363. /**
  364. * Get the services provided by the provider.
  365. *
  366. * @return array
  367. */
  368. public function provides()
  369. {
  370. return [];
  371. }
  372. /**
  373. * Get the events that trigger this service provider to register.
  374. *
  375. * @return array
  376. */
  377. public function when()
  378. {
  379. return [];
  380. }
  381. /**
  382. * Determine if the provider is deferred.
  383. *
  384. * @return bool
  385. */
  386. public function isDeferred()
  387. {
  388. return $this instanceof DeferrableProvider;
  389. }
  390. /**
  391. * Get the default providers for a Laravel application.
  392. *
  393. * @return \Illuminate\Support\DefaultProviders
  394. */
  395. public static function defaultProviders()
  396. {
  397. return new DefaultProviders;
  398. }
  399. }