Skip to content

Commit

Permalink
WIP: Re-working the autoloading internals.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 15, 2024
1 parent e3f72ad commit ee5daaf
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 232 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"Lunarstorm\\LaravelDDD\\LaravelDDDServiceProvider"
],
"aliases": {
"LaravelDDD": "Lunarstorm\\LaravelDDD\\Facades\\LaravelDDD"
"DDD": "Lunarstorm\\LaravelDDD\\Facades\\DDD"
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions routes/testing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Support\Facades\Route;
use Lunarstorm\LaravelDDD\Facades\Autoload;

Route::prefix('laravel-ddd')
->middleware(['web'])
->as('ddd.')
->group(function () {
Route::get('/', function () {
return response('home');
})->name('home');

Route::get('/config', function () {
return response(config('ddd'));
})->name('config');

Route::get('/autoload', function () {
return response([
'providers' => Autoload::getRegisteredProviders(),
'commands' => Autoload::getRegisteredCommands(),
]);
})->name('autoload');
});
6 changes: 3 additions & 3 deletions src/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lunarstorm\LaravelDDD;

use Lunarstorm\LaravelDDD\Support\Autoloader;
use Lunarstorm\LaravelDDD\Support\AutoloadManager;
use Lunarstorm\LaravelDDD\Support\GeneratorBlueprint;
use Lunarstorm\LaravelDDD\Support\Path;

Expand Down Expand Up @@ -43,9 +43,9 @@ public function __construct()
$this->commandContext = null;
}

public function autoloader(): Autoloader
public function autoloader(): AutoloadManager
{
return app(Autoloader::class);
return app(AutoloadManager::class);
}

public function composer(): ComposerManager
Expand Down
25 changes: 25 additions & 0 deletions src/Facades/Autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Lunarstorm\LaravelDDD\Facades;

use Illuminate\Support\Facades\Facade;
use Lunarstorm\LaravelDDD\Support\AutoloadManager;

/**
* @see \Lunarstorm\LaravelDDD\Support\AutoloadManager
*
* @method static void boot()
* @method static array getAllLayerPaths()
* @method static array getCustomLayerPaths()
* @method static array getRegisteredCommands()
* @method static array getRegisteredProviders()
* @method static Autoload cacheCommands()
* @method static Autoload cacheProviders()
*/
class Autoload extends Facade
{
protected static function getFacadeAccessor()
{
return AutoloadManager::class;
}
}
2 changes: 1 addition & 1 deletion src/Facades/DDD.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @method static void filterAutoloadPathsUsing(callable $filter)
* @method static void resolveObjectSchemaUsing(callable $resolver)
* @method static string packagePath(string $path = '')
* @method static \Lunarstorm\LaravelDDD\Support\Autoloader autoloader()
* @method static \Lunarstorm\LaravelDDD\Support\AutoloadManager autoloader()
* @method static \Lunarstorm\LaravelDDD\ConfigManager config()
* @method static \Lunarstorm\LaravelDDD\StubManager stubs()
* @method static \Lunarstorm\LaravelDDD\ComposerManager composer()
Expand Down
27 changes: 10 additions & 17 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Lunarstorm\LaravelDDD;

use Illuminate\Database\Migrations\MigrationCreator;
use Lunarstorm\LaravelDDD\Facades\DDD;
use Lunarstorm\LaravelDDD\Support\Autoloader;
use Lunarstorm\LaravelDDD\Facades\Autoload;
use Lunarstorm\LaravelDDD\Support\AutoloadManager;
use Lunarstorm\LaravelDDD\Support\DomainMigration;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -17,8 +16,8 @@ public function configurePackage(Package $package): void
return new DomainManager;
});

$this->app->scoped(Autoloader::class, function () {
return new Autoloader;
$this->app->scoped(AutoloadManager::class, function () {
return new AutoloadManager;
});

$this->app->scoped(ComposerManager::class, function () {
Expand All @@ -34,7 +33,7 @@ public function configurePackage(Package $package): void
});

$this->app->bind('ddd', DomainManager::class);
$this->app->bind('ddd.autoloader', Autoloader::class);
$this->app->bind('ddd.autoloader', AutoloadManager::class);
$this->app->bind('ddd.config', ConfigManager::class);
$this->app->bind('ddd.composer', ComposerManager::class);
$this->app->bind('ddd.stubs', StubManager::class);
Expand Down Expand Up @@ -92,6 +91,10 @@ public function configurePackage(Package $package): void
$package->hasCommand(Commands\DomainInterfaceMakeCommand::class);
$package->hasCommand(Commands\DomainTraitMakeCommand::class);
}

if ($this->app->runningUnitTests()) {
$package->hasRoutes(['testing']);
}
}

protected function laravelVersion($value)
Expand All @@ -111,21 +114,11 @@ protected function registerMigrations()
return new Commands\Migration\DomainMigrateMakeCommand($creator, $composer);
});

// $this->app->when(MigrationCreator::class)
// ->needs('$customStubPath')
// ->give(function ($app) {
// return $app->basePath('stubs');
// });

$this->loadMigrationsFrom(DomainMigration::paths());
}

public function packageBooted()
{
// $this->publishes([
// $this->package->basePath('/../stubs') => $this->app->basePath("stubs/{$this->package->shortName()}"),
// ], "{$this->package->shortName()}-stubs");

if ($this->app->runningInConsole() && method_exists($this, 'optimizes')) {
$this->optimizes(
optimize: 'ddd:optimize',
Expand All @@ -134,7 +127,7 @@ public function packageBooted()
);
}

DDD::autoloader()->boot();
Autoload::boot();
}

public function packageRegistered()
Expand Down
113 changes: 85 additions & 28 deletions src/Support/Autoloader.php → src/Support/AutoloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\Finder\SplFileInfo;
use Throwable;

class Autoloader
class AutoloadManager
{
use Conditionable;

Expand All @@ -30,7 +30,13 @@ class Autoloader

protected array $registeredProviders = [];

protected bool $isBooted = false;
protected array $resolvedPolicies = [];

protected array $resolvedFactories = [];

protected bool $booted = false;

protected bool $consoleBooted = false;

public function __construct()
{
Expand All @@ -43,31 +49,44 @@ public function boot()
return $this;
}

// if ($this->isBooted) {
// return $this;
// }

$this
->flush()
->when(config('ddd.autoload.providers') === true, fn () => $this->handleProviders())
->when(app()->runningInConsole() && config('ddd.autoload.commands') === true, fn () => $this->handleCommands())
->when(config('ddd.autoload.policies') === true, fn () => $this->handlePolicies())
->when(config('ddd.autoload.factories') === true, fn () => $this->handleFactories());
->when(config('ddd.autoload.factories') === true, fn () => $this->handleFactories())
->run();

if (app()->runningInConsole()) {
ConsoleApplication::starting(function ($artisan) {
foreach ($this->registeredCommands as $command) {
$artisan->resolve($command);
}
});
$this->booted = true;
}

public function isBooted(): bool
{
return $this->booted;
}

public function isConsoleBooted(): bool
{
return $this->consoleBooted;
}

protected function flush()
{
foreach ($this->registeredProviders as $provider) {
app()->forgetInstance($provider);
}

$this->isBooted = true;
$this->registeredProviders = [];

$this->registeredCommands = [];

$this->resolvedPolicies = [];

$this->resolvedFactories = [];

return $this;
}

public function run() {}

protected function normalizePaths($path): array
{
return collect($path)
Expand Down Expand Up @@ -97,6 +116,10 @@ protected function handleProviders()
? DomainCache::get('domain-providers')
: $this->discoverProviders();

foreach ($this->registeredProviders as $provider) {
app()->forgetInstance($provider);
}

$this->registeredProviders = [];

foreach ($providers as $provider) {
Expand All @@ -117,7 +140,25 @@ protected function handleCommands()

foreach ($commands as $command) {
$this->registeredCommands[$command] = $command;
$this->registerCommand($command);
}

return $this;
}

protected function run()
{
foreach ($this->registeredProviders as $provider) {
app()->register($provider);
}

if (app()->runningInConsole() && ! $this->isConsoleBooted()) {
ConsoleApplication::starting(function ($artisan) {
foreach ($this->registeredCommands as $command) {
$artisan->resolve($command);
}
});

$this->consoleBooted = true;
}

return $this;
Expand All @@ -133,21 +174,31 @@ public function getRegisteredProviders(): array
return $this->registeredProviders;
}

protected function registerCommand($class)
public function getResolvedPolicies(): array
{
// ConsoleApplication::starting(function ($artisan) use ($class) {
// dump('resolving command', $class, $this->registeredCommands);
// $artisan->resolve($class);
// });
return $this->resolvedPolicies;
}

public function getResolvedFactories(): array
{
return $this->resolvedFactories;
}

protected function handlePolicies()
{
Gate::guessPolicyNamesUsing(static function (string $class): array|string {
Gate::guessPolicyNamesUsing(function (string $class): array|string {
if (array_key_exists($class, $this->resolvedPolicies)) {
return $this->resolvedPolicies[$class];
}

if ($model = DomainObject::fromClass($class, 'model')) {
return (new Domain($model->domain))
$resolved = (new Domain($model->domain))
->object('policy', "{$model->name}Policy")
->fullyQualifiedName;

$this->resolvedPolicies[$class] = $resolved;

return $resolved;
}

$classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class)));
Expand All @@ -169,7 +220,13 @@ protected function handlePolicies()
protected function handleFactories()
{
Factory::guessFactoryNamesUsing(function (string $modelName) {
if (array_key_exists($modelName, $this->resolvedFactories)) {
return $this->resolvedFactories[$modelName];
}

if ($factoryName = DomainFactory::resolveFactoryName($modelName)) {
$this->resolvedFactories[$modelName] = $factoryName;

return $factoryName;
}

Expand Down Expand Up @@ -253,16 +310,16 @@ protected function discoverCommands(): array
->toArray();
}

public function cacheProviders()
public function cacheCommands()
{
DomainCache::set('domain-providers', $this->discoverProviders());
DomainCache::set('domain-commands', $this->discoverCommands());

return $this;
}

public function cacheCommands()
public function cacheProviders()
{
DomainCache::set('domain-commands', $this->discoverCommands());
DomainCache::set('domain-providers', $this->discoverProviders());

return $this;
}
Expand Down
Loading

0 comments on commit ee5daaf

Please sign in to comment.