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

Rename "custom" type to "doctrine" type mapping #2

Merged
merged 2 commits into from
May 25, 2024
Merged
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ return [

## Features

- [Auto-Register Custom Doctrine Types](#auto-register-custom-doctrine-types)
- [Auto-Register Carbon Doctrine Types](#auto-register-carbon-datetime-types)
- [Auto-Register Custom Doctrine Type Mappings](#auto-register-custom-doctrine-types)
- [Auto-Register Carbon Doctrine Type Mappings](#auto-register-carbon-datetime-types)

### Auto-Register Custom Doctrine Types

Expand All @@ -44,12 +44,12 @@ headsnet_doctrine_tools:
- 'src/Infra/Persistence/DBAL/Types'
```

Then add the `#[CustomType]` attribute to the custom type class:
Then add the `#[DoctrineTypeMapping]` attribute to the custom type class:

```php
use Doctrine\DBAL\Types\Type;

#[CustomType]
#[DoctrineTypeMapping]
final class ReservationIdType extends Type
{
// defines "reservation_id" type
Expand All @@ -59,11 +59,11 @@ final class ReservationIdType extends Type
This will register a custom type based on the class name - in this case the custom column type will be called
`reservation_id`.

To customise the type name, specify it in the `#[CustomType]` attribute. The following will register a type
To customise the type name, specify it in the `#[DoctrineTypeMapping]` attribute. The following will register a type
called `my_reservation_id`.

```php
#[CustomType(name: 'my_reservation_id')]
#[DoctrineTypeMapping(name: 'my_reservation_id')]
final class ReservationIdType extends Type
{
// customised name "my_reservation_id" type
Expand Down
2 changes: 2 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

Expand All @@ -16,6 +17,7 @@
$ecsConfig->skip([
BlankLineAfterOpeningTagFixer::class,
NotOperatorWithSuccessorSpaceFixer::class,
MethodChainingIndentationFixer::class
]);

$ecsConfig->rules([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\CustomTypes;
namespace Headsnet\DoctrineToolsBundle\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
final class CustomType
final class DoctrineTypeMapping
{
public string $name;

Expand Down
37 changes: 17 additions & 20 deletions src/HeadsnetDoctrineToolsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Headsnet\DoctrineToolsBundle;

use Headsnet\DoctrineToolsBundle\CarbonTypes\RegisterCarbonTypesCompilerPass;
use Headsnet\DoctrineToolsBundle\CustomTypes\RegisterDoctrineTypesCompilerPass;
use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingsCompilerPass;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand All @@ -15,22 +15,19 @@ public function configure(DefinitionConfigurator $definition): void
{
$definition->rootNode()
->children()
->arrayNode('custom_types')
->children()
->arrayNode('scan_dirs')
->scalarPrototype()->end()
->end()
->end()
->end() // End custom_types
->arrayNode('carbon_types')
->canBeDisabled()
->children()
->booleanNode('enabled')
->defaultTrue()->end()
->booleanNode('replace')
->defaultTrue()->end()
->end()
->end() // End carbon_types
->arrayNode('custom_types')
->children()
->arrayNode('scan_dirs')
->scalarPrototype()->end()
->end()
->end()
->end() // End custom_types
->arrayNode('carbon_types')
->canBeDisabled()
->children()
->booleanNode('replace')->defaultTrue()->end()
->end()
->end() // End carbon_types
;
}

Expand All @@ -54,11 +51,11 @@ public function build(ContainerBuilder $container): void
parent::build($container);

$container->addCompilerPass(
new RegisterDoctrineTypesCompilerPass()
new DoctrineTypeMappingsCompilerPass()
);

$container->addCompilerPass(
new RegisterCarbonTypesCompilerPass()
new CarbonTypeMappingsCompilerPass()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\CarbonTypes;
namespace Headsnet\DoctrineToolsBundle\Mapping;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Automatically registers the custom Doctrine types provided by the Carbon library.
*/
final class RegisterCarbonTypesCompilerPass implements CompilerPassInterface
final class CarbonTypeMappingsCompilerPass implements CompilerPassInterface
{
private const TYPE_DEFINITION_PARAMETER = 'doctrine.dbal.connection_factory.types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\CustomTypes;
namespace Headsnet\DoctrineToolsBundle\Mapping;

use Doctrine\DBAL\Types\Type;
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineTypeMapping;
use ReflectionClass;

/**
* If the #[CustomType] attribute has a name specified, use it.
*
* Otherwise derive it from the class name - e.g. "CarbonDateType" => "carbon_date"
*/
final class CustomTypeNamer
final class DoctrineTypeMappingNamer
{
/**
* @param ReflectionClass<Type> $reflection
*/
public static function getTypeName(ReflectionClass $reflection): string
{
$attribute = $reflection->getAttributes(CustomType::class)[0];
$attribute = $reflection->getAttributes(DoctrineTypeMapping::class)[0];

$attributeArgs = $attribute->getArguments();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\CustomTypes;
namespace Headsnet\DoctrineToolsBundle\Mapping;

use Doctrine\DBAL\Types\Type;
use Generator;
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineTypeMapping;
use League\ConstructFinder\ConstructFinder;
use ReflectionClass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -15,7 +16,7 @@
*
* This saves having to specify them all individually in the Doctrine configuration which is tedious.
*/
final class RegisterDoctrineTypesCompilerPass implements CompilerPassInterface
final class DoctrineTypeMappingsCompilerPass implements CompilerPassInterface
{
private const TYPE_DEFINITION_PARAMETER = 'doctrine.dbal.connection_factory.types';

Expand Down Expand Up @@ -72,9 +73,9 @@ private function findTypesInApplication(array $scanDirs): iterable
}

// Only register types that have the #[CustomType] attribute
if ($reflection->getAttributes(CustomType::class)) {
if ($reflection->getAttributes(DoctrineTypeMapping::class)) {
yield [
'name' => CustomTypeNamer::getTypeName($reflection),
'name' => DoctrineTypeMappingNamer::getTypeName($reflection),
'class' => $className,
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\Tests\CustomTypes;
namespace Headsnet\DoctrineToolsBundle\Tests\Attribute;

use Headsnet\DoctrineToolsBundle\CustomTypes\CustomType;
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineTypeMapping;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(CustomType::class)]
class CustomTypeTest extends TestCase
#[CoversClass(DoctrineTypeMapping::class)]
class DoctrineTypeMappingTest extends TestCase
{
#[Test]
public function name_can_be_specified(): void
{
$sut = new CustomType('custom_name');
$sut = new DoctrineTypeMapping('custom_name');

$this->assertEquals('custom_name', $sut->name);
}

#[Test]
public function name_is_normalised(): void
{
$sut = new CustomType('some-custom Name');
$sut = new DoctrineTypeMapping('some-custom Name');

$this->assertEquals('some_custom_name', $sut->name);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/HeadsnetDoctrineToolsBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

namespace Headsnet\DoctrineToolsBundle\Tests;

use Headsnet\DoctrineToolsBundle\CarbonTypes\RegisterCarbonTypesCompilerPass;
use Headsnet\DoctrineToolsBundle\CustomTypes\RegisterDoctrineTypesCompilerPass;
use Headsnet\DoctrineToolsBundle\HeadsnetDoctrineToolsBundle;
use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingsCompilerPass;
use Nyholm\BundleTest\TestKernel;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;

#[CoversClass(HeadsnetDoctrineToolsBundle::class)]
#[CoversClass(RegisterCarbonTypesCompilerPass::class)]
#[CoversClass(RegisterDoctrineTypesCompilerPass::class)]
#[CoversClass(CarbonTypeMappingsCompilerPass::class)]
#[CoversClass(DoctrineTypeMappingsCompilerPass::class)]
class HeadsnetDoctrineToolsBundleTest extends KernelTestCase
{
protected static function getKernelClass(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\Tests\CarbonTypes;
namespace Headsnet\DoctrineToolsBundle\Tests\Mapping;

use Carbon\Doctrine\DateTimeImmutableType;
use Carbon\Doctrine\DateTimeType;
use Headsnet\DoctrineToolsBundle\CarbonTypes\RegisterCarbonTypesCompilerPass;
use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

#[CoversClass(RegisterCarbonTypesCompilerPass::class)]
class RegisterCarbonTypesCompilerPassTest extends TestCase
#[CoversClass(CarbonTypeMappingsCompilerPass::class)]
class CarbonTypeMappingsCompilerPassTest extends TestCase
{
#[Test]
public function carbon_types_are_registered(): void
Expand All @@ -21,7 +21,7 @@ public function carbon_types_are_registered(): void
$container->setParameter('doctrine.dbal.connection_factory.types', []);
$container->setParameter('headsnet_doctrine_tools.carbon_types.enabled', true);
$container->setParameter('headsnet_doctrine_tools.carbon_types.replace', true);
$sut = new RegisterCarbonTypesCompilerPass();
$sut = new CarbonTypeMappingsCompilerPass();

$sut->process($container);

Expand All @@ -44,7 +44,7 @@ public function carbon_types_are_registered_separately(): void
$container->setParameter('doctrine.dbal.connection_factory.types', []);
$container->setParameter('headsnet_doctrine_tools.carbon_types.enabled', true);
$container->setParameter('headsnet_doctrine_tools.carbon_types.replace', false);
$sut = new RegisterCarbonTypesCompilerPass();
$sut = new CarbonTypeMappingsCompilerPass();

$sut->process($container);

Expand All @@ -66,7 +66,7 @@ public function if_disabled_then_register_nothing(): void
$container = new ContainerBuilder();
$container->setParameter('doctrine.dbal.connection_factory.types', []);
$container->setParameter('headsnet_doctrine_tools.carbon_types.enabled', false);
$sut = new RegisterCarbonTypesCompilerPass();
$sut = new CarbonTypeMappingsCompilerPass();

$sut->process($container);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\Tests\CustomTypes;
namespace Headsnet\DoctrineToolsBundle\Tests\Mapping;

use Headsnet\DoctrineToolsBundle\CustomTypes\CustomTypeNamer;
use Headsnet\DoctrineToolsBundle\Tests\CustomTypes\Fixtures\DummyCustomType;
use Headsnet\DoctrineToolsBundle\Tests\CustomTypes\Fixtures\DummyCustomTypeWithName;
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingNamer;
use Headsnet\DoctrineToolsBundle\Tests\Mapping\Fixtures\DummyCustomType;
use Headsnet\DoctrineToolsBundle\Tests\Mapping\Fixtures\DummyCustomTypeWithName;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

#[CoversClass(CustomTypeNamer::class)]
class CustomTypeNamerTest extends TestCase
#[CoversClass(DoctrineTypeMappingNamer::class)]
class DoctrineTypeMappingNamerTest extends TestCase
{
#[Test]
public function default_name_derived_from_class(): void
{
$reflection = new ReflectionClass(new DummyCustomType());

$sut = CustomTypeNamer::getTypeName($reflection);
$sut = DoctrineTypeMappingNamer::getTypeName($reflection);

$this->assertEquals('dummy_custom', $sut);
}
Expand All @@ -29,7 +29,7 @@ public function name_can_be_specified(): void
{
$reflection = new ReflectionClass(new DummyCustomTypeWithName());

$sut = CustomTypeNamer::getTypeName($reflection);
$sut = DoctrineTypeMappingNamer::getTypeName($reflection);

$this->assertEquals('my_custom_name', $sut);
}
Expand Down
Loading