/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php

https://gitlab.com/daniruizcamacho/pfcascensores · PHP · 231 lines · 94 code · 32 blank · 105 comment · 7 complexity · 5666d6a05eedaea179db1bff568f88a9 MD5 · raw file

  1. <?php namespace Illuminate\Foundation;
  2. use Illuminate\Filesystem\Filesystem;
  3. class ProviderRepository {
  4. /**
  5. * The filesystem instance.
  6. *
  7. * @var \Illuminate\Filesystem\Filesystem
  8. */
  9. protected $files;
  10. /**
  11. * The path to the manifest.
  12. *
  13. * @var string
  14. */
  15. protected $manifestPath;
  16. /**
  17. * Default manifest structure.
  18. *
  19. * @var array
  20. */
  21. protected $default = array('when' => array());
  22. /**
  23. * Create a new service repository instance.
  24. *
  25. * @param \Illuminate\Filesystem\Filesystem $files
  26. * @param string $manifestPath
  27. * @return void
  28. */
  29. public function __construct(Filesystem $files, $manifestPath)
  30. {
  31. $this->files = $files;
  32. $this->manifestPath = $manifestPath;
  33. }
  34. /**
  35. * Register the application service providers.
  36. *
  37. * @param \Illuminate\Foundation\Application $app
  38. * @param array $providers
  39. * @param string $path
  40. * @return void
  41. */
  42. public function load(Application $app, array $providers)
  43. {
  44. $manifest = $this->loadManifest();
  45. // First we will load the service manifest, which contains information on all
  46. // service providers registered with the application and which services it
  47. // provides. This is used to know which services are "deferred" loaders.
  48. if ($this->shouldRecompile($manifest, $providers))
  49. {
  50. $manifest = $this->compileManifest($app, $providers);
  51. }
  52. // If the application is running in the console, we will not lazy load any of
  53. // the service providers. This is mainly because it's not as necessary for
  54. // performance and also so any provided Artisan commands get registered.
  55. if ($app->runningInConsole())
  56. {
  57. $manifest['eager'] = $manifest['providers'];
  58. }
  59. // Next, we will register events to load the providers for each of the events
  60. // that it has requested. This allows the service provider to defer itself
  61. // while still getting automatically loaded when a certain event occurs.
  62. foreach ($manifest['when'] as $provider => $events)
  63. {
  64. $this->registerLoadEvents($app, $provider, $events);
  65. }
  66. // We will go ahead and register all of the eagerly loaded providers with the
  67. // application so their services can be registered with the application as
  68. // a provided service. Then we will set the deferred service list on it.
  69. foreach ($manifest['eager'] as $provider)
  70. {
  71. $app->register($this->createProvider($app, $provider));
  72. }
  73. $app->setDeferredServices($manifest['deferred']);
  74. }
  75. /**
  76. * Register the load events for the given provider.
  77. *
  78. * @param \Illuminate\Foundation\Application $app
  79. * @param string $provider
  80. * @param array $events
  81. * @return void
  82. */
  83. protected function registerLoadEvents(Application $app, $provider, array $events)
  84. {
  85. if (count($events) < 1) return;
  86. $app->make('events')->listen($events, function() use ($app, $provider)
  87. {
  88. $app->register($provider);
  89. });
  90. }
  91. /**
  92. * Compile the application manifest file.
  93. *
  94. * @param \Illuminate\Foundation\Application $app
  95. * @param array $providers
  96. * @return array
  97. */
  98. protected function compileManifest(Application $app, $providers)
  99. {
  100. // The service manifest should contain a list of all of the providers for
  101. // the application so we can compare it on each request to the service
  102. // and determine if the manifest should be recompiled or is current.
  103. $manifest = $this->freshManifest($providers);
  104. foreach ($providers as $provider)
  105. {
  106. $instance = $this->createProvider($app, $provider);
  107. // When recompiling the service manifest, we will spin through each of the
  108. // providers and check if it's a deferred provider or not. If so we'll
  109. // add it's provided services to the manifest and note the provider.
  110. if ($instance->isDeferred())
  111. {
  112. foreach ($instance->provides() as $service)
  113. {
  114. $manifest['deferred'][$service] = $provider;
  115. }
  116. $manifest['when'][$provider] = $instance->when();
  117. }
  118. // If the service providers are not deferred, we will simply add it to an
  119. // of eagerly loaded providers that will be registered with the app on
  120. // each request to the applications instead of being lazy loaded in.
  121. else
  122. {
  123. $manifest['eager'][] = $provider;
  124. }
  125. }
  126. return $this->writeManifest($manifest);
  127. }
  128. /**
  129. * Create a new provider instance.
  130. *
  131. * @param \Illuminate\Foundation\Application $app
  132. * @param string $provider
  133. * @return \Illuminate\Support\ServiceProvider
  134. */
  135. public function createProvider(Application $app, $provider)
  136. {
  137. return new $provider($app);
  138. }
  139. /**
  140. * Determine if the manifest should be compiled.
  141. *
  142. * @param array $manifest
  143. * @param array $providers
  144. * @return bool
  145. */
  146. public function shouldRecompile($manifest, $providers)
  147. {
  148. return is_null($manifest) || $manifest['providers'] != $providers;
  149. }
  150. /**
  151. * Load the service provider manifest JSON file.
  152. *
  153. * @return array
  154. */
  155. public function loadManifest()
  156. {
  157. $path = $this->manifestPath.'/services.json';
  158. // The service manifest is a file containing a JSON representation of every
  159. // service provided by the application and whether its provider is using
  160. // deferred loading or should be eagerly loaded on each request to us.
  161. if ($this->files->exists($path))
  162. {
  163. $manifest = json_decode($this->files->get($path), true);
  164. return array_merge($this->default, $manifest);
  165. }
  166. }
  167. /**
  168. * Write the service manifest file to disk.
  169. *
  170. * @param array $manifest
  171. * @return array
  172. */
  173. public function writeManifest($manifest)
  174. {
  175. $path = $this->manifestPath.'/services.json';
  176. $this->files->put($path, json_encode($manifest));
  177. return $manifest;
  178. }
  179. /**
  180. * Create a fresh manifest array.
  181. *
  182. * @param array $providers
  183. * @return array
  184. */
  185. protected function freshManifest(array $providers)
  186. {
  187. list($eager, $deferred) = array(array(), array());
  188. return compact('providers', 'eager', 'deferred');
  189. }
  190. /**
  191. * Get the filesystem instance.
  192. *
  193. * @return \Illuminate\Filesystem\Filesystem
  194. */
  195. public function getFilesystem()
  196. {
  197. return $this->files;
  198. }
  199. }