This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Kdyby/nette3
Nette 3 support
- Loading branch information
Showing
24 changed files
with
148 additions
and
200 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -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": { | ||
|
This file contains 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 |
---|---|---|
|
@@ -20,12 +20,11 @@ | |
|
||
|
||
|
||
/** | ||
/**W | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class AopExtension extends Nette\DI\CompilerExtension | ||
{ | ||
const CG_INJECT_METHOD = '__injectAopContainer'; | ||
|
||
/** | ||
* @var array | ||
|
@@ -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); | ||
|
@@ -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)); | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -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]; | ||
|
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -19,9 +19,8 @@ | |
/** | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class PointcutMethod extends Code\Method | ||
class PointcutMethod | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -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) { | ||
|
@@ -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()); | ||
} | ||
|
@@ -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) | ||
{ | ||
|
@@ -153,7 +167,7 @@ private function generateRuntimeCondition(Kdyby\Aop\DI\AdviceDefinition $adviceD | |
/** | ||
* @return string | ||
*/ | ||
public function __toString() | ||
public function beforePrint() | ||
{ | ||
$this->setBody(''); | ||
|
||
|
@@ -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 */ | ||
|
@@ -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); | ||
} | ||
} |
This file contains 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.