| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\View;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\ServiceProvider;
|
|
|
6 |
use Illuminate\View\Compilers\BladeCompiler;
|
|
|
7 |
use Illuminate\View\Engines\CompilerEngine;
|
|
|
8 |
use Illuminate\View\Engines\EngineResolver;
|
|
|
9 |
use Illuminate\View\Engines\FileEngine;
|
|
|
10 |
use Illuminate\View\Engines\PhpEngine;
|
|
|
11 |
|
|
|
12 |
class ViewServiceProvider extends ServiceProvider
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* Register the service provider.
|
|
|
16 |
*
|
|
|
17 |
* @return void
|
|
|
18 |
*/
|
|
|
19 |
public function register()
|
|
|
20 |
{
|
|
|
21 |
$this->registerFactory();
|
|
|
22 |
$this->registerViewFinder();
|
|
|
23 |
$this->registerBladeCompiler();
|
|
|
24 |
$this->registerEngineResolver();
|
|
|
25 |
|
|
|
26 |
$this->app->terminating(static function () {
|
|
|
27 |
Component::flushCache();
|
|
|
28 |
});
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Register the view environment.
|
|
|
33 |
*
|
|
|
34 |
* @return void
|
|
|
35 |
*/
|
|
|
36 |
public function registerFactory()
|
|
|
37 |
{
|
|
|
38 |
$this->app->singleton('view', function ($app) {
|
|
|
39 |
// Next we need to grab the engine resolver instance that will be used by the
|
|
|
40 |
// environment. The resolver will be used by an environment to get each of
|
|
|
41 |
// the various engine implementations such as plain PHP or Blade engine.
|
|
|
42 |
$resolver = $app['view.engine.resolver'];
|
|
|
43 |
|
|
|
44 |
$finder = $app['view.finder'];
|
|
|
45 |
|
|
|
46 |
$factory = $this->createFactory($resolver, $finder, $app['events']);
|
|
|
47 |
|
|
|
48 |
// We will also set the container instance on this view environment since the
|
|
|
49 |
// view composers may be classes registered in the container, which allows
|
|
|
50 |
// for great testable, flexible composers for the application developer.
|
|
|
51 |
$factory->setContainer($app);
|
|
|
52 |
|
|
|
53 |
$factory->share('app', $app);
|
|
|
54 |
|
|
|
55 |
$app->terminating(static function () {
|
|
|
56 |
Component::forgetFactory();
|
|
|
57 |
});
|
|
|
58 |
|
|
|
59 |
return $factory;
|
|
|
60 |
});
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Create a new Factory Instance.
|
|
|
65 |
*
|
|
|
66 |
* @param \Illuminate\View\Engines\EngineResolver $resolver
|
|
|
67 |
* @param \Illuminate\View\ViewFinderInterface $finder
|
|
|
68 |
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
|
|
69 |
* @return \Illuminate\View\Factory
|
|
|
70 |
*/
|
|
|
71 |
protected function createFactory($resolver, $finder, $events)
|
|
|
72 |
{
|
|
|
73 |
return new Factory($resolver, $finder, $events);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Register the view finder implementation.
|
|
|
78 |
*
|
|
|
79 |
* @return void
|
|
|
80 |
*/
|
|
|
81 |
public function registerViewFinder()
|
|
|
82 |
{
|
|
|
83 |
$this->app->bind('view.finder', function ($app) {
|
|
|
84 |
return new FileViewFinder($app['files'], $app['config']['view.paths']);
|
|
|
85 |
});
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Register the Blade compiler implementation.
|
|
|
90 |
*
|
|
|
91 |
* @return void
|
|
|
92 |
*/
|
|
|
93 |
public function registerBladeCompiler()
|
|
|
94 |
{
|
|
|
95 |
$this->app->singleton('blade.compiler', function ($app) {
|
|
|
96 |
return tap(new BladeCompiler(
|
|
|
97 |
$app['files'],
|
|
|
98 |
$app['config']['view.compiled'],
|
|
|
99 |
$app['config']->get('view.relative_hash', false) ? $app->basePath() : '',
|
|
|
100 |
$app['config']->get('view.cache', true),
|
|
|
101 |
$app['config']->get('view.compiled_extension', 'php'),
|
|
|
102 |
), function ($blade) {
|
|
|
103 |
$blade->component('dynamic-component', DynamicComponent::class);
|
|
|
104 |
});
|
|
|
105 |
});
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Register the engine resolver instance.
|
|
|
110 |
*
|
|
|
111 |
* @return void
|
|
|
112 |
*/
|
|
|
113 |
public function registerEngineResolver()
|
|
|
114 |
{
|
|
|
115 |
$this->app->singleton('view.engine.resolver', function () {
|
|
|
116 |
$resolver = new EngineResolver;
|
|
|
117 |
|
|
|
118 |
// Next, we will register the various view engines with the resolver so that the
|
|
|
119 |
// environment will resolve the engines needed for various views based on the
|
|
|
120 |
// extension of view file. We call a method for each of the view's engines.
|
|
|
121 |
foreach (['file', 'php', 'blade'] as $engine) {
|
|
|
122 |
$this->{'register'.ucfirst($engine).'Engine'}($resolver);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
return $resolver;
|
|
|
126 |
});
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Register the file engine implementation.
|
|
|
131 |
*
|
|
|
132 |
* @param \Illuminate\View\Engines\EngineResolver $resolver
|
|
|
133 |
* @return void
|
|
|
134 |
*/
|
|
|
135 |
public function registerFileEngine($resolver)
|
|
|
136 |
{
|
|
|
137 |
$resolver->register('file', function () {
|
|
|
138 |
return new FileEngine($this->app['files']);
|
|
|
139 |
});
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Register the PHP engine implementation.
|
|
|
144 |
*
|
|
|
145 |
* @param \Illuminate\View\Engines\EngineResolver $resolver
|
|
|
146 |
* @return void
|
|
|
147 |
*/
|
|
|
148 |
public function registerPhpEngine($resolver)
|
|
|
149 |
{
|
|
|
150 |
$resolver->register('php', function () {
|
|
|
151 |
return new PhpEngine($this->app['files']);
|
|
|
152 |
});
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Register the Blade engine implementation.
|
|
|
157 |
*
|
|
|
158 |
* @param \Illuminate\View\Engines\EngineResolver $resolver
|
|
|
159 |
* @return void
|
|
|
160 |
*/
|
|
|
161 |
public function registerBladeEngine($resolver)
|
|
|
162 |
{
|
|
|
163 |
$resolver->register('blade', function () {
|
|
|
164 |
$compiler = new CompilerEngine($this->app['blade.compiler'], $this->app['files']);
|
|
|
165 |
|
|
|
166 |
$this->app->terminating(static function () use ($compiler) {
|
|
|
167 |
$compiler->forgetCompiledOrNotExpired();
|
|
|
168 |
});
|
|
|
169 |
|
|
|
170 |
return $compiler;
|
|
|
171 |
});
|
|
|
172 |
}
|
|
|
173 |
}
|