From 865ea9f35d35d36e6f0ca0cd1e84b55efee25b12 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 25 Apr 2023 20:13:58 +0300 Subject: [PATCH] Added the ability to specify a custom directory for specific declaration types in the [spiral/scaffolder] componentt (#925) --- CHANGELOG.md | 7 + .../src/Config/ScaffolderConfig.php | 15 +- .../tests/Config/ScaffolderConfigTest.php | 144 ++++++++++++++++++ 3 files changed, 165 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db4b83791..e7e16d23b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## Unreleased +- **Other Features** + - [spiral/scaffolder] Added new public method `declarationDirectory` to the `Spiral\Scaffolder\Config\ScaffolderConfig` + class that returns the directory path of the specified declaration, or default directory path if not specified. +- **Medium Impact Changes** + - [spiral/scaffolder] Method `baseDirectory` of `Spiral\Scaffolder\Config\ScaffolderConfig` class is deprecated. + ## 3.7.1 - 2023-04-21 - **Bug Fixes** - [spiral/filters] Fixed InputScope to allow retrieval of non-bag input sources diff --git a/src/Scaffolder/src/Config/ScaffolderConfig.php b/src/Scaffolder/src/Config/ScaffolderConfig.php index fbbd3ad80..e41965576 100644 --- a/src/Scaffolder/src/Config/ScaffolderConfig.php +++ b/src/Scaffolder/src/Config/ScaffolderConfig.php @@ -30,11 +30,24 @@ public function headerLines(): array return $this->config['header']; } + /** + * @deprecated since v3.8.0. + */ public function baseDirectory(): string { return $this->config['directory']; } + /** + * @param non-empty-string $element + */ + public function declarationDirectory(string $element): string + { + $declaration = $this->getDeclaration($element); + + return !empty($declaration['directory']) ? $declaration['directory'] : $this->config['directory']; + } + /** * @param non-empty-string $element */ @@ -79,7 +92,7 @@ public function classFilename(string $element, string $name, ?string $namespace $elementNamespace = \substr($elementNamespace, \strlen($this->baseNamespace($element))); return $this->joinPathChunks([ - $this->baseDirectory(), + $this->declarationDirectory($element), \str_replace('\\', '/', $elementNamespace), $this->className($element, $name) . '.php', ], '/'); diff --git a/src/Scaffolder/tests/Config/ScaffolderConfigTest.php b/src/Scaffolder/tests/Config/ScaffolderConfigTest.php index 624dc9ac0..570cbe6c0 100644 --- a/src/Scaffolder/tests/Config/ScaffolderConfigTest.php +++ b/src/Scaffolder/tests/Config/ScaffolderConfigTest.php @@ -114,4 +114,148 @@ public function testPartialOverrideDefaultDeclaration(): void $this->assertSame('Bootloader', $ref->invoke($config, BootloaderDeclaration::TYPE, 'postfix')); $this->assertSame(BootloaderDeclaration::class, $ref->invoke($config, BootloaderDeclaration::TYPE, 'class')); } + + /** + * @dataProvider declarationDirectoryDataProvider + */ + public function testDeclarationDirectory(array $config, string $expected): void + { + $config = new ScaffolderConfig($config); + + $this->assertSame($expected, $config->declarationDirectory('some')); + } + + /** + * @dataProvider classFilenameDataProvider + */ + public function testClassFilename(array $config, string $expected, string $namespace): void + { + $config = new ScaffolderConfig($config); + + $this->assertSame($expected, $config->classFilename('foo', 'Test', $namespace)); + } + + public static function declarationDirectoryDataProvider(): \Traversable + { + yield [['directory' => 'foo'], 'foo']; + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['some' => []] + ] + ], + 'foo' + ]; + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['some' => ['directory' => null]] + ] + ], + 'foo' + ]; + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['some' => ['directory' => '']] + ] + ], + 'foo' + ]; + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['some' => ['directory' => 'bar']] + ] + ], + 'bar' + ]; + yield [ + [ + 'directory' => 'foo', + 'declarations' => ['some' => []] + ], + 'foo' + ]; + yield [ + [ + 'directory' => 'foo', + 'declarations' => ['some' => ['directory' => null]] + ], + 'foo' + ]; + yield [ + [ + 'directory' => 'foo', + 'declarations' => ['some' => ['directory' => '']] + ], + 'foo' + ]; + yield [ + [ + 'directory' => 'foo', + 'declarations' => ['some' => ['directory' => 'bar']] + ], + 'bar' + ]; + yield [ + [ + 'directory' => 'foo', + 'declarations' => ['some' => ['directory' => 'baz']], + 'defaults' => [ + 'declarations' => ['some' => ['directory' => 'bar']] + ] + ], + 'baz' + ]; + } + + public static function classFilenameDataProvider(): \Traversable + { + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['foo' => ['class' => 'bar']] + ] + ], + 'foo/App/Test/Test.php', + 'App\\Test' + ]; + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['foo' => ['postfix' => 'Controller']] + ] + ], + 'foo/App/Test/TestController.php', + 'App\\Test' + ]; + yield [ + [ + 'directory' => 'foo', + 'defaults' => [ + 'declarations' => ['foo' => ['postfix' => 'Controller', 'directory' => 'baz']] + ] + ], + 'baz/App/Test/TestController.php', + 'App\\Test' + ]; + yield [ + [ + 'directory' => 'foo', + 'declarations' => ['foo' => ['postfix' => 'Controller', 'directory' => 'changed']], + 'defaults' => [ + 'declarations' => ['foo' => ['postfix' => 'Controller', 'directory' => 'baz']] + ] + ], + 'changed/App/Test/TestController.php', + 'App\\Test' + ]; + } }