123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
-
- namespace Illuminate\View;
-
- use Illuminate\View\Engines\PhpEngine;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\View\Engines\FileEngine;
- use Illuminate\View\Engines\CompilerEngine;
- use Illuminate\View\Engines\EngineResolver;
- use Illuminate\View\Compilers\BladeCompiler;
-
- class ViewServiceProvider extends ServiceProvider
- {
- /**
- * Register the service provider.
- *
- * @return void
- */
- public function register()
- {
- $this->registerFactory();
-
- $this->registerViewFinder();
-
- $this->registerEngineResolver();
- }
-
- /**
- * Register the view environment.
- *
- * @return void
- */
- public function registerFactory()
- {
- $this->app->singleton('view', function ($app) {
- // Next we need to grab the engine resolver instance that will be used by the
- // environment. The resolver will be used by an environment to get each of
- // the various engine implementations such as plain PHP or Blade engine.
- $resolver = $app['view.engine.resolver'];
-
- $finder = $app['view.finder'];
-
- $factory = $this->createFactory($resolver, $finder, $app['events']);
-
- // We will also set the container instance on this view environment since the
- // view composers may be classes registered in the container, which allows
- // for great testable, flexible composers for the application developer.
- $factory->setContainer($app);
-
- $factory->share('app', $app);
-
- return $factory;
- });
- }
-
- /**
- * Create a new Factory Instance.
- *
- * @param \Illuminate\View\Engines\EngineResolver $resolver
- * @param \Illuminate\View\ViewFinderInterface $finder
- * @param \Illuminate\Contracts\Events\Dispatcher $events
- * @return \Illuminate\View\Factory
- */
- protected function createFactory($resolver, $finder, $events)
- {
- return new Factory($resolver, $finder, $events);
- }
-
- /**
- * Register the view finder implementation.
- *
- * @return void
- */
- public function registerViewFinder()
- {
- $this->app->bind('view.finder', function ($app) {
- return new FileViewFinder($app['files'], $app['config']['view.paths']);
- });
- }
-
- /**
- * Register the engine resolver instance.
- *
- * @return void
- */
- public function registerEngineResolver()
- {
- $this->app->singleton('view.engine.resolver', function () {
- $resolver = new EngineResolver;
-
- // Next, we will register the various view engines with the resolver so that the
- // environment will resolve the engines needed for various views based on the
- // extension of view file. We call a method for each of the view's engines.
- foreach (['file', 'php', 'blade'] as $engine) {
- $this->{'register'.ucfirst($engine).'Engine'}($resolver);
- }
-
- return $resolver;
- });
- }
-
- /**
- * Register the file engine implementation.
- *
- * @param \Illuminate\View\Engines\EngineResolver $resolver
- * @return void
- */
- public function registerFileEngine($resolver)
- {
- $resolver->register('file', function () {
- return new FileEngine;
- });
- }
-
- /**
- * Register the PHP engine implementation.
- *
- * @param \Illuminate\View\Engines\EngineResolver $resolver
- * @return void
- */
- public function registerPhpEngine($resolver)
- {
- $resolver->register('php', function () {
- return new PhpEngine;
- });
- }
-
- /**
- * Register the Blade engine implementation.
- *
- * @param \Illuminate\View\Engines\EngineResolver $resolver
- * @return void
- */
- public function registerBladeEngine($resolver)
- {
- // The Compiler engine requires an instance of the CompilerInterface, which in
- // this case will be the Blade compiler, so we'll first create the compiler
- // instance to pass into the engine so it can compile the views properly.
- $this->app->singleton('blade.compiler', function () {
- return new BladeCompiler(
- $this->app['files'], $this->app['config']['view.compiled']
- );
- });
-
- $resolver->register('blade', function () {
- return new CompilerEngine($this->app['blade.compiler']);
- });
- }
- }
|