Skip to content

Commit 38ade4c

Browse files
committed
NamespacesHelper accepts array of namespaces.
MakeEntity is backwards compatible.
1 parent 5121cd8 commit 38ade4c

File tree

7 files changed

+27
-108
lines changed

7 files changed

+27
-108
lines changed

src/DependencyInjection/MakerExtension.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ public function load(array $configs, ContainerBuilder $container): void
7373
$config = $this->processConfiguration($configuration, $configs);
7474

7575
$namespacesHelperDefinition = $container->getDefinition('maker.namespaces_helper');
76-
foreach (static::$namespaces as $index => $namespace) {
77-
$namespacesHelperDefinition->replaceArgument($index, trim($config[$namespace], '\\'));
78-
}
76+
$namespacesHelperDefinition->replaceArgument(0, $config);
7977

8078
$container->registerForAutoconfiguration(MakerInterface::class)
8179
->addTag(MakeCommandRegistrationPass::MAKER_TAG);

src/Maker/MakeEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464

6565
if (null === $generator) {
6666
@trigger_error(sprintf('Passing a "%s" instance as 4th argument is mandatory since version 1.5.', Generator::class), \E_USER_DEPRECATED);
67-
$this->generator = new Generator($fileManager, new NamespacesHelper('App\\'));
67+
$this->generator = new Generator($fileManager, new NamespacesHelper());
6868
} else {
6969
$this->generator = $generator;
7070
}

src/Resources/config/services.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,7 @@
4141
</service>
4242

4343
<service id="maker.namespaces_helper" class="Symfony\Bundle\MakerBundle\Util\NamespacesHelper">
44-
<argument /> <!-- root namespace -->
45-
<argument /> <!-- command namespace -->
46-
<argument /> <!-- controller namespace -->
47-
<argument /> <!-- entity namespace -->
48-
<argument /> <!-- fixtures namespace -->
49-
<argument /> <!-- form namespace -->
50-
<argument /> <!-- functional_test namespace -->
51-
<argument /> <!-- repository namespace -->
52-
<argument /> <!-- security namespace -->
53-
<argument /> <!-- serializer namespace -->
54-
<argument /> <!-- subscriber namespace -->
55-
<argument /> <!-- twig namespace -->
56-
<argument /> <!-- unit_test namespace -->
57-
<argument /> <!-- validator namespace -->
44+
<argument type="collection" /> <!-- namespaces -->
5845
</service>
5946

6047
<service id="maker.auto_command.abstract" class="Symfony\Bundle\MakerBundle\Command\MakerCommand" abstract="true">

src/Util/NamespacesHelper.php

Lines changed: 20 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,148 +15,82 @@
1515

1616
final class NamespacesHelper
1717
{
18-
/** @var string */
19-
private $command;
20-
21-
/** @var string */
22-
private $controller;
23-
24-
/** @var string */
25-
private $entity;
26-
27-
/** @var string */
28-
private $fixtures;
29-
30-
/** @var string */
31-
private $form;
32-
33-
/** @var string */
34-
private $functionalTest;
35-
36-
/** @var string */
37-
private $repository;
38-
39-
/** @var string */
40-
private $root;
41-
42-
/** @var string */
43-
private $security;
44-
45-
/** @var string */
46-
private $serializer;
47-
48-
/** @var string */
49-
private $subscriber;
50-
51-
/** @var string */
52-
private $twig;
53-
54-
/** @var string */
55-
private $unitTest;
56-
57-
/** @var string */
58-
private $validator;
59-
60-
public function __construct(
61-
string $root,
62-
string $command = null,
63-
string $controller = null,
64-
string $entity = null,
65-
string $fixtures = null,
66-
string $form = null,
67-
string $functionalTest = null,
68-
string $repository = null,
69-
string $security = null,
70-
string $serializer = null,
71-
string $subscriber = null,
72-
string $twig = null,
73-
string $unitTest = null,
74-
string $validator = null
75-
) {
76-
$this->root = $root;
77-
$this->command = $command ?? 'Command\\';
78-
$this->controller = $controller ?? 'Controller\\';
79-
$this->entity = $entity ?? 'Entity\\';
80-
$this->fixtures = $fixtures ?? 'DataFixtures\\';
81-
$this->form = $form ?? 'Form\\';
82-
$this->functionalTest = $functionalTest ?? 'Tests\\';
83-
$this->repository = $repository ?? 'Repository\\';
84-
$this->security = $security ?? 'Security\\';
85-
$this->serializer = $serializer ?? 'Serializer\\';
86-
$this->subscriber = $subscriber ?? 'EventSubscriber\\';
87-
$this->twig = $twig ?? 'Twig\\';
88-
$this->unitTest = $unitTest ?? 'Tests\\';
89-
$this->validator = $validator ?? 'Validator\\';
18+
/** @var string[] */
19+
private $namespaces;
20+
21+
public function __construct(array $namespaces = null)
22+
{
23+
$this->namespaces = $namespaces ?? [];
9024
}
9125

9226
public function getCommandNamespace(): string
9327
{
94-
return $this->trim($this->command);
28+
return $this->trim($this->namespaces['command_namespace'] ?? 'Command\\');
9529
}
9630

9731
public function getControllerNamespace(): string
9832
{
99-
return $this->trim($this->controller);
33+
return $this->trim($this->namespaces['controller_namespace'] ?? 'Controller\\');
10034
}
10135

10236
public function getEntityNamespace(): string
10337
{
104-
return $this->trim($this->entity);
38+
return $this->trim($this->namespaces['entity_namespace'] ?? 'Entity\\');
10539
}
10640

10741
public function getFixturesNamespace(): string
10842
{
109-
return $this->trim($this->fixtures);
43+
return $this->trim($this->namespaces['fixtures_namespace'] ?? 'DataFixtures\\');
11044
}
11145

11246
public function getFormNamespace(): string
11347
{
114-
return $this->trim($this->form);
48+
return $this->trim($this->namespaces['form_namespace'] ?? 'Form\\');
11549
}
11650

11751
public function getFunctionalTestNamespace(): string
11852
{
119-
return $this->trim($this->functionalTest);
53+
return $this->trim($this->namespaces['functional_test_namespace'] ?? 'Tests\\');
12054
}
12155

12256
public function getRepositoryNamespace(): string
12357
{
124-
return $this->trim($this->repository);
58+
return $this->trim($this->namespaces['repository_namespace'] ?? 'Repository\\');
12559
}
12660

12761
public function getRootNamespace(): string
12862
{
129-
return $this->trim($this->root);
63+
return $this->trim($this->namespaces['root_namespace'] ?? 'App\\');
13064
}
13165

13266
public function getSecurityNamespace(): string
13367
{
134-
return $this->trim($this->security);
68+
return $this->trim($this->namespaces['security_namespace'] ?? 'Security\\');
13569
}
13670

13771
public function getSerializerNamespace(): string
13872
{
139-
return $this->trim($this->serializer);
73+
return $this->trim($this->namespaces['serializer_namespace'] ?? 'Serializer\\');
14074
}
14175

14276
public function getSubscriberNamespace(): string
14377
{
144-
return $this->trim($this->subscriber);
78+
return $this->trim($this->namespaces['subscriber_namespace'] ?? 'EventSubscriber\\');
14579
}
14680

14781
public function getTwigNamespace(): string
14882
{
149-
return $this->trim($this->twig);
83+
return $this->trim($this->namespaces['twig_namespace'] ?? 'Twig\\');
15084
}
15185

15286
public function getUnitTestNamespace(): string
15387
{
154-
return $this->trim($this->unitTest);
88+
return $this->trim($this->namespaces['unit_test_namespace'] ?? 'Tests\\');
15589
}
15690

15791
public function getValidatorNamespace(): string
15892
{
159-
return $this->trim($this->validator);
93+
return $this->trim($this->namespaces['validator_namespace'] ?? 'Validator\\');
16094
}
16195

16296
/**

tests/Command/MakerCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testExceptionOnMissingDependencies(): void
4040

4141
$fileManager = $this->createMock(FileManager::class);
4242

43-
$namespacesHelper = new NamespacesHelper('App');
43+
$namespacesHelper = new NamespacesHelper(['root_namespace' => 'App']);
4444

4545
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, $namespacesHelper));
4646
// needed because it's normally set by the Application
@@ -55,7 +55,7 @@ public function testExceptionOnUnknownRootNamespace(): void
5555

5656
$fileManager = $this->createMock(FileManager::class);
5757

58-
$namespacesHelper = new NamespacesHelper('Unknown');
58+
$namespacesHelper = new NamespacesHelper(['root_namespace' => 'Unknown']);
5959

6060
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, $namespacesHelper));
6161
// needed because it's normally set by the Application

tests/Doctrine/EntityRegeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $n
9797
return $tmpDir.'/src/'.str_replace('\\', '/', $shortClassName).'.php';
9898
});
9999

100-
$namespacesHelper = new NamespacesHelper('App\\');
100+
$namespacesHelper = new NamespacesHelper();
101101
$fileManager = new FileManager($fs, $autoloaderUtil, new MakerFileLinkFormatter(null), $tmpDir);
102102
$doctrineHelper = new DoctrineHelper($namespacesHelper, $container->get('doctrine'));
103103
$generator = new Generator($fileManager, $namespacesHelper);

tests/GeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testCreateClassNameDetails(string $name, string $prefix, string
2828
->method('getNamespacePrefixForClass')
2929
->willReturn('Foo');
3030

31-
$generator = new Generator($fileManager, new NamespacesHelper('App\\'));
31+
$generator = new Generator($fileManager, new NamespacesHelper());
3232
$classNameDetails = $generator->createClassNameDetails($name, $prefix, $suffix);
3333

3434
$this->assertSame($expectedFullClassName, $classNameDetails->getFullName());

0 commit comments

Comments
 (0)