ServiceProvider.php 13 KB

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