Skip to content

Commit

Permalink
Ensure autoloading supports application and custom layers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 14, 2024
1 parent 0da548f commit efac879
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 81 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
"App\\": "vendor/orchestra/testbench-core/laravel/app",
"Database\\Factories\\": "vendor/orchestra/testbench-core/laravel/database/factories",
"Database\\Seeders\\": "vendor/orchestra/testbench-core/laravel/database/seeders",
"Domain\\": "vendor/orchestra/testbench-core/laravel/src/Domain"
"Domain\\": "vendor/orchestra/testbench-core/laravel/src/Domain",
"Application\\": "vendor/orchestra/testbench-core/laravel/src/Application",
"Infrastructure\\": "vendor/orchestra/testbench-core/laravel/src/Infrastructure"
}
},
"scripts": {
Expand Down
20 changes: 18 additions & 2 deletions src/Support/DomainAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ protected static function normalizePaths($path): array
->toArray();
}

protected static function getAllLayerPaths(): array
{
return collect([
DomainResolver::domainPath(),
DomainResolver::applicationLayerPath(),
...array_values(config('ddd.layers', [])),
])->map(fn ($path) => app()->basePath($path))->toArray();
}

protected static function getCustomLayerPaths(): array
{
return collect([
...array_values(config('ddd.layers', [])),
])->map(fn ($path) => app()->basePath($path))->toArray();
}

protected function handleProviders(): void
{
$providers = DomainCache::has('domain-providers')
Expand Down Expand Up @@ -155,7 +171,7 @@ protected static function discoverProviders(): array

$paths = static::normalizePaths(
$configValue === true
? app()->basePath(DomainResolver::domainPath())
? static::getAllLayerPaths()
: $configValue
);

Expand All @@ -179,7 +195,7 @@ protected static function discoverCommands(): array

$paths = static::normalizePaths(
$configValue === true ?
app()->basePath(DomainResolver::domainPath())
static::getAllLayerPaths()
: $configValue
);

Expand Down
10 changes: 0 additions & 10 deletions src/Support/DomainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,6 @@ public static function resolveLayer(string $domain, ?string $type = null): ?Laye
*/
public static function getDomainObjectNamespace(string $domain, string $type, ?string $name = null): string
{
// $customResolver = app('ddd')->getNamespaceResolver();

// $resolved = is_callable($customResolver)
// ? $customResolver($domain, $type, $name, app('ddd')->getCommandContext())
// : null;

// if (! is_null($resolved)) {
// return $resolved;
// }

$resolver = function (string $domain, string $type, ?string $name) {
$layer = static::resolveLayer($domain, $type);

Expand Down
3 changes: 2 additions & 1 deletion tests/.skeleton/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"psr-4": {
"App\\": "app/",
"Support\\": "src/Support"
"Application\\": "src/Application",
"Infrastructure\\": "src/Infrastructure"
}
},
"autoload-dev": {
Expand Down
24 changes: 24 additions & 0 deletions tests/.skeleton/src/Application/Commands/ApplicationSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Application\Commands;

use Illuminate\Console\Command;
use Infrastructure\Models\AppSession;

class ApplicationSync extends Command
{
protected $signature = 'application:sync';

protected $description = 'Sync application state.';

public function handle()
{
$this->info('Application state synced!');

if ($secret = AppSession::getSecret()) {
$this->line($secret);

return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
//
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
23 changes: 23 additions & 0 deletions tests/.skeleton/src/Application/Models/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Application\Models;

use Illuminate\Database\Eloquent\Model;
use Lunarstorm\LaravelDDD\Factories\HasDomainFactory;

class Login extends Model
{
use HasDomainFactory;

protected static $secret = null;

public static function setSecret($secret): void
{
self::$secret = $secret;
}

public static function getSecret(): ?string
{
return self::$secret;
}
}
47 changes: 47 additions & 0 deletions tests/.skeleton/src/Application/Policies/LoginPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Application\Policies;

use App\Models\User;
use Application\Models\Login;
use Illuminate\Auth\Access\HandlesAuthorization;

class LoginPolicy
{
use HandlesAuthorization;

public function viewAny(User $user)
{
//
}

public function view(User $user, Login $login)
{
//
}

public function create(User $user)
{
//
}

public function update(User $user, Login $login)
{
//
}

public function delete(User $user, Login $login)
{
//
}

public function restore(User $user, Login $login)
{
//
}

public function forceDelete(User $user, Login $login)
{
//
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Application\Providers;

use Infrastructure\Models\AppSession;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class ApplicationServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('application-layer', function (Application $app) {
return 'application-layer-singleton';
});
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
AppSession::setSecret('application-secret');
}
}
25 changes: 25 additions & 0 deletions tests/.skeleton/src/Infrastructure/Commands/LogPrune.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Infrastructure\Commands;

use Illuminate\Console\Command;
use Infrastructure\Models\AppSession;
use Infrastructure\Support\Clipboard;

class LogPrune extends Command
{
protected $signature = 'log:prune';

protected $description = 'Prune system logs.';

public function handle()
{
$this->info('System logs pruned!');

if ($secret = Clipboard::get('secret')) {
$this->line($secret);

return;
}
}
}
23 changes: 23 additions & 0 deletions tests/.skeleton/src/Infrastructure/Models/AppSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Infrastructure\Models;

use Illuminate\Database\Eloquent\Model;
use Lunarstorm\LaravelDDD\Factories\HasDomainFactory;

class AppSession extends Model
{
use HasDomainFactory;

protected static $secret = null;

public static function setSecret($secret): void
{
self::$secret = $secret;
}

public static function getSecret(): ?string
{
return self::$secret;
}
}
47 changes: 47 additions & 0 deletions tests/.skeleton/src/Infrastructure/Policies/AppSessionPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Infrastructure\Policies;

use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Infrastructure\Models\AppSession;

class AppSessionPolicy
{
use HandlesAuthorization;

public function viewAny(User $user)
{
//
}

public function view(User $user, AppSession $appSession)
{
//
}

public function create(User $user)
{
//
}

public function update(User $user, AppSession $appSession)
{
//
}

public function delete(User $user, AppSession $appSession)
{
//
}

public function restore(User $user, AppSession $appSession)
{
//
}

public function forceDelete(User $user, AppSession $appSession)
{
//
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Infrastructure\Providers;

use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Infrastructure\Support\Clipboard;

class InfrastructureServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('infrastructure-layer', function (Application $app) {
return 'infrastructure-layer-singleton';
});
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Clipboard::set('secret', 'infrastructure-secret');
}
}
18 changes: 18 additions & 0 deletions tests/.skeleton/src/Infrastructure/Support/Clipboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Infrastructure\Support;

class Clipboard
{
protected static $clips = [];

public static function set(string $key, $value): void
{
data_set(static::$clips, $key, $value);
}

public static function get(string $key)
{
return data_get(static::$clips, $key);
}
}
Loading

0 comments on commit efac879

Please sign in to comment.