Skip to content
Draft
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
10 changes: 0 additions & 10 deletions src/FrameworkBridge/Symfony/Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use OpenClassrooms\ServiceProxy\FrameworkBridge\Symfony\Messenger\Transport\Serialization\MessageSerializer;
use OpenClassrooms\ServiceProxy\FrameworkBridge\Symfony\Subscriber\ServiceProxySubscriber;
use OpenClassrooms\ServiceProxy\Handler\Impl\Cache\SymfonyCacheHandler;
use OpenClassrooms\ServiceProxy\Invoker\Impl\AggregateMethodInvoker;
use OpenClassrooms\ServiceProxy\ProxyFactory;
Expand Down Expand Up @@ -65,13 +64,4 @@
->autowire()
->autoconfigure()
;

$services->set(ServiceProxySubscriber::class)
->public()
->args([
tagged_iterator('openclassrooms.service_proxy'),
tagged_iterator('openclassrooms.service_proxy.start_up_interceptor'),
service('annotation_reader')->nullOnInvalid(),
])
->tag('kernel.event_subscriber');
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace OpenClassrooms\ServiceProxy\FrameworkBridge\Symfony\DependencyInjection\Compiler;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use OpenClassrooms\ServiceProxy\Attribute\Event\Listen;
use OpenClassrooms\ServiceProxy\Model\Request\Method;
use OpenClassrooms\ServiceProxy\ProxyFactory;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -23,6 +27,7 @@ public function process(ContainerBuilder $container): void
$this->compiler = $container->getCompiler();

$this->buildServiceProxies();
$this->addStartupListeners();
}

private function buildServiceProxies(): void
Expand All @@ -49,4 +54,57 @@ private function buildServiceProxyFactoryDefinition(string $taggedServiceName):
$factoryDefinition->setLazy($definition->isLazy());
$factoryDefinition->setTags($definition->getTags());
}

private function addStartupListeners(): void
{
if (!$this->container->findDefinition('event_dispatcher')) {
return;
}

$eventDispatcherDefinition = $this->container->findDefinition('event_dispatcher');
$proxies = $this->container->findTaggedServiceIds('openclassrooms.service_proxy');

foreach (\array_keys($proxies) as $proxy) {
$definition = $this->container->findDefinition($proxy);
$class = $definition->getClass();
$instanceRef = new \ReflectionClass($class);
$methods = $instanceRef->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $methodRef) {

try {
$methodAnnotations = (new AnnotationReader())->getMethodAnnotations($methodRef);
} catch (AnnotationException) {
continue;
}

$method = Method::create(
$methodRef,
$methodAnnotations,
);

if ($method->hasAttribute(Listen::class)) {
$attributes = $method->getAttributesInstances(Listen::class);

foreach ($attributes as $attribute) {
/**
* @var Listen $attribute
*/
$name = $attribute->name;
$transport = $attribute->transport;

if ($transport !== null) {
$name .= '.' . $transport->value;
}

$eventDispatcherDefinition->addMethodCall('addListener', [
$name,
[new Reference($proxy), $method->getName()],
$attribute->priority
]);
}

}
}
}
}
}
104 changes: 0 additions & 104 deletions src/FrameworkBridge/Symfony/Subscriber/ServiceProxySubscriber.php

This file was deleted.