-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathServiceProvider.php
More file actions
340 lines (296 loc) · 9.52 KB
/
ServiceProvider.php
File metadata and controls
340 lines (296 loc) · 9.52 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* This file is part of the TwigBridge package.
*
* @copyright Robert Crowe <hello@vivalacrowe.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TwigBridge;
use Illuminate\View\Factory as LaravelFactory;
use Illuminate\View\ViewServiceProvider;
use InvalidArgumentException;
use Twig\Environment;
use Twig\Lexer;
use Twig\Extension\DebugExtension;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\EscaperExtension;
use Twig\Runtime\EscaperRuntime;
use Twig\Loader\ArrayLoader;
use Twig\Loader\ChainLoader;
use Twig\RuntimeLoader\ContainerRuntimeLoader;
use TwigBridge\View\Factory;
/**
* Bootstrap Laravel TwigBridge.
*
* You need to include this `ServiceProvider` in your app.php file:
*
* <code>
* 'providers' => [
* 'TwigBridge\ServiceProvider'
* ];
* </code>
*/
class ServiceProvider extends ViewServiceProvider
{
/**
* {@inheritdoc}
*/
public function register()
{
$this->registerCommands();
$this->registerOptions();
$this->registerLoaders();
$this->registerEngine();
$this->registerAliases();
}
/**
* {@inheritdoc}
*/
public function boot()
{
$this->loadConfiguration();
$this->registerExtension();
}
/**
* Check if we are running Lumen or not.
*
* @return bool
*/
protected function isLumen()
{
return strpos($this->app->version(), 'Lumen') !== false;
}
/**
* Check if we are running on PHP 7.
*
* @return bool
*/
protected function isRunningOnPhp7()
{
return version_compare(PHP_VERSION, '7.0-dev', '>=');
}
/**
* Load the configuration files and allow them to be published.
*
* @return void
*/
protected function loadConfiguration()
{
$configPath = __DIR__ . '/../config/twigbridge.php';
if (! $this->isLumen()) {
$this->publishes([$configPath => config_path('twigbridge.php')], 'config');
}
$this->mergeConfigFrom($configPath, 'twigbridge');
}
/**
* Register the Twig extension in the Laravel View component.
*
* @return void
*/
protected function registerExtension()
{
$this->app['view']->addExtension(
$this->app['twig.extension'],
'twig',
function () {
return $this->app['twig.engine'];
}
);
}
/**
* Register console command bindings.
*
* @return void
*/
protected function registerCommands()
{
$this->app->bindIf('command.twig', function () {
return new Command\TwigBridge;
});
$this->app->bindIf('command.twig.clean', function () {
return new Command\Clean;
});
$this->app->bindIf('command.twig.lint', function () {
return new Command\Lint;
});
$this->commands(
'command.twig',
'command.twig.clean',
'command.twig.lint'
);
}
/**
* Register Twig config option bindings.
*
* @return void
*/
protected function registerOptions()
{
$this->app->bindIf('twig.extension', function () {
return $this->app['config']->get('twigbridge.twig.extension');
});
$this->app->bindIf('twig.options', function () {
$options = $this->app['config']->get('twigbridge.twig.environment', []);
// Check whether we have the cache path set
if (! isset($options['cache']) || is_null($options['cache'])) {
// No cache path set for Twig, lets set to the Laravel views storage folder
$options['cache'] = storage_path('framework/views/twig');
}
return $options;
});
$this->app->bindIf('twig.extensions', function () {
$load = $this->app['config']->get('twigbridge.extensions.enabled', []);
// Is debug enabled?
// If so enable debug extension
$options = $this->app['twig.options'];
$isDebug = (bool) (isset($options['debug'])) ? $options['debug'] : false;
if ($isDebug) {
array_unshift($load, DebugExtension::class);
}
return $load;
});
$this->app->bindIf('twig.lexer', function () {
return null;
});
}
/**
* Register Twig loader bindings.
*
* @return void
*/
protected function registerLoaders()
{
// The array used in the ArrayLoader
$this->app->bindIf('twig.templates', function () {
return [];
});
$this->app->bindIf('twig.loader.array', function ($app) {
return new ArrayLoader($app['twig.templates']);
});
$this->app->bindIf('twig.loader.viewfinder', function () {
return new Twig\Loader(
$this->app['files'],
$this->app['view']->getFinder(),
$this->app['twig.extension']
);
});
$this->app->bindIf(
'twig.loader',
function () {
return new ChainLoader([
$this->app['twig.loader.array'],
$this->app['twig.loader.viewfinder'],
]);
},
true
);
}
/**
* Register Twig engine bindings.
*
* @return void
*/
protected function registerEngine()
{
$this->app->bindIf(
'twig',
function () {
$extensions = $this->app['twig.extensions'];
$lexer = $this->app['twig.lexer'];
$twig = new Bridge(
$this->app['twig.loader'],
$this->app['twig.options'],
$this->app
);
foreach ($this->app['config']->get('twigbridge.twig.safe_classes', []) as $safeClass => $strategy) {
$twig->getRuntime(EscaperRuntime::class)->addSafeClass($safeClass, $strategy);
}
// Register container-based runtime extension loader
$twig->addRuntimeLoader(new ContainerRuntimeLoader($this->app));
// Instantiate and add extensions
foreach ($extensions as $extension) {
// Get an instance of the extension
// Support for string, closure and an object
if (is_string($extension)) {
try {
$extension = $this->app->make($extension);
} catch (\Exception $e) {
throw new InvalidArgumentException(
"Cannot instantiate Twig extension '$extension': " . $e->getMessage()
);
}
} elseif (is_callable($extension)) {
$extension = $extension($this->app, $twig);
} elseif (! is_a($extension, ExtensionInterface::class)) {
throw new InvalidArgumentException('Incorrect extension type');
}
$twig->addExtension($extension);
}
// Set lexer
if (is_a($lexer, Lexer::class)) {
$twig->setLexer($lexer);
}
return $twig;
},
true
);
$this->app->alias('twig', Environment::class);
$this->app->alias('twig', Bridge::class);
$this->app->bindIf('twig.compiler', function () {
return new Engine\Compiler($this->app['twig']);
});
$this->app->bindIf('twig.engine', function () {
return new Engine\Twig(
$this->app['twig.compiler'],
$this->app['twig.loader.viewfinder'],
$this->app['config']->get('twigbridge.twig.globals', [])
);
});
$this->app->extend('view', function (LaravelFactory $view, $app) {
$factory = new Factory($view->getEngineResolver(), $view->getFinder(), $view->getDispatcher());
// 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;
});
}
/**
* Register aliases for classes that had to be renamed because of reserved names in PHP7.
*
* @return void
*/
protected function registerAliases()
{
if (! $this->isRunningOnPhp7() and ! class_exists('TwigBridge\Extension\Laravel\String')) {
class_alias('TwigBridge\Extension\Laravel\Str', 'TwigBridge\Extension\Laravel\String');
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'command.twig',
'command.twig.clean',
'command.twig.lint',
'twig.extension',
'twig.options',
'twig.extensions',
'twig.lexer',
'twig.templates',
'twig.loader.array',
'twig.loader.viewfinder',
'twig.loader',
'twig',
'twig.compiler',
'twig.engine',
];
}
}