From 84d9920ad3a574fd51a6e80dabbe33907d2de694 Mon Sep 17 00:00:00 2001 From: Indra Gunawan Date: Tue, 30 Nov 2021 01:36:44 +0800 Subject: [PATCH 1/3] add support to custom type hint --- .../SetDoctrineAnnotatedPrefixesPass.php | 2 +- src/DependencyInjection/Configuration.php | 5 ++ src/DependencyInjection/MakerExtension.php | 3 +- src/Doctrine/DoctrineHelper.php | 10 ++- src/Doctrine/EntityRegenerator.php | 5 +- src/Maker/MakeEntity.php | 9 ++- src/Maker/MakeResetPassword.php | 3 +- src/Maker/MakeUser.php | 3 +- src/Resources/config/services.xml | 8 ++- src/Util/ClassSourceManipulator.php | 11 +++- .../DependencyInjection/ConfigurationTest.php | 65 +++++++++++++++++++ 11 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 tests/DependencyInjection/ConfigurationTest.php diff --git a/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php b/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php index 5829f4a53..b2c379624 100644 --- a/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php +++ b/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php @@ -63,7 +63,7 @@ public function process(ContainerBuilder $container): void } if (null !== $annotatedPrefixes) { - $container->getDefinition('maker.doctrine_helper')->setArgument(4, $annotatedPrefixes); + $container->getDefinition('maker.doctrine_helper')->replaceArgument('$annotatedPrefixes', $annotatedPrefixes); } } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 787672159..0bef0d593 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -32,6 +32,11 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->children() ->scalarNode('root_namespace')->defaultValue('App')->end() + ->arrayNode('custom_type_hints') + ->fixXmlConfig('custom_type_hints') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() ->end() ; diff --git a/src/DependencyInjection/MakerExtension.php b/src/DependencyInjection/MakerExtension.php index 0a173bcce..1d020b04f 100644 --- a/src/DependencyInjection/MakerExtension.php +++ b/src/DependencyInjection/MakerExtension.php @@ -63,7 +63,8 @@ public function load(array $configs, ContainerBuilder $container): void $makeCommandDefinition->replaceArgument(1, $rootNamespace); $doctrineHelperDefinition = $container->getDefinition('maker.doctrine_helper'); - $doctrineHelperDefinition->replaceArgument(0, $rootNamespace.'\\Entity'); + $doctrineHelperDefinition->replaceArgument('$entityNamespace', $rootNamespace.'\\Entity'); + $doctrineHelperDefinition->replaceArgument('$customTypeHints', $config['custom_type_hints']); $container->registerForAutoconfiguration(MakerInterface::class) ->addTag(MakeCommandRegistrationPass::MAKER_TAG); diff --git a/src/Doctrine/DoctrineHelper.php b/src/Doctrine/DoctrineHelper.php index 961d42981..a48b887ce 100644 --- a/src/Doctrine/DoctrineHelper.php +++ b/src/Doctrine/DoctrineHelper.php @@ -57,16 +57,19 @@ final class DoctrineHelper private $attributeMappingSupport; + private $customTypeHints; + /** * @var ManagerRegistry|LegacyManagerRegistry */ - public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null) + public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null, array $customTypeHints = []) { $this->entityNamespace = trim($entityNamespace, '\\'); $this->phpCompatUtil = $phpCompatUtil; $this->registry = $registry; $this->attributeMappingSupport = $attributeMappingSupport; $this->mappingDriversByPrefix = $annotatedPrefixes; + $this->customTypeHints = $customTypeHints; } /** @@ -324,4 +327,9 @@ private function getMappingDriverForNamespace(string $namespace): ?MappingDriver return $foundDriver; } + + public function getCustomTypeHints(): array + { + return $this->customTypeHints; + } } diff --git a/src/Doctrine/EntityRegenerator.php b/src/Doctrine/EntityRegenerator.php index d72b65364..bed267fb0 100644 --- a/src/Doctrine/EntityRegenerator.php +++ b/src/Doctrine/EntityRegenerator.php @@ -206,7 +206,10 @@ private function createClassManipulator(string $classPath): ClassSourceManipulat // if properties need to be generated then, by definition, // some non-annotation config is being used, and so, the // properties should not have annotations added to them - false + false, + true, + false, + $this->doctrineHelper->getCustomTypeHints() ); } diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index 917013d4d..f61810231 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -801,7 +801,14 @@ private function createClassManipulator(string $path, ConsoleStyle $io, bool $ov $useAttributes = $this->doctrineHelper->doesClassUsesAttributes($className) && $this->doctrineHelper->isDoctrineSupportingAttributes(); $useAnnotations = $this->doctrineHelper->isClassAnnotated($className) || !$useAttributes; - $manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite, $useAnnotations, true, $useAttributes); + $manipulator = new ClassSourceManipulator( + $this->fileManager->getFileContents($path), + $overwrite, + $useAnnotations, + true, + $useAttributes, + $this->doctrineHelper->getCustomTypeHints() + ); $manipulator->setIo($io); diff --git a/src/Maker/MakeResetPassword.php b/src/Maker/MakeResetPassword.php index d73ace9f3..063a59ec4 100644 --- a/src/Maker/MakeResetPassword.php +++ b/src/Maker/MakeResetPassword.php @@ -380,7 +380,8 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r false, !$useAttributesForDoctrineMapping, true, - $useAttributesForDoctrineMapping + $useAttributesForDoctrineMapping, + $this->doctrineHelper->getCustomTypeHints() ); $manipulator->addInterface(ResetPasswordRequestInterface::class); diff --git a/src/Maker/MakeUser.php b/src/Maker/MakeUser.php index d2a8a92d1..61c657435 100644 --- a/src/Maker/MakeUser.php +++ b/src/Maker/MakeUser.php @@ -172,7 +172,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen true, !$useAttributesForDoctrineMapping, true, - $useAttributesForDoctrineMapping + $useAttributesForDoctrineMapping, + $this->doctrineHelper->getCustomTypeHints() ); $manipulator->setIo($io); diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 26af9d257..48f3e43b0 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -36,10 +36,12 @@ - - - + + + %maker.compatible_check.doctrine.supports_attributes% + + diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index c74dc0641..feda7f06a 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -56,7 +56,9 @@ final class ClassSourceManipulator private $pendingComments = []; - public function __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true, bool $useAttributesForDoctrineMapping = false) + private $customTypeHints = []; + + public function __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true, bool $useAttributesForDoctrineMapping = false, array $customTypeHints = []) { $this->overwrite = $overwrite; $this->useAnnotations = $useAnnotations; @@ -71,6 +73,7 @@ public function __construct(string $sourceCode, bool $overwrite = false, bool $u ]); $this->parser = new Parser\Php7($this->lexer); $this->printer = new PrettyPrinter(); + $this->customTypeHints = $customTypeHints; $this->setSourceCode($sourceCode); } @@ -1158,6 +1161,12 @@ private function getEntityTypeHint($doctrineType): ?string case 'binary': case 'blob': default: + if (isset($this->customTypeHints[$doctrineType])) { + $type = $this->customTypeHints[$doctrineType]; + + return '\\'.\ltrim($type, '\\'); + } + return null; } } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 000000000..e8f0acf4c --- /dev/null +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,65 @@ +configuration = new Configuration(); + $this->processor = new Processor(); + } + + protected function tearDown(): void + { + $this->configuration = null; + $this->processor = null; + } + + public function testDefaultConfig() + { + $config = $this->processor->processConfiguration( + $this->configuration, + [ + [], + ] + ); + + $this->assertSame('App', $config['root_namespace']); + $this->assertEmpty($config['custom_type_hints']); + } + + public function testCustomTypeHints() + { + $config = $this->processor->processConfiguration( + $this->configuration, + [ + [ + 'custom_type_hints' => [ + 'my_type' => DateTimeImmutable::class, + ], + ], + ] + ); + + $this->assertSame(['my_type' => DateTimeImmutable::class], $config['custom_type_hints']); + } +} From 0f6186538cb1e88e0be14a6545a00d4b684b243b Mon Sep 17 00:00:00 2001 From: Indra Gunawan Date: Tue, 30 Nov 2021 12:21:31 +0800 Subject: [PATCH 2/3] fix cs --- src/Util/ClassSourceManipulator.php | 4 ++-- tests/DependencyInjection/ConfigurationTest.php | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index feda7f06a..ef1eb0071 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -1162,9 +1162,9 @@ private function getEntityTypeHint($doctrineType): ?string case 'blob': default: if (isset($this->customTypeHints[$doctrineType])) { - $type = $this->customTypeHints[$doctrineType]; + $type = $this->customTypeHints[$doctrineType]; - return '\\'.\ltrim($type, '\\'); + return '\\'.ltrim($type, '\\'); } return null; diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index e8f0acf4c..f4ddaf141 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Indragunawan\ApiRateLimitBundle\Tests\DependencyInjection; use DateTimeImmutable; From 1863d55d93a41b08e46640864e10c23886d36cb5 Mon Sep 17 00:00:00 2001 From: Indra Gunawan Date: Wed, 1 Dec 2021 01:17:10 +0800 Subject: [PATCH 3/3] fix service argument --- src/Resources/config/services.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 48f3e43b0..6652d2b29 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -40,7 +40,7 @@ %maker.compatible_check.doctrine.supports_attributes% - + null