From 22e7c41a4dfec0944c3963796924f9e68002e80c Mon Sep 17 00:00:00 2001 From: coolsam726 Date: Sat, 6 Apr 2024 09:40:55 +0000 Subject: [PATCH] Fix styling --- src/Commands/ActivateModuleCommand.php | 6 ++- src/Commands/DeactivateModuleCommand.php | 6 ++- src/Commands/MakeModuleCommand.php | 42 +++++++++++++-------- src/ModularServiceProvider.php | 17 +++++---- src/Support/Concerns/CanManipulateFiles.php | 14 +++---- 5 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/Commands/ActivateModuleCommand.php b/src/Commands/ActivateModuleCommand.php index c52903c..ceed144 100644 --- a/src/Commands/ActivateModuleCommand.php +++ b/src/Commands/ActivateModuleCommand.php @@ -5,13 +5,15 @@ use Illuminate\Console\Command; use Illuminate\Support\Str; use Savannabits\Modular\Facades\Modular; + use function Laravel\Prompts\text; class ActivateModuleCommand extends Command { - public $signature = 'modular:activate {name?}'; + public $description = 'Activate a module'; + private string $moduleName; public function handle(): void @@ -24,7 +26,7 @@ public function handle(): void private function activateModule(): void { $moduleName = $this->moduleName; - $repoName = config('modular.vendor','modular').'/'.$moduleName; + $repoName = config('modular.vendor', 'modular').'/'.$moduleName; Modular::execCommand('composer require '.$repoName.':@dev'); Modular::execCommand("php artisan $moduleName:install"); } diff --git a/src/Commands/DeactivateModuleCommand.php b/src/Commands/DeactivateModuleCommand.php index 7e85782..11fd989 100644 --- a/src/Commands/DeactivateModuleCommand.php +++ b/src/Commands/DeactivateModuleCommand.php @@ -5,13 +5,15 @@ use Illuminate\Console\Command; use Illuminate\Support\Str; use Savannabits\Modular\Facades\Modular; + use function Laravel\Prompts\text; class DeactivateModuleCommand extends Command { - public $signature = 'modular:deactivate {name?}'; + public $description = 'Deactivate a module'; + private string $moduleName; public function handle(): void @@ -24,7 +26,7 @@ public function handle(): void private function deactivateModule(): void { $moduleName = $this->moduleName; - $repoName = config('modular.vendor','modular').'/'.$moduleName; + $repoName = config('modular.vendor', 'modular').'/'.$moduleName; Modular::execCommand("composer remove $repoName"); } } diff --git a/src/Commands/MakeModuleCommand.php b/src/Commands/MakeModuleCommand.php index a026c7c..ecd04e0 100644 --- a/src/Commands/MakeModuleCommand.php +++ b/src/Commands/MakeModuleCommand.php @@ -8,17 +8,25 @@ use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Savannabits\Modular\Support\Concerns\CanManipulateFiles; + use function Laravel\Prompts\text; class MakeModuleCommand extends Command { use CanManipulateFiles; + public $signature = 'modular:make {name?} {--F|force}'; + public $description = 'Create a new module'; + private string $moduleName; + private string $moduleTitle; + private string $moduleNamespace; + private string $modulePath; + private string $moduleStudlyName; public function handle() @@ -27,8 +35,8 @@ public function handle() $this->moduleName = Str::of($name)->kebab()->toString(); $this->moduleStudlyName = Str::of($name)->studly()->toString(); $this->moduleTitle = Str::of($name)->kebab()->title()->replace('-', ' ')->toString(); - $this->moduleNamespace = config('modular.namespace') . '\\' . Str::of($name)->studly()->toString(); - $this->modulePath = config('modular.path') . '/' . $this->moduleName; + $this->moduleNamespace = config('modular.namespace').'\\'.Str::of($name)->studly()->toString(); + $this->modulePath = config('modular.path').'/'.$this->moduleName; $this->info("Creating module: $this->moduleName in $this->modulePath"); $this->generateModuleDirectories(); @@ -39,18 +47,20 @@ public function handle() private function generateModuleDirectories(): bool { $directories = config('modular.directory_tree'); - if (!count($directories)) { + if (! count($directories)) { $this->error('No directories found in the configuration file'); + return false; } foreach ($directories as $directory) { - $path = $this->modulePath . '/' . ltrim($directory,'/'); - if (!is_dir($path)) { + $path = $this->modulePath.'/'.ltrim($directory, '/'); + if (! is_dir($path)) { mkdir($path, 0775, true); $this->info("Created directory: $path"); } } $this->info('Module directories created successfully'); + return true; } @@ -62,28 +72,28 @@ private function generateModuleFiles(): void } catch (FileNotFoundException|NotFoundExceptionInterface|ContainerExceptionInterface $e) { $this->error($e->getMessage()); } -// $this->generateModuleFacade(); + // $this->generateModuleFacade(); } private function generateModuleComposerFile(): void { $composerJson = [ - 'name' => config('modular.vendor') . '/' . $this->moduleName, + 'name' => config('modular.vendor').'/'.$this->moduleName, 'type' => 'library', - 'description' => $this->moduleTitle . ' module', + 'description' => $this->moduleTitle.' module', 'require' => [ 'php' => '^8.2', ], 'autoload' => [ 'psr-4' => [ - $this->moduleNamespace . '\\' => 'src/', - $this->moduleNamespace . '\\Database\\Factories\\' => 'database/factories/', - $this->moduleNamespace . '\\Database\\Seeders\\' => 'database/seeders/', + $this->moduleNamespace.'\\' => 'src/', + $this->moduleNamespace.'\\Database\\Factories\\' => 'database/factories/', + $this->moduleNamespace.'\\Database\\Seeders\\' => 'database/seeders/', ], ], 'autoload-dev' => [ 'psr-4' => [ - $this->moduleNamespace . '\\Tests\\' => 'tests/', + $this->moduleNamespace.'\\Tests\\' => 'tests/', ], ], 'config' => [ @@ -96,12 +106,12 @@ private function generateModuleComposerFile(): void 'extra' => [ 'laravel' => [ 'providers' => [ - $this->moduleNamespace . '\\' . $this->moduleStudlyName . 'ServiceProvider', + $this->moduleNamespace.'\\'.$this->moduleStudlyName.'ServiceProvider', ], ], ], ]; - $composerJsonPath = $this->modulePath . '/composer.json'; + $composerJsonPath = $this->modulePath.'/composer.json'; file_put_contents($composerJsonPath, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $this->info("Created composer.json file: $composerJsonPath"); } @@ -113,9 +123,9 @@ private function generateModuleComposerFile(): void */ private function generateModuleServiceProvider(): void { - $path = $this->modulePath . '/src/' . $this->moduleStudlyName . 'ServiceProvider.php'; + $path = $this->modulePath.'/src/'.$this->moduleStudlyName.'ServiceProvider.php'; $namespace = $this->moduleNamespace; - $class = $this->moduleStudlyName . 'ServiceProvider'; + $class = $this->moduleStudlyName.'ServiceProvider'; $this->copyStubToApp('module.provider', $path, [ 'class' => $class, 'namespace' => $namespace, diff --git a/src/ModularServiceProvider.php b/src/ModularServiceProvider.php index a0a5c44..dfb110c 100644 --- a/src/ModularServiceProvider.php +++ b/src/ModularServiceProvider.php @@ -7,11 +7,15 @@ use Spatie\LaravelPackageTools\Commands\InstallCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; + class ModularServiceProvider extends PackageServiceProvider { public static string $name = 'modular'; + public static string $vendor = 'savannabits'; + public static string $viewNamespace = 'modular'; + public function configurePackage(Package $package): void { /* @@ -30,8 +34,7 @@ public function configurePackage(Package $package): void $command ->askToStarRepoOnGitHub($name) ->startWith(fn (InstallCommand $command) => $this->installationSteps($command)); - }) - ; + }); $this->mergeConfigFrom($this->package->basePath('/../config/modular.php'), 'modular'); } @@ -40,10 +43,10 @@ private function configureComposerMerge(InstallCommand $command): void $command->comment('Configuring Composer merge plugin:'); $composerJson = json_decode(file_get_contents(base_path('composer.json')), true); // Add the modules repositories into compose if they don't exist - if (!isset($composerJson['repositories'])){ + if (! isset($composerJson['repositories'])) { $composerJson['repositories'] = []; } - if (!collect($composerJson['repositories'])->contains(fn ($repo) => $repo['type'] === 'path' && $repo['url'] === config('modular.path').'/*')) { + if (! collect($composerJson['repositories'])->contains(fn ($repo) => $repo['type'] === 'path' && $repo['url'] === config('modular.path').'/*')) { $composerJson['repositories'][] = [ 'type' => 'path', 'url' => config('modular.path').'/*', @@ -52,7 +55,7 @@ private function configureComposerMerge(InstallCommand $command): void ], ]; } - if (!isset($composerJson['extra']['merge-plugin'])) { + if (! isset($composerJson['extra']['merge-plugin'])) { $composerJson['extra']['merge-plugin'] = [ 'include' => [ 'modules/*/composer.json', @@ -65,7 +68,7 @@ private function configureComposerMerge(InstallCommand $command): void ]; // Ensure the composer-merge-plugin is in the list of allowed plugins - if (!isset($composerJson['config']['allow-plugins'])) { + if (! isset($composerJson['config']['allow-plugins'])) { $composerJson['config']['allow-plugins'] = []; } // If allowed-plugins is set to true, disregard @@ -93,7 +96,7 @@ private function ensureModularPathExists(InstallCommand $command): void { $command->comment('Ensuring modular path exists:'); $path = config('modular.path'); - if (!is_dir($path)) { + if (! is_dir($path)) { mkdir($path, 0755, true); $command->info("Directory $path created successfully"); } else { diff --git a/src/Support/Concerns/CanManipulateFiles.php b/src/Support/Concerns/CanManipulateFiles.php index c6eb0aa..fe9f04f 100644 --- a/src/Support/Concerns/CanManipulateFiles.php +++ b/src/Support/Concerns/CanManipulateFiles.php @@ -17,16 +17,16 @@ trait CanManipulateFiles { /** - * @param array $paths + * @param array $paths */ protected function checkForCollision(array $paths): bool { foreach ($paths as $path) { - if (!$this->fileExists($path)) { + if (! $this->fileExists($path)) { continue; } - if (!confirm(basename($path) . ' already exists, do you want to overwrite it?')) { + if (! confirm(basename($path).' already exists, do you want to overwrite it?')) { $this->components->error("{$path} already exists, aborting."); return true; @@ -39,7 +39,7 @@ protected function checkForCollision(array $paths): bool } /** - * @param array $replacements + * @param array $replacements * * @throws FileNotFoundException * @throws ContainerExceptionInterface @@ -50,7 +50,7 @@ protected function copyStubToApp(string $stub, string $targetPath, array $replac $filesystem = app(Filesystem::class); // Get the correct stub path whether published or not - $stubPath = $this->getDefaultStubPath() . "/{$stub}.stub"; + $stubPath = $this->getDefaultStubPath()."/{$stub}.stub"; $stub = str($filesystem->get($stubPath)); @@ -58,7 +58,7 @@ protected function copyStubToApp(string $stub, string $targetPath, array $replac $stub = $stub->replace("{{ {$key} }}", $replacement); } - $stub = (string)$stub; + $stub = (string) $stub; $this->writeFile($targetPath, $stub); } @@ -85,7 +85,7 @@ protected function getDefaultStubPath(): string { $reflectionClass = new ReflectionClass($this); - return (string)str($reflectionClass->getFileName()) + return (string) str($reflectionClass->getFileName()) ->beforeLast('Commands') ->append('../stubs'); }