10000 add support to custom type hint · symfony/maker-bundle@84d9920 · GitHub
[go: up one dir, main page]

Skip to content

Commit 84d9920

Browse files
committed
add support to custom type hint
1 parent 0f40c82 commit 84d9920

File tree

11 files changed

+113
-11
lines changed

11 files changed

+113
-11
lines changed

src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function process(ContainerBuilder $container): void
6363
}
6464

6565
if (null !== $annotatedPrefixes) {
66-
$container->getDefinition('maker.doctrine_helper')->setArgument(4, $annotatedPrefixes);
66+
$container->getDefinition('maker.doctrine_helper')->replaceArgument('$annotatedPrefixes', $annotatedPrefixes);
6767
}
6868
}
6969
}

src/DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function getConfigTreeBuilder(): TreeBuilder
3232
$rootNode
3333
->children()
3434
->scalarNode('root_namespace')->defaultValue('App')->end()
35+
->arrayNode('custom_type_hints')
36+
->fixXmlConfig('custom_type_hints')
37+
->useAttributeAsKey('name')
38+
->prototype('scalar')->end()
39+
->end()
3540
->end()
3641
;
3742

src/DependencyInjection/MakerExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function load(array $configs, ContainerBuilder $container): void
6363
$makeCommandDefinition->replaceArgument(1, $rootNamespace);
6464

6565
$doctrineHelperDefinition = $container->getDefinition('maker.doctrine_helper');
66-
$doctrineHelperDefinition->replaceArgument(0, $rootNamespace.'\\Entity');
66+
$doctrineHelperDefinition->replaceArgument('$entityNamespace', $rootNamespace.'\\Entity');
67+
$doctrineHelperDefinition->replaceArgument('$customTypeHints', $config['custom_type_hints']);
6768

6869
$container->registerForAutoconfiguration(MakerInterface::class)
6970
->addTag(MakeCommandRegistrationPass::MAKER_TAG);

src/Doctrine/DoctrineHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ final class DoctrineHelper
5757

5858
private $attributeMappingSupport;
5959

60+
private $customTypeHints;
61+
6062
/**
6163
* @var ManagerRegistry|LegacyManagerRegistry
6264
*/
63-
public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null)
65+
public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null, array $customTypeHints = [])
6466
{
6567
$this->entityNamespace = trim($entityNamespace, '\\');
6668
$this->phpCompatUtil = $phpCompatUtil;
6769
$this->registry = $registry;
6870
$this->attributeMappingSupport = $attributeMappingSupport;
6971
$this->mappingDriversByPrefix = $annotatedPrefixes;
72+
$this->customTypeHints = $customTypeHints;
7073
}
7174

7275
/**
@@ -324,4 +327,9 @@ private function getMappingDriverForNamespace(string $namespace): ?MappingDriver
324327

325328
return $foundDriver;
326329
}
330+
331+
public function getCustomTypeHints(): array
332+
{
333+
return $this->customTypeHints;
334+
}
327335
}

src/Doctrine/EntityRegenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ private function createClassManipulator(string $classPath): ClassSourceManipulat
206206
// if properties need to be generated then, by definition,
207207
// some non-annotation config is being used, and so, the
208208
// properties should not have annotations added to them
209-
false
209+
false,
210+
true,
211+
false,
212+
$this->doctrineHelper->getCustomTypeHints()
210213
);
211214
}
212215

src/Maker/MakeEntity.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,14 @@ private function createClassManipulator(string $path, ConsoleStyle $io, bool $ov
801801
$useAttributes = $this->doctrineHelper->doesClassUsesAttributes($className) && $this->doctrineHelper->isDoctrineSupportingAttributes();
802802
$useAnnotations = $this->doctrineHelper->isClassAnnotated($className) || !$useAttributes;
803803

804-
$manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite, $useAnnotations, true, $useAttributes);
804+
$manipulator = new ClassSourceManipulator(
805+
$this->fileManager->getFileContents($path),
806+
$overwrite,
807+
$useAnnotations,
808+
true,
809+
$useAttributes,
810+
$this->doctrineHelper->getCustomTypeHints()
811+
);
805812

806813
$manipulator->setIo($io);
807814

src/Maker/MakeResetPassword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
380380
false,
381381
!$useAttributesForDoctrineMapping,
382382
true,
383-
$useAttributesForDoctrineMapping
383+
$useAttributesForDoctrineMapping,
384+
$this->doctrineHelper->getCustomTypeHints()
384385
);
385386

386387
$manipulator->addInterface(ResetPasswordRequestInterface::class);

src/Maker/MakeUser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
172172
true,
173173
!$useAttributesForDoctrineMapping,
174174
true,
175-
$useAttributesForDoctrineMapping
175+
$useAttributesForDoctrineMapping,
176+
$this->doctrineHelper->getCustomTypeHints()
176177
);
177178

178179
$manipulator->setIo($io);

src/Resources/config/services.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@
3636
</service>
3737

3838
<service id="maker.doctrine_helper" class="Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper">
39-
<argument /> <!-- entity namespace -->
40-
<argument type="service" id="maker.php_compat_util" />
41-
<argument type="service" id="doctrine" on-invalid="ignore" />
39+
<argument key="$entityNamespace"/>
40+
<argument key="$phpCompatUtil" type="service" id="maker.php_compat_util" />
41+
<argument key="$registry" type="service" id="doctrine" on-invalid="ignore" />
4242
<argument key="$attributeMappingSupport">%maker.compatible_check.doctrine.supports_attributes%</argument>
43+
<argument key="$annotatedPrefixes" />
44+
<argument key="$customTypeHints" />
4345
</service>
4446

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

src/Util/ClassSourceManipulator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ final class ClassSourceManipulator
5656

5757
private $pendingComments = [];
5858

59-
public functio 68F6 n __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true, bool $useAttributesForDoctrineMapping = false)
59+
private $customTypeHints = [];
60+
61+
public function __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true, bool $useAttributesForDoctrineMapping = false, array $customTypeHints = [])
6062
{
6163
$this->overwrite = $overwrite;
6264
$this->useAnnotations = $useAnnotations;
@@ -71,6 +73,7 @@ public function __construct(string $sourceCode, bool $overwrite = false, bool $u
7173
]);
7274
$this->parser = new Parser\Php7($this->lexer);
7375
$this->printer = new PrettyPrinter();
76+
$this->customTypeHints = $customTypeHints;
7477

7578
$this->setSourceCode($sourceCode);
7679
}
@@ -1158,6 +1161,12 @@ private function getEntityTypeHint($doctrineType): ?string
11581161
case 'binary':
11591162
case 'blob':
11601163
default:
1164+
if (isset($this->customTypeHints[$doctrineType])) {
1165+
$type = $this->customTypeHints[$doctrineType];
1166+
1167+
return '\\'.\ltrim($type, '\\');
1168+
}
1169+
11611170
return null;
11621171
}
11631172
}

0 commit comments

Comments
 (0)
0