Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #39 from Kdyby/nette3
Browse files Browse the repository at this point in the history
Nette 3 support
  • Loading branch information
dakorpar authored May 4, 2019
2 parents 114d410 + f30aa46 commit 18175ab
Show file tree
Hide file tree
Showing 24 changed files with 148 additions and 200 deletions.
54 changes: 27 additions & 27 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@
"email": "[email protected]",
"issues": "https://github.com/kdyby/aop/issues"
},
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"nette/di": "~2.4",
"nette/php-generator": "~2.6",

"kdyby/annotations": "^2.1",
"symfony/property-access": "~3.2"
"php": ">=7.1",
"nette/di": "~3.0",
"nette/php-generator": "^3.0",
"nette/tokenizer": "~3.0",
"kdyby/annotations": "dev-master",
"symfony/property-access": "~4.2"
},
"require-dev": {
"php": ">=7.1",
"nette/application": "~2.4",
"nette/bootstrap": "~2.4",
"nette/caching": "~2.4",
"nette/component-model": "~2.3",
"nette/database": "~2.4",
"nette/finder": "~2.4",
"nette/forms": "~2.4",
"nette/http": "~2.4",
"nette/mail": "~2.4",
"nette/neon": "~2.4",
"nette/reflection": "~2.4",
"nette/robot-loader": "~2.4",
"nette/safe-stream": "~2.3",
"nette/security": "~2.4",
"nette/tokenizer": "~2.2",
"nette/utils": "^2.5",
"latte/latte": "~2.4",
"tracy/tracy": "~2.4",
"kdyby/doctrine-cache": "^2.5.2",
"nette/tester": "~1.7",
"doctrine/collections": "~1.2"
"nette/application": "~3.0",
"nette/bootstrap": "~3.0",
"nette/caching": "~3.0",
"nette/component-model": "~3.0",
"nette/database": "~3.0",
"nette/finder": "~2.5",
"nette/forms": "~3.0",
"nette/http": "~3.0",
"nette/mail": "~3.0",
"nette/neon": "~3.0",
"nette/robot-loader": "^3.0",
"nette/safe-stream": "~2.4",
"nette/security": "~3.0",
"nette/utils": "^3.0",
"latte/latte": "~2.5",
"tracy/tracy": "~2.6",
"kdyby/doctrine-cache": "^2.6",
"nette/tester": "~2.2",
"doctrine/collections": "~1.6"
},
"autoload": {
"psr-0": {
Expand Down
33 changes: 21 additions & 12 deletions src/Kdyby/Aop/DI/AopExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@



/**
/**W
* @author Filip Procházka <[email protected]>
*/
class AopExtension extends Nette\DI\CompilerExtension
{
const CG_INJECT_METHOD = '__injectAopContainer';

/**
* @var array
Expand Down Expand Up @@ -78,14 +77,15 @@ public function beforeCompile()
$targetMethod = reset($methodAdvices)->getTargetMethod();

$newMethod = $targetMethod->getPointcutCode();
AdvisedClassType::setMethodInstance($advisedClass, $newMethod);
AdvisedClassType::generatePublicProxyMethod($advisedClass, $targetMethod->getCode());
AdvisedClassType::setMethodInstance($advisedClass, $newMethod->getMethod());
AdvisedClassType::generatePublicProxyMethod($advisedClass, $targetMethod->getCode()->getMethod());
$constructorInject = $constructorInject || strtolower($newMethod->getName()) === '__construct';

/** @var AdviceDefinition[] $methodAdvices */
foreach ($methodAdvices as $adviceDef) {
$newMethod->addAdvice($adviceDef);
}
$newMethod->beforePrint();
}

$this->patchService($serviceId, $advisedClass, $cg, $constructorInject);
Expand Down Expand Up @@ -115,13 +115,15 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
private function patchService($serviceId, Code\ClassType $advisedClass, Code\PhpNamespace $cg, $constructorInject = FALSE)
{
static $publicSetup;

if ($publicSetup === NULL) {
$refl = new Nette\Reflection\Property('Nette\DI\ServiceDefinition', 'setup');
$refl = new \ReflectionProperty('Nette\DI\ServiceDefinition', 'setup');
$publicSetup = $refl->isPublic();
}

$def = $this->getContainerBuilder()->getDefinition($serviceId);
if($def instanceof Nette\DI\Definitions\FactoryDefinition) {
$def = $def->getResultDefinition();
}
$factory = $def->getFactory();
if ($factory) {
$def->setFactory(new Nette\DI\Statement($cg->getName() . '\\' . $advisedClass->getName(), $factory->arguments));
Expand Down Expand Up @@ -150,7 +152,7 @@ private function writeGeneratedCode(Code\PhpFile $file, Code\PhpNamespace $names
{
$builder = $this->getContainerBuilder();

if (!is_dir($tempDir = $builder->expand('%tempDir%/cache/_Kdyby.Aop'))) {
if (!is_dir($tempDir = Nette\DI\Helpers::expand('%tempDir%/cache/_Kdyby.Aop', $builder->parameters))) {
mkdir($tempDir, 0777, TRUE);
}

Expand All @@ -168,7 +170,7 @@ private function writeGeneratedCode(Code\PhpFile $file, Code\PhpNamespace $names
private function findAdvisedMethods()
{
$builder = $this->getContainerBuilder();
$builder->prepareClassList();
$builder->resolve();

$annotationReader = new AnnotationReader();
$matcherFactory = new Pointcut\MatcherFactory($builder, $annotationReader);
Expand Down Expand Up @@ -213,9 +215,12 @@ private function findByTypes($types)
if ($this->classes === NULL) {
$this->classes = [];
foreach ($this->getContainerBuilder()->getDefinitions() as $name => $def) {
$class = $def->class;
if ($class) {
foreach (class_parents($class) + class_implements($class) + [$class] as $parent) {
if ($def->getType() !== NULL) {
$additional = [];
if ($def instanceof Nette\DI\Definitions\FactoryDefinition) {
$this->classes[strtolower($def->getResultDefinition()->getType())][] = (string) $name;
}
foreach (class_parents($def->getType()) + class_implements($def->getType()) + [$def->getType()] + $additional as $parent) {
$this->classes[strtolower($parent)][] = (string) $name;
}
}
Expand All @@ -242,7 +247,11 @@ private function findByTypes($types)
private function getWrappedDefinition($id)
{
if (!isset($this->serviceDefinitions[$id])) {
$this->serviceDefinitions[$id] = new Pointcut\ServiceDefinition($this->getContainerBuilder()->getDefinition($id), $id);
$def =$this->getContainerBuilder()->getDefinition($id);
if ($def instanceof Nette\DI\Definitions\FactoryDefinition) {
$def = $def->getResultDefinition();
}
$this->serviceDefinitions[$id] = new Pointcut\ServiceDefinition($def, $id);
}

return $this->serviceDefinitions[$id];
Expand Down
26 changes: 8 additions & 18 deletions src/Kdyby/Aop/DI/AspectsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,16 @@ class AspectsConfig
*/
private $aspectsList;

/**
* @var \Nette\DI\CompilerExtension
*/
private $extension;

/**
* @var bool
*/
private $prefix = TRUE;



public function __construct(array $aspectsList, Nette\DI\CompilerExtension $extension)
public function __construct(array $aspectsList)
{
$this->aspectsList = $aspectsList;
$this->extension = $extension;
}


Expand All @@ -58,20 +52,16 @@ public function disablePrefixing()

public function load(Nette\DI\Compiler $compiler, Nette\DI\ContainerBuilder $containerBuilder)
{
$aspects = [];
foreach ($this->aspectsList as $def) {
if (!is_array($def)) {
if (!is_string($def) && (!$def instanceof \stdClass || empty($def->value)) && !$def instanceof Nette\DI\Statement) {
$serialised = Nette\Utils\Json::encode($def);
throw new Kdyby\Aop\UnexpectedValueException("The service definition $serialised is expected to be an array or Neon entity.");
}
$def = ['factory' => $def];
if ( (!is_array($def)) && !is_string($def) && (!$def instanceof \stdClass || empty($def->value)) && !$def instanceof Nette\DI\Statement) {
$serialised = Nette\Utils\Json::encode($def);
throw new Kdyby\Aop\UnexpectedValueException("The service definition $serialised is expected to be an array or Neon entity.");
}
$def['tags'][] = AspectsExtension::ASPECT_TAG;
$aspects[] = $def;
$definition = new Nette\DI\Definitions\ServiceDefinition();
$definition->setFactory(is_array($def) ? $def['class'] : $def);
$definition->setTags([AspectsExtension::ASPECT_TAG => true]);
$containerBuilder->addDefinition(null, $definition);
}

$compiler->parseServices($containerBuilder, ['services' => $aspects], $this->prefix ? substr($this->extension->prefix('self'), 0, -5) : NULL);
}

}
2 changes: 1 addition & 1 deletion src/Kdyby/Aop/DI/AspectsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function loadConfiguration()
}

if (!($config = $extension->getAspectsConfiguration()) || !$config instanceof AspectsConfig) {
$refl = new Nette\Reflection\Method($extension, 'getAspectsConfiguration');
$refl = new \ReflectionMethod($extension, 'getAspectsConfiguration');
$given = is_object($config) ? 'instance of ' . get_class($config) : gettype($config);
throw new Kdyby\Aop\UnexpectedValueException("Method $refl is expected to return instance of Kdyby\\Aop\\DI\\AspectsConfig, but $given given.");
}
Expand Down
18 changes: 4 additions & 14 deletions src/Kdyby/Aop/JoinPoint/MethodInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

use Kdyby;
use Nette;
use Nette\Reflection\ClassType;
use Nette\Reflection\Method;



Expand Down Expand Up @@ -70,23 +68,15 @@ public function getArguments()
}



/**
* @return ClassType
*/
public function getTargetObjectReflection()
public function getTargetObjectReflection(): \ReflectionClass
{
return ClassType::from($this->targetObject);
return new \ReflectionClass($this->targetObject);
}



/**
* @return Method
*/
public function getTargetReflection()
public function getTargetReflection(): \ReflectionMethod
{
return new Method($this->targetObject, $this->targetMethod);
return new \ReflectionMethod($this->targetObject, $this->targetMethod);
}

}
10 changes: 2 additions & 8 deletions src/Kdyby/Aop/PhpGenerator/AdvisedClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ class AdvisedClassType
const CG_PUBLIC_PROXY_PREFIX = '__publicAopProxy_';



/**
* @param Code\Method $method
* @return Code\Method
*/
public static function setMethodInstance(Code\ClassType $class, Code\Method $method)
public static function setMethodInstance(Code\ClassType $class, Code\Method $method): Code\Method
{
$methods = [$method->getName() => $method] + $class->getMethods();
$class->setMethods($methods);
Expand Down Expand Up @@ -73,8 +68,7 @@ public static function generatePublicProxyMethod(Code\ClassType $class, Code\Met
public static function fromServiceDefinition(Kdyby\Aop\Pointcut\ServiceDefinition $service, Code\PhpNamespace $namespace)
{
$originalType = $service->getTypeReflection();

$class = $namespace->addClass(str_replace(['\\', '.'], '_', "{$originalType}Class_{$service->serviceId}"));
$class = $namespace->addClass(str_replace(['\\', '.'], '_', "{$originalType->getName()}Class_{$service->serviceId}"));

$class->setExtends('\\' . $originalType->getName());
$class->setFinal(TRUE);
Expand Down
39 changes: 26 additions & 13 deletions src/Kdyby/Aop/PhpGenerator/PointcutMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
/**
* @author Filip Procházka <[email protected]>
*/
class PointcutMethod extends Code\Method
class PointcutMethod
{

/**
* @var array
*/
Expand All @@ -47,10 +46,19 @@ class PointcutMethod extends Code\Method
*/
private $after = [];

/**
* @var Code\Method
*/
private $method;

public function __construct(\ReflectionMethod $from)
{
$this->method = (new Code\Factory())->fromMethodReflection($from);
}

public static function from($from)
public static function from(\ReflectionMethod $from): PointcutMethod
{
$method = new static($from->isClosure() ? NULL : $from->getName());
$method = new self($from);
$params = [];
$factory = new Code\Factory();
foreach ($from->getParameters() as $param) {
Expand All @@ -68,7 +76,7 @@ public static function from($from)
$method->setReturnReference($from->returnsReference());
$method->setVariadic($from->isVariadic());
$method->setComment(Code\Helpers::unformatDocComment($from->getDocComment()));
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
if ($from->hasReturnType()) {
$method->setReturnType((string) $from->getReturnType());
$method->setReturnNullable($from->getReturnType()->allowsNull());
}
Expand Down Expand Up @@ -129,7 +137,13 @@ public function addAdvice(Kdyby\Aop\DI\AdviceDefinition $adviceDef)
}
}


/**
* @return Code\Method
*/
public function getMethod(): Code\Method
{
return $this->method;
}

private function generateRuntimeCondition(Kdyby\Aop\DI\AdviceDefinition $adviceDef, $code)
{
Expand All @@ -153,7 +167,7 @@ private function generateRuntimeCondition(Kdyby\Aop\DI\AdviceDefinition $adviceD
/**
* @return string
*/
public function __toString()
public function beforePrint()
{
$this->setBody('');

Expand Down Expand Up @@ -228,19 +242,14 @@ public function __toString()
if($this->getReturnType() !== 'void') {
$this->addBody('return $__result;');
}

return parent::__toString();
}



/**
* @param \ReflectionMethod $from
* @param Code\Method $method
* @throws \ReflectionException
* @return Code\Method
*/
public static function expandTypeHints(\ReflectionMethod $from, Code\Method $method)
public static function expandTypeHints(\ReflectionMethod $from, PointcutMethod $method): PointcutMethod
{
$parameters = $method->getParameters();
/** @var Code\Parameter[] $parameters */
Expand All @@ -267,4 +276,8 @@ public static function expandTypeHints(\ReflectionMethod $from, Code\Method $met
return $method;
}

public function __call(string $name, array $args)
{
return call_user_func_array([$this->method, $name], $args);
}
}
4 changes: 2 additions & 2 deletions src/Kdyby/Aop/Pointcut/Matcher/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function doEvaluateValueResolve(ContainerBuilder $builder, $expression)
$expression = self::resolveExpression($expression);

} else {
$expression = $builder->expand('%' . $expression . '%');
$expression = Nette\DI\Helpers::expand('%' . $expression . '%', $builder->parameters);
}

return $expression;
Expand Down Expand Up @@ -210,7 +210,7 @@ private function doSerializeValueResolve(ContainerBuilder $builder, $expression)
$expression = self::resolveExpression($expression);

} elseif (substr($expression, 0, 1) === '%') {
$expression = $builder->expand($expression);
$expression = Nette\DI\Helpers::expand($expression, $builder->parameters);

} elseif (substr($expression, 0, 1) === '$') {
$expression = new Code\PhpLiteral($expression);
Expand Down
Loading

0 comments on commit 18175ab

Please sign in to comment.