ServiceProvider.php 7.1KB

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