Skip to content

Commit

Permalink
Merge pull request #27 from savannabits/develop
Browse files Browse the repository at this point in the history
Bug Fixes and Refactoring: Generation of additional Contexts in the same module is now working!
  • Loading branch information
coolsam726 committed Jul 17, 2023
2 parents 6e525cb + e6fcf52 commit 6fc7c55
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 70 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Fix PHP code style issues

on: [push]
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
php-code-styling:
Expand All @@ -18,4 +22,4 @@ jobs:
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
commit_message: Fix styling
76 changes: 38 additions & 38 deletions src/Commands/FilamentModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle(): int
try {
$this->module = app('modules')->findOrFail($this->getModuleName());
} catch (\Throwable $exception) {
\Log::info($exception->getMessage().' Creating It ...');
\Log::info($exception->getMessage() . ' Creating It ...');
Artisan::call('module:make', ['name' => [$this->argument('module')]]);
$this->module = app('modules')->findOrFail($this->getModuleName());
}
Expand All @@ -44,7 +44,7 @@ public function handle(): int
$this->copyStubs($context);

$this->createDirectories($context);

Artisan::call('module:make-filament-page', ['module' => $this->module->getName(), 'name' => 'Dashboard', 'context' => $context, '-n']);
$this->info("Successfully created {$context} context!");

if ($this->option('guard')) {
Expand All @@ -62,7 +62,7 @@ public function handle(): int
protected function getContextInput(): string
{
return $this->validateInput(
fn () => $this->argument('module') ?? $this->askRequired('Module Name (e.g. `sales`)', 'module'),
fn() => $this->argument('module') ?? $this->askRequired('Module Name (e.g. `sales`)', 'module'),
'module',
['required']
);
Expand Down Expand Up @@ -92,7 +92,7 @@ protected function getOptions(): array

public function getModuleNamespace()
{
return $this->laravel['modules']->config('namespace').'\\'.$this->getModuleName();
return $this->laravel['modules']->config('namespace') . '\\' . $this->getModuleName();
}

protected function copyStubs($context)
Expand All @@ -107,7 +107,7 @@ protected function copyStubs($context)
->append('.php');

$configPath = $contextName
->prepend($this->module->getLowerName().'-')
->prepend($this->module->getLowerName() . '-')
->prepend('/')
->prepend(module_path($this->getModuleName(), 'Config'))
->append('.php');
Expand All @@ -118,21 +118,21 @@ protected function copyStubs($context)
->prepend('\\\\')
->prepend($moduleNs->replace('\\', '\\\\')->toString());

if (! $this->option('force') && $this->checkForCollision([
$serviceProviderPath,
])) {
if (!$this->option('force') && $this->checkForCollision([
$serviceProviderPath,
])) {
return static::INVALID;
}

if (! $this->option('force') && $this->checkForCollision([
$configPath,
])) {
if (!$this->option('force') && $this->checkForCollision([
$configPath,
])) {
return static::INVALID;
}

$this->copyStubToApp('ContextServiceProvider', $serviceProviderPath, [
'class' => (string) $serviceProviderClass,
'name' => (string) $contextName,
'class' => (string)$serviceProviderClass,
'name' => (string)$contextName,
'Module' => $this->module->getStudlyName(),
'module' => $this->module->getLowerName(),
'module_' => $this->module->getSnakeName(),
Expand All @@ -150,14 +150,14 @@ protected function copyStubs($context)
->prepend(module_path($this->getModuleName(), 'Http/Middleware/'))
->append('.php');

if (! $this->option('force') && $this->checkForCollision([$middlewarePath])) {
if (!$this->option('force') && $this->checkForCollision([$middlewarePath])) {
return static::INVALID;
}

$middlewareNs = $moduleNs->append('\\Http\\\Middleware');
$this->copyStubToApp('ContextMiddleware', $middlewarePath, [
'class' => (string) $middlewareClass,
'context' => (string) $contextName,
'class' => (string)$middlewareClass,
'context' => (string)$contextName,
'module' => $this->module->getStudlyName(),
'namespace' => $middlewareNs->replace('\\\\', '\\')->toString(),
]);
Expand All @@ -170,23 +170,23 @@ protected function copyStubs($context)
->prepend(module_path($this->getModuleName(), 'Http/Livewire/Auth/'))
->append('.php');

if (! $this->option('force') && $this->checkForCollision([$loginPath])) {
if (!$this->option('force') && $this->checkForCollision([$loginPath])) {
return static::INVALID;
}

$loginNs = $moduleNs->append('\\Http\\Livewire\\Auth');
$this->copyStubToApp('ContextLogin', $loginPath, [
'class' => (string) $loginClass,
'context' => (string) $contextName,
'class' => (string)$loginClass,
'context' => (string)$contextName,
'module' => $this->module->getStudlyName(),
'namespace' => $loginNs->replace('\\\\', '\\')->toString(),
]);

// CONFIG
$this->copyStubToApp('config', $configPath, [
'namespace' => (string) $contextNamespace,
'namespace' => (string)$contextNamespace,
'moduleNamespace' => $moduleNs->replace('\\\\', '\\')->toString(),
'path' => (string) $context->replace('\\', '/'),
'path' => (string)$context->replace('\\', '/'),
'loginClass' => $loginNs->append('\\')->append($loginClass)->append('::class')->replace('\\\\', '\\')->toString(),
'authMiddleware' => $middlewareNs->append('\\')->append($middlewareClass)->append('::class')->replace('\\\\', '\\')->toString(),
'module' => $this->module->getStudlyName(),
Expand All @@ -195,20 +195,20 @@ protected function copyStubs($context)

protected function createDirectories($context)
{
$directoryPath = module_path($this->getModuleName(), (string) $context->replace('\\', '/'));
$directoryPath = module_path($this->getModuleName(), (string)$context->replace('\\', '/'));

app(Filesystem::class)->makeDirectory($directoryPath, force: $this->option('force'));
app(Filesystem::class)->makeDirectory($directoryPath.'/Pages', force: $this->option('force'));
app(Filesystem::class)->makeDirectory($directoryPath.'/Resources', force: $this->option('force'));
app(Filesystem::class)->makeDirectory($directoryPath.'/Widgets', force: $this->option('force'));
app(Filesystem::class)->makeDirectory($directoryPath . '/Pages', force: $this->option('force'));
app(Filesystem::class)->makeDirectory($directoryPath . '/Resources', force: $this->option('force'));
app(Filesystem::class)->makeDirectory($directoryPath . '/Widgets', force: $this->option('force'));
}

protected function copyStubToApp(string $stub, string $targetPath, array $replacements = []): void
{
$filesystem = app(Filesystem::class);

if (! $this->fileExists($stubPath = base_path("stubs/filament/{$stub}.stub"))) {
$stubPath = __DIR__."/../../stubs/{$stub}.stub";
if (!$this->fileExists($stubPath = base_path("stubs/filament/{$stub}.stub"))) {
$stubPath = __DIR__ . "/../../stubs/{$stub}.stub";
}

$stub = Str::of($filesystem->get($stubPath));
Expand All @@ -217,22 +217,22 @@ protected function copyStubToApp(string $stub, string $targetPath, array $replac
$stub = $stub->replace("{{ {$key} }}", $replacement);
}

$stub = (string) $stub;
$stub = (string)$stub;

$this->writeFile($targetPath, $stub);
}

/**
* Install the service provider in the application configuration file.
*
* @param string $providerClass | Fully namespaced service class
* @param string $providerClass | Fully namespaced service class
*/
protected function installServiceProvider(string $providerClass, string $after = 'RouteServiceProvider'): void
{
if (! Str::contains($appConfig = file_get_contents(config_path('app.php')), $providerClass)) {
if (!Str::contains($appConfig = file_get_contents(config_path('app.php')), $providerClass)) {
file_put_contents(config_path('app.php'), str_replace(
'App\\Providers\\'.$after.'::class,',
'App\\Providers\\'.$after.'::class,'.PHP_EOL." $providerClass,",
'App\\Providers\\' . $after . '::class,',
'App\\Providers\\' . $after . '::class,' . PHP_EOL . " $providerClass,",
$appConfig
));
}
Expand All @@ -241,9 +241,9 @@ protected function installServiceProvider(string $providerClass, string $after =
/**
* Install the middleware to a group in the application Http Kernel.
*
* @param string $after
* @param string $name
* @param string $group
* @param string $after
* @param string $name
* @param string $group
* @return void
*/
protected function installMiddlewareAfter($after, $name, $group = 'web')
Expand All @@ -253,10 +253,10 @@ protected function installMiddlewareAfter($after, $name, $group = 'web')
$middlewareGroups = Str::before(Str::after($httpKernel, '$middlewareGroups = ['), '];');
$middlewareGroup = Str::before(Str::after($middlewareGroups, "'$group' => ["), '],');

if (! Str::contains($middlewareGroup, $name)) {
if (!Str::contains($middlewareGroup, $name)) {
$modifiedMiddlewareGroup = str_replace(
$after.',',
$after.','.PHP_EOL.' '.$name.',',
$after . ',',
$after . ',' . PHP_EOL . ' ' . $name . ',',
$middlewareGroup,
);

Expand Down
18 changes: 11 additions & 7 deletions src/Commands/FilamentModuleMakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ class FilamentModuleMakePageCommand extends MakePageCommand

protected $description = 'Creates a Filament page class and view.';

protected $signature = 'module:make-filament-page {module?} {name?} {--R|resource=} {--T|type=} {--F|force}';
protected $signature = 'module:make-filament-page {name} {context?} {module?} {--R|resource=} {--T|type=} {--F|force}';

protected ?Module $module;

public function handle(): int
{
$module = (string) Str::of($this->argument('module') ?? $this->askRequired('Module Name (e.g. `sales`)', 'module'));
$context = Str::of($this->argument('context') ?? 'Filament')->studly()->toString();
$module = $this->argument('module') ?: app('modules')->getUsedNow();
if (!$module) {
$module = (string) Str::of($this->askRequired('Module Name (e.g. `Sales`)', 'module'));
}
$this->module = app('modules')->findOrFail($this->getModuleName());
$path = module_path($module, 'Filament/Pages/');
$resourcePath = module_path($module, 'Filament/Resources/');
$namespace = $this->getModuleNamespace().'\\Filament\\Pages';
$resourceNamespace = $this->getModuleNamespace().'\\Filament\\Resources';
$path = module_path($module, "$context/Pages/");
$resourcePath = module_path($module, "$context/Resources/");
$namespace = $this->getModuleNamespace()."\\$context\\Pages";
$resourceNamespace = $this->getModuleNamespace()."\\$context\\Resources";

$page = (string) Str::of($this->argument('name') ?? $this->askRequired('Name (e.g. `Settings`)', 'name'))
->trim('/')
Expand Down Expand Up @@ -111,7 +115,7 @@ public function handle(): int
]);
} else {
$this->copyStubToApp($resourcePage === 'custom' ? 'CustomResourcePage' : 'ResourcePage', $path, [
'baseResourcePage' => 'Filament\\Resources\\Pages\\'.($resourcePage === 'custom' ? 'Page' : $resourcePage),
'baseResourcePage' => "{{$context}}\\Resources\\Pages\\".($resourcePage === 'custom' ? 'Page' : $resourcePage),
'baseResourcePageClass' => $resourcePage === 'custom' ? 'Page' : $resourcePage,
'namespace' => "{$resourceNamespace}\\{$resource}\\Pages".($pageNamespace !== '' ? "\\{$pageNamespace}" : ''),
'resource' => "{$resourceNamespace}\\{$resource}",
Expand Down
14 changes: 10 additions & 4 deletions src/Commands/FilamentModuleMakeRelationManagerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ class FilamentModuleMakeRelationManagerCommand extends MakeRelationManagerComman

protected $description = 'Creates a Filament relation manager class for a resource.';

protected $signature = 'module:make-filament-relation-manager {module?} {resource?} {relationship?} {recordTitleAttribute?} {--attach} {--associate} {--soft-deletes} {--view} {--F|force}';
protected $signature = 'module:make-filament-relation-manager {resource?} {relationship?} {recordTitleAttribute?} {context?} {module?} {--attach} {--associate} {--soft-deletes} {--view} {--F|force}';

protected ?Module $module;

public function handle(): int
{
$module = (string) Str::of($this->argument('module') ?? $this->askRequired('Module Name (e.g. `sales`)', 'module'));
$module = $this->argument('module') ?: app('modules')->getUsedNow();
if (!$module) {
$module = (string) Str::of($this->askRequired('Module Name (e.g. `Sales`)', 'module'));
}
$this->module = app('modules')->findOrFail($this->getModuleName());

$resourcePath = module_path($module, 'Filament/Resources/');
$resourceNamespace = $this->getModuleNamespace().'\\Filament\\Resources';
$contextInput = $this->argument('context') ?? $this->askRequired("Context Name",'context','Filament');
$context = Str::of($contextInput)->studly()->toString();

$resourcePath = module_path($module, "$context/Resources/");
$resourceNamespace = $this->getModuleNamespace()."\\$context\\Resources";

$resource = (string) Str::of($this->argument('resource') ?? $this->askRequired('Resource (e.g. `DepartmentResource`)', 'resource'))
->studly()
Expand Down
14 changes: 9 additions & 5 deletions src/Commands/FilamentModuleMakeResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ class FilamentModuleMakeResourceCommand extends MakeResourceCommand

protected $description = 'Creates a Filament resource class and default page classes.';

protected $signature = 'module:make-filament-resource {module?} {name?} {--soft-deletes} {--view} {--G|generate} {--S|simple} {--F|force}';
protected $signature = 'module:make-filament-resource {name?} {context?} {module?} {--soft-deletes} {--view} {--G|generate} {--S|simple} {--F|force}';

protected ?Module $module;

public function handle(): int
{
$module = (string) Str::of($this->argument('module') ?? $this->askRequired('Module Name (e.g. `sales`)', 'module'));
$context = Str::of($this->argument('context') ?? 'Filament')->studly()->toString();
$module = $this->argument('module') ?: app('modules')->getUsedNow();
if (!$module) {
$module = (string) Str::of($this->askRequired('Module Name (e.g. `Sales`)', 'module'));
}
$this->module = app('modules')->findOrFail($this->getModuleName());

$path = module_path($module, 'Filament/Resources/');
$namespace = $this->getModuleNamespace().'\\Filament\\Resources';
$path = module_path($module, "$context/Resources/");
$namespace = $this->getModuleNamespace()."\\$context\\Resources";

$model = Str::of($this->argument('name') ?? $this->askRequired('Model (e.g. `BlogPost`)', 'name'))
->studly()
Expand Down Expand Up @@ -173,6 +177,7 @@ public function handle(): int
'namespace' => "{$namespace}\\{$resourceClass}\\Pages",
'resource' => "{$namespace}\\{$resourceClass}",
'resourceClass' => $resourceClass,
'resourceNamespace' => $resourceNamespace,
'resourcePageClass' => $manageResourcePageClass,
]);
} else {
Expand Down Expand Up @@ -245,6 +250,5 @@ private function ensureSubdirectoryExists(string $path): void
$filesystem->ensureDirectoryExists(
Str::of($path)->rtrim('/')->toString(),
);

}
}
26 changes: 26 additions & 0 deletions src/FilamentModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Filament\Facades\Filament;
use Filament\FilamentManager;
use Filament\Navigation\NavigationItem;
use Illuminate\Support\Traits\ForwardsCalls;

class FilamentModules
Expand Down Expand Up @@ -106,4 +107,29 @@ public function __call(string $method, array $parameters)

return $response;
}

public static function registerFilamentNavigationItem($module, $context): void
{
$panel = \Str::of($context)->after('-')->replace('filament','default')->slug()->replace('-', ' ')->title()->title();
Filament::registerNavigationItems([
NavigationItem::make($context)->label("$panel Panel")->url(route($context.'.pages.dashboard'))->icon('heroicon-o-bookmark')->group("$module Module")
]);
}
public static function renderContextNavigation($module, $context): void
{
Filament::registerRenderHook('sidebar.start',fn():string => \Blade::render('<div class="p-2 px-6 bg-primary-100 font-black w-full">'."$module Module</div>"));
Filament::registerRenderHook('sidebar.end',fn():string => \Blade::render('<a class="p-2 px-6 bg-primary-100 font-black w-full inline-flex space-x-2" href="'.route('filament.pages.dashboard').'"><x-heroicon-o-arrow-left class="w-5"/> Main Panel</a>'));
}
public function prepareDefaultNavigation($module, $context): void
{
Filament::serving(function () use($module, $context) {
Filament::forContext('filament', function () use($module, $context) {
app(FilamentModules::class)::registerFilamentNavigationItem($module,$context);
});
Filament::forContext($context, function () use ($module, $context) {
app(FilamentModules::class)::renderContextNavigation($module, $context);
});
});
}

}
12 changes: 2 additions & 10 deletions stubs/ContextServiceProvider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace {{ namespace }};
use Filament\Facades\Filament;
use Filament\Navigation\NavigationItem;
use Savannabits\FilamentModules\ContextServiceProvider;
use Savannabits\FilamentModules\FilamentModules;

class {{ class }} extends ContextServiceProvider
{
Expand All @@ -29,15 +30,6 @@ class {{ class }} extends ContextServiceProvider
public function boot()
{
parent::boot();
Filament::serving(function () {
Filament::forContext('filament', function (){
Filament::registerNavigationItems([
NavigationItem::make(static::$module)->label(static::$module.' Module')->url(route(static::$name.'.pages.dashboard'))->icon('heroicon-o-bookmark')->group('Modules')
]);
});
Filament::forContext(static::$name, function (){
Filament::registerRenderHook('sidebar.start',fn():string => \Blade::render('<div class="p-2 px-6 bg-primary-100 font-black w-full">'.static::$module.' Module</div>'));
});
});
app(FilamentModules::class)->prepareDefaultNavigation(static::$module, static::$name);
}
}
Loading

0 comments on commit 6fc7c55

Please sign in to comment.