-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from headsnet/carbon-type-support
Auto-register Doctrine types provided by Carbon
- Loading branch information
Showing
6 changed files
with
206 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\CarbonTypes; | ||
|
||
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 | ||
{ | ||
private const TYPE_DEFINITION_PARAMETER = 'doctrine.dbal.connection_factory.types'; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
// Skip if carbon_types is disabled in the configuration | ||
if (!$container->getParameter('headsnet_doctrine_tools.carbon_types.enabled')) { | ||
return; | ||
} | ||
|
||
// Skip if Doctrine is not installed | ||
if (!$container->hasParameter(self::TYPE_DEFINITION_PARAMETER)) { | ||
return; | ||
} | ||
|
||
// Skip if Carbon is not installed | ||
if (!class_exists('Carbon\Carbon')) { | ||
return; | ||
} | ||
|
||
/** @var array<string, array{class: class-string}> $typeDefinitions */ | ||
$typeDefinitions = $container->getParameter(self::TYPE_DEFINITION_PARAMETER); | ||
|
||
// Use Carbon to upgrade standard datetime and datetime_immutable column types | ||
if ($container->getParameter('headsnet_doctrine_tools.carbon_types.replace')) { | ||
$immutableName = 'datetime_immutable'; | ||
$mutableName = 'datetime'; | ||
} | ||
// Otherwise, register additional types and leave the existing datetime and | ||
// datetime_immutable types in place | ||
else { | ||
$immutableName = 'carbon_immutable'; | ||
$mutableName = 'carbon'; | ||
} | ||
|
||
$typeDefinitions[$immutableName] = [ | ||
'class' => 'Carbon\Doctrine\DateTimeImmutableType', | ||
]; | ||
|
||
$typeDefinitions[$mutableName] = [ | ||
'class' => 'Carbon\Doctrine\DateTimeType', | ||
]; | ||
|
||
$container->setParameter(self::TYPE_DEFINITION_PARAMETER, $typeDefinitions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\CarbonTypes; | ||
|
||
use Carbon\Doctrine\DateTimeImmutableType; | ||
use Carbon\Doctrine\DateTimeType; | ||
use Headsnet\DoctrineToolsBundle\CarbonTypes\RegisterCarbonTypesCompilerPass; | ||
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 | ||
{ | ||
#[Test] | ||
public function carbon_types_are_registered(): void | ||
{ | ||
$container = new ContainerBuilder(); | ||
$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->process($container); | ||
|
||
$result = $container->getParameter('doctrine.dbal.connection_factory.types'); | ||
$expected = [ | ||
'datetime_immutable' => [ | ||
'class' => DateTimeImmutableType::class, | ||
], | ||
'datetime' => [ | ||
'class' => DateTimeType::class, | ||
], | ||
]; | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
#[Test] | ||
public function carbon_types_are_registered_separately(): void | ||
{ | ||
$container = new ContainerBuilder(); | ||
$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->process($container); | ||
|
||
$result = $container->getParameter('doctrine.dbal.connection_factory.types'); | ||
$expected = [ | ||
'carbon_immutable' => [ | ||
'class' => DateTimeImmutableType::class, | ||
], | ||
'carbon' => [ | ||
'class' => DateTimeType::class, | ||
], | ||
]; | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
#[Test] | ||
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->process($container); | ||
|
||
$result = $container->getParameter('doctrine.dbal.connection_factory.types'); | ||
$expected = []; | ||
$this->assertEquals($expected, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters