/** * 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;"); }
public function testGeneratesMethodWithCallableType() { if (PHP_VERSION_ID < 50400) { $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0'); } $generator = new ParameterGenerator(); $generator->setType('callable'); $generator->setName('foo'); $this->assertSame('callable $foo', $generator->generate()); }
/** * 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')); $this->setParameter($interceptor); $this->setDocblock('{@inheritDoc}'); $this->setBody('$this->' . $suffixInterceptor->getName() . '[$methodName] = $suffixInterceptor;'); }
/** * 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;'); }
/** * 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); $publicProperties = Properties::fromReflectionClass($originalClass)->getPublicProperties(); $unsetProperties = []; foreach ($publicProperties as $publicProperty) { $unsetProperties[] = '$instance->' . $publicProperty->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" . ($unsetProperties ? 'unset(' . implode(', ', $unsetProperties) . ");\n\n" : '') . '$instance->' . $valueHolder->getName() . " = \$wrappedObject;\n" . '$instance->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" . '$instance->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;\n\n" . 'return $instance;'); }
/** * 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;"); }