Skip to content

Commit 38c09be

Browse files
authored
Load Twig environment
1 parent 37b911f commit 38c09be

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/ServiceProvider.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ public function boot()
3434
$configPath = __DIR__ . '/../config/form.php';
3535
$this->publishes([$configPath => config_path('form.php')], 'config');
3636

37-
if ($this->app->bound(\Twig_Environment::class)) {
38-
/** @var \Twig_Environment $twig */
39-
$twig = $this->app->make(\Twig_Environment::class);
40-
} else {
41-
$twig = $this->createTwigEnvironment();
42-
}
4337

38+
$twig = $this->getTwigEnvironment();
4439
$loader = $twig->getLoader();
4540

4641
// If the loader is not already a chain, make it one
@@ -87,7 +82,7 @@ public function register()
8782

8883
$this->app->singleton(TwigRendererEngine::class, function ($app) {
8984
$theme = (array) $app['config']->get('form.theme', 'bootstrap_3_layout.html.twig');
90-
return new TwigRendererEngine($theme, $app->make(\Twig_Environment::class));
85+
return new TwigRendererEngine($theme, $this->getTwigEnvironment());
9186
});
9287

9388
$this->app->singleton(\Symfony\Component\Form\FormRenderer::class, function ($app) {
@@ -203,20 +198,22 @@ protected function getTemplateDirectories()
203198
$dirs = array_merge([$path], $dirs);
204199
return $dirs;
205200
}
206-
201+
202+
207203
/**
208-
* Create a new TwigEnvironment
204+
* Get or create a new TwigEnvironment
209205
*
210206
* @return \Twig_Environment
211207
*/
212-
protected function createTwigEnvironment()
208+
protected function getTwigEnvironment()
213209
{
214-
$loader = new \Twig_Loader_Chain([]);
210+
if ($this->app->bound(\Twig_Environment::class)) {
211+
/** @var \Twig_Environment $twig */
212+
return $this->app->make(\Twig_Environment::class);
213+
}
215214

216-
$environment = new \Twig_Environment($loader, [
215+
return new \Twig_Environment(new \Twig_Loader_Chain([]), [
217216
'cache' => storage_path('framework/views/twig'),
218217
]);
219-
220-
return $environment;
221218
}
222219
}

0 commit comments

Comments
 (0)