Skip to content

Commit

Permalink
Merge pull request #1580 from dpfaffenbauer/issues/doctrine
Browse files Browse the repository at this point in the history
[ResourceBundle] fix compatibility with Doctrine EventSubscriber
  • Loading branch information
dpfaffenbauer authored Jan 29, 2021
2 parents 32b736a + 18289fd commit 56051aa
Showing 1 changed file with 35 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler;

use CoreShop\Component\Resource\Model\ResourceInterface;
use Doctrine\Common\EventSubscriber;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Expand All @@ -34,86 +36,63 @@ public function process(ContainerBuilder $container)
return;
}

$interfaces = $this->getInterfacesMapping($resources);
$interfaces = $this->resolve($resources);
foreach ($interfaces as $interface => $model) {
$resolveTargetEntityListener->addMethodCall('addResolveTargetEntity', [
$this->getInterface($container, $interface),
$this->getClass($container, $model),
[],
]);
$resolveTargetEntityListener->addMethodCall('addResolveTargetEntity', [$interface, $model, []]);
}

if (!$resolveTargetEntityListener->hasTag('doctrine.event_listener')) {
$resolveTargetEntityListenerClass = $container->getParameterBag()->resolveValue($resolveTargetEntityListener->getClass());
if (is_a($resolveTargetEntityListenerClass, EventSubscriber::class, true)) {
if (!$resolveTargetEntityListener->hasTag('doctrine.event_subscriber')) {
$resolveTargetEntityListener->addTag('doctrine.event_subscriber');
}
} elseif (!$resolveTargetEntityListener->hasTag('doctrine.event_listener')) {
$resolveTargetEntityListener->addTag('doctrine.event_listener', ['event' => 'loadClassMetadata']);
}
}

/**
* @param array $resources
*
* @return array
*/
private function getInterfacesMapping($resources)
public function resolve(array $resources): array
{
$interfaces = [];

foreach ($resources as $alias => $configuration) {
if (isset($configuration['classes']['interface'])) {
$alias = explode('.', $alias);
$model = $this->getModel($alias, $configuration);

if (!isset($alias[0], $alias[1])) {
throw new \RuntimeException(sprintf('Error configuring "%s" resource. The resource alias should follow the "prefix.name" format.', $alias[0]));
foreach (class_implements($model) as $interface) {
if ($interface === ResourceInterface::class) {
continue;
}

$interfaces[$configuration['classes']['interface']] = sprintf('%s.model.%s.class', $alias[0], $alias[1]);
$interfaces[$interface][] = $model;
}
}

return $interfaces;
}
$interfaces = array_filter($interfaces, static function (array $classes): bool {
return count($classes) === 1;
});

/**
* @param ContainerBuilder $container
* @param string $key
*
* @return string
*
* @throws \InvalidArgumentException
*/
private function getInterface(ContainerBuilder $container, $key)
{
if ($container->hasParameter($key)) {
return $container->getParameter($key);
}
$interfaces = array_map(static function (array $classes): string {
return (string) current($classes);
}, $interfaces);

if (interface_exists($key)) {
return $key;
foreach ($resources as $alias => $configuration) {
if (isset($configuration['classes']['interface'])) {
$model = $this->getModel($alias, $configuration);
$interface = $configuration['classes']['interface'];

$interfaces[$interface] = $model;
}
}

throw new \InvalidArgumentException(
sprintf('The interface %s does not exist.', $key)
);
return $interfaces;
}

/**
* @param ContainerBuilder $container
* @param string $key
*
* @return string
*
* @throws \InvalidArgumentException
*/
private function getClass(ContainerBuilder $container, $key)
private function getModel(string $alias, array $configuration): string
{
if ($container->hasParameter($key)) {
return $container->getParameter($key);
}

if (class_exists($key)) {
return $key;
if (!isset($configuration['classes']['model'])) {
throw new \InvalidArgumentException(sprintf('Could not get model class from resource "%s".', $alias));
}

throw new \InvalidArgumentException(
sprintf('The class %s does not exist.', $key)
);
return $configuration['classes']['model'];
}
}

0 comments on commit 56051aa

Please sign in to comment.