Skip to content

Commit

Permalink
Modifications: Adjust the Path and Namespace generation for laravel-m…
Browse files Browse the repository at this point in the history
…odules v11

- Bug Fix: Fix translation key in cluster generation
- Changed and tested Plugin and Cluster Generation
- Changed and Tested Resource Generation
  • Loading branch information
coolsam726 committed Apr 15, 2024
1 parent 9cbcf50 commit 71ac6cc
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ phpunit.xml
phpstan.neon
testbench.yaml
vendor
.phpunit.cache
29 changes: 23 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
"require": {
"php": "^8.1",
"filament/filament": "^3.0",
"nwidart/laravel-modules": "^10.0",
"nwidart/laravel-modules": "^11.0",
"spatie/laravel-package-tools": "^1.15.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9|^8.1",
"nunomaduro/collision": "^8.1",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^8.0|^9.0",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^2.1",
"pestphp/pest-plugin-arch": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
Expand All @@ -51,17 +51,34 @@
}
},
"scripts": {
"post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi",
"post-autoload-dump": [
"@clear",
"@prepare",
"@php ./vendor/bin/testbench package:discover --ansi"
],
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"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",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"wikimedia/composer-merge-plugin": true
}
},
"extra": {
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/ModuleFilamentInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function getModule(): \Nwidart\Modules\Module
{
try {
return Module::findOrFail($this->moduleName);
} catch (ModuleNotFoundException | \Throwable $exception) {
} catch (ModuleNotFoundException|\Throwable $exception) {
if (confirm("Module $this->moduleName does not exist. Would you like to generate it?", true)) {
$this->call('module:make', ['name' => [$this->moduleName]]);

Expand All @@ -101,9 +101,11 @@ private function ensureFilamentDirectoriesExist(): void
}

if ($this->cluster) {
$dir = $this->getModule()->appPath('Filament/Clusters');
if (! is_dir($dir = $this->getModule()->appPath('Filament/Clusters'))) {
$this->makeDirectory($dir);
}

} else {
if (! is_dir($dir = $this->getModule()->appPath('Filament/Pages'))) {
$this->makeDirectory($dir);
Expand Down Expand Up @@ -131,7 +133,7 @@ protected function createDefaultFilamentPlugin(): void
{
$module = $this->getModule();
$this->call('module:make:filament-plugin', [
'name' => $module->getStudlyName() . 'Plugin',
'name' => $module->getStudlyName().'Plugin',
'module' => $module->getStudlyName(),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ModuleMakeFilamentClusterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ModuleMakeFilamentClusterCommand extends GeneratorCommand

protected function getRelativeNamespace(): string
{
return 'App\\Filament\\Clusters';
return 'Filament\\Clusters';
}

protected function getStub(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ModuleMakeFilamentPluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ModuleMakeFilamentPluginCommand extends GeneratorCommand

protected function getRelativeNamespace(): string
{
return 'App\\Filament';
return 'Filament';
}

protected function getStub(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/stubs/filament-cluster.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class {{ class }} extends Cluster

public static function getNavigationLabel(): string
{
return '__({{ navigationLabel }})';
return __('{{ navigationLabel }}');
}

public static function getNavigationIcon(): ?string
Expand Down
12 changes: 6 additions & 6 deletions src/Concerns/GeneratesModularFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function getArguments(): array

protected function resolveStubPath($stub): string
{
return FilamentModules::packagePath('src/Commands/' . trim($stub, DIRECTORY_SEPARATOR));
return FilamentModules::packagePath('src/Commands/'.trim($stub, DIRECTORY_SEPARATOR));
}

public function getModule(): Module
Expand All @@ -29,7 +29,7 @@ public function getModule(): Module

protected function getDefaultNamespace($rootNamespace): string
{
return trim($rootNamespace, '\\') . '\\' . trim(Str::replace(DIRECTORY_SEPARATOR, '\\', $this->getRelativeNamespace()), '\\');
return trim($rootNamespace, '\\').'\\'.trim(Str::replace(DIRECTORY_SEPARATOR, '\\', $this->getRelativeNamespace()), '\\');
}

abstract protected function getRelativeNamespace(): string;
Expand All @@ -41,9 +41,9 @@ protected function rootNamespace(): string

protected function getPath($name): string
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$name = Str::replaceFirst($this->rootNamespace(), 'app', $name);

return $this->getModule()->getExtraPath(str_replace('\\', '/', $name) . '.php');
return $this->getModule()->getExtraPath(str_replace('\\', '/', $name).'.php');
}

protected function possibleModels()
Expand All @@ -61,7 +61,7 @@ protected function viewPath($path = ''): string
{
$views = $this->getModule()->resourcesPath('views');

return $views . ($path ? DIRECTORY_SEPARATOR . $path : $path);
return $views.($path ? DIRECTORY_SEPARATOR.$path : $path);
}

protected function buildClass($name)
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function promptForMissingArgumentsUsing(): array
{
return [
'name' => [
'What should the ' . strtolower($this->type ?: 'class') . ' be named?',
'What should the '.strtolower($this->type ?: 'class').' be named?',
match ($this->type) {
'Cast' => 'E.g. Json',
'Channel' => 'E.g. OrderChannel',
Expand Down
15 changes: 9 additions & 6 deletions src/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ public function getModule(string $name): \Nwidart\Modules\Module
public function convertPathToNamespace(string $fullPath): string
{
$base = str(trim(config('modules.paths.modules', base_path('Modules')), '/'));
$relative = str(trim(config('modules.namespace', 'Modules'), '\\'))->replace('\\', DIRECTORY_SEPARATOR)->toString();

return str($fullPath)
->replace($base, $relative)
->replace('.php', '')
$relative = str($fullPath)->afterLast($base)->replaceFirst('/app/', '/');

return str($relative)
->ltrim('/')
->prepend('/')
->prepend(config('modules.namespace', 'Modules'))
->replace(DIRECTORY_SEPARATOR, '\\')
->rtrim('.php')
->explode(DIRECTORY_SEPARATOR)
->map(fn ($piece) => str($piece)->studly()->toString())
->implode('\\');
Expand All @@ -45,6 +48,6 @@ public function execCommand(string $command, ?Command $artisan = null): void
public function packagePath(string $path = ''): string
{
//return the base path of this package
return dirname(__DIR__ . '../') . ($path ? DIRECTORY_SEPARATOR . trim($path, DIRECTORY_SEPARATOR) : '');
return dirname(__DIR__.'../').($path ? DIRECTORY_SEPARATOR.trim($path, DIRECTORY_SEPARATOR) : '');
}
}
5 changes: 3 additions & 2 deletions src/ModulesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function register(Panel $panel): void
{
$panel
->topNavigation(config('filament-modules.clusters.enabled', false) && config('filament-modules.clusters.use-top-navigation', false));
foreach ($this->getModulePlugins() as $modulePlugin) {
$plugins = $this->getModulePlugins();
foreach ($plugins as $modulePlugin) {
$panel->plugin($modulePlugin::make());
}
}
Expand Down Expand Up @@ -46,7 +47,7 @@ protected function getModulePlugins(): array
}
// get a glob of all Filament plugins
$basePath = str(config('modules.paths.modules', 'Modules'));
$pattern = $basePath . '/*/App/Filament/*Plugin.php';
$pattern = $basePath.'/*/app/Filament/*Plugin.php';
$pluginPaths = glob($pattern);

return collect($pluginPaths)->map(fn ($path) => FilamentModules::convertPathToNamespace($path))->toArray();
Expand Down
24 changes: 12 additions & 12 deletions src/ModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function packageBooted(): void

// Handle Stubs
if (app()->runningInConsole()) {
foreach (app(Filesystem::class)->files(__DIR__ . '/../stubs/') as $file) {
foreach (app(Filesystem::class)->files(__DIR__.'/../stubs/') as $file) {
$this->publishes([
$file->getRealPath() => base_path("stubs/modules/{$file->getFilename()}"),
], 'modules-stubs');
Expand Down Expand Up @@ -170,44 +170,44 @@ protected function registerModuleMacros(): void
$relativeNamespace = str_replace('App\\', '', $relativeNamespace);
$relativeNamespace = str_replace('App', '', $relativeNamespace);
$relativeNamespace = trim($relativeNamespace, '\\');
$relativeNamespace = 'App\\' . $relativeNamespace;
$relativeNamespace = '\\'.$relativeNamespace;

return $this->namespace($relativeNamespace);
});
Module::macro('appPath', function (string $relativePath = '') {
$appPath = $this->getExtraPath('App');
$appPath = $this->getExtraPath('app');

return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : '');
return $appPath.($relativePath ? DIRECTORY_SEPARATOR.$relativePath : '');
});

Module::macro('databasePath', function (string $relativePath = '') {
$appPath = $this->getExtraPath('Database');
$appPath = $this->getExtraPath('database');

return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : '');
return $appPath.($relativePath ? DIRECTORY_SEPARATOR.$relativePath : '');
});

Module::macro('resourcesPath', function (string $relativePath = '') {
$appPath = $this->getExtraPath('resources');

return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : '');
return $appPath.($relativePath ? DIRECTORY_SEPARATOR.$relativePath : '');
});

Module::macro('migrationsPath', function (string $relativePath = '') {
$appPath = $this->databasePath('migrations');

return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : '');
return $appPath.($relativePath ? DIRECTORY_SEPARATOR.$relativePath : '');
});

Module::macro('seedersPath', function (string $relativePath = '') {
$appPath = $this->databasePath('Seeders');
$appPath = $this->databasePath('seeders');

return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : '');
return $appPath.($relativePath ? DIRECTORY_SEPARATOR.$relativePath : '');
});

Module::macro('factoriesPath', function (string $relativePath = '') {
$appPath = $this->databasePath('Factories');
$appPath = $this->databasePath('factories');

return $appPath . ($relativePath ? DIRECTORY_SEPARATOR . $relativePath : '');
return $appPath.($relativePath ? DIRECTORY_SEPARATOR.$relativePath : '');
});
}
}
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

12 changes: 12 additions & 0 deletions tests/ModulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

it('can test', function () {
expect(true)->toBeTrue();
});

// test that a module can be generated successfully
it('can generate a module', function () {
$this->artisan('module:make', ['name' => ['Example']])
->expectsOutput('Module created successfully.')
->assertExitCode(0);
});
9 changes: 7 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@
use Filament\Widgets\WidgetsServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Livewire\LivewireServiceProvider;
use Nwidart\Modules\LaravelModulesServiceProvider;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;
use RyanChandler\BladeCaptureDirective\BladeCaptureDirectiveServiceProvider;

class TestCase extends Orchestra
{
use WithWorkbench;

protected function setUp(): void
{
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Coolsam\\Modules\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
fn (string $modelName) => 'Coolsam\\Modules\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
}

protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [
ActionsServiceProvider::class,
Expand All @@ -44,6 +48,7 @@ protected function getPackageProviders($app)
SupportServiceProvider::class,
TablesServiceProvider::class,
WidgetsServiceProvider::class,
LaravelModulesServiceProvider::class,
ModulesServiceProvider::class,
];
}
Expand Down

0 comments on commit 71ac6cc

Please sign in to comment.