Skip to content

Commit 04e337f

Browse files
committed
Switch to a simple prefix. Fix and add tests.
1 parent 355ab4f commit 04e337f

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

src/TypesGenerator.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -827,21 +827,8 @@ private function generateClassUses(array $annotationGenerators, array $classes,
827827
*/
828828
private function namespaceToDir(array $config, string $namespace): string
829829
{
830-
if (!empty($config['namespaces']['alias'])) {
831-
$prepend = '';
832-
foreach ($config['namespaces']['alias'] as $a) {
833-
if (!file_exists($a['resolvePath']) && !is_dir($a['resolvePath'])) {
834-
$this->logger->warning(sprintf('the path to resolve "%s" does not exists or is not a directory.', $a['resolvePath']));
835-
} else {
836-
if (null !== $a['namespace']) {
837-
$namespace = str_replace($a['namespace'], $a['resolvePath'], $namespace);
838-
} else {
839-
// Keep the last resolvePath if there is multiple definition with empty namespace.
840-
$prepend = $a['resolvePath'];
841-
}
842-
}
843-
}
844-
$namespace = $prepend . $namespace;
830+
if (null !== ($prefix = $config['namespaces']['prefix'] ?? null) && 0 === strpos($namespace, $prefix)) {
831+
$namespace = substr($namespace, strlen($prefix));
845832
}
846833

847834
return sprintf('%s/%s/', $config['output'], strtr($namespace, '\\', '/'));

src/TypesGeneratorConfiguration.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,10 @@ function ($rdfa) {
9292
->addDefaultsIfNotSet()
9393
->info('PHP namespaces')
9494
->children()
95-
->arrayNode('alias')
96-
->cannotBeOverwritten()
97-
->prototype('array')
98-
->children()
99-
->scalarNode('namespace')
100-
->defaultNull()
101-
->info('Namespace to alias')
102-
->example('App\\')
103-
->end()
104-
->scalarNode('resolvePath')
105-
->defaultNull()
106-
->info('Path to resolve')
107-
->example('src/')
108-
->end()
109-
->end()
110-
->end()
111-
->end()
112-
->scalarNode('entity')->defaultValue('AppBundle\Entity')->info('The namespace of the generated entities')->example('Acme\Entity')->end()
113-
->scalarNode('enum')->defaultValue('AppBundle\Enum')->info('The namespace of the generated enumerations')->example('Acme\Enum')->end()
114-
->scalarNode('interface')->defaultValue('AppBundle\Model')->info('The namespace of the generated interfaces')->example('Acme\Model')->end()
95+
->scalarNode('prefix')->defaultNull()->info('The global namespace\'s prefix')->example('App\\')->end()
96+
->scalarNode('entity')->defaultValue('AppBundle\Entity')->info('The namespace of the generated entities')->example('App\Entity')->end()
97+
->scalarNode('enum')->defaultValue('AppBundle\Enum')->info('The namespace of the generated enumerations')->example('App\Enum')->end()
98+
->scalarNode('interface')->defaultValue('AppBundle\Model')->info('The namespace of the generated interfaces')->example('App\Model')->end()
11599
->end()
116100
->end()
117101
->arrayNode('doctrine')

tests/Command/DumpConfigurationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,17 @@ public function testDumpConfiguration()
7676
# PHP namespaces
7777
namespaces:
7878
79+
# The global namespace's prefix
80+
prefix: null # Example: App\
81+
7982
# The namespace of the generated entities
80-
entity: AppBundle\Entity # Example: Acme\Entity
83+
entity: AppBundle\Entity # Example: App\Entity
8184
8285
# The namespace of the generated enumerations
83-
enum: AppBundle\Enum # Example: Acme\Enum
86+
enum: AppBundle\Enum # Example: App\Enum
8487
8588
# The namespace of the generated interfaces
86-
interface: AppBundle\Model # Example: Acme\Model
89+
interface: AppBundle\Model # Example: App\Model
8790
8891
# Doctrine
8992
doctrine:

tests/Command/GenerateTypesCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,19 @@ public function testDoNotGenerateId()
308308
$this->assertNotContains('function getId', $person);
309309
$this->assertNotContains('function setId', $person);
310310
}
311+
312+
public function testNamespacesPrefix()
313+
{
314+
$outputDir = __DIR__.'/../../build/namespaces-prefix';
315+
$config = __DIR__.'/../config/namespaces-prefix.yaml';
316+
317+
$this->fs->mkdir($outputDir);
318+
319+
$commandTester = new CommandTester(new GenerateTypesCommand());
320+
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
321+
322+
$person = file_get_contents("$outputDir/Entity/Person.php");
323+
324+
$this->assertContains('namespace App\Entity;', $person);
325+
}
311326
}

tests/TypesGeneratorTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,16 @@ private function getClasses(): array
392392
private function getContextMatcher(array $class)
393393
{
394394
$config = $this->getConfig();
395-
$classes = $this->getClasses();
396395

397-
return function ($context) use ($config, $classes, $class) {
396+
return function ($context) use ($config, $class) {
398397
if (!isset($context['config']) || $config !== $context['config']) {
399398
return false;
400399
}
401400

402401
$baseClass = $class;
403402
unset($baseClass['fields']);
404403

405-
if (!isset($context['class']) || !is_array($context['class']) || $this->arrayEqual($baseClass, array_intersect_key($context['class'], $baseClass))) {
404+
if (!isset($context['class']) || !\is_array($context['class']) || $this->arrayEqual($baseClass, array_intersect_key($context['class'], $baseClass))) {
406405
return false;
407406
}
408407

@@ -416,6 +415,6 @@ private function getContextMatcher(array $class)
416415

417416
private function arrayEqual(array $a, array $b): bool
418417
{
419-
return count($a) === count($b) && !array_diff($a, $b);
418+
return \count($a) === \count($b) && !array_diff($a, $b);
420419
}
421420
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespaces:
2+
prefix: App\
3+
entity: App\Entity
4+
types:
5+
Person:
6+
properties:
7+
name: ~

0 commit comments

Comments
 (0)