Skip to content

Commit

Permalink
Added the ability to specify a custom directory for specific declarat…
Browse files Browse the repository at this point in the history
…ion types in the [spiral/scaffolder] componentt (#925)
  • Loading branch information
msmakouz committed Apr 25, 2023
1 parent 8d52bb6 commit 865ea9f
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/Scaffolder/src/Config/ScaffolderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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',
], '/');
Expand Down
144 changes: 144 additions & 0 deletions src/Scaffolder/tests/Config/ScaffolderConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}
}

0 comments on commit 865ea9f

Please sign in to comment.