Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Delay instantiation of translator and view factory #31009

Merged
merged 2 commits into from Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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