You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServiceProvider.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace Illuminate\Support;
  3. use Illuminate\Console\Application as Artisan;
  4. use Illuminate\Contracts\Support\DeferrableProvider;
  5. abstract class ServiceProvider
  6. {
  7. /**
  8. * The application instance.
  9. *
  10. * @var \Illuminate\Contracts\Foundation\Application
  11. */
  12. protected $app;
  13. /**
  14. * Indicates if loading of the provider is deferred.
  15. *
  16. * @deprecated Implement the \Illuminate\Contracts\Support\DeferrableProvider interface instead. Will be removed in Laravel 5.9.
  17. *
  18. * @var bool
  19. */
  20. protected $defer = false;
  21. /**
  22. * The paths that should be published.
  23. *
  24. * @var array
  25. */
  26. public static $publishes = [];
  27. /**
  28. * The paths that should be published by group.
  29. *
  30. * @var array
  31. */
  32. public static $publishGroups = [];
  33. /**
  34. * Create a new service provider instance.
  35. *
  36. * @param \Illuminate\Contracts\Foundation\Application $app
  37. * @return void
  38. */
  39. public function __construct($app)
  40. {
  41. $this->app = $app;
  42. }
  43. /**
  44. * Register any application services.
  45. *
  46. * @return void
  47. */
  48. public function register()
  49. {
  50. //
  51. }
  52. /**
  53. * Merge the given configuration with the existing configuration.
  54. *
  55. * @param string $path
  56. * @param string $key
  57. * @return void
  58. */
  59. protected function mergeConfigFrom($path, $key)
  60. {
  61. $config = $this->app['config']->get($key, []);
  62. $this->app['config']->set($key, array_merge(require $path, $config));
  63. }
  64. /**
  65. * Load the given routes file if routes are not already cached.
  66. *
  67. * @param string $path
  68. * @return void
  69. */
  70. protected function loadRoutesFrom($path)
  71. {
  72. if (! $this->app->routesAreCached()) {
  73. require $path;
  74. }
  75. }
  76. /**
  77. * Register a view file namespace.
  78. *
  79. * @param string|array $path
  80. * @param string $namespace
  81. * @return void
  82. */
  83. protected function loadViewsFrom($path, $namespace)
  84. {
  85. if (is_array($this->app->config['view']['paths'])) {
  86. foreach ($this->app->config['view']['paths'] as $viewPath) {
  87. if (is_dir($appPath = $viewPath.'/vendor/'.$namespace)) {
  88. $this->app['view']->addNamespace($namespace, $appPath);
  89. }
  90. }
  91. }
  92. $this->app['view']->addNamespace($namespace, $path);
  93. }
  94. /**
  95. * Register a translation file namespace.
  96. *
  97. * @param string $path
  98. * @param string $namespace
  99. * @return void
  100. */
  101. protected function loadTranslationsFrom($path, $namespace)
  102. {
  103. $this->app['translator']->addNamespace($namespace, $path);
  104. }
  105. /**
  106. * Register a JSON translation file path.
  107. *
  108. * @param string $path
  109. * @return void
  110. */
  111. protected function loadJsonTranslationsFrom($path)
  112. {
  113. $this->app['translator']->addJsonPath($path);
  114. }
  115. /**
  116. * Register a database migration path.
  117. *
  118. * @param array|string $paths
  119. * @return void
  120. */
  121. protected function loadMigrationsFrom($paths)
  122. {
  123. $this->app->afterResolving('migrator', function ($migrator) use ($paths) {
  124. foreach ((array) $paths as $path) {
  125. $migrator->path($path);
  126. }
  127. });
  128. }
  129. /**
  130. * Register paths to be published by the publish command.
  131. *
  132. * @param array $paths
  133. * @param mixed $groups
  134. * @return void
  135. */
  136. protected function publishes(array $paths, $groups = null)
  137. {
  138. $this->ensurePublishArrayInitialized($class = static::class);
  139. static::$publishes[$class] = array_merge(static::$publishes[$class], $paths);
  140. if (! is_null($groups)) {
  141. foreach ((array) $groups as $group) {
  142. $this->addPublishGroup($group, $paths);
  143. }
  144. }
  145. }
  146. /**
  147. * Ensure the publish array for the service provider is initialized.
  148. *
  149. * @param string $class
  150. * @return void
  151. */
  152. protected function ensurePublishArrayInitialized($class)
  153. {
  154. if (! array_key_exists($class, static::$publishes)) {
  155. static::$publishes[$class] = [];
  156. }
  157. }
  158. /**
  159. * Add a publish group / tag to the service provider.
  160. *
  161. * @param string $group
  162. * @param array $paths
  163. * @return void
  164. */
  165. protected function addPublishGroup($group, $paths)
  166. {
  167. if (! array_key_exists($group, static::$publishGroups)) {
  168. static::$publishGroups[$group] = [];
  169. }
  170. static::$publishGroups[$group] = array_merge(
  171. static::$publishGroups[$group], $paths
  172. );
  173. }
  174. /**
  175. * Get the paths to publish.
  176. *
  177. * @param string $provider
  178. * @param string $group
  179. * @return array
  180. */
  181. public static function pathsToPublish($provider = null, $group = null)
  182. {
  183. if (! is_null($paths = static::pathsForProviderOrGroup($provider, $group))) {
  184. return $paths;
  185. }
  186. return collect(static::$publishes)->reduce(function ($paths, $p) {
  187. return array_merge($paths, $p);
  188. }, []);
  189. }
  190. /**
  191. * Get the paths for the provider or group (or both).
  192. *
  193. * @param string|null $provider
  194. * @param string|null $group
  195. * @return array
  196. */
  197. protected static function pathsForProviderOrGroup($provider, $group)
  198. {
  199. if ($provider && $group) {
  200. return static::pathsForProviderAndGroup($provider, $group);
  201. } elseif ($group && array_key_exists($group, static::$publishGroups)) {
  202. return static::$publishGroups[$group];
  203. } elseif ($provider && array_key_exists($provider, static::$publishes)) {
  204. return static::$publishes[$provider];
  205. } elseif ($group || $provider) {
  206. return [];
  207. }
  208. }
  209. /**
  210. * Get the paths for the provider and group.
  211. *
  212. * @param string $provider
  213. * @param string $group
  214. * @return array
  215. */
  216. protected static function pathsForProviderAndGroup($provider, $group)
  217. {
  218. if (! empty(static::$publishes[$provider]) && ! empty(static::$publishGroups[$group])) {
  219. return array_intersect_key(static::$publishes[$provider], static::$publishGroups[$group]);
  220. }
  221. return [];
  222. }
  223. /**
  224. * Get the service providers available for publishing.
  225. *
  226. * @return array
  227. */
  228. public static function publishableProviders()
  229. {
  230. return array_keys(static::$publishes);
  231. }
  232. /**
  233. * Get the groups available for publishing.
  234. *
  235. * @return array
  236. */
  237. public static function publishableGroups()
  238. {
  239. return array_keys(static::$publishGroups);
  240. }
  241. /**
  242. * Register the package's custom Artisan commands.
  243. *
  244. * @param array|mixed $commands
  245. * @return void
  246. */
  247. public function commands($commands)
  248. {
  249. $commands = is_array($commands) ? $commands : func_get_args();
  250. Artisan::starting(function ($artisan) use ($commands) {
  251. $artisan->resolveCommands($commands);
  252. });
  253. }
  254. /**
  255. * Get the services provided by the provider.
  256. *
  257. * @return array
  258. */
  259. public function provides()
  260. {
  261. return [];
  262. }
  263. /**
  264. * Get the events that trigger this service provider to register.
  265. *
  266. * @return array
  267. */
  268. public function when()
  269. {
  270. return [];
  271. }
  272. /**
  273. * Determine if the provider is deferred.
  274. *
  275. * @return bool
  276. */
  277. public function isDeferred()
  278. {
  279. return $this->defer || $this instanceof DeferrableProvider;
  280. }
  281. }