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

Ensure autoloading on cached config. #1242

Merged
merged 3 commits into from
Nov 18, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/CapsulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CapsulesServiceProvider extends RouteServiceProvider
{
use HasRoutes, HasCapsules;

public static $capsulesBootstrapped = false;

protected $manager;

protected function mergeTwillConfig()
Expand All @@ -31,6 +33,7 @@ public function register()
{
$this->registerManager();
$this->mergeTwillConfig();
$this->bootCapsules();
}

public function boot()
Expand All @@ -46,6 +49,23 @@ public function registerCapsules()
});
}

/*
* Boot the capsules so their psr, config and service providers are booted.
*
* @see HasCapsules::bootstrapCapsule
*/
public function bootCapsules()
{
if (!self::$capsulesBootstrapped) {
$this->getCapsuleList()
->where('enabled', true)
->each(function ($capsule) {
$this->bootstrapCapsule($capsule);
});
self::$capsulesBootstrapped = true;
}
}

protected function registerCapsule($capsule)
{
$this->loadMigrationsFrom($capsule['migrations_dir']);
Expand Down
29 changes: 14 additions & 15 deletions src/Services/Capsules/HasCapsules.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public function getCapsuleList()

$list = collect(config('twill.capsules.list'));

if (config('twill.capsules.loaded')) {
return $list;
if (!config('twill.capsules.loaded')) {
$list = $list
->where('enabled', true)
->map(function ($capsule) use ($path) {
return $this->makeCapsule($capsule, $path);
});
}

return $list
->where('enabled', true)
->map(function ($capsule) use ($path) {
return $this->makeCapsule($capsule, $path);
});
return $list;
}

public function getCapsuleByModel($model)
Expand Down Expand Up @@ -60,12 +60,10 @@ public function getCapsulesPath()
*/
public function getCapsulesSubdir()
{
$subdir = config('twill.capsules.namespaces.subdir');

return $subdir;
return config('twill.capsules.namespaces.subdir');
}

public function makeCapsule($capsule, $basePath = null)
public function makeCapsule($capsule, $basePath = null): array
{
$basePath = $basePath ?? $this->getCapsulesPath();

Expand Down Expand Up @@ -159,13 +157,14 @@ public function makeCapsule($capsule, $basePath = null)

$capsule['config'] = $this->loadCapsuleConfig($capsule);

$this->registerPsr4Autoloader($capsule);
return $capsule;
}

public function bootstrapCapsule($capsule): void
{
$this->registerPsr4Autoloader($capsule);
$this->autoloadConfigFiles($capsule);

$this->registerServiceProvider($capsule);

return $capsule;
}

public function registerPsr4Autoloader($capsule)
Expand Down