-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
ViewServiceProvider.php
169 lines (138 loc) · 4.48 KB
/
ViewServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php namespace Illuminate\View;
use Illuminate\Support\MessageBag;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\BladeEngine;
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->registerEngineResolver();
$this->registerViewFinder();
$this->registerEnvironment();
}
/**
* Register the engine resolver instance.
*
* @return void
*/
public function registerEngineResolver()
{
list($me, $app) = array($this, $this->app);
$app['view.engine.resolver'] = $app->share(function($app) use ($me)
{
$resolver = new EngineResolver;
// Next we will register the various engines with the resolver so that the
// environment can resolve the engines it needs for various views based
// on the extension of view files. We call a method for each engines.
foreach (array('php', 'blade') as $engine)
{
$me->{'register'.ucfirst($engine).'Engine'}($resolver);
}
return $resolver;
});
}
/**
* 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)
{
$app = $this->app;
$resolver->register('blade', function() use ($app)
{
$cache = $app['path'].'/storage/views';
// 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.
$compiler = new BladeCompiler($app['files'], $cache);
return new CompilerEngine($compiler, $app['files']);
});
}
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->app['view.finder'] = $this->app->share(function($app)
{
$paths = $app['config']['view.paths'];
return new FileViewFinder($app['files'], $paths);
});
}
/**
* Register the view environment.
*
* @return void
*/
public function registerEnvironment()
{
$me = $this;
$this->app['view'] = $this->app->share(function($app) use ($me)
{
// 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'];
$events = $app['events'];
$environment = new Environment($resolver, $finder, $events);
// If the current session has an "errors" variable bound to it, we will share
// its value with all view instances so the views can easily access errors
// without having to bind. An empty bag is set when there aren't errors.
if ($me->sessionHasErrors($app))
{
$errors = $app['session']->get('errors');
$environment->share('errors', $errors);
}
// Putting the errors in the view for every view allows the developer to just
// assume that some errors are always available, which is convenient since
// they don't have to continually run checks for the presence of errors.
else
{
$environment->share('errors', new MessageBag);
}
// 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.
$environment->setContainer($app);
$environment->share('app', $app);
return $environment;
});
}
/**
* Determine if the application session has errors.
*
* @param Illuminate\Foundation\Application $app
* @return bool
*/
public function sessionHasErrors($app)
{
$config = $app['config']['session'];
if (isset($app['session']) and ! is_null($config['driver']))
{
return $app['session']->has('errors');
}
}
}