Skip to content

Commit

Permalink
[6.x] Delay instantiation of translator and view factory (#31009)
Browse files Browse the repository at this point in the history
* Revert "Revert "[6.x] Delay instantiation of transator and view factory""

* Make sure the blade compiler is registered immediately
  • Loading branch information
GrahamCampbell authored and taylorotwell committed Jan 6, 2020
1 parent ef72716 commit d5deffa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
44 changes: 32 additions & 12 deletions src/Illuminate/Support/ServiceProvider.php
Expand Up @@ -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);
});
}

/**
Expand All @@ -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);
});
}

/**
Expand All @@ -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);
});
}

/**
Expand All @@ -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.
*
Expand Down
25 changes: 16 additions & 9 deletions src/Illuminate/View/ViewServiceProvider.php
Expand Up @@ -22,6 +22,8 @@ public function register()

$this->registerViewFinder();

$this->registerBladeCompiler();

$this->registerEngineResolver();
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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']);
});
Expand Down

0 comments on commit d5deffa

Please sign in to comment.