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

Don't use ControllerResolver #60

Closed
wants to merge 1 commit into from
Closed
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 config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
->args([
service('sofascore.purgatory.mapping.configuration'),
service('router'),
service('controller_resolver'),
service('sofascore.purgatory.annotation_reader'),
service('doctrine.orm.entity_manager'),
abstract_arg('ControllerClassMapPass'),
])

->set('sofascore.purgatory.mapping.annotation_loader.warmer', AnnotationsLoaderWarmer::class)
Expand Down
22 changes: 22 additions & 0 deletions src/DependencyInjection/CompilerPass/ControllerClassMapPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Sofascore\PurgatoryBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class ControllerClassMapPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$classMap = [];

foreach ($container->findTaggedServiceIds('controller.service_arguments', true) as $id => $tags) {
$classMap[$id] = $container->getDefinition($id)->getClass();
}

$container->getDefinition('sofascore.purgatory.mapping.annotation_loader')->replaceArgument(4, $classMap);
}
}
73 changes: 45 additions & 28 deletions src/Mapping/Loader/AnnotationsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,30 @@
use Sofascore\PurgatoryBundle\Mapping\MappingValue;
use Sofascore\PurgatoryBundle\Mapping\PropertySubscription;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;

class AnnotationsLoader implements LoaderInterface, WarmableInterface
{
protected RouterInterface $router;
protected Configuration $config;
protected ControllerResolverInterface $controllerResolver;
protected Reader $annotationReader;
protected ObjectManager $manager;
protected array $serviceClassMap;

public function __construct(
Configuration $config,
RouterInterface $router,
ControllerResolverInterface $controllerResolver,
Reader $annotationReader,
ObjectManager $manager,
array $serviceClassMap,
) {
$this->config = $config;
$this->router = $router;
$this->controllerResolver = $controllerResolver;
$this->annotationReader = $annotationReader;
$this->manager = $manager;
$this->serviceClassMap = $serviceClassMap;
}

/**
Expand Down Expand Up @@ -118,15 +116,17 @@ private function loadMappings(): MappingCollection
}
}

$controllerCallable = $this->resolveController($route->getDefault('_controller'));
if (null === $routeController = $route->getDefault('_controller')) {
continue;
}

// if controller cannot be resolved, skip route
if (false === $controllerCallable) {
if (null === $controllerCallableReflection = $this->resolveControllerCallable($routeController)) {
continue;
}

// get class/property subscriptions
$this->parseControllerMappings($controllerCallable, $routeName, $route, $subscriptions);
$this->parseControllerMappings($controllerCallableReflection, $routeName, $route, $subscriptions);
}

// resolve subscription classes and properties
Expand Down Expand Up @@ -154,22 +154,46 @@ private function loadMappings(): MappingCollection
return $mappingCollection;
}

protected function resolveController(?string $controllerPath): callable|false
private function resolveControllerCallable(array|object|string $controller): ?\ReflectionMethod
{
if (null === $controllerPath) {
return false;
if (\is_array($controller) && isset($controller[0]) && \is_string($controller[0]) && isset($controller[1])) {
$resolvedClass = $this->resolveClass($controller[0]);

return $resolvedClass ? new \ReflectionMethod($resolvedClass, $controller[1]) : null;
}

// set controller path
$request = new Request([], [], ['_controller' => $controllerPath]);
if (\is_object($controller) && \is_callable($controller)) {
return new \ReflectionMethod($controller, '__invoke');
}

try {
// resolve controller path
return $this->controllerResolver->getController($request);
} catch (\Exception $e) {
// if error happens skip route
return false;
if (\function_exists($controller)) {
return null;
}

if (!str_contains($controller, '::')) {
$resolvedClass = $this->resolveClass($controller);

return $resolvedClass ? new \ReflectionMethod($resolvedClass, '__invoke') : null;
}

[$class, $method] = explode('::', $controller, 2);

$resolvedClass = $this->resolveClass($class);

return $resolvedClass ? new \ReflectionMethod($resolvedClass, $method) : null;
}

private function resolveClass(string $serviceIdOrClass): ?string
{
if (isset($this->serviceClassMap[$serviceIdOrClass])) {
return $this->serviceClassMap[$serviceIdOrClass];
}

if (class_exists($serviceIdOrClass)) {
return $serviceIdOrClass;
}

return null;
}

/**
Expand All @@ -178,19 +202,12 @@ protected function resolveController(?string $controllerPath): callable|false
* @throws \ReflectionException|ReaderException
*/
private function parseControllerMappings(
callable $controllerCallable,
\ReflectionMethod $controllerCallableReflection,
string $routeName,
Route $route,
array &$subscriptions,
): array {
if (!\is_array($controllerCallable)) {
return [];
}

[$controller, $method] = $controllerCallable;

$reflectionMethod = new \ReflectionMethod($controller, $method);
$methodAnnotations = $this->annotationReader->getAnnotations($reflectionMethod);
$methodAnnotations = $this->annotationReader->getAnnotations($controllerCallableReflection);

foreach ($methodAnnotations as $class => $annotations) {
if (PurgeOn::class === $class) {
Expand Down
2 changes: 2 additions & 0 deletions src/PurgatoryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sofascore\PurgatoryBundle;

use Sofascore\PurgatoryBundle\DependencyInjection\CompilerPass\ControllerClassMapPass;
use Sofascore\PurgatoryBundle\DependencyInjection\CompilerPass\RegisterPurgerImplementationCompilerPass;
use Sofascore\PurgatoryBundle\DependencyInjection\CompilerPass\SymfonyPurgerRemovalCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -20,5 +21,6 @@ public function build(ContainerBuilder $container): void

$container->addCompilerPass(new RegisterPurgerImplementationCompilerPass());
$container->addCompilerPass(new SymfonyPurgerRemovalCompilerPass());
$container->addCompilerPass(new ControllerClassMapPass());
}
}