Skip to content

Commit

Permalink
using scouts for discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
mozex committed Feb 24, 2024
1 parent 47785d6 commit a4b4e66
Show file tree
Hide file tree
Showing 81 changed files with 1,522 additions and 580 deletions.
39 changes: 27 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
"php": "^8.1",
"illuminate/contracts": "^10.0",
"spatie/laravel-package-tools": "^1.14.0",
"spatie/php-structure-discoverer": "^2.1"
"spatie/php-structure-discoverer": "^2.1",
"spatie/regex": "^3.1"
},
"require-dev": {
"larastan/larastan": "^2.0.1",
"laravel/pint": "^1.0",
"livewire/livewire": "^3.4",
"nunomaduro/collision": "^7.8",
"larastan/larastan": "^2.0.1",
"orchestra/testbench": "^8.8",
"orchestra/testbench": "^8.21",
"pestphp/pest": "^2.20",
"pestphp/pest-plugin-arch": "^2.5",
"pestphp/pest-plugin-laravel": "^2.0",
Expand All @@ -42,26 +44,39 @@
"autoload-dev": {
"psr-4": {
"Mozex\\Modules\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/"
"App\\": "workbench/app/",
"Modules\\": "workbench/Modules/",
"Database\\Factories\\": "workbench/database/factories/"
}
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
"clear": "@php vendor/bin/testbench package:purge-laravel-modules --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": [
"@composer run prepare",
"@php vendor/bin/testbench workbench:build --ansi"
"post-autoload-dump": [
"@clear",
"@prepare",
"@composer run prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"start": [
"Composer\\Config::disableProcessTimeout",
"@composer run build",
"@php vendor/bin/testbench serve"
],
"analyse": "vendor/bin/phpstan analyse",
"analyse": "vendor/bin/phpstan analyse --memory-limit=-1",
"baseline": "@analyse --allow-empty-baseline --generate-baseline",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
]
},
"config": {
"sort-packages": true,
Expand Down
161 changes: 110 additions & 51 deletions config/modules.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,117 @@
<?php

use Mozex\Modules\Enums\AssetType;

return [
'migration_patterns' => [
'Modules/*/Database/Migrations',
[
'pattern' => 'Modules/*/Database/Migrations/*',
'flags' => GLOB_ONLYDIR,
'modules_directory' => 'Modules',
'modules_namespace' => 'Modules\\',
'assets' => [
AssetType::Commands->value => [
'active' => true,
'patterns' => [
'*/Console/Commands',
],
],
AssetType::Migrations->value => [
'active' => true,
'patterns' => [
'*/Database/Migrations',
],
],
AssetType::Helpers->value => [
'active' => true,
'patterns' => [
'*/Helpers/*.php',
'*/Helpers.php',
],
],
AssetType::ServiceProviders->value => [
'active' => true,
'patterns' => [
'*/Providers',
],
],
AssetType::Seeders->value => [
'active' => true,
'patterns' => [
'*/Database/Seeders',
],
],
AssetType::Translations->value => [
'active' => true,
'patterns' => [
'*/Resources/lang',
],
],
AssetType::Schedules->value => [
'active' => true,
'patterns' => [
'*/Console',
],
],
AssetType::Configs->value => [
'active' => true,
'patterns' => [
'*/Config/*.php',
],
],
AssetType::Views->value => [
'active' => true,
'patterns' => [
'*/Resources/views',
],
],
AssetType::Routes->value => [
'active' => true,
'patterns' => [
'*/Routes/*.php',
],
'groups' => [
'api' => [
'prefix' => 'api',
'middlewares' => ['api'],
],
'web' => [
'middlewares' => ['web'],
],
// 'localized' => [
// 'middlewares' => [
// 'web',
// 'localeSessionRedirect',
// 'localizationRedirect',
// ],
// 'prefix' => LaravelLocalization::setLocale()
// ]
],
],
AssetType::Models->value => [
'namespace' => 'Models\\',
],
AssetType::Factories->value => [
'active' => true,
'namespace' => 'Database\\Factories\\',
],
AssetType::Policies->value => [
'active' => true,
'namespace' => 'Policies\\',
],
AssetType::LivewireComponents->value => [
'active' => false,
'patterns' => [
'*/Livewire',
],
],
AssetType::NovaResources->value => [
'active' => false,
'patterns' => [
'*/Nova',
],
],
],
'seeder_patterns' => [
'Modules/*/Database/Seeders/*DatabaseSeeder.php',
],
'route_patterns' => [
'Modules/*/Routes/*.php',
'routes/Modules/*/*.php',
],
'view_patterns' => [
'Modules/*/Resources/views',
'resources/Modules/*/views',
],
'translation_patterns' => [
'Modules/*/Resources/lang',
'resources/Modules/*/lang',
'lang/Modules/*/',
],
'config_patterns' => [
'Modules/*/Config/*.php',
'config/Modules/*/*.php',
],
'command_patterns' => [
'Modules/*/Console/Commands/*.php',
],
'nova_resource_patterns' => [
'Modules/*/Nova/*.php',
],
'livewire_component_patterns' => [
'Modules/*/Livewire/*.php',
],
'helper_patterns' => [
'Modules/*/Helpers/*.php',
'Modules/*/Helpers.php',
],
'service_provider_patterns' => [
'Modules/*/Providers/*.php',
],
'kernel_patterns' => [
'Modules/*/Console/Kernel.php',
],
'api_middleware' => [
'api',
],
'default_middleware' => [
'web',
'modules' => [
// 'Shared' => [
// 'active' => true,
// 'order' => 1, // Optional
// ],
],
'route_groups' => [],
'modules' => [],
];
9 changes: 7 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: src/Contracts/BaseScout.php

-
message: "#^Call to static method resources\\(\\) on an unknown class Laravel\\\\Nova\\\\Nova\\.$#"
count: 1
Expand All @@ -8,9 +13,9 @@ parameters:
-
message: "#^Class Laravel\\\\Nova\\\\Actions\\\\ActionResource not found\\.$#"
count: 1
path: src/ModulesServiceProvider.php
path: src/Scouts/NovaResourcesScout.php

-
message: "#^Class Laravel\\\\Nova\\\\Resource not found\\.$#"
count: 1
path: src/ModulesServiceProvider.php
path: src/Scouts/NovaResourcesScout.php
43 changes: 43 additions & 0 deletions src/Commands/CacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Mozex\Modules\Commands;

use Illuminate\Console\Command;
use Mozex\Modules\Contracts\BaseScout;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;
use Spatie\StructureDiscoverer\Discover;

use function Laravel\Prompts\progress;

class CacheCommand extends Command
{
protected $signature = 'modules:cache';

protected $description = 'Cache all module assets.';

public function handle(): void
{
progress(
label: 'Caching Modules',
steps: Discover::in(__DIR__.'/../')
->classes()
->extending(BaseScout::class)
->custom(fn (DiscoveredClass $structure) => ! $structure->isAbstract)
->get(),
callback: function (string $scout, $progress) {
/** @var BaseScout $discoverer */
$discoverer = app($scout);

if ($discoverer->asset()->isDeactive()) {
return;
}

$progress->label("Caching {$discoverer->asset()->title()}");

$discoverer->cacheDriver()->forget($discoverer->identifier());

$discoverer->cache();
},
);
}
}
41 changes: 41 additions & 0 deletions src/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Mozex\Modules\Commands;

use Illuminate\Console\Command;
use Mozex\Modules\Contracts\BaseScout;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;
use Spatie\StructureDiscoverer\Discover;

use function Laravel\Prompts\progress;

class ClearCommand extends Command
{
protected $signature = 'modules:clear';

protected $description = 'Clear all module assets cache.';

public function handle(): void
{
progress(
label: 'Clearing Modules Cache',
steps: Discover::in(__DIR__.'/../')
->classes()
->extending(BaseScout::class)
->custom(fn (DiscoveredClass $structure) => ! $structure->isAbstract)
->get(),
callback: function (string $scout, $progress) {
/** @var BaseScout $discoverer */
$discoverer = app($scout);

if ($discoverer->asset()->isDeactive()) {
return;
}

$progress->label("Clearing {$discoverer->asset()->title()}");

$discoverer->cacheDriver()->forget($discoverer->identifier());
},
);
}
}
27 changes: 0 additions & 27 deletions src/Concerns/FindsSeeder.php

This file was deleted.

Loading

0 comments on commit a4b4e66

Please sign in to comment.