Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
auto add enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyvastis committed Jun 15, 2021
1 parent 55e9d28 commit b48d172
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/DependencyInjection/DoctrineODMEnumTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PhpArsenal\DoctrineODMEnumTypeBundle\DependencyInjection;

use MyCLabs\Enum\Enum;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;

class DoctrineODMEnumTypeExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
// TODO: Implement load() method.
}

public function prepend(ContainerBuilder $container)
{
$kernelClass = array_values(array_filter($container->getServiceIds(), function (string $serviceId) {
return str_ends_with($serviceId, '\\Kernel');
}))[0];
[$projectNamespace] = explode('\\Kernel', $kernelClass);

$enumClasses = array_values(array_filter(get_declared_classes(), function (string $declaredClass) use ($projectNamespace) {
return str_starts_with($declaredClass, "$projectNamespace\\") && strpos($declaredClass, 'Enum') > 0 && is_subclass_of($declaredClass, Enum::class);
}));

$container->prependExtensionConfig('doctrine_mongodb', [
'type' => array_map(function (string $enumClass) {
return [
'name' => $enumClass,
'class' => $enumClass,
];
}, $enumClasses),
]);
}
}
13 changes: 10 additions & 3 deletions src/DoctrineExtensions/ManagerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace PhpArsenal\DoctrineODMEnumTypeBundle\DoctrineExtensions;

use MyCLabs\Enum\Enum;
use PhpArsenal\DoctrineODMEnumTypeBundle\MappingTypes\PhpEnumType;

class ManagerConfigurator extends \Doctrine\Bundle\MongoDBBundle\ManagerConfigurator
{
public static function loadTypes(array $types): void
{
parent::loadTypes($types);
foreach($types as $i => $type) {
if($type['class'] && is_subclass_of($type['class'], Enum::class)) {
PhpEnumType::registerEnumType($type['class']);
unset($types[$i]);
}
}

// todo: get all enums
// PhpEnumType::registerEnumType(OrderStageEnum::class);
parent::loadTypes($types);
}
}
1 change: 0 additions & 1 deletion src/MappingTypes/PhpEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function getName(): string
*/
public function convertToPHPValue($value)
{
var_dump($value);
if ($value === null) {
return null;
}
Expand Down

0 comments on commit b48d172

Please sign in to comment.