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 cross OM references support #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private function getVendorNode($name)
->prototype('array')
->performNoDeepMerging()
->children()

->scalarNode('translatable')->defaultFalse()->end()
->scalarNode('timestampable')->defaultFalse()->end()
->scalarNode('blameable')->defaultFalse()->end()
Expand All @@ -59,6 +60,7 @@ private function getVendorNode($name)
->scalarNode('softdeleteable')->defaultFalse()->end()
->scalarNode('uploadable')->defaultFalse()->end()
->scalarNode('reference_integrity')->defaultFalse()->end()
->scalarNode('references')->cannotBeEmpty()->end()
->end()
->end()
;
Expand Down Expand Up @@ -114,6 +116,10 @@ private function getClassNode()
->cannotBeEmpty()
->defaultValue('Gedmo\\ReferenceIntegrity\\ReferenceIntegrityListener')
->end()
->scalarNode('references')
->cannotBeEmpty()
->defaultValue('Stof\\DoctrineExtensionsBundle\\Listener\\ReferencesListener')
->end()
->end()
;

Expand Down
14 changes: 14 additions & 0 deletions DependencyInjection/StofDoctrineExtensionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function load(array $configs, ContainerBuilder $container)
$useLoggable = false;
$useBlameable = false;

$managers = array();

foreach ($config['orm'] as $name => $listeners) {
foreach ($listeners as $ext => $enabled) {
$listener = sprintf('stof_doctrine_extensions.listener.%s', $ext);
Expand All @@ -53,6 +55,10 @@ public function load(array $configs, ContainerBuilder $container)
}
$definition = $container->getDefinition($listener);
$definition->addTag('doctrine.event_subscriber', $attributes);

if ('references' === $ext) {
$managers[$enabled] = sprintf('doctrine.orm.%s_entity_manager', $name);
}
}
}

Expand All @@ -75,6 +81,10 @@ public function load(array $configs, ContainerBuilder $container)
}
$definition = $container->getDefinition($listener);
$definition->addTag('doctrine_mongodb.odm.event_subscriber', $attributes);

if ('references' === $ext) {
$managers[$enabled] = sprintf('doctrine_mongodb.odm.%s_document_manager', $name);
}
}
}
$this->documentManagers[$name] = $listeners;
Expand Down Expand Up @@ -117,6 +127,10 @@ public function load(array $configs, ContainerBuilder $container)
);
}

$container
->getDefinition('stof_doctrine_extensions.listener.references')
->setArguments(array(new Reference('service_container'), $managers));

foreach ($config['class'] as $listener => $class) {
$container->setParameter(sprintf('stof_doctrine_extensions.listener.%s.class', $listener), $class);
}
Expand Down
28 changes: 28 additions & 0 deletions Listener/ReferencesListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Stof\DoctrineExtensionsBundle\Listener;

use Gedmo\References\ReferencesListener as BaseReferencesListener;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* ReferencesListener
*
* @author Bulat Shakirzyanov <[email protected]>
*/
class ReferencesListener extends BaseReferencesListener
{
private $container;

public function __construct(ContainerInterface $container, array $managers = array())
{
$this->container = $container;
parent::__construct($managers);
}

public function getManager($type)
{
return $this->container->get(parent::getManager($type));
}
}
5 changes: 5 additions & 0 deletions Resources/config/listeners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</parameters>
<services>
<!-- Doctrine listeners -->
<service id="stof_doctrine_extensions.listener.references" class="%stof_doctrine_extensions.listener.references.class%" public="false">
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" />
</call>
</service>
<service id="stof_doctrine_extensions.listener.timestampable" class="%stof_doctrine_extensions.listener.timestampable.class%" public="false">
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" />
Expand Down