Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to custom type hint #1021

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;

Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -323,4 +326,9 @@ private function getMappingDriverForNamespace(string $namespace): ?MappingDriver

return $foundDriver;
}

public function getCustomTypeHints(): array
{
return $this->customTypeHints;
}
}
5 changes: 4 additions & 1 deletion src/Doctrine/EntityRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,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()
);
}

Expand Down
9 changes: 8 additions & 1 deletion src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/Maker/MakeResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
false,
!$useAttributesForDoctrineMapping,
true,
$useAttributesForDoctrineMapping
$useAttributesForDoctrineMapping,
$this->doctrineHelper->getCustomTypeHints()
);

$manipulator->addInterface(ResetPasswordRequestInterface::class);
Expand Down
3 changes: 2 additions & 1 deletion src/Maker/MakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
true,
!$useAttributesForDoctrineMapping,
true,
$useAttributesForDoctrineMapping
$useAttributesForDoctrineMapping,
$this->doctrineHelper->getCustomTypeHints()
);

$manipulator->setIo($io);
Expand Down
8 changes: 5 additions & 3 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
</service>

<service id="maker.doctrine_helper" class="Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper">
<argument /> <!-- entity namespace -->
<argument type="service" id="maker.php_compat_util" />
<argument type="service" id="doctrine" on-invalid="ignore" />
<argument key="$entityNamespace"/>
<argument key="$phpCompatUtil" type="service" id="maker.php_compat_util" />
<argument key="$registry" type="service" id="doctrine" on-invalid="ignore" />
<argument key="$attributeMappingSupport">%maker.compatible_check.doctrine.supports_attributes%</argument>
<argument key="$annotatedPrefixes">null</argument>
<argument key="$customTypeHints" />
</service>

<service id="maker.auto_command.abstract" class="Symfony\Bundle\MakerBundle\Command\MakerCommand" abstract="true">
Expand Down
11 changes: 10 additions & 1 deletion src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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;
Expand All @@ -78,6 +80,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);
}
Expand Down Expand Up @@ -1202,6 +1205,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;
}
}
Expand Down
74 changes: 74 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Indragunawan\ApiRateLimitBundle\Tests\DependencyInjection;
IndraGunawan marked this conversation as resolved.
Show resolved Hide resolved

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MakerBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends TestCase
{
/**
* @var Configuration
*/
private $configuration;

/**
* @var Processor
*/
private $processor;

/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->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']);
}
}