/** * @param \Zend\Code\Reflection\MethodReflection $originalMethod * @param \Zend\Code\Generator\PropertyGenerator $valueHolderProperty * @param \Zend\Code\Generator\PropertyGenerator $prefixInterceptors * @param \Zend\Code\Generator\PropertyGenerator $suffixInterceptors * * @return self */ public static function generateMethod(MethodReflection $originalMethod, PropertyGenerator $valueHolderProperty, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { /* @var $method self */ $method = static::fromReflection($originalMethod); $forwardedParams = array(); foreach ($originalMethod->getParameters() as $parameter) { $forwardedParams[] = '$' . $parameter->getName(); } $method->setDocblock('{@inheritDoc}'); $method->setBody(InterceptorGenerator::createInterceptedMethodBody('$returnValue = $this->' . $valueHolderProperty->getName() . '->' . $originalMethod->getName() . '(' . implode(', ', $forwardedParams) . ');', $method, $valueHolderProperty, $prefixInterceptors, $suffixInterceptors)); return $method; }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $valueHolder, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors, PublicPropertiesMap $publicProperties) { parent::__construct($originalClass, '__set', array(new ParameterGenerator('name'), new ParameterGenerator('value'))); $override = $originalClass->hasMethod('__set'); $valueHolderName = $valueHolder->getName(); $this->setDocblock(($override ? "{@inheritDoc}\n" : '') . '@param string $name'); $callParent = PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_SET, 'name', 'value', $valueHolder, 'returnValue'); if (!$publicProperties->isEmpty()) { $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . ' $returnValue = ($this->' . $valueHolderName . '->$name = $value);' . "\n} else {\n {$callParent}\n}\n\n"; } $this->setBody(InterceptorGenerator::createInterceptedMethodBody($callParent, $this, $valueHolder, $prefixInterceptors, $suffixInterceptors)); }
/** * @covers \ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util\InterceptorGenerator */ public function testInterceptorGenerator() { $method = $this->getMock('ProxyManager\\Generator\\MethodGenerator'); $bar = $this->getMock('ProxyManager\\Generator\\ParameterGenerator'); $baz = $this->getMock('ProxyManager\\Generator\\ParameterGenerator'); $valueHolder = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); $prefixInterceptors = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); $suffixInterceptors = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); $bar->expects($this->any())->method('getName')->will($this->returnValue('bar')); $baz->expects($this->any())->method('getName')->will($this->returnValue('baz')); $method->expects($this->any())->method('getName')->will($this->returnValue('fooMethod')); $method->expects($this->any())->method('getParameters')->will($this->returnValue(array($bar, $baz))); $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); $prefixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('pre')); $suffixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('post')); $body = InterceptorGenerator::createInterceptedMethodBody('$returnValue = "foo";', $method, $valueHolder, $prefixInterceptors, $suffixInterceptors); $this->assertSame('if (isset($this->pre[\'fooMethod\'])) {' . "\n" . ' $returnEarly = false;' . "\n" . ' $prefixReturnValue = $this->pre[\'fooMethod\']->__invoke($this, $this->foo, \'fooMethod\', ' . 'array(\'bar\' => $bar, \'baz\' => $baz), $returnEarly);' . "\n\n" . ' if ($returnEarly) {' . "\n" . ' return $prefixReturnValue;' . "\n" . ' }' . "\n" . '}' . "\n\n" . '$returnValue = "foo";' . "\n\n" . 'if (isset($this->post[\'fooMethod\'])) {' . "\n" . ' $returnEarly = false;' . "\n" . ' $suffixReturnValue = $this->post[\'fooMethod\']->__invoke($this, $this->foo, \'fooMethod\', ' . 'array(\'bar\' => $bar, \'baz\' => $baz), $returnValue, $returnEarly);' . "\n\n" . ' if ($returnEarly) {' . "\n" . ' return $suffixReturnValue;' . "\n" . ' }' . "\n" . '}' . "\n\n" . 'return $returnValue;', $body); }