-
Notifications
You must be signed in to change notification settings - Fork 2
Generate Access Interceptor Proxy #43
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
kletord
wants to merge
4
commits into
master
Choose a base branch
from
warmp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/FrameworkBridge/Symfony/CacheWarmer/ProxyCacheWarmer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenClassrooms\ServiceProxy\FrameworkBridge\Symfony\CacheWarmer; | ||
|
||
use ProxyManager\Proxy\LazyLoadingInterface; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\VarExporter\LazyObjectInterface; | ||
|
||
final class ProxyCacheWarmer implements CacheWarmerInterface | ||
{ | ||
/** | ||
* @var iterable<object> | ||
*/ | ||
private iterable $proxies; | ||
|
||
/** | ||
* @param iterable<object> $proxies | ||
*/ | ||
public function __construct(iterable $proxies) | ||
{ | ||
$this->proxies = $proxies; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function warmUp(string $cacheDir): array | ||
{ | ||
foreach ($this->proxies as $proxy) { | ||
if ($proxy instanceof LazyLoadingInterface && !$proxy->isProxyInitialized()) { | ||
$proxy->initializeProxy(); | ||
} | ||
|
||
if (class_exists(LazyObjectInterface::class) | ||
&& $proxy instanceof LazyObjectInterface | ||
&& !$proxy->isLazyObjectInitialized()) { | ||
$proxy->initializeLazyObject(); | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isOptional() | ||
{ | ||
return true; | ||
kletord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
use Symfony\Component\DependencyInjection\Compiler\Compiler; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class ServiceProxyPass implements CompilerPassInterface | ||
|
@@ -30,23 +29,28 @@ private function buildServiceProxies(): void | |
$serviceProxyIds = []; | ||
$taggedServices = $this->container->findTaggedServiceIds('openclassrooms.service_proxy'); | ||
foreach ($taggedServices as $taggedServiceId => $tagParameters) { | ||
$this->buildServiceProxyFactoryDefinition($taggedServiceId); | ||
$this->overrideServiceDefinition($taggedServiceId); | ||
$serviceProxyIds[] = $taggedServiceId; | ||
$this->compiler->log($this, "Add proxy for {$taggedServiceId} service."); | ||
$this->compiler->log($this, "Override service definition for {$taggedServiceId} service."); | ||
} | ||
|
||
$this->container->setParameter('openclassrooms.service_proxy.service_proxy_ids', $serviceProxyIds); | ||
} | ||
|
||
private function buildServiceProxyFactoryDefinition(string $taggedServiceName): void | ||
private function overrideServiceDefinition(string $taggedServiceName): void | ||
{ | ||
$definition = $this->container->findDefinition($taggedServiceName); | ||
$factoryDefinition = new Definition($definition->getClass()); | ||
$factoryDefinition->setFactory([new Reference(ProxyFactory::class), 'createProxy']); | ||
$factoryDefinition->setArguments([$definition]); | ||
$this->container->setDefinition($taggedServiceName, $factoryDefinition); | ||
$factoryDefinition->setPublic($definition->isPublic()); | ||
$factoryDefinition->setLazy($definition->isLazy()); | ||
$factoryDefinition->setTags($definition->getTags()); | ||
|
||
if ($definition->getFactory() !== null) { | ||
$this->compiler->log($this, "Service {$taggedServiceName} is not compatible with service proxy"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make an error |
||
|
||
throw new \RuntimeException( | ||
"Unable to override {$taggedServiceName}, remove the factory definition or the Interceptable interface." | ||
); | ||
} | ||
|
||
$definition->setFactory([new Reference(ProxyFactory::class), 'createInstance']); | ||
|
||
$definition->setArguments([$definition->getClass(), ...$definition->getArguments()]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Generator/AccessInterceptorGenerator/AccessInterceptorGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenClassrooms\ServiceProxy\Generator\AccessInterceptorGenerator; | ||
|
||
use Laminas\Code\Generator\ClassGenerator; | ||
use Laminas\Code\Generator\MethodGenerator; | ||
use Laminas\Code\Reflection\MethodReflection; | ||
use OpenClassrooms\ServiceProxy\Generator\AccessInterceptorGenerator\Method\InterceptedMethod; | ||
use OpenClassrooms\ServiceProxy\Generator\AccessInterceptorGenerator\Method\SetMethodPrefixInterceptors; | ||
use OpenClassrooms\ServiceProxy\Generator\AccessInterceptorGenerator\Method\SetMethodSuffixInterceptors; | ||
use OpenClassrooms\ServiceProxy\Proxy\AccessInterceptorsInterface; | ||
use ProxyManager\Exception\InvalidProxiedClassException; | ||
use ProxyManager\Generator\Util\ClassGeneratorUtils; | ||
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors; | ||
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors; | ||
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion; | ||
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface; | ||
|
||
class AccessInterceptorGenerator implements ProxyGeneratorInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param array{'methods'?: array<string>} $proxyOptions | ||
* | ||
* @throws \InvalidArgumentException | ||
* @throws InvalidProxiedClassException | ||
*/ | ||
public function generate(\ReflectionClass $originalClass, ClassGenerator $classGenerator, array $proxyOptions = []) | ||
{ | ||
if (!\array_key_exists('methods', $proxyOptions)) { | ||
throw new \InvalidArgumentException(sprintf('Missing methods options for %s.', __CLASS__)); | ||
} | ||
|
||
CanProxyAssertion::assertClassCanBeProxied($originalClass, false); | ||
|
||
$classGenerator->setExtendedClass($originalClass->getName()); | ||
$classGenerator->setImplementedInterfaces([AccessInterceptorsInterface::class]); | ||
$classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); | ||
$classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); | ||
|
||
array_map( | ||
static function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator): void { | ||
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); | ||
}, | ||
array_merge( | ||
array_map( | ||
$this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors), | ||
array_map( | ||
static fn (string $method) => $originalClass->getMethod($method), | ||
$proxyOptions['methods'] | ||
) | ||
), | ||
[ | ||
new SetMethodPrefixInterceptors($prefixInterceptors), | ||
new SetMethodSuffixInterceptors($suffixInterceptors), | ||
] | ||
) | ||
); | ||
} | ||
|
||
private function buildMethodInterceptor( | ||
MethodPrefixInterceptors $prefixInterceptors, | ||
MethodSuffixInterceptors $suffixInterceptors | ||
): callable { | ||
return static function (\ReflectionMethod $method) use ( | ||
$prefixInterceptors, | ||
$suffixInterceptors | ||
): InterceptedMethod { | ||
return InterceptedMethod::generateMethod( | ||
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), | ||
$prefixInterceptors, | ||
$suffixInterceptors | ||
); | ||
}; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Generator/AccessInterceptorGenerator/Factory/AccessInterceptorFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenClassrooms\ServiceProxy\Generator\AccessInterceptorGenerator\Factory; | ||
|
||
use OpenClassrooms\ServiceProxy\Generator\AccessInterceptorGenerator\AccessInterceptorGenerator; | ||
use ProxyManager\Configuration; | ||
use ProxyManager\Factory\AbstractBaseFactory; | ||
|
||
class AccessInterceptorFactory extends AbstractBaseFactory | ||
{ | ||
private AccessInterceptorGenerator $generator; | ||
|
||
public function __construct(?Configuration $configuration = null) | ||
{ | ||
parent::__construct($configuration); | ||
|
||
$this->generator = new AccessInterceptorGenerator(); | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @param class-string<T> $class | ||
* @param array<mixed> $args | ||
* @param array<string, \Closure> $prefixInterceptors an array (indexed by method name) of interceptor closures to be called | ||
* before method logic is executed | ||
* @param array<string, \Closure> $suffixInterceptors an array (indexed by method name) of interceptor closures to be called | ||
* after method logic is executed | ||
* | ||
* @return T | ||
*/ | ||
public function createInstance( | ||
string $class, | ||
array $args, | ||
array $prefixInterceptors = [], | ||
array $suffixInterceptors = [] | ||
): object { | ||
$methods = array_merge( | ||
array_keys($prefixInterceptors), | ||
array_keys($suffixInterceptors) | ||
); | ||
$methods = array_unique($methods); | ||
|
||
$proxyClassName = $this->generateProxy($class, [ | ||
'methods' => $methods, | ||
]); | ||
|
||
$instance = new $proxyClassName(...$args); | ||
|
||
$instance->setPrefixInterceptors($prefixInterceptors); | ||
$instance->setSuffixInterceptors($suffixInterceptors); | ||
|
||
return $instance; | ||
} | ||
|
||
public function getGenerator(): AccessInterceptorGenerator | ||
{ | ||
return $this->generator; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add continue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is LazyLoadingInterface doesn't exist