From d5deffa4d5468521e170cb7650019e5972b34c97 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 6 Jan 2020 14:43:15 +0000 Subject: [PATCH] [6.x] Delay instantiation of translator and view factory (#31009) * Revert "Revert "[6.x] Delay instantiation of transator and view factory"" * Make sure the blade compiler is registered immediately --- src/Illuminate/Support/ServiceProvider.php | 44 +++++++++++++++------ src/Illuminate/View/ViewServiceProvider.php | 25 +++++++----- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index 6894bbf1494a..2ac1120a62d7 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -87,18 +87,18 @@ protected function loadRoutesFrom($path) */ protected function loadViewsFrom($path, $namespace) { - $view = $this->app['view']; - - if (isset($this->app->config['view']['paths']) && - is_array($this->app->config['view']['paths'])) { - foreach ($this->app->config['view']['paths'] as $viewPath) { - if (is_dir($appPath = $viewPath.'/vendor/'.$namespace)) { - $view->addNamespace($namespace, $appPath); + $this->callAfterResolving('view', function ($view) use ($path, $namespace) { + if (isset($this->app->config['view']['paths']) && + is_array($this->app->config['view']['paths'])) { + foreach ($this->app->config['view']['paths'] as $viewPath) { + if (is_dir($appPath = $viewPath.'/vendor/'.$namespace)) { + $view->addNamespace($namespace, $appPath); + } } } - } - $view->addNamespace($namespace, $path); + $view->addNamespace($namespace, $path); + }); } /** @@ -110,7 +110,9 @@ protected function loadViewsFrom($path, $namespace) */ protected function loadTranslationsFrom($path, $namespace) { - $this->app['translator']->addNamespace($namespace, $path); + $this->callAfterResolving('translator', function ($translator) use ($path, $namespace) { + $translator->addNamespace($namespace, $path); + }); } /** @@ -121,7 +123,9 @@ protected function loadTranslationsFrom($path, $namespace) */ protected function loadJsonTranslationsFrom($path) { - $this->app['translator']->addJsonPath($path); + $this->callAfterResolving('translator', function ($translator) use ($path) { + $translator->addJsonPath($path); + }); } /** @@ -132,13 +136,29 @@ protected function loadJsonTranslationsFrom($path) */ protected function loadMigrationsFrom($paths) { - $this->app->afterResolving('migrator', function ($migrator) use ($paths) { + $this->callAfterResolving('migrator', function ($migrator) use ($paths) { foreach ((array) $paths as $path) { $migrator->path($path); } }); } + /** + * Setup an after resolving listener, or fire immediately if already resolved. + * + * @param string $name + * @param callable $callback + * @return void + */ + protected function callAfterResolving($name, $callback) + { + $this->app->afterResolving($name, $callback); + + if ($this->app->resolved($name)) { + $callback($this->app->make($name), $this->app); + } + } + /** * Register paths to be published by the publish command. * diff --git a/src/Illuminate/View/ViewServiceProvider.php b/src/Illuminate/View/ViewServiceProvider.php index c136300c0e53..c06624f82f7c 100755 --- a/src/Illuminate/View/ViewServiceProvider.php +++ b/src/Illuminate/View/ViewServiceProvider.php @@ -22,6 +22,8 @@ public function register() $this->registerViewFinder(); + $this->registerBladeCompiler(); + $this->registerEngineResolver(); } @@ -78,6 +80,20 @@ public function registerViewFinder() }); } + /** + * Register the Blade compiler implementation. + * + * @return void + */ + public function registerBladeCompiler() + { + $this->app->singleton('blade.compiler', function () { + return new BladeCompiler( + $this->app['files'], $this->app['config']['view.compiled'] + ); + }); + } + /** * Register the engine resolver instance. * @@ -133,15 +149,6 @@ public function registerPhpEngine($resolver) */ 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']); });