diff --git a/src/Actions/CopyAndRefactorDirectoryAction.php b/src/Actions/CopyAndRefactorDirectoryAction.php index 20bcbd0..63a8833 100644 --- a/src/Actions/CopyAndRefactorDirectoryAction.php +++ b/src/Actions/CopyAndRefactorDirectoryAction.php @@ -21,8 +21,8 @@ public function execute(string $sourcePath, string $targetPath, array $refactor foreach ($files as $file) { $this->copyAndRefactorFileAction->execute( - beyond_os_aware_path($sourcePath.'/'.$file->getFilename()), - beyond_os_aware_path($targetPath.'/'.$file->getFilename()), + $sourcePath.'/'.$file->getFilename(), + $targetPath.'/'.$file->getFilename(), $refactor, $force ); diff --git a/src/Actions/CopyDirectoryAction.php b/src/Actions/CopyDirectoryAction.php index 12cee0c..a7954cd 100644 --- a/src/Actions/CopyDirectoryAction.php +++ b/src/Actions/CopyDirectoryAction.php @@ -7,8 +7,18 @@ class CopyDirectoryAction { + public function __construct( + private readonly NormalizePathAction $normalizePathAction + ) { + } + public function execute(string $srcPath, string $targetPath, bool $force = false): void { + [$srcPath, $targetPath] = $this->normalizePathAction->execute([ + $srcPath, + $targetPath, + ]); + $fs = new Filesystem(); if (! $force && $fs->exists($targetPath)) { diff --git a/src/Actions/CopyFileAction.php b/src/Actions/CopyFileAction.php index 02de490..a902e16 100644 --- a/src/Actions/CopyFileAction.php +++ b/src/Actions/CopyFileAction.php @@ -7,8 +7,18 @@ class CopyFileAction { + public function __construct( + private readonly NormalizePathAction $normalizePathAction + ) { + } + public function execute(string $srcPath, string $targetPath, bool $force = false): void { + [$srcPath, $targetPath] = $this->normalizePathAction->execute([ + $srcPath, + $targetPath, + ]); + $fs = new Filesystem(); $fs->ensureDirectoryExists( diff --git a/src/Actions/CreateDirectoryAction.php b/src/Actions/CreateDirectoryAction.php index 95bac23..cfdabc8 100644 --- a/src/Actions/CreateDirectoryAction.php +++ b/src/Actions/CreateDirectoryAction.php @@ -6,6 +6,11 @@ class CreateDirectoryAction { + public function __construct( + private readonly NormalizePathAction $normalizePathAction + ) { + } + /** * @param string|array $directory */ @@ -19,6 +24,8 @@ public function execute(string|array $directory): void return; } - (new Filesystem())->ensureDirectoryExists(beyond_os_aware_path(base_path()."/modules/$directory")); + (new Filesystem())->ensureDirectoryExists( + $this->normalizePathAction->execute(base_path('modules/'.$directory)) + ); } } diff --git a/src/Actions/CreateFileAction.php b/src/Actions/CreateFileAction.php index c702498..52d72ed 100644 --- a/src/Actions/CreateFileAction.php +++ b/src/Actions/CreateFileAction.php @@ -7,6 +7,11 @@ class CreateFileAction { + public function __construct( + private readonly NormalizePathAction $normalizePathAction + ) { + } + /** * @param string|array|array $files */ @@ -21,6 +26,8 @@ public function execute(string|array $files): void $contents = ''; } + $file = $this->normalizePathAction->execute($file); + $fs->ensureDirectoryExists(dirname($file)); $fs->put($file, $contents); diff --git a/src/Actions/DeletePathAction.php b/src/Actions/DeletePathAction.php index 8e82aef..0ae9ff7 100644 --- a/src/Actions/DeletePathAction.php +++ b/src/Actions/DeletePathAction.php @@ -6,8 +6,15 @@ class DeletePathAction { + public function __construct( + private readonly NormalizePathAction $normalizePathAction + ) { + } + public function execute(string $path): void { + $path = $this->normalizePathAction->execute($path); + $fs = new Filesystem(); if ($fs->isDirectory($path)) { diff --git a/src/Actions/MoveFileAction.php b/src/Actions/MoveFileAction.php index 071ce70..0534939 100644 --- a/src/Actions/MoveFileAction.php +++ b/src/Actions/MoveFileAction.php @@ -6,8 +6,18 @@ class MoveFileAction { + public function __construct( + private readonly NormalizePathAction $normalizePathAction + ) { + } + public function execute(string $sourcePath, string $targetPath, bool $force = false): void { + [$sourcePath, $targetPath] = $this->normalizePathAction->execute([ + $sourcePath, + $targetPath, + ]); + $fs = new Filesystem(); $fs->ensureDirectoryExists(dirname($targetPath)); diff --git a/src/Actions/NormalizePathAction.php b/src/Actions/NormalizePathAction.php new file mode 100644 index 0000000..cd4d90c --- /dev/null +++ b/src/Actions/NormalizePathAction.php @@ -0,0 +1,29 @@ +|string $path + * @return array|string + */ + public function execute(array|string $path): array|string + { + $single = is_string($path); + $paths = Arr::wrap($path); + + foreach ($paths as $k => $p) { + $paths[$k] = Str::replace('/', DIRECTORY_SEPARATOR, $p); + } + + if ($single) { + return array_pop($paths); + } + + return $paths; + } +} diff --git a/src/Commands/MakeModelCommand.php b/src/Commands/MakeModelCommand.php index ddb4abf..6a1359a 100644 --- a/src/Commands/MakeModelCommand.php +++ b/src/Commands/MakeModelCommand.php @@ -36,7 +36,7 @@ public function setup(NameResolver $nameResolver): void beyond_copy_stub( 'migration.create.stub', - beyond_os_aware_path(base_path()."/modules/$module/Infrastructure/Database/Migrations/$fileName.php"), + base_path()."/modules/$module/Infrastructure/Database/Migrations/$fileName.php", [ '{{ table }}' => $tableName, ], @@ -49,7 +49,7 @@ public function setup(NameResolver $nameResolver): void beyond_copy_stub( 'factory.stub', - beyond_os_aware_path(base_path()."/modules/$module/Infrastructure/factories/$fileName.php"), + base_path()."/modules/$module/Infrastructure/factories/$fileName.php", [ '{{ namespace }}' => $namespace, '{{ model }}' => $fileName, diff --git a/src/Commands/MakeModuleCommand.php b/src/Commands/MakeModuleCommand.php index aad7097..ca674de 100644 --- a/src/Commands/MakeModuleCommand.php +++ b/src/Commands/MakeModuleCommand.php @@ -29,7 +29,7 @@ public function handle(): void $full = (bool) $this->option('full'); $this->copyDirectoryAction->execute( - beyond_os_aware_path(__DIR__.'/../../stubs/Module'), + __DIR__.'/../../stubs/Module', beyond_modules_path($module), $force, ); @@ -44,11 +44,11 @@ public function handle(): void private function moveAndRefactorModuleFiles(string $module, bool $force = false): void { $files = [ - beyond_os_aware_path('Providers/ModuleAuthServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}AuthServiceProvider.php"), - beyond_os_aware_path('Providers/ModuleEventServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}EventServiceProvider.php"), - beyond_os_aware_path('Providers/ModuleRouteServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}RouteServiceProvider.php"), - beyond_os_aware_path('Providers/ModuleServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}ServiceProvider.php"), - beyond_os_aware_path('App/routes.stub') => beyond_os_aware_path('App/routes.php'), + 'Providers/ModuleAuthServiceProvider.stub' => "Providers/{$module}AuthServiceProvider.php", + 'Providers/ModuleEventServiceProvider.stub' => "Providers/{$module}EventServiceProvider.php", + 'Providers/ModuleRouteServiceProvider.stub' => "Providers/{$module}RouteServiceProvider.php", + 'Providers/ModuleServiceProvider.stub' => "Providers/{$module}ServiceProvider.php", + 'App/routes.stub' => 'App/routes.php', ]; foreach ($files as $from => $to) { diff --git a/src/LaravelBeyondServiceProvider.php b/src/LaravelBeyondServiceProvider.php index 80001b6..8d481cf 100644 --- a/src/LaravelBeyondServiceProvider.php +++ b/src/LaravelBeyondServiceProvider.php @@ -22,7 +22,7 @@ public function beyondCommands(): array $exclude = []; $fs = new Filesystem(); - $files = $fs->files(beyond_os_aware_path(__DIR__.'/Commands')); + $files = $fs->files(__DIR__.DIRECTORY_SEPARATOR.'Commands'); return array_map( fn ($file) => 'AkrilliA\\LaravelBeyond\\Commands\\'.$file->getBasename('.php'), diff --git a/src/NameResolver.php b/src/NameResolver.php index 7423714..1dd33b1 100644 --- a/src/NameResolver.php +++ b/src/NameResolver.php @@ -84,12 +84,10 @@ private function init(): void $this->directory ? '\\'.$this->directory : '', ); - $this->path = beyond_os_aware_path( - sprintf( - '%s/'.$this->command->getFileNameTemplate(), - Str::lcfirst(Str::replace('\\', '/', $this->namespace)), - $this->className, - ) + $this->path = sprintf( + '%s'.DIRECTORY_SEPARATOR.$this->command->getFileNameTemplate(), + Str::lcfirst(Str::replace('\\', DIRECTORY_SEPARATOR, $this->namespace)), + $this->className, ); } @@ -99,6 +97,6 @@ private function setDirectoryAndClassName(string $name): void $this->className = array_pop($parts); - $this->directory = beyond_os_aware_path(implode('/', $parts)); + $this->directory = implode('\\', $parts); } } diff --git a/src/helper.php b/src/helper.php index 82b3e50..f8a7202 100644 --- a/src/helper.php +++ b/src/helper.php @@ -2,6 +2,7 @@ use AkrilliA\LaravelBeyond\Actions\CopyAndRefactorFileAction; use AkrilliA\LaravelBeyond\Actions\CopyFileAction; +use AkrilliA\LaravelBeyond\Actions\NormalizePathAction; use AkrilliA\LaravelBeyond\Actions\RefactorFileAction; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; @@ -16,7 +17,7 @@ function beyond_path(): string if (! function_exists('beyond_modules_path')) { function beyond_modules_path(string $path = ''): string { - return base_path(beyond_os_aware_path("modules/$path")); + return base_path('modules'.DIRECTORY_SEPARATOR.$path); } } @@ -33,12 +34,12 @@ function beyond_module_name(string $name): string */ function beyond_copy_stub(string $stub, string $path, array $refactor = [], bool $force = false): void { - $stub = file_exists($stubPath = base_path(beyond_os_aware_path('stubs/beyond.'.$stub))) + $stub = file_exists($stubPath = base_path('stubs/beyond.'.$stub)) ? $stubPath - : beyond_os_aware_path(beyond_path().'/stubs/'.$stub); + : beyond_path().'/stubs/'.$stub; $action = new CopyAndRefactorFileAction( - new CopyFileAction(), + new CopyFileAction(new NormalizePathAction()), new RefactorFileAction() ); @@ -71,11 +72,3 @@ function ($directory) { return $directories; } } - -if (! function_exists('beyond_os_aware_path')) { - - function beyond_os_aware_path(string $path): string - { - return Str::of($path)->replace('/', DIRECTORY_SEPARATOR)->value(); - } -} diff --git a/tests/Commands/MakeModuleCommandTest.php b/tests/Commands/MakeModuleCommandTest.php index 6b06888..5f232a9 100644 --- a/tests/Commands/MakeModuleCommandTest.php +++ b/tests/Commands/MakeModuleCommandTest.php @@ -65,6 +65,9 @@ public function testCanMakeFullModuleUsingForce(): void $code->assertOk(); } + /** + * @return array + */ protected function getPaths(): array { return [ diff --git a/tests/TestCase.php b/tests/TestCase.php index 9a1ea70..683aa24 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -41,7 +41,7 @@ private function removeDirectory(string $directory): void while (false !== $file = readdir($handle)) { if (! in_array($file, ['.', '..'], true)) { - $path = $directory.'/'.$file; + $path = $directory.DIRECTORY_SEPARATOR.$file; if (is_dir($path)) { $this->removeDirectory($path);