Skip to content

feat(service-proxy-pass): Lazy load proxy method instances #37

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

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion src/FrameworkBridge/Symfony/Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
$services->set(ServiceProxySubscriber::class)
->public()
->args([
tagged_iterator('openclassrooms.service_proxy'),
tagged_iterator('openclassrooms.service_proxy.proxy_method_instance'),
tagged_iterator('openclassrooms.service_proxy.start_up_interceptor'),
])
->tag('kernel.event_subscriber');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

use OC\Common\Util\Chrono;
use OpenClassrooms\ServiceProxy\Model\Request\Instance;
use OpenClassrooms\ServiceProxy\Model\Request\Method;
use OpenClassrooms\ServiceProxy\ProxyFactory;
use ProxyManager\Proxy\ValueHolderInterface;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -40,6 +44,7 @@ private function buildServiceProxies(): void

private function buildServiceProxyFactoryDefinition(string $taggedServiceName): void
{
Chrono::start();
$definition = $this->container->findDefinition($taggedServiceName);
$factoryDefinition = new Definition($definition->getClass());
$factoryDefinition->setFactory([new Reference(ProxyFactory::class), 'createProxy']);
Expand All @@ -48,5 +53,20 @@ private function buildServiceProxyFactoryDefinition(string $taggedServiceName):
$factoryDefinition->setPublic($definition->isPublic());
$factoryDefinition->setLazy($definition->isLazy());
$factoryDefinition->setTags($definition->getTags());

$proxyRef = new \ReflectionClass($definition->getClass());
$proxyMethodsRef = $proxyRef->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($proxyMethodsRef as $proxyMethodRef) {
$instance = new Definition(Instance::class);
$instance->setFactory([new Reference(Instance::class), 'createFromProxy']);
$instance->setArguments([
new Reference($taggedServiceName),
$proxyMethodRef->getName(),
]);
$instance->setTags(['openclassrooms.service_proxy.proxy_method_instance']);
dump($taggedServiceName.'_'.$proxyMethodRef->getName().'_instance');
$this->container->setDefinition($taggedServiceName.'_'.$proxyMethodRef->getName().'_instance', $instance);
}
Chrono::end();
}
}
41 changes: 4 additions & 37 deletions src/FrameworkBridge/Symfony/Subscriber/ServiceProxySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Doctrine\Common\Annotations\AnnotationReader;
use OpenClassrooms\ServiceProxy\Interceptor\Contract\StartUpInterceptor;
use OpenClassrooms\ServiceProxy\Model\Request\Instance;
use OpenClassrooms\ServiceProxy\Model\Request\Method;
use ProxyManager\Proxy\ValueHolderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;

Expand All @@ -28,7 +25,7 @@ final class ServiceProxySubscriber implements EventSubscriberInterface
* @throws \Doctrine\Common\Annotations\AnnotationException
*/
public function __construct(
private readonly iterable $proxies,
private readonly iterable $proxyMethodInstances,
iterable $startUpInterceptors,
) {
if (!\is_array($startUpInterceptors)) {
Expand Down Expand Up @@ -56,42 +53,12 @@ public function startUp(RequestEvent $event): void
StartUpInterceptor $b
) => $a->getStartUpPriority() <=> $b->getStartUpPriority()
);
foreach ($this->getInstances() as $instance) {
foreach ($this->proxyMethodInstances as $proxyMethodInstance) {
foreach ($this->startUpInterceptors as $interceptor) {
if ($interceptor->supportsStartUp($instance)) {
$interceptor->startUp($instance);
if ($interceptor->supportsStartUp($proxyMethodInstance)) {
$interceptor->startUp($proxyMethodInstance);
}
}
}
}

/**
* @return iterable<Instance>
*/
public function getInstances(): iterable
{
foreach ($this->proxies as $proxy) {
$object = $proxy;
if ($proxy instanceof ValueHolderInterface) {
$object = $proxy->getWrappedValueHolderValue();
if ($object === null) {
continue;
}
}
$instanceRef = new \ReflectionObject($object);
$methods = $instanceRef->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $methodRef) {
$methodAnnotations = $this->annotationReader->getMethodAnnotations($methodRef);
$instance = Instance::create(
$proxy,
$instanceRef,
Method::create(
$methodRef,
$methodAnnotations,
)
);
yield $instance;
}
}
}
}
19 changes: 19 additions & 0 deletions src/Model/Request/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use ProxyManager\Proxy\ValueHolderInterface;

final class Instance
{
Expand Down Expand Up @@ -64,6 +65,24 @@ public static function create(
return $self;
}

public static function createFromProxy(
object $proxy,
string $methodName
): self {
$object = $proxy;
if ($proxy instanceof ValueHolderInterface) {
$object = $proxy->getWrappedValueHolderValue();
if ($object === null) {
throw new \Exception('The proxy is not initialized.');
}
}
return Instance::create(
$proxy,
new \ReflectionObject($object),
Method::create(new \ReflectionMethod($proxy, $methodName))
);
}

/**
* @param array<string, mixed> $parameters
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Request/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private function __construct()
/**
* @param array<int, object> $annotations
*/
public static function create(\ReflectionMethod $reflection, array $annotations): self
public static function create(\ReflectionMethod $reflection, array $annotations = []): self
{
$self = new self();
$self->reflection = $reflection;
Expand Down