PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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