/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $valueHolder * * @return MethodGenerator */ public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder) { $originalConstructor = self::getConstructor($originalClass); $constructor = $originalConstructor ? self::fromReflection($originalConstructor) : new self('__construct'); $constructor->setDocblock('{@inheritDoc}'); $constructor->setBody('static $reflection;' . "\n\n" . 'if (! $this->' . $valueHolder->getName() . ') {' . "\n" . ' $reflection = $reflection ?: new \\ReflectionClass(' . var_export($originalClass->getName(), true) . ");\n" . ' $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n" . self::getUnsetPropertiesString($originalClass) . "}\n\n" . '$this->' . $valueHolder->getName() . '->' . $constructor->getName() . '(' . implode(', ', array_map(function (ParameterGenerator $parameter) { return ($parameter->getVariadic() ? '...' : '') . '$' . $parameter->getName(); }, $constructor->getParameters())) . ');'); return $constructor; }
/** * Constructor * * @param PropertyGenerator $initializerProperty * @param ZendMethodGenerator $callInitializer */ public function __construct(PropertyGenerator $initializerProperty, ZendMethodGenerator $callInitializer) { parent::__construct('initializeProxy'); $this->setDocblock('{@inheritDoc}'); $this->setReturnType('bool'); $this->setBody('return $this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'initializeProxy\', []);'); }
/** * Creates a new {@link \bitExpert\Disco\Proxy\MethodGenerator\MagicWakeup}. * * @param ReflectionClass $originalClass * @param PropertyGenerator $valueHolderProperty * @param PropertyGenerator $valueHolderBeanIdProperty * @throws InvalidArgumentException */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $valueHolderProperty, PropertyGenerator $valueHolderBeanIdProperty) { parent::__construct($originalClass, '__wakeup'); $valueHolder = $valueHolderProperty->getName(); $valueHolderBeanId = $valueHolderBeanIdProperty->getName(); $this->setBody('$beanFactory = \\' . BeanFactoryRegistry::class . '::getInstance();' . PHP_EOL . PHP_EOL . '$this->' . $valueHolder . ' = $beanFactory->get($this->' . $valueHolderBeanId . ');' . PHP_EOL . 'if ($this->' . $valueHolder . ' instanceof \\' . VirtualProxyInterface::class . ') {' . PHP_EOL . ' $this->' . $valueHolder . ' = $this->' . $valueHolder . '->getWrappedValueHolderValue();' . PHP_EOL . '}' . PHP_EOL); }
/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $initializerProperty * @param PropertyGenerator $valueHolderProperty */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty) { parent::__construct($originalClass, '__sleep'); $initializer = $initializerProperty->getName(); $valueHolder = $valueHolderProperty->getName(); $this->setBody('$this->' . $initializer . ' && $this->' . $initializer . '->__invoke($this->' . $valueHolder . ', $this, \'__sleep\', array(), $this->' . $initializer . ');' . "\n\n" . 'return array(' . var_export($valueHolder, true) . ');'); }
/** * Static constructor * * @param PropertyGenerator $initializerProperty * @param Properties $properties */ public function __construct(PropertyGenerator $initializerProperty, Properties $properties) { parent::__construct('staticProxyConstructor', [], static::FLAG_PUBLIC | static::FLAG_STATIC); $this->setParameter(new ParameterGenerator('initializer')); $this->setDocblock("Constructor for lazy initialization\n\n@param \\Closure|null \$initializer"); $this->setBody('static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance = (new \\ReflectionClass(get_class()))->newInstanceWithoutConstructor();' . "\n\n" . $this->generateUnsetPropertiesCode($properties) . '$instance->' . $initializerProperty->getName() . ' = $initializer;' . "\n\n" . 'return $instance;'); }
/** * Constructor * * @param PropertyGenerator $valueHolderProperty */ public function __construct(PropertyGenerator $valueHolderProperty) { parent::__construct('isProxyInitialized'); $this->setDocblock('{@inheritDoc}'); $this->setReturnType('bool'); $this->setBody('return null !== $this->' . $valueHolderProperty->getName() . ';'); }
/** * Constructor * * @param PropertyGenerator $initializerProperty * @param PropertyGenerator $initTracker * @param Properties $properties */ public function __construct(PropertyGenerator $initializerProperty, PropertyGenerator $initTracker, Properties $properties) { $docblock = <<<'DOCBLOCK' Triggers initialization logic for this ghost object @param string $methodName @param mixed[] $parameters @return mixed DOCBLOCK; parent::__construct(UniqueIdentifierGenerator::getIdentifier('callInitializer'), [new ParameterGenerator('methodName'), new ParameterGenerator('parameters', 'array')], static::VISIBILITY_PRIVATE, null, $docblock); $initializer = $initializerProperty->getName(); $initialization = $initTracker->getName(); $bodyTemplate = <<<'PHP' if ($this->%s || ! $this->%s) { return; } $this->%s = true; %s %s $result = $this->%s->__invoke($this, $methodName, $parameters, $this->%s, $properties); $this->%s = false; return $result; PHP; $this->setBody(sprintf($bodyTemplate, $initialization, $initializer, $initialization, $this->propertiesInitializationCode($properties), $this->propertiesReferenceArrayCode($properties), $initializer, $initializer, $initialization)); }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { parent::__construct('__construct'); $localizedObject = new ParameterGenerator('localizedObject'); $prefix = new ParameterGenerator('prefixInterceptors'); $suffix = new ParameterGenerator('suffixInterceptors'); $localizedObject->setType($originalClass->getName()); $prefix->setDefaultValue(array()); $suffix->setDefaultValue(array()); $prefix->setType('array'); $suffix->setType('array'); $this->setParameter($localizedObject); $this->setParameter($prefix); $this->setParameter($suffix); $localizedProperties = array(); foreach ($originalClass->getProperties() as $originalProperty) { if (!method_exists('Closure', 'bind') && $originalProperty->isPrivate()) { // @codeCoverageIgnoreStart throw UnsupportedProxiedClassException::unsupportedLocalizedReflectionProperty($originalProperty); // @codeCoverageIgnoreEnd } $propertyName = $originalProperty->getName(); if ($originalProperty->isPrivate()) { $localizedProperties[] = "\\Closure::bind(function () use (\$localizedObject) {\n " . '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";\n" . '}, $this, ' . var_export($originalProperty->getDeclaringClass()->getName(), true) . ')->__invoke();'; } else { $localizedProperties[] = '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";"; } } $this->setDocblock("@override constructor to setup interceptors\n\n" . "@param \\" . $originalClass->getName() . " \$localizedObject\n" . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" . "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic"); $this->setBody((empty($localizedProperties) ? '' : implode("\n\n", $localizedProperties) . "\n\n") . '$this->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" . '$this->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;"); }
/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $initializerProperty * @param PropertyGenerator $valueHolderProperty */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty) { parent::__construct($originalClass, '__clone'); $initializer = $initializerProperty->getName(); $valueHolder = $valueHolderProperty->getName(); $this->setBody('$this->' . $initializer . ' && $this->' . $initializer . '->__invoke($this->' . $valueHolder . ', $this, \'__clone\', array(), $this->' . $initializer . ');' . "\n\n" . '$this->' . $valueHolder . ' = clone $this->' . $valueHolder . ';'); }
/** * Constructor * * @param ReflectionClass $originalClass Reflection of the class to proxy * @param PropertyGenerator $adapter Adapter property */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $adapter) { $adapterName = $adapter->getName(); parent::__construct('staticProxyConstructor', [new ParameterGenerator($adapterName, AdapterInterface::class)], MethodGenerator::FLAG_PUBLIC | MethodGenerator::FLAG_STATIC, null, 'Constructor for remote object control\\n\\n' . '@param \\ProxyManager\\Factory\\RemoteObject\\AdapterInterface \\$adapter'); $body = 'static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance = (new \\ReflectionClass(get_class()))->newInstanceWithoutConstructor();' . "\n\n" . '$instance->' . $adapterName . ' = $' . $adapterName . ";\n\n" . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'instance'); $this->setBody($body . "\n\nreturn \$instance;"); }
/** * Constructor */ public function __construct(PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty) { parent::__construct('initializeProxy'); $this->setDocblock('{@inheritDoc}'); $initializer = $initializerProperty->getName(); $this->setBody('return $this->' . $initializer . ' && $this->' . $initializer . '->__invoke($this->' . $valueHolderProperty->getName() . ', $this, \'initializeProxy\', array(), $this->' . $initializer . ');'); }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $valueHolderProperty, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { parent::__construct($originalClass, '__clone'); $valueHolder = $valueHolderProperty->getName(); $prefix = $prefixInterceptors->getName(); $suffix = $suffixInterceptors->getName(); $this->setBody("\$this->{$valueHolder} = clone \$this->{$valueHolder};\n\n" . "foreach (\$this->{$prefix} as \$key => \$value) {\n" . " \$this->{$prefix}" . "[\$key] = clone \$value;\n" . "}\n\n" . "foreach (\$this->{$suffix} as \$key => \$value) {\n" . " \$this->{$suffix}" . "[\$key] = clone \$value;\n" . "}"); }
/** * Constructor */ public function __construct(PropertyGenerator $initializerProperty) { parent::__construct('setProxyInitializer'); $initializerParameter = new ParameterGenerator('initializer'); $initializerParameter->setType('Closure'); $initializerParameter->setDefaultValue(null); $this->setParameter($initializerParameter); $this->setDocblock('{@inheritDoc}'); $this->setBody('$this->' . $initializerProperty->getName() . ' = $initializer;'); }
/** * Constructor */ public function __construct(PropertyGenerator $initializerProperty, PropertyGenerator $publicPropertiesDefaults, PropertyGenerator $initializationTracker) { parent::__construct(UniqueIdentifierGenerator::getIdentifier('callInitializer')); $this->setDocblock("Triggers initialization logic for this ghost object"); $this->setParameters(array(new ParameterGenerator('methodName'), new ParameterGenerator('parameters', 'array'))); $this->setVisibility(static::VISIBILITY_PRIVATE); $initializer = $initializerProperty->getName(); $initialization = $initializationTracker->getName(); $this->setBody('if ($this->' . $initialization . ' || ! $this->' . $initializer . ') {' . "\n return;\n}\n\n" . "\$this->" . $initialization . " = true;\n\n" . "foreach (self::\$" . $publicPropertiesDefaults->getName() . " as \$key => \$default) {\n" . " \$this->\$key = \$default;\n" . "}\n\n" . '$this->' . $initializer . '->__invoke' . '($this, $methodName, $parameters, $this->' . $initializer . ');' . "\n\n" . "\$this->" . $initialization . " = false;"); }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty, PublicPropertiesMap $publicProperties) { parent::__construct($originalClass, '__get', array(new ParameterGenerator('name'))); $this->setDocblock(($originalClass->hasMethod('__get') ? "{@inheritDoc}\n" : '') . '@param string $name'); $initializer = $initializerProperty->getName(); $valueHolder = $valueHolderProperty->getName(); $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . ' return $this->' . $valueHolder . '->$name;' . "\n}\n\n"; $callParent .= PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'name', null, $valueHolderProperty); $this->setBody('$this->' . $initializer . ' && $this->' . $initializer . '->__invoke($this->' . $valueHolder . ', $this, \'__get\', array(\'name\' => $name), $this->' . $initializer . ');' . "\n\n" . $callParent); }
/** * Constructor * * @param PropertyGenerator $suffixInterceptor */ public function __construct(PropertyGenerator $suffixInterceptor) { parent::__construct('setMethodSuffixInterceptor'); $interceptor = new ParameterGenerator('suffixInterceptor'); $interceptor->setType(Closure::class); $interceptor->setDefaultValue(null); $this->setParameter(new ParameterGenerator('methodName', 'string')); $this->setParameter($interceptor); $this->setDocblock('{@inheritDoc}'); $this->setBody('$this->' . $suffixInterceptor->getName() . '[$methodName] = $suffixInterceptor;'); }
private static function generateOriginalConstructorCall(ReflectionClass $class, PropertyGenerator $valueHolder) : string { $originalConstructor = self::getConstructor($class); if (!$originalConstructor) { return ''; } $constructor = self::fromReflection($originalConstructor); return "\n\n" . '$this->' . $valueHolder->getName() . '->' . $constructor->getName() . '(' . implode(', ', array_map(function (ParameterGenerator $parameter) : string { return ($parameter->getVariadic() ? '...' : '') . '$' . $parameter->getName(); }, $constructor->getParameters())) . ');'; }
/** * @param \Zend\Code\Reflection\MethodReflection $originalMethod * @param \Zend\Code\Generator\PropertyGenerator $adapterProperty * @param \ReflectionClass $originalClass * * @return RemoteObjectMethod|static */ public static function generateMethod(MethodReflection $originalMethod, PropertyGenerator $adapterProperty, ReflectionClass $originalClass) { /* @var $method self */ $method = static::fromReflection($originalMethod); $parameters = $originalMethod->getParameters(); $list = array(); foreach ($parameters as $parameter) { $list[] = '$' . $parameter->getName(); } $method->setBody('$return = $this->' . $adapterProperty->getName() . '->call(' . var_export($originalClass->getName(), true) . ', ' . var_export($originalMethod->getName(), true) . ', array(' . implode(', ', $list) . '));' . "\n\n" . 'return $return;'); 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)); }
/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $initializerProperty * @param PropertyGenerator $valueHolderProperty * @param PublicPropertiesMap $publicProperties */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty, PublicPropertiesMap $publicProperties) { parent::__construct($originalClass, '__set', [new ParameterGenerator('name'), new ParameterGenerator('value')]); $initializer = $initializerProperty->getName(); $valueHolder = $valueHolderProperty->getName(); $callParent = ''; $this->setDocblock(($originalClass->hasMethod('__set') ? "{@inheritDoc}\n" : '') . "@param string \$name\n@param mixed \$value"); if (!$publicProperties->isEmpty()) { $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . ' return ($this->' . $valueHolder . '->$name = $value);' . "\n}\n\n"; } $callParent .= PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_SET, 'name', 'value', $valueHolderProperty); $this->setBody('$this->' . $initializer . ' && $this->' . $initializer . '->__invoke($this->' . $valueHolder . ', $this, ' . '\'__set\', array(\'name\' => $name, \'value\' => $value), $this->' . $initializer . ');' . "\n\n" . $callParent); }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty) { parent::__construct('__construct'); $this->setParameter(new ParameterGenerator('initializer')); /* @var $publicProperties \ReflectionProperty[] */ $publicProperties = $originalClass->getProperties(ReflectionProperty::IS_PUBLIC); $unsetProperties = array(); foreach ($publicProperties as $publicProperty) { $unsetProperties[] = '$this->' . $publicProperty->getName(); } $this->setDocblock("@override constructor for lazy initialization\n\n@param \\Closure|null \$initializer"); $this->setBody(($unsetProperties ? 'unset(' . implode(', ', $unsetProperties) . ");\n\n" : '') . '$this->' . $initializerProperty->getName() . ' = $initializer;'); }
/** * Constructor * * @param ReflectionClass $originalClass Reflection of the class to proxy * @param PropertyGenerator $adapter Adapter property */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $adapter) { parent::__construct('__construct'); $adapterName = $adapter->getName(); $this->setParameter(new ParameterGenerator($adapterName, 'ProxyManager\\Factory\\RemoteObject\\AdapterInterface')); $this->setDocblock('@override constructor for remote object control\\n\\n' . '@param \\ProxyManager\\Factory\\RemoteObject\\AdapterInterface \\$adapter'); $body = '$this->' . $adapterName . ' = $' . $adapterName . ';'; foreach ($originalClass->getProperties() as $property) { if ($property->isPublic() && !$property->isStatic()) { $body .= "\nunset(\$this->" . $property->getName() . ');'; } } $this->setBody($body); }
/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $prefixInterceptors * @param PropertyGenerator $suffixInterceptors */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { parent::__construct('bindProxyProperties', [new ParameterGenerator('localizedObject', $originalClass->getName()), new ParameterGenerator('prefixInterceptors', 'array', []), new ParameterGenerator('suffixInterceptors', 'array', [])], static::FLAG_PRIVATE, null, "@override constructor to setup interceptors\n\n" . "@param \\" . $originalClass->getName() . " \$localizedObject\n" . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" . "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic"); $localizedProperties = []; $properties = Properties::fromReflectionClass($originalClass); foreach ($properties->getAccessibleProperties() as $property) { $propertyName = $property->getName(); $localizedProperties[] = '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";"; } foreach ($properties->getPrivateProperties() as $property) { $propertyName = $property->getName(); $localizedProperties[] = "\\Closure::bind(function () use (\$localizedObject) {\n " . '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";\n" . '}, $this, ' . var_export($property->getDeclaringClass()->getName(), true) . ')->__invoke();'; } $this->setBody((empty($localizedProperties) ? '' : implode("\n\n", $localizedProperties) . "\n\n") . '$this->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" . '$this->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;"); }
/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $valueHolder * @param PropertyGenerator $prefixInterceptors * @param PropertyGenerator $suffixInterceptors */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $valueHolder, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { parent::__construct('staticProxyConstructor', [], static::FLAG_PUBLIC | static::FLAG_STATIC); $prefix = new ParameterGenerator('prefixInterceptors'); $suffix = new ParameterGenerator('suffixInterceptors'); $prefix->setDefaultValue([]); $suffix->setDefaultValue([]); $prefix->setType('array'); $suffix->setType('array'); $this->setParameter(new ParameterGenerator('wrappedObject')); $this->setParameter($prefix); $this->setParameter($suffix); $this->setReturnType($originalClass->getName()); $this->setDocblock("Constructor to setup interceptors\n\n" . "@param \\" . $originalClass->getName() . " \$wrappedObject\n" . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" . "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic\n\n" . "@return self"); $this->setBody('static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance = (new \\ReflectionClass(get_class()))->newInstanceWithoutConstructor();' . "\n\n" . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'instance') . '$instance->' . $valueHolder->getName() . " = \$wrappedObject;\n" . '$instance->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" . '$instance->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;\n\n" . 'return $instance;'); }
/** * @param \ReflectionClass $originalClass * @param \Zend\Code\Generator\PropertyGenerator $initializerProperty * @param \Zend\Code\Generator\MethodGenerator $callInitializer * @param \ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap $publicProperties */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, MethodGenerator $callInitializer, PublicPropertiesMap $publicProperties) { parent::__construct($originalClass, '__get', array(new ParameterGenerator('name'))); $override = $originalClass->hasMethod('__get'); $callParent = ''; $this->setDocblock(($override ? "{@inheritDoc}\n" : '') . '@param string $name'); if (!$publicProperties->isEmpty()) { $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . ' return $this->$name;' . "\n}\n\n"; } if ($override) { $callParent .= 'return parent::__get($name);'; } else { $callParent .= PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'name'); } $this->setBody('$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'__get\', array(\'name\' => $name));' . "\n\n" . $callParent); }
/** * @param \Zend\Code\Reflection\MethodReflection $originalMethod * @param \Zend\Code\Generator\PropertyGenerator $initializerProperty * @param \Zend\Code\Generator\MethodGenerator $callInitializer * * @return LazyLoadingMethodInterceptor|static */ public static function generateMethod(MethodReflection $originalMethod, PropertyGenerator $initializerProperty, ZendMethodGenerator $callInitializer) { /* @var $method self */ $method = static::fromReflection($originalMethod); $parameters = $originalMethod->getParameters(); $methodName = $originalMethod->getName(); $initializerParams = array(); $forwardedParams = array(); foreach ($parameters as $parameter) { $parameterName = $parameter->getName(); $initializerParams[] = var_export($parameterName, true) . ' => $' . $parameterName; $forwardedParams[] = '$' . $parameterName; } $method->setBody('$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(' . var_export($methodName, true) . ', array(' . implode(', ', $initializerParams) . "));\n\n" . 'return parent::' . $methodName . '(' . implode(', ', $forwardedParams) . ');'); $method->setDocblock('{@inheritDoc}'); return $method; }
/** * @param \Zend\Code\Reflection\MethodReflection $originalMethod * @param \Zend\Code\Generator\PropertyGenerator $initializerProperty * @param \Zend\Code\Generator\PropertyGenerator $valueHolderProperty * * @return LazyLoadingMethodInterceptor|static */ public static function generateMethod(MethodReflection $originalMethod, PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty) { /* @var $method self */ $method = static::fromReflection($originalMethod); $initializerName = $initializerProperty->getName(); $valueHolderName = $valueHolderProperty->getName(); $parameters = $originalMethod->getParameters(); $methodName = $originalMethod->getName(); $initializerParams = []; $forwardedParams = []; foreach ($parameters as $parameter) { $parameterName = $parameter->getName(); $variadicPrefix = $parameter->isVariadic() ? '...' : ''; $initializerParams[] = var_export($parameterName, true) . ' => $' . $parameterName; $forwardedParams[] = $variadicPrefix . '$' . $parameterName; } $method->setBody('$this->' . $initializerName . ' && $this->' . $initializerName . '->__invoke($this->' . $valueHolderName . ', $this, ' . var_export($methodName, true) . ', array(' . implode(', ', $initializerParams) . '), $this->' . $initializerName . ");\n\n" . 'return $this->' . $valueHolderName . '->' . $methodName . '(' . implode(', ', $forwardedParams) . ');'); $method->setDocblock('{@inheritDoc}'); return $method; }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $valueHolder, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { parent::__construct('__construct'); $prefix = new ParameterGenerator('prefixInterceptors'); $suffix = new ParameterGenerator('suffixInterceptors'); $prefix->setDefaultValue(array()); $suffix->setDefaultValue(array()); $prefix->setType('array'); $suffix->setType('array'); $this->setParameter(new ParameterGenerator('wrappedObject')); $this->setParameter($prefix); $this->setParameter($suffix); /* @var $publicProperties \ReflectionProperty[] */ $publicProperties = $originalClass->getProperties(ReflectionProperty::IS_PUBLIC); $unsetProperties = array(); foreach ($publicProperties as $publicProperty) { $unsetProperties[] = '$this->' . $publicProperty->getName(); } $this->setDocblock("@override constructor to setup interceptors\n\n" . "@param \\" . $originalClass->getName() . " \$wrappedObject\n" . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" . "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic"); $this->setBody(($unsetProperties ? 'unset(' . implode(', ', $unsetProperties) . ");\n\n" : '') . '$this->' . $valueHolder->getName() . " = \$wrappedObject;\n" . '$this->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" . '$this->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;"); }
/** * Constructor */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, MethodGenerator $callInitializer) { parent::__construct($originalClass, '__sleep'); $this->setBody('$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'__sleep\', array());' . "\n\n" . ($originalClass->hasMethod('__sleep') ? 'return parent::__sleep();' : 'return array_keys((array) $this);')); }
/** * @param ReflectionClass $originalClass * @param PropertyGenerator $initializerProperty * @param MethodGenerator $callInitializer * @param PublicPropertiesMap $publicProperties * @param ProtectedPropertiesMap $protectedProperties * @param PrivatePropertiesMap $privateProperties */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, MethodGenerator $callInitializer, PublicPropertiesMap $publicProperties, ProtectedPropertiesMap $protectedProperties, PrivatePropertiesMap $privateProperties) { parent::__construct($originalClass, '__set', [new ParameterGenerator('name'), new ParameterGenerator('value')]); $override = $originalClass->hasMethod('__set'); $this->setDocblock(($override ? "{@inheritDoc}\n" : '') . '@param string $name'); $parentAccess = 'return parent::__set($name, $value);'; if (!$override) { $parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_SET, 'name', 'value'); } $this->setBody(sprintf($this->callParentTemplate, '$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'__set\', array(\'name\' => $name, \'value\' => $value));', $publicProperties->getName(), $protectedProperties->getName(), $protectedProperties->getName(), $privateProperties->getName(), $privateProperties->getName(), $privateProperties->getName(), $parentAccess)); }