/**
  * @param string                                  $methodBody         the body of the previously generated code.
  *                                                                    It MUST assign the return value to a variable
  *                                                                    `$returnValue` instead of directly returning
  * @param \ProxyManager\Generator\MethodGenerator $method
  * @param \Zend\Code\Generator\PropertyGenerator  $prefixInterceptors
  * @param \Zend\Code\Generator\PropertyGenerator  $suffixInterceptors
  *
  * @return string
  */
 public static function createInterceptedMethodBody(string $methodBody, MethodGenerator $method, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) : string
 {
     $name = var_export($method->getName(), true);
     $prefixInterceptors = $prefixInterceptors->getName();
     $suffixInterceptors = $suffixInterceptors->getName();
     $params = [];
     foreach ($method->getParameters() as $parameter) {
         $parameterName = $parameter->getName();
         $params[] = var_export($parameterName, true) . ' => $' . $parameter->getName();
     }
     $paramsString = 'array(' . implode(', ', $params) . ')';
     return "if (isset(\$this->{$prefixInterceptors}" . "[{$name}])) {\n" . "    \$returnEarly       = false;\n" . "    \$prefixReturnValue = \$this->{$prefixInterceptors}" . "[{$name}]->__invoke(" . "\$this, \$this, {$name}, {$paramsString}, \$returnEarly);\n\n" . "    if (\$returnEarly) {\n" . "        return \$prefixReturnValue;\n" . "    }\n" . "}\n\n" . $methodBody . "\n\n" . "if (isset(\$this->{$suffixInterceptors}" . "[{$name}])) {\n" . "    \$returnEarly       = false;\n" . "    \$suffixReturnValue = \$this->{$suffixInterceptors}" . "[{$name}]->__invoke(" . "\$this, \$this, {$name}, {$paramsString}, \$returnValue, \$returnEarly);\n\n" . "    if (\$returnEarly) {\n" . "        return \$suffixReturnValue;\n" . "    }\n" . "}\n\n" . "return \$returnValue;";
 }
 /**
  * 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;');
 }
예제 #3
0
    /**
     * 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));
    }
예제 #4
0
 /**
  * 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\', []);');
 }
예제 #5
0
 /**
  * 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 . ');');
 }
예제 #6
0
 /**
  * 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() . ';');
 }
예제 #7
0
 public function __construct()
 {
     parent::__construct('setIndexOriginalValue');
     $this->setParameter(new ParameterGenerator('name', 'string'));
     $this->setParameter(new ParameterGenerator('value', 'string'));
     $this->setBody('$this->_original_indices[$name] = $value; return $this;');
 }
예제 #8
0
 /**
  * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\GetParameter}.
  *
  * @param ReflectionClass $originalClass
  * @param ParameterValuesProperty $parameterValueProperty
  * @throws InvalidArgumentException
  */
 public function __construct(ReflectionClass $originalClass, ParameterValuesProperty $parameterValueProperty)
 {
     parent::__construct('getParameter');
     $propertyNameParameter = new ParameterGenerator('propertyName');
     $requiredParameter = new ParameterGenerator('required');
     $requiredParameter->setDefaultValue(true);
     $defaultValueParameter = new ParameterGenerator('defaultValue');
     $defaultValueParameter->setDefaultValue(null);
     $body = '$steps = explode(\'.\', $' . $propertyNameParameter->getName() . ');' . PHP_EOL;
     $body .= '$value = $this->' . $parameterValueProperty->getName() . ';' . PHP_EOL;
     $body .= '$currentPath = [];' . PHP_EOL;
     $body .= 'foreach ($steps as $step) {' . PHP_EOL;
     $body .= '    $currentPath[] = $step;' . PHP_EOL;
     $body .= '    if (isset($value[$step])) {' . PHP_EOL;
     $body .= '        $value = $value[$step];' . PHP_EOL;
     $body .= '    } else {' . PHP_EOL;
     $body .= '        $value = $' . $defaultValueParameter->getName() . ';' . PHP_EOL;
     $body .= '        break;' . PHP_EOL;
     $body .= '    }' . PHP_EOL;
     $body .= '}' . PHP_EOL . PHP_EOL;
     $body .= 'if ($' . $requiredParameter->getName() . ' && (null === $value)) {' . PHP_EOL;
     $body .= '    if (null === $' . $defaultValueParameter->getName() . ') {' . PHP_EOL;
     $body .= '        throw new \\RuntimeException(\'Parameter "\' .$' . $propertyNameParameter->getName() . '. \'" is required but not defined and no default value provided!\');' . PHP_EOL;
     $body .= '    }' . PHP_EOL;
     $body .= '    throw new \\RuntimeException(\'Parameter "\' .$' . $propertyNameParameter->getName() . '. \'" not defined!\');' . PHP_EOL;
     $body .= '}' . PHP_EOL . PHP_EOL;
     $body .= 'return $value;' . PHP_EOL;
     $this->setParameter($propertyNameParameter);
     $this->setParameter($requiredParameter);
     $this->setParameter($defaultValueParameter);
     $this->setVisibility(self::VISIBILITY_PROTECTED);
     $this->setBody($body);
 }
예제 #9
0
 /**
  * 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 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;");
 }
 /**
  * @param ReflectionClass $originalClass
  * @param string          $name
  * @param array           $parameters
  */
 public function __construct(ReflectionClass $originalClass, $name, array $parameters = array())
 {
     parent::__construct($name, $parameters, static::FLAG_PUBLIC, null, $originalClass->hasMethod($name) ? '{@inheritDoc}' : null);
     $this->setReturnsReference(strtolower($name) === '__get');
     if ($originalClass->hasMethod($name)) {
         $this->setReturnsReference($originalClass->getMethod($name)->returnsReference());
     }
 }
예제 #12
0
 /**
  * Constructor
  *
  * @param ReflectionClass $originalClass Reflection of the class to proxy
  */
 public function __construct(ReflectionClass $originalClass)
 {
     parent::__construct('staticProxyConstructor', [], static::FLAG_PUBLIC | static::FLAG_STATIC);
     $nullableProperties = array_map(function (ReflectionProperty $publicProperty) {
         return '$instance->' . $publicProperty->getName() . ' = null;';
     }, Properties::fromReflectionClass($originalClass)->getPublicProperties());
     $this->setDocblock("Constructor for null object initialization");
     $this->setBody('static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance = (new \\ReflectionClass(get_class()))->newInstanceWithoutConstructor();' . "\n\n" . ($nullableProperties ? implode("\n", $nullableProperties) . "\n\n" : '') . 'return $instance;');
 }
예제 #13
0
 public function testGeneratedParametersFromReflection()
 {
     $method = MethodGenerator::fromReflection(new MethodReflection('ProxyManagerTestAsset\\BaseClass', 'publicTypeHintedMethod'));
     $this->assertSame('publicTypeHintedMethod', $method->getName());
     $parameters = $method->getParameters();
     $this->assertCount(1, $parameters);
     $param = $parameters['param'];
     $this->assertSame('stdClass', $param->getType());
 }
 /**
  * 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;");
 }
예제 #15
0
 /**
  * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\HasAlias}.
  *
  * @param ReflectionClass $originalClass
  * @param AliasesProperty $aliasesProperty
  * @throws InvalidArgumentException
  */
 public function __construct(ReflectionClass $originalClass, AliasesProperty $aliasesProperty)
 {
     parent::__construct('hasAlias');
     $aliasParameter = new ParameterGenerator('alias');
     $aliasParameter->setType('string');
     $this->setParameter($aliasParameter);
     $this->setVisibility(self::VISIBILITY_PUBLIC);
     $this->setReturnType('bool');
     $this->setBody('return !empty($' . $aliasParameter->getName() . ') && ' . 'isset($this->' . $aliasesProperty->getName() . '[$' . $aliasParameter->getName() . ']);');
 }
예제 #16
0
 /**
  * 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
  *
  * @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;');
 }
 /**
  * Verify that building from reflection works
  */
 public function testGenerateFromReflection()
 {
     $method = MethodGenerator::fromReflection(new MethodReflection(__CLASS__, __FUNCTION__));
     $this->assertSame(__FUNCTION__, $method->getName());
     $this->assertSame(MethodGenerator::VISIBILITY_PUBLIC, $method->getVisibility());
     $this->assertFalse($method->isStatic());
     $this->assertSame('Verify that building from reflection works', $method->getDocBlock()->getShortDescription());
     $method = MethodGenerator::fromReflection(new MethodReflection('ProxyManagerTestAsset\\BaseClass', 'protectedMethod'));
     $this->assertSame(MethodGenerator::VISIBILITY_PROTECTED, $method->getVisibility());
     $method = MethodGenerator::fromReflection(new MethodReflection('ProxyManagerTestAsset\\BaseClass', 'privateMethod'));
     $this->assertSame(MethodGenerator::VISIBILITY_PRIVATE, $method->getVisibility());
 }
예제 #19
0
 /**
  * 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;');
 }
예제 #20
0
 /**
  * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\GetAlias}.
  *
  * @param ReflectionClass $originalClass
  * @param AliasesProperty $aliasesProperty
  * @throws InvalidArgumentException
  */
 public function __construct(ReflectionClass $originalClass, AliasesProperty $aliasesProperty)
 {
     parent::__construct('getAlias');
     $aliasParameter = new ParameterGenerator('alias');
     $aliasParameter->setType('string');
     $body = 'if ($this->hasAlias($' . $aliasParameter->getName() . ')) {' . PHP_EOL;
     $body .= '    $methodname = $this->' . $aliasesProperty->getName() . '[$' . $aliasParameter->getName() . '];' . PHP_EOL;
     $body .= '    return $this->$methodname();' . PHP_EOL;
     $body .= '}' . PHP_EOL . PHP_EOL;
     $body .= 'throw new ' . BeanNotFoundException::class . '(sprintf(\'Alias "%s" is not defined!\', $' . $aliasParameter->getName() . '));' . PHP_EOL;
     $this->setParameter($aliasParameter);
     $this->setVisibility(self::VISIBILITY_PUBLIC);
     $this->setBody($body);
 }
예제 #21
0
 /**
  * Constructor
  *
  * @param ReflectionClass $originalClass Reflection of the class to proxy
  */
 public function __construct(ReflectionClass $originalClass)
 {
     parent::__construct('__construct');
     /* @var $publicProperties \ReflectionProperty[] */
     $publicProperties = $originalClass->getProperties(ReflectionProperty::IS_PUBLIC);
     $nullableProperties = array();
     foreach ($publicProperties as $publicProperty) {
         $nullableProperties[] = '$this->' . $publicProperty->getName() . ' = null;';
     }
     $this->setDocblock("@override constructor for null object initialization");
     if ($nullableProperties) {
         $this->setBody(implode("\n", $nullableProperties));
     }
 }
예제 #22
0
 /**
  * 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);
 }
예제 #23
0
 /**
  * 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;');
 }
예제 #25
0
 /**
  * Creates a new {@link \bitExpert\Disco\Proxy\LazyBean\MethodGenerator\Constructor}.
  *
  * @param ReflectionClass $originalClass
  * @param BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty
  */
 public function __construct(ReflectionClass $originalClass, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty)
 {
     parent::__construct('wrapBeanAsLazy');
     $this->setParameter(new ParameterGenerator('beanId'));
     $this->setParameter(new ParameterGenerator('beanType'));
     $this->setParameter(new ParameterGenerator('instance'));
     $content = '$factory = new \\' . \bitExpert\Disco\Proxy\LazyBean\LazyBeanFactory::class . '($beanId, $this->' . $beanFactoryConfigurationProperty->getName() . '->getProxyManagerConfiguration());' . PHP_EOL;
     $content .= '$initializer = function (&$wrappedObject, \\' . \ProxyManager\Proxy\LazyLoadingInterface::class . ' $proxy, $method, array $parameters, &$initializer) use($instance) {' . PHP_EOL;
     $content .= '    $initializer = null;' . PHP_EOL;
     $content .= '    $wrappedObject = $instance;' . PHP_EOL;
     $content .= '    return true;' . PHP_EOL;
     $content .= '};' . PHP_EOL;
     $content .= PHP_EOL;
     $content .= '$initializer->bindTo($this);' . PHP_EOL;
     $content .= 'return $factory->createProxy($beanType, $initializer);' . PHP_EOL;
     $this->setBody($content);
 }
예제 #26
0
 /**
  * Constructor
  *
  * @param ReflectionClass $originalClass
  */
 public function __construct(ReflectionClass $originalClass)
 {
     parent::__construct('staticProxyConstructor', [], static::FLAG_PUBLIC | static::FLAG_STATIC);
     $localizedObject = new ParameterGenerator('localizedObject');
     $prefix = new ParameterGenerator('prefixInterceptors');
     $suffix = new ParameterGenerator('suffixInterceptors');
     $localizedObject->setType($originalClass->getName());
     $prefix->setDefaultValue([]);
     $suffix->setDefaultValue([]);
     $prefix->setType('array');
     $suffix->setType('array');
     $this->setParameter($localizedObject);
     $this->setParameter($prefix);
     $this->setParameter($suffix);
     $this->setDocblock("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\n\n" . "@return self");
     $this->setBody('static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance   = $reflection->newInstanceWithoutConstructor();' . "\n\n" . '$instance->bindProxyProperties($localizedObject, $prefixInterceptors, $suffixInterceptors);' . "\n\n" . 'return $instance;');
 }
예제 #27
0
 /**
  * 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;");
 }
예제 #28
0
 /**
  * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\Constructor}.
  *
  * @param ReflectionClass $originalClass
  * @param ParameterValuesProperty $parameterValuesProperty
  * @param SessionBeansProperty $sessionBeansProperty
  * @param BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty
  * @param BeanPostProcessorsProperty $beanPostProcessorsProperty
  * @param string[] $beanPostProcessorMethodNames
  */
 public function __construct(ReflectionClass $originalClass, ParameterValuesProperty $parameterValuesProperty, SessionBeansProperty $sessionBeansProperty, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty, BeanPostProcessorsProperty $beanPostProcessorsProperty, array $beanPostProcessorMethodNames)
 {
     parent::__construct('__construct');
     $beanFactoryConfigurationParameter = new ParameterGenerator('config');
     $beanFactoryConfigurationParameter->setType(BeanFactoryConfiguration::class);
     $parametersParameter = new ParameterGenerator('params');
     $parametersParameter->setDefaultValue([]);
     $body = '$this->' . $parameterValuesProperty->getName() . ' = $' . $parametersParameter->getName() . ';' . PHP_EOL;
     $body .= '$this->' . $beanFactoryConfigurationProperty->getName() . ' = $' . $beanFactoryConfigurationParameter->getName() . ';' . PHP_EOL;
     $body .= '$this->' . $sessionBeansProperty->getName() . ' = $' . $beanFactoryConfigurationParameter->getName() . '->getSessionBeanStore();' . PHP_EOL;
     $body .= '// register {@link \\bitExpert\\Disco\\BeanPostProcessor} instances' . PHP_EOL;
     $body .= '$this->' . $beanPostProcessorsProperty->getName() . '[] = new \\bitExpert\\Disco\\BeanFactoryPostProcessor();' . PHP_EOL;
     foreach ($beanPostProcessorMethodNames as $methodName) {
         $body .= '$this->' . $beanPostProcessorsProperty->getName() . '[] = $this->' . $methodName . '(); ';
         $body .= PHP_EOL;
     }
     $this->setParameter($beanFactoryConfigurationParameter);
     $this->setParameter($parametersParameter);
     $this->setBody($body);
     $this->setDocBlock("@override constructor");
 }
예제 #29
0
 public function __construct()
 {
     parent::__construct('getIndexOriginalValue');
     $this->setParameter(new ParameterGenerator('name', 'string'));
     $this->setBody('return isset($this->_original_indices[$name]) ? $this->_original_indices[$name] : null;');
 }
 /**
  * Retrieves all abstract methods to be proxied
  *
  * @param ReflectionClass $originalClass
  *
  * @return MethodGenerator[]
  */
 private function getAbstractProxiedMethods(ReflectionClass $originalClass)
 {
     return array_map(function (ReflectionMethod $method) {
         return ProxyManagerMethodGenerator::fromReflection(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()))->setAbstract(false);
     }, ProxiedMethodsFilter::getAbstractProxiedMethods($originalClass));
 }