Skip to content
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
24 changes: 14 additions & 10 deletions src/Domain/Infra/DependencyInjection/Bundle/BundleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace MsgPhp\Domain\Infra\DependencyInjection\Bundle;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\Events as DoctrineOrmEvents;
use Doctrine\ORM\Version as DoctrineOrmVersion;
use MsgPhp\Domain\Infra\DependencyInjection\Compiler;
use MsgPhp\Domain\Infra\Doctrine as DoctrineInfra;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
Expand All @@ -22,23 +23,26 @@ public static function initDomain(ContainerBuilder $container): void
{
ContainerHelper::addCompilerPassOnce($container, Compiler\ResolveDomainPass::class);

if (ContainerHelper::isDoctrineOrmEnabled($container)) {
if (class_exists(DoctrineOrmVersion::class)) {
ContainerHelper::addCompilerPassOnce($container, Compiler\DoctrineObjectFieldMappingPass::class);

$container->setParameter('msgphp.doctrine.mapping_cache_dirname', 'msgphp/doctrine-mapping');
(new Filesystem())->mkdir($mappingDir = $container->getParameterBag()->resolveValue('%kernel.cache_dir%/%msgphp.doctrine.mapping_cache_dirname%'));

$container->prependExtensionConfig('doctrine', ['orm' => ['mappings' => ['msgphp' => [
'dir' => $mappingDir,
'type' => 'xml',
'prefix' => 'MsgPhp',
'is_bundle' => false,
]]]]);

$container->register(DoctrineInfra\Event\ObjectFieldMappingListener::class)
->setPublic(false)
->setArgument('$typeConfig', '%msgphp.doctrine.type_config%')
->addTag('doctrine.event_listener', ['event' => DoctrineOrmEvents::loadClassMetadata]);

if (ContainerHelper::hasBundle($container, DoctrineBundle::class)) {
@mkdir($mappingDir = $container->getParameterBag()->resolveValue('%kernel.cache_dir%/%msgphp.doctrine.mapping_cache_dirname%'), 0777, true);

$container->prependExtensionConfig('doctrine', ['orm' => ['mappings' => ['msgphp' => [
'dir' => $mappingDir,
'type' => 'xml',
'prefix' => 'MsgPhp',
'is_bundle' => false,
]]]]);
}
}
}

Expand Down
55 changes: 26 additions & 29 deletions src/Domain/Infra/DependencyInjection/Bundle/ContainerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,9 @@ public static function configureEntityFactory(ContainerBuilder $container, array
$container->setParameter($param, $idClassMap);
}

public static function configureDoctrineOrmMapping(ContainerBuilder $container, array $mappingFiles, array $objectFieldMappings = []): void
{
if (!self::isDoctrineOrmEnabled($container)) {
return;
}

$mappingFileList = $container->hasParameter($param = 'msgphp.doctrine.mapping_files') ? $container->getParameter($param) : [];
$mappingFileList[] = $mappingFiles;

$container->setParameter($param, $mappingFileList);

foreach ($objectFieldMappings as $class) {
$container->register($class)
->setPublic(false)
->addTag('msgphp.doctrine.object_field_mapping', ['priority' => -100]);
}
}

public static function configureDoctrineTypes(ContainerBuilder $container, array $dataTypeMapping, array $classMapping, array $typeMapping): void
{
if (!self::hasBundle($container, DoctrineBundle::class)) {
if (!class_exists(DoctrineType::class)) {
return;
}

Expand Down Expand Up @@ -156,21 +138,41 @@ public static function configureDoctrineTypes(ContainerBuilder $container, array
}

if ($config) {
$container->prependExtensionConfig('doctrine', [
'dbal' => $config,
]);

if ($container->hasParameter($param = 'msgphp.doctrine.type_config')) {
$typeConfig += $container->getParameter($param);
}

$container->setParameter($param, $typeConfig);

if (self::hasBundle($container, DoctrineBundle::class)) {
$container->prependExtensionConfig('doctrine', [
'dbal' => $config,
]);
}
}
}

public static function configureDoctrineOrmMapping(ContainerBuilder $container, array $mappingFiles, array $objectFieldMappings = []): void
{
if (!class_exists(DoctrineOrmVersion::class)) {
return;
}

$mappingFileList = $container->hasParameter($param = 'msgphp.doctrine.mapping_files') ? $container->getParameter($param) : [];
$mappingFileList[] = $mappingFiles;

$container->setParameter($param, $mappingFileList);

foreach ($objectFieldMappings as $class) {
$container->register($class)
->setPublic(false)
->addTag('msgphp.doctrine.object_field_mapping', ['priority' => -100]);
}
}

public static function configureDoctrineOrmTargetEntities(ContainerBuilder $container, array $classMapping): void
{
if (!self::isDoctrineOrmEnabled($container)) {
if (!class_exists(DoctrineOrmVersion::class) || !self::hasBundle($container, DoctrineBundle::class)) {
return;
}

Expand All @@ -181,11 +183,6 @@ public static function configureDoctrineOrmTargetEntities(ContainerBuilder $cont
]);
}

public static function isDoctrineOrmEnabled(Container $container): bool
{
return self::hasBundle($container, DoctrineBundle::class) && class_exists(DoctrineOrmVersion::class);
}

private function __construct()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace MsgPhp\Domain\Infra\DependencyInjection\Compiler;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityManagerInterface as DoctrineEntityManager;
use MsgPhp\Domain\{Factory, DomainIdentityMappingInterface};
use MsgPhp\Domain\Infra\DependencyInjection\Bundle\ContainerHelper;
use MsgPhp\Domain\Infra\{Doctrine as DoctrineInfra, InMemory as InMemoryInfra};
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
Expand All @@ -26,16 +26,18 @@ public function process(ContainerBuilder $container): void
$this->registerIdentityMap($container);
$this->registerEntityFactory($container);

if (ContainerHelper::isDoctrineOrmEnabled($container)) {
if (interface_exists(DoctrineEntityManager::class)) {
self::register($container, DoctrineInfra\DomainIdentityMapping::class)
->setArgument('$em', new Reference(EntityManagerInterface::class));
->setArgument('$em', new Reference(DoctrineEntityManager::class));

self::alias($container, DomainIdentityMappingInterface::class, DoctrineInfra\DomainIdentityMapping::class);

self::register($container, DoctrineInfra\MappingCacheWarmer::class)
->setArgument('$dirname', '%msgphp.doctrine.mapping_cache_dirname%')
->setArgument('$mappingFiles', array_merge(...$container->getParameter('msgphp.doctrine.mapping_files')))
->addTag('kernel.cache_warmer');
if (interface_exists(CacheWarmerInterface::class)) {
self::register($container, DoctrineInfra\MappingCacheWarmer::class)
->setArgument('$dirname', '%msgphp.doctrine.mapping_cache_dirname%')
->setArgument('$mappingFiles', array_merge(...$container->getParameter('msgphp.doctrine.mapping_files')))
->addTag('kernel.cache_warmer');
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Domain/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
"php": "^7.1.0"
},
"require-dev": {
"doctrine/doctrine-bundle": "^1.8",
"doctrine/orm": "^2.6",
"ramsey/uuid": "^3.7",
"ramsey/uuid-doctrine": "^1.4",
"symfony/config": "^3.4|^4.0",
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/http-kernel": "^3.4|^4.0",
"symfony/phpunit-bridge": "^3.4|^4.0",
"symfony/property-access": "^3.4|^4.0",
"symfony/var-dumper": "^3.4|^4.0"
Expand Down
14 changes: 4 additions & 10 deletions src/EavBundle/DependencyInjection/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MsgPhp\EavBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\Version as DoctrineOrmVersion;
use MsgPhp\Domain\Infra\DependencyInjection\Bundle\{ConfigHelper, ContainerHelper};
use MsgPhp\Eav\{AttributeIdInterface, AttributeValueIdInterface, Entity};
use MsgPhp\Eav\Infra\Doctrine as DoctrineInfra;
Expand Down Expand Up @@ -45,11 +45,9 @@ public function load(array $configs, ContainerBuilder $container): void
ContainerHelper::configureEntityFactory($container, $config['class_mapping'], Configuration::AGGREGATE_ROOTS);
ContainerHelper::configureDoctrineOrmMapping($container, self::getDoctrineMappingFiles($config, $container), [DoctrineInfra\EntityFieldsMapping::class]);

$bundles = ContainerHelper::getBundles($container);

// persistence infra
if (isset($bundles[DoctrineBundle::class])) {
$this->prepareDoctrineBundle($config, $loader, $container);
if (class_exists(DoctrineOrmVersion::class)) {
$this->prepareDoctrineOrm($config, $loader, $container);
}
}

Expand All @@ -67,12 +65,8 @@ public function prepend(ContainerBuilder $container): void
ContainerHelper::configureDoctrineOrmTargetEntities($container, $config['class_mapping']);
}

private function prepareDoctrineBundle(array $config, LoaderInterface $loader, ContainerBuilder $container): void
private function prepareDoctrineOrm(array $config, LoaderInterface $loader, ContainerBuilder $container): void
{
if (!ContainerHelper::isDoctrineOrmEnabled($container)) {
return;
}

$loader->load('doctrine.php');

$classMapping = $config['class_mapping'];
Expand Down
1 change: 1 addition & 0 deletions src/EavBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"require-dev": {
"doctrine/doctrine-bundle": "^1.8",
"doctrine/orm": "^2.6",
"symfony/phpunit-bridge": "^3.4|^4.0",
"symfony/var-dumper": "^3.4|^4.0"
},
Expand Down
4 changes: 4 additions & 0 deletions src/User/Infra/Doctrine/Event/UsernameListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function update($entity, PreUpdateEventArgs $event): void
}

foreach ($this->mapping[$class] as $mapping) {
if (!$event->hasChangedField($mapping['field'])) {
continue;
}

$this->updateUsernames[$event->getOldValue($mapping['field'])] = $event->getNewValue($mapping['field']);
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/UserBundle/DependencyInjection/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MsgPhp\UserBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\Version as DoctrineOrmVersion;
use MsgPhp\Domain\Factory\EntityFactoryInterface;
use MsgPhp\Domain\Infra\DependencyInjection\Bundle\{ConfigHelper, ContainerHelper};
use MsgPhp\EavBundle\MsgPhpEavBundle;
Expand Down Expand Up @@ -57,13 +57,19 @@ public function load(array $configs, ContainerBuilder $container): void
$bundles = ContainerHelper::getBundles($container);

// persistence infra
if (isset($bundles[DoctrineBundle::class])) {
$this->prepareDoctrineBundle($config, $loader, $container);
if (class_exists(DoctrineOrmVersion::class)) {
$this->prepareDoctrineOrm($config, $loader, $container);
}

// framework infra
if (class_exists(Security::class)) {
$loader->load('security.php');

if (!$container->has(Repository\UserRepositoryInterface::class)) {
$container->removeDefinition(SecurityInfra\SecurityUserProvider::class);
$container->removeDefinition(SecurityInfra\UserParamConverter::class);
$container->removeDefinition(SecurityInfra\UserValueResolver::class);
}
}

if (class_exists(Validation::class)) {
Expand Down Expand Up @@ -107,12 +113,8 @@ public function process(ContainerBuilder $container): void
}
}

private function prepareDoctrineBundle(array $config, LoaderInterface $loader, ContainerBuilder $container): void
private function prepareDoctrineOrm(array $config, LoaderInterface $loader, ContainerBuilder $container): void
{
if (!ContainerHelper::isDoctrineOrmEnabled($container)) {
return;
}

$loader->load('doctrine.php');

$classMapping = $config['class_mapping'];
Expand Down
5 changes: 4 additions & 1 deletion src/UserBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
},
"require-dev": {
"doctrine/doctrine-bundle": "^1.8",
"symfony/security-bundle": "^3.4|^4.0",
"doctrine/orm": "^2.6",
"symfony/phpunit-bridge": "^3.4|^4.0",
"symfony/console": "^3.4|^4.0",
"symfony/security-bundle": "^3.4|^4.0",
"symfony/validator": "^3.4|^4.0",
"symfony/var-dumper": "^3.4|^4.0"
},
"suggest": {
Expand Down