ViewServiceProvider.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Illuminate\View;
  3. use Illuminate\View\Engines\PhpEngine;
  4. use Illuminate\Support\ServiceProvider;
  5. use Illuminate\View\Engines\FileEngine;
  6. use Illuminate\View\Engines\CompilerEngine;
  7. use Illuminate\View\Engines\EngineResolver;
  8. use Illuminate\View\Compilers\BladeCompiler;
  9. class ViewServiceProvider extends ServiceProvider
  10. {
  11. /**
  12. * Register the service provider.
  13. *
  14. * @return void
  15. */
  16. public function register()
  17. {
  18. $this->registerFactory();
  19. $this->registerViewFinder();
  20. $this->registerEngineResolver();
  21. }
  22. /**
  23. * Register the view environment.
  24. *
  25. * @return void
  26. */
  27. public function registerFactory()
  28. {
  29. $this->app->singleton('view', function ($app) {
  30. // Next we need to grab the engine resolver instance that will be used by the
  31. // environment. The resolver will be used by an environment to get each of
  32. // the various engine implementations such as plain PHP or Blade engine.
  33. $resolver = $app['view.engine.resolver'];
  34. $finder = $app['view.finder'];
  35. $factory = $this->createFactory($resolver, $finder, $app['events']);
  36. // We will also set the container instance on this view environment since the
  37. // view composers may be classes registered in the container, which allows
  38. // for great testable, flexible composers for the application developer.
  39. $factory->setContainer($app);
  40. $factory->share('app', $app);
  41. return $factory;
  42. });
  43. }
  44. /**
  45. * Create a new Factory Instance.
  46. *
  47. * @param \Illuminate\View\Engines\EngineResolver $resolver
  48. * @param \Illuminate\View\ViewFinderInterface $finder
  49. * @param \Illuminate\Contracts\Events\Dispatcher $events
  50. * @return \Illuminate\View\Factory
  51. */
  52. protected function createFactory($resolver, $finder, $events)
  53. {
  54. return new Factory($resolver, $finder, $events);
  55. }
  56. /**
  57. * Register the view finder implementation.
  58. *
  59. * @return void
  60. */
  61. public function registerViewFinder()
  62. {
  63. $this->app->bind('view.finder', function ($app) {
  64. return new FileViewFinder($app['files'], $app['config']['view.paths']);
  65. });
  66. }
  67. /**
  68. * Register the engine resolver instance.
  69. *
  70. * @return void
  71. */
  72. public function registerEngineResolver()
  73. {
  74. $this->app->singleton('view.engine.resolver', function () {
  75. $resolver = new EngineResolver;
  76. // Next, we will register the various view engines with the resolver so that the
  77. // environment will resolve the engines needed for various views based on the
  78. // extension of view file. We call a method for each of the view's engines.
  79. foreach (['file', 'php', 'blade'] as $engine) {
  80. $this->{'register'.ucfirst($engine).'Engine'}($resolver);
  81. }
  82. return $resolver;
  83. });
  84. }
  85. /**
  86. * Register the file engine implementation.
  87. *
  88. * @param \Illuminate\View\Engines\EngineResolver $resolver
  89. * @return void
  90. */
  91. public function registerFileEngine($resolver)
  92. {
  93. $resolver->register('file', function () {
  94. return new FileEngine;
  95. });
  96. }
  97. /**
  98. * Register the PHP engine implementation.
  99. *
  100. * @param \Illuminate\View\Engines\EngineResolver $resolver
  101. * @return void
  102. */
  103. public function registerPhpEngine($resolver)
  104. {
  105. $resolver->register('php', function () {
  106. return new PhpEngine;
  107. });
  108. }
  109. /**
  110. * Register the Blade engine implementation.
  111. *
  112. * @param \Illuminate\View\Engines\EngineResolver $resolver
  113. * @return void
  114. */
  115. public function registerBladeEngine($resolver)
  116. {
  117. // The Compiler engine requires an instance of the CompilerInterface, which in
  118. // this case will be the Blade compiler, so we'll first create the compiler
  119. // instance to pass into the engine so it can compile the views properly.
  120. $this->app->singleton('blade.compiler', function () {
  121. return new BladeCompiler(
  122. $this->app['files'], $this->app['config']['view.compiled']
  123. );
  124. });
  125. $resolver->register('blade', function () {
  126. return new CompilerEngine($this->app['blade.compiler']);
  127. });
  128. }
  129. }