/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { $publicProperties = new PublicPropertiesMap($originalClass); $interfaces = array('ProxyManager\\Proxy\\AccessInterceptorInterface', 'ProxyManager\\Proxy\\ValueHolderInterface'); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty()); $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); $classGenerator->addPropertyFromGenerator($publicProperties); foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { $classGenerator->addMethodFromGenerator(InterceptedMethod::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $valueHolder, $prefixInterceptors, $suffixInterceptors)); } $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new GetWrappedValueHolderValue($valueHolder)); $classGenerator->addMethodFromGenerator(new SetMethodPrefixInterceptor($prefixInterceptors)); $classGenerator->addMethodFromGenerator(new SetMethodSuffixInterceptor($suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $valueHolder)); $classGenerator->addMethodFromGenerator(new MagicWakeup($originalClass, $valueHolder)); }
/** * {@inheritdoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $classGenerator->setExtendedClass($originalClass->getName()); $additionalInterfaces = ['OpenClassrooms\\ServiceProxy\\ServiceProxyInterface']; $additionalProperties['proxy_realSubject'] = new PropertyGenerator('proxy_realSubject', null, PropertyGenerator::FLAG_PRIVATE); $additionalMethods['setProxy_realSubject'] = new MethodGenerator('setProxy_realSubject', [['name' => 'realSubject']], MethodGenerator::FLAG_PUBLIC, '$this->proxy_realSubject = $realSubject;'); $methods = $originalClass->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { $preSource = ''; $postSource = ''; $exceptionSource = ''; $methodAnnotations = $this->annotationReader->getMethodAnnotations($method); foreach ($methodAnnotations as $methodAnnotation) { if ($methodAnnotation instanceof Cache) { $additionalInterfaces['cache'] = 'OpenClassrooms\\ServiceProxy\\ServiceProxyCacheInterface'; $response = $this->cacheStrategy->execute($this->serviceProxyStrategyRequestBuilder->create()->withAnnotation($methodAnnotation)->withClass($originalClass)->withMethod($method)->build()); foreach ($response->getMethods() as $methodToAdd) { $additionalMethods[$methodToAdd->getName()] = $methodToAdd; } foreach ($response->getProperties() as $propertyToAdd) { $additionalProperties[$propertyToAdd->getName()] = $propertyToAdd; } $preSource .= $response->getPreSource(); $postSource .= $response->getPostSource(); $exceptionSource .= $response->getExceptionSource(); } } $classGenerator->addMethodFromGenerator($this->generateProxyMethod($method, $preSource, $postSource, $exceptionSource)); } $classGenerator->setImplementedInterfaces($additionalInterfaces); $classGenerator->addProperties($additionalProperties); $classGenerator->addMethods($additionalMethods); }
/** * {@inheritDoc} */ public function setImplementedInterfaces(array $interfaces) : self { foreach ($interfaces as &$interface) { $interface = '\\' . trim($interface, '\\'); } return parent::setImplementedInterfaces($interfaces); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { $interfaces = array('ProxyManager\\Proxy\\GhostObjectInterface'); $publicProperties = new PublicPropertiesMap($originalClass); $publicPropsDefaults = new PublicPropertiesDefaults($originalClass); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($initializationTracker = new InitializationTracker()); $classGenerator->addPropertyFromGenerator($publicProperties); $classGenerator->addPropertyFromGenerator($publicPropsDefaults); $init = new CallInitializer($initializer, $publicPropsDefaults, $initializationTracker); $classGenerator->addMethodFromGenerator($init); foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { $classGenerator->addMethodFromGenerator(LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $init)); } $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $initializer)); $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $initializer, $init, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $initializer, $init, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $initializer, $init, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $initializer, $init, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $initializer, $init)); $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $initializer, $init)); $classGenerator->addMethodFromGenerator(new SetProxyInitializer($initializer)); $classGenerator->addMethodFromGenerator(new GetProxyInitializer($initializer)); $classGenerator->addMethodFromGenerator(new InitializeProxy($initializer, $init)); $classGenerator->addMethodFromGenerator(new IsProxyInitialized($initializer)); }
public function fixInterfaces(Generator\ClassGenerator $class) { $interfaces = $class->getImplementedInterfaces(); if (in_array('Traversable', $interfaces) && in_array('IteratorAggregate', $interfaces)) { unset($interfaces[array_search('Traversable', $interfaces)]); } $class->setImplementedInterfaces($interfaces); return $class; }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass, false); $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces([AccessInterceptorInterface::class]); $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map($this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors), ProxiedMethodsFilter::getProxiedMethods($originalClass, ['__get', '__set', '__isset', '__unset', '__clone', '__sleep'])), [new StaticProxyConstructor($originalClass, $prefixInterceptors, $suffixInterceptors), new BindProxyProperties($originalClass, $prefixInterceptors, $suffixInterceptors), new SetMethodPrefixInterceptor($prefixInterceptors), new SetMethodSuffixInterceptor($suffixInterceptors), new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors)])); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = array('ProxyManager\\Proxy\\NullObjectInterface'); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } $classGenerator->setImplementedInterfaces($interfaces); foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { $classGenerator->addMethodFromGenerator(NullObjectMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()))); } ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, new Constructor($originalClass)); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass, false); $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces(array('ProxyManager\\Proxy\\AccessInterceptorInterface')); $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodPrefixInterceptors()); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map(function (ReflectionMethod $method) use($prefixInterceptors, $suffixInterceptors) { return InterceptedMethod::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $prefixInterceptors, $suffixInterceptors); }, ProxiedMethodsFilter::getProxiedMethods($originalClass, array('__get', '__set', '__isset', '__unset', '__clone', '__sleep'))), array(new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors), new SetMethodPrefixInterceptor($prefixInterceptors), new SetMethodSuffixInterceptor($suffixInterceptors), new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors)))); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { $interfaces = array('ProxyManager\\Proxy\\NullObjectInterface'); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { foreach ($originalClass->getInterfaceNames() as $name) { $interfaces[] = $name; } } $classGenerator->setImplementedInterfaces($interfaces); foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { $classGenerator->addMethodFromGenerator(NullObjectMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()))); } $classGenerator->addMethodFromGenerator(new Constructor($originalClass)); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = [RemoteObjectInterface::class]; if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($adapter = new AdapterProperty()); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map(function (ReflectionMethod $method) use($adapter, $originalClass) : RemoteObjectMethod { return RemoteObjectMethod::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $adapter, $originalClass); }, ProxiedMethodsFilter::getProxiedMethods($originalClass, ['__get', '__set', '__isset', '__unset'])), [new StaticProxyConstructor($originalClass, $adapter), new MagicGet($originalClass, $adapter), new MagicSet($originalClass, $adapter), new MagicIsset($originalClass, $adapter), new MagicUnset($originalClass, $adapter)])); }
/** * Build generators * * @param State $state * @return void */ public function build(State $state) { $model = $state->getFormFactoryModel(); $generator = new ClassGenerator($model->getName()); $generator->setImplementedInterfaces(['FactoryInterface']); $generator->addUse('Doctrine\\ORM\\EntityManager'); $generator->addUse('DoctrineModule\\Stdlib\\Hydrator\\DoctrineObject'); $generator->addUse('Zend\\ServiceManager\\FactoryInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $generator->addUse('Zend\\Form\\Form'); $generator->addUse('Zend\\Form\\Element'); $generator->addUse('Zend\\InputFilter\\Factory'); $generator->addUse('Zend\\InputFilter\\InputFilterInterface'); $this->buildCreateService($generator, $state); $this->buildGetInputFilter($generator, $state); $model->setGenerator($generator); }
/** * Generates a dynamic class implementing specified interfaces (and ServiceAwareInterface). * @return string class name */ public function build() { $generator = new ClassGenerator(uniqid('Proxy_'), 'SoPhp\\Rpc\\Proxy'); $generator->setImplementedInterfaces($this->getImplements()); $generator->setExtendedClass('\\SoPhp\\Rpc\\Proxy\\ProxyAbstract'); $source = $generator->generate(); $className = $generator->getNamespaceName() . '\\' . $generator->getName(); try { eval($source); } catch (\Exception $e) { throw new BuildFailed("Could not evaluate source code for proxy. " . $source, 0, $e); } if (!class_exists($className, false)) { throw new BuildFailed("Proxy class `{$className}` does not exist"); } return $className; }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = [VirtualProxyInterface::class]; $publicProperties = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass)); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty()); $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($publicProperties); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map($this->buildLazyLoadingMethodInterceptor($initializer, $valueHolder), ProxiedMethodsFilter::getProxiedMethods($originalClass)), [new StaticProxyConstructor($initializer, Properties::fromReflectionClass($originalClass)), Constructor::generateMethod($originalClass, $valueHolder), new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties), new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties), new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties), new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties), new MagicClone($originalClass, $initializer, $valueHolder), new MagicSleep($originalClass, $initializer, $valueHolder), new MagicWakeup($originalClass), new SetProxyInitializer($initializer), new GetProxyInitializer($initializer), new InitializeProxy($initializer, $valueHolder), new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder)])); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator, array $proxyOptions = []) { CanProxyAssertion::assertClassCanBeProxied($originalClass, false); $filteredProperties = Properties::fromReflectionClass($originalClass)->filter(isset($proxyOptions['skippedProperties']) ? $proxyOptions['skippedProperties'] : []); $publicProperties = new PublicPropertiesMap($filteredProperties); $privateProperties = new PrivatePropertiesMap($filteredProperties); $protectedProperties = new ProtectedPropertiesMap($filteredProperties); $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces([GhostObjectInterface::class]); $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($initializationTracker = new InitializationTracker()); $classGenerator->addPropertyFromGenerator($publicProperties); $classGenerator->addPropertyFromGenerator($privateProperties); $classGenerator->addPropertyFromGenerator($protectedProperties); $init = new CallInitializer($initializer, $initializationTracker, $filteredProperties); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge($this->getAbstractProxiedMethods($originalClass), [$init, new StaticProxyConstructor($initializer, $filteredProperties), new MagicGet($originalClass, $initializer, $init, $publicProperties, $protectedProperties, $privateProperties, $initializationTracker), new MagicSet($originalClass, $initializer, $init, $publicProperties, $protectedProperties, $privateProperties), new MagicIsset($originalClass, $initializer, $init, $publicProperties, $protectedProperties, $privateProperties), new MagicUnset($originalClass, $initializer, $init, $publicProperties, $protectedProperties, $privateProperties), new MagicClone($originalClass, $initializer, $init, $publicProperties), new MagicSleep($originalClass, $initializer, $init, $publicProperties), new SetProxyInitializer($initializer), new GetProxyInitializer($initializer), new InitializeProxy($initializer, $init), new IsProxyInitialized($initializer)])); }
/** * Build generators * * @param State|\Scaffold\State $state * @return \Scaffold\State|void */ public function build(State $state) { $model = $this->model; $generator = new ClassGenerator($model->getName()); $generator->addUse('Zend\\ServiceManager\\FactoryInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceManager'); $generator->addUse($state->getServiceModel()->getName()); $generator->setImplementedInterfaces(['FactoryInterface']); $method = new MethodGenerator('createService'); $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface')); $method->setBody('return new ' . $state->getServiceModel()->getClassName() . '($serviceLocator);'); $doc = new DocBlockGenerator('Create service'); $doc->setTag(new Tag\GenericTag('param', 'ServiceLocatorInterface|ServiceManager $serviceLocator')); $doc->setTag(new Tag\GenericTag('return', $state->getServiceModel()->getClassName())); $method->setDocBlock($doc); $generator->addMethodFromGenerator($method); $model->setGenerator($generator); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = array('ProxyManager\\Proxy\\VirtualProxyInterface'); $publicProperties = new PublicPropertiesMap($originalClass); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty()); $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($publicProperties); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map(function (ReflectionMethod $method) use($initializer, $valueHolder) { return LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $valueHolder); }, ProxiedMethodsFilter::getProxiedMethods($originalClass)), array(new Constructor($originalClass, $initializer), new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties), new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties), new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties), new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties), new MagicClone($originalClass, $initializer, $valueHolder), new MagicSleep($originalClass, $initializer, $valueHolder), new MagicWakeup($originalClass), new SetProxyInitializer($initializer), new GetProxyInitializer($initializer), new InitializeProxy($initializer, $valueHolder), new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder)))); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { $interfaces = array('ProxyManager\\Proxy\\RemoteObjectInterface'); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($adapter = new AdapterProperty()); $methods = ProxiedMethodsFilter::getProxiedMethods($originalClass, array('__get', '__set', '__isset', '__unset')); foreach ($methods as $method) { $classGenerator->addMethodFromGenerator(RemoteObjectMethod::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $adapter, $originalClass)); } $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $adapter)); $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $adapter)); $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $adapter)); $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $adapter)); $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $adapter)); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $publicProperties = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass)); $interfaces = [AccessInterceptorInterface::class, ValueHolderInterface::class]; if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty()); $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); $classGenerator->addPropertyFromGenerator($publicProperties); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map(function (ReflectionMethod $method) use($prefixInterceptors, $suffixInterceptors, $valueHolder) { return InterceptedMethod::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $valueHolder, $prefixInterceptors, $suffixInterceptors); }, ProxiedMethodsFilter::getProxiedMethods($originalClass)), [Constructor::generateMethod($originalClass, $valueHolder), new StaticProxyConstructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), new GetWrappedValueHolderValue($valueHolder), new SetMethodPrefixInterceptor($prefixInterceptors), new SetMethodSuffixInterceptor($suffixInterceptors), new MagicGet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties), new MagicSet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties), new MagicIsset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties), new MagicUnset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties), new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), new MagicSleep($originalClass, $valueHolder), new MagicWakeup($originalClass, $valueHolder)])); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { if ($originalClass->isInterface()) { throw InvalidProxiedClassException::interfaceNotSupported($originalClass); } $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces(array('ProxyManager\\Proxy\\AccessInterceptorInterface')); $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodPrefixInterceptors()); $methods = ProxiedMethodsFilter::getProxiedMethods($originalClass, array('__get', '__set', '__isset', '__unset', '__clone', '__sleep')); foreach ($methods as $method) { $classGenerator->addMethodFromGenerator(InterceptedMethod::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $prefixInterceptors, $suffixInterceptors)); } $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new SetMethodPrefixInterceptor($prefixInterceptors)); $classGenerator->addMethodFromGenerator(new SetMethodSuffixInterceptor($suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors)); $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors)); }
/** * Generate Madel class from metadata * * @param stdClass $metadata * @return Generator\ClassGenerator|null */ protected function generateModelFromMetadata($metadata) { $console = $this->getServiceLocator()->get('console'); $name = $metadata->Name; $ucName = ucfirst($name); // Create Api Class $class = new Generator\ClassGenerator($ucName, 'Mailjet\\Model'); $class->setImplementedInterfaces(array('ModelInterface'))->setDocBlock(new Generator\DocBlockGenerator($class->getName() . ' Model', $metadata->Description, array())); $this->addProperties($class, $metadata->Properties); // Create and Write Api Class File $file = new Generator\FileGenerator(); $file->setClass($class); $file->setDocBlock(new Generator\DocBlockGenerator('MailJet Model', self::LICENSE)); $file->setFilename(dirname(__DIR__) . '/Model/' . $class->getName() . '.php'); if (file_put_contents($file->getFilename(), $file->generate())) { $console->writeLine(sprintf("The Model '%s' has been created.", $class->getName()), Color::GREEN); return $class; } else { $console->writeLine(sprintf("There was an error during '%s' Model creation.", $class->getName()), Color::RED); } return $class; }
/** * Copied from ClassGenerator::fromReflection and tweaked slightly * @param ClassReflection $classReflection * * @return ClassGenerator */ public function getGeneratorFromReflection(ClassReflection $classReflection) { // class generator $cg = new ClassGenerator($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock()); $docblock->setIndentation(Generator::$indentation); $cg->setDocBlock($docblock); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); if ($parentClass) { $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\')); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $property = PropertyGenerator::fromReflection($reflectionProperty); $property->setIndentation(Generator::$indentation); $properties[] = $property; } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $method = MethodGenerator::fromReflection($reflectionMethod); $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody())); $method->setIndentation(Generator::$indentation); $methods[] = $method; } } $cg->addMethods($methods); return $cg; }
/** * @param Tag $tagItem * @return ClassGenerator */ protected function createClass(Tag $tagItem) { $className = ucfirst($this->stringToCamelCaseConverter->convert($tagItem->getName())); $class = new ClassGenerator(); $class->setNamespaceName('Flex\\Code\\Html\\Tag'); $class->setName($className); $class->addUse('Flex\\Code\\Html\\Tag\\Model\\AbstractTag'); $class->setExtendedClass('AbstractTag'); $implementedInterfaces = []; if ($tagItem->isGlobalAttributeAware()) { $implementedInterfaces[] = 'GlobalAttributeAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Attribute\\GlobalAttributeAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Attribute\\GlobalAttributeAwareTrait'); $class->addTrait('GlobalAttributeAwareTrait'); } if ($tagItem->isClipboardEventAware()) { $implementedInterfaces[] = 'ClipboardEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\ClipboardEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\ClipboardEventAwareTrait'); $class->addTrait('ClipboardEventAwareTrait'); } if ($tagItem->isFormEventAware()) { $implementedInterfaces[] = 'FormEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\FormEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\FormEventAwareTrait'); $class->addTrait('FormEventAwareTrait'); } if ($tagItem->isKeyboardEventAware()) { $implementedInterfaces[] = 'KeyboardEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\KeyboardEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\KeyboardEventAwareTrait'); $class->addTrait('KeyboardEventAwareTrait'); } if ($tagItem->isMediaEventAware()) { $implementedInterfaces[] = 'MediaEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\MediaEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\MediaEventAwareTrait'); $class->addTrait('MediaEventAwareTrait'); } if ($tagItem->isMiscEventAware()) { $implementedInterfaces[] = 'MiscEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\MiscEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\MiscEventAwareTrait'); $class->addTrait('MiscEventAwareTrait'); } if ($tagItem->isMouseEventAware()) { $implementedInterfaces[] = 'MouseEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\MouseEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\MouseEventAwareTrait'); $class->addTrait('MouseEventAwareTrait'); } if ($tagItem->isWindowEventAware()) { $implementedInterfaces[] = 'WindowEventAwareInterface'; $class->addUse('Flex\\Code\\Html\\Tag\\Event\\WindowEventAwareInterface'); $class->addUse('Flex\\Code\\Html\\Tag\\Event\\WindowEventAwareTrait'); $class->addTrait('WindowEventAwareTrait'); } $class->setImplementedInterfaces($implementedInterfaces); $docBlock = new DocBlockGenerator(); $docBlock->setTag(new GenericTag('author', 'elnebuloso/flex-code-html-generator')); $docBlock->setTag(new GenericTag('link', $tagItem->getLink())); if (!is_null($tagItem->getShortDescription())) { $docBlock->setShortDescription($tagItem->getShortDescription()); } if (!is_null($tagItem->getLongDescription())) { $docBlock->setLongDescription($tagItem->getLongDescription()); } $class->setDocBlock($docBlock); return $class; }
public function testImplementedInterfacesAccessors() { $classGenerator = new ClassGenerator(); $classGenerator->setImplementedInterfaces(array('Class1', 'Class2')); $this->assertEquals($classGenerator->getImplementedInterfaces(), array('Class1', 'Class2')); }
/** * Create view helper factory * * @return bool */ public function createViewHelperFactory() { // get needed options to shorten code $moduleName = $this->requestOptions->getModuleName(); $viewHelperClass = $this->requestOptions->getViewHelperClass(); $viewHelperPath = $this->requestOptions->getViewHelperPath(); $factoryClass = $viewHelperClass . 'Factory'; $factoryFile = $factoryClass . '.php'; $factoryFilePath = $viewHelperPath . $factoryFile; // check for factory class if (file_exists($factoryFilePath)) { throw new GeneratorException('The factory for this view helper exists already.'); } // create controller class with class generator $code = new ClassGenerator(); $code->setNamespaceName($moduleName . '\\View\\Helper'); $code->addUse('Zend\\ServiceManager\\FactoryInterface'); $code->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $code->setName($factoryClass); $code->setImplementedInterfaces(array('FactoryInterface')); $code->addMethodFromGenerator($this->generateCreateServiceMethod($viewHelperClass, 'viewHelperManager', 'viewHelper')); // add optional doc block if ($this->flagCreateApiDocs) { $code->setDocBlock(new DocBlockGenerator('Factory for ' . $viewHelperClass, 'Please add a proper description for the ' . $viewHelperClass . ' factory', array($this->generatePackageTag($moduleName)))); } // create file with file generator $file = new FileGenerator(); $file->setClass($code); // add optional doc block if ($this->flagCreateApiDocs) { $file->setDocBlock(new DocBlockGenerator('This file was generated by FrilleZFTool.', null, array($this->generatePackageTag($moduleName), $this->generateSeeTag()))); } // write controller class if (!file_put_contents($factoryFilePath, $file->generate())) { return false; } return true; }
private function buildProxyClass(string $entityInterfaceName, string $proxyNamespace, string $proxyClassName) : string { $reflectionClass = new ReflectionClass($entityInterfaceName); if (!$reflectionClass->isInterface()) { throw InvalidInterfaceException::fromInvalidInterface($entityInterfaceName); } $classGenerator = new ClassGenerator(); $classGenerator->setNamespaceName($proxyNamespace); $classGenerator->setName($proxyClassName); $classGenerator->setImplementedInterfaces([$entityInterfaceName, ProxyInterface::class]); $classGenerator->addProperty('initializer', null, PropertyGenerator::FLAG_PRIVATE); $classGenerator->addProperty('relationId', null, PropertyGenerator::FLAG_PRIVATE); $classGenerator->addProperty('realEntity', null, PropertyGenerator::FLAG_PRIVATE); $constructorGenerator = new MethodGenerator('__construct', [['name' => 'initializer', 'type' => 'callable'], ['name' => 'relationId']]); $constructorGenerator->setBody(' $this->initializer = $initializer; $this->relationId = $relationId; '); $classGenerator->addMethodFromGenerator($constructorGenerator); $getRelationIdGenerator = new MethodGenerator('__getRelationId'); $getRelationIdGenerator->setBody(' return $this->relationId; '); $classGenerator->addMethodFromGenerator($getRelationIdGenerator); $getRealEntityGenerator = new MethodGenerator('__getRealEntity'); $getRealEntityGenerator->setBody(' if (null === $this->realEntity) { $this->realEntity = ($this->initializer)(); \\Assert\\Assertion::isInstanceOf($this->realEntity, \\' . $entityInterfaceName . '::class); }; return $this->realEntity; '); $classGenerator->addMethodFromGenerator($getRealEntityGenerator); foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { $parameters = []; $parameterGenerators = []; $returnType = $reflectionMethod->getReturnType(); foreach ($reflectionMethod->getParameters() as $reflectionParameter) { $parameterGenerator = new ParameterGenerator($reflectionParameter->getName(), $reflectionParameter->getType(), $reflectionParameter->isDefaultValueAvailable() ? $reflectionParameter->getDefaultValue() : null); $parameterGenerator->setVariadic($reflectionParameter->isVariadic()); $parameterGenerators[] = $parameterGenerator; if ($reflectionParameter->isVariadic()) { $parameters[] = '...$' . $reflectionParameter->getName(); } else { $parameters[] = '$' . $reflectionParameter->getName(); } } $methodGenerator = new MethodGenerator(); $methodGenerator->setName($reflectionMethod->getName()); $methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); $methodGenerator->setParameters($parameterGenerators); $methodGenerator->setReturnType($returnType); $body = ' if (null === $this->realEntity) { $this->realEntity = ($this->initializer)(); \\Assert\\Assertion::isInstanceOf($this->realEntity, \\' . $entityInterfaceName . '::class); }; '; if ('void' !== $returnType) { $body .= 'return '; } $body .= '$this->realEntity->' . $reflectionMethod->getName() . '(' . implode(', ', $parameters) . ');'; $methodGenerator->setBody($body); $classGenerator->addMethodFromGenerator($methodGenerator); } $fileGenerator = new FileGenerator(); $fileGenerator->setClass($classGenerator); $filename = null === $this->proxyFolder ? tempnam(sys_get_temp_dir(), $proxyClassName) : sprintf('%s/%s.php', $this->proxyFolder, $proxyClassName); $fileGenerator->setFilename($filename); $fileGenerator->write(); return $filename; }
private function buildClass($replacement) { $type = $this->splitNsandClass($replacement['originalFullyQualifiedType']); $class = new ClassGenerator(); $class->setName($type['class']); $class->setNamespaceName($type['ns']); $class->setExtendedClass('\\Brads\\Ppm\\Proxy'); $properties = []; $methods = []; $implementedInterfaces = []; foreach ($versions as $version => $info) { foreach ($info['files'] as $file) { echo "Parsing: " . $this->vendorDir . '/' . $package . '/' . $version . '/' . $file . "\n"; $parsedFile = new ReflectionFile($this->vendorDir . '/' . $package . '/' . $version . '/' . $file); $parsedClass = $parsedFile->getFileNamespace($info['toNs'])->getClass($info['toNs'] . '\\' . $type['class']); if ($parsedClass->getInterfaceNames() != null) { $implementedInterfaces = array_merge($implementedInterfaces, $parsedClass->getInterfaceNames()); } foreach ($parsedClass->getMethods() as $method) { if ($method->isPublic()) { $generatedMethod = new MethodGenerator(); $generatedMethod->setName($method->name); $generatedMethod->setBody('echo "Hello world!";'); $generatedMethod->setAbstract($method->isAbstract()); $generatedMethod->setFinal($method->isFinal()); $generatedMethod->setStatic($method->isStatic()); $generatedMethod->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); foreach ($method->getParameters() as $param) { $generatedParam = new ParameterGenerator(); $generatedParam->setName($param->name); if ($param->hasType()) { $generatedParam->setType($param->getType()); } //$generatedParam->setDefaultValue($param->getDefaultValue()); $generatedParam->setPosition($param->getPosition()); $generatedParam->setVariadic($param->isVariadic()); $generatedParam->setPassedByReference($param->isPassedByReference()); $generatedMethod->setParameter($generatedParam); } $existingMethod = Linq::from($methods)->firstOrDefault(null, function (MethodGenerator $v) use($method) { return $v->getName() == $method->name; }); if ($existingMethod != null) { $existingParams = $existingMethod->getParameters(); foreach ($generatedMethod->getParameters() as $newParam) { $existingParam = Linq::from($existingParams)->firstOrDefault(function (ParameterGenerator $v) { return $v->getName() == $newParam->getName(); }); if ($existingParam == null) { $existingMethod->setParameter($newParam); } } } else { $methods[] = $generatedMethod; } } } foreach ($parsedClass->getProperties() as $prop) { //$properties[] = PropertyGenerator::fromReflection($prop); } } } $class->setImplementedInterfaces($implementedInterfaces); $class->addMethods($methods); $class->addProperties($properties); return (new FileGenerator(['classes' => [$class]]))->generate(); }
protected function createRealCommandQuery($commandPath, $name, $moduleName) { $this->directoryService->makeSureDirExist($commandPath); $this->directoryService->createAllNamespacedDir($name, $commandPath); $nameParts = explode('/', trim($name, '/')); $className = sprintf('%s%s', $nameParts[count($nameParts) - 1], $this->getSuffixClass()); $handlerName = sprintf('%s%s', $className, 'Handler'); $handlerFactoryName = sprintf('%s%s', $handlerName, 'Factory'); unset($nameParts[count($nameParts) - 1]); //Set namespace for generated files $namespace = sprintf('%s\\%s\\%s', $moduleName, $this->getDirectory(), implode('\\', $nameParts)); $classGenerator = new ClassGenerator(); $classGenerator->setNamespaceName(trim($namespace, '\\')); $handlerGenerator = new ClassGenerator(); $handlerGenerator->setNamespaceName(trim($namespace, '\\')); $handlerFactoryGenerator = new ClassGenerator(); $handlerFactoryGenerator->setNamespaceName(trim($namespace, '\\')); //Set basic properties for command $commandQueryInterfaceToImplement = $this->getCommandQueryInterfaceToImplement(); $classGenerator->setName($className); // $classGenerator->addUse($commandQueryInterfaceToImplement); $tmpRef = new \ReflectionClass($commandQueryInterfaceToImplement); $classGenerator->setImplementedInterfaces([$tmpRef->getName()]); $this->addMethodsFromInterface($commandQueryInterfaceToImplement, $classGenerator); //Set basic properties for command handler $commandHandlerClassToImplement = $this->getAbstractHandlerClassName(); $tmpRef = new \ReflectionClass($commandHandlerClassToImplement); $handlerGenerator->setName($handlerName); $handlerGenerator->setExtendedClass($tmpRef->getName()); $this->addMethodsFromAbstractClass($commandHandlerClassToImplement, $handlerGenerator); //Set basic properties for command handler factory $commandHandlerFactoryClassToImplement = FactoryInterface::class; $handlerFactoryGenerator->setName($handlerFactoryName); $handlerFactoryGenerator->addUse($commandHandlerFactoryClassToImplement); $handlerFactoryGenerator->setImplementedInterfaces([FactoryInterface::class]); $this->addMethodsFromInterface($commandHandlerFactoryClassToImplement, $handlerFactoryGenerator); // $method = $handlerFactoryGenerator->getMethod('__invoke'); //// $method->setParameters() // $method->setBody(sprintf('return new %s();', $handlerGenerator->getName())); //GENERATE IT !!! $fileGenerator = FileGenerator::fromArray(['classes' => [$classGenerator]]); file_put_contents(sprintf('%s%s%s%s%s%s', $commandPath, DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $nameParts), DIRECTORY_SEPARATOR, $className, '.php'), $fileGenerator->generate()); $fileGenerator = FileGenerator::fromArray(['classes' => [$handlerGenerator]]); file_put_contents(sprintf('%s%s%s%s%s%s', $commandPath, DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $nameParts), DIRECTORY_SEPARATOR, $handlerName, '.php'), $fileGenerator->generate()); $fileGenerator = FileGenerator::fromArray(['classes' => [$handlerFactoryGenerator]]); file_put_contents(sprintf('%s%s%s%s%s%s', $commandPath, DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $nameParts), DIRECTORY_SEPARATOR, $handlerFactoryName, '.php'), $fileGenerator->generate()); return [sprintf('%s\\%s', $classGenerator->getNamespaceName(), $classGenerator->getName()), sprintf('%s\\%s', $handlerGenerator->getNamespaceName(), $handlerGenerator->getName()), sprintf('%s\\%s', $handlerFactoryGenerator->getNamespaceName(), $handlerFactoryGenerator->getName())]; }
/** * {@inheritDoc} * @throws InvalidProxiedClassException * @throws InvalidArgumentException */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass, false); $annotation = null; $forceLazyInitProperty = new ForceLazyInitProperty(); $sessionBeansProperty = new SessionBeansProperty(); $postProcessorsProperty = new BeanPostProcessorsProperty(); $parameterValuesProperty = new ParameterValuesProperty(); $beanFactoryConfigurationProperty = new BeanFactoryConfigurationProperty(); $aliasesProperty = new AliasesProperty(); $getParameterMethod = new GetParameter($originalClass, $parameterValuesProperty); $wrapBeanAsLazyMethod = new WrapBeanAsLazy($originalClass, $beanFactoryConfigurationProperty); try { $reader = new AnnotationReader(); $annotation = $reader->getClassAnnotation($originalClass, Configuration::class); } catch (Exception $e) { throw new InvalidProxiedClassException($e->getMessage(), $e->getCode(), $e); } if (null === $annotation) { throw new InvalidProxiedClassException(sprintf('"%s" seems not to be a valid configuration class. @Configuration annotation missing!', $originalClass->getName())); } $classGenerator->setExtendedClass($originalClass->getName()); $classGenerator->setImplementedInterfaces([AliasContainerInterface::class]); $classGenerator->addPropertyFromGenerator($forceLazyInitProperty); $classGenerator->addPropertyFromGenerator($sessionBeansProperty); $classGenerator->addPropertyFromGenerator($postProcessorsProperty); $classGenerator->addPropertyFromGenerator($parameterValuesProperty); $classGenerator->addPropertyFromGenerator($beanFactoryConfigurationProperty); $classGenerator->addPropertyFromGenerator($aliasesProperty); $postProcessorMethods = []; $aliases = []; $methods = $originalClass->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED); foreach ($methods as $method) { if (null !== $reader->getMethodAnnotation($method, BeanPostProcessor::class)) { $postProcessorMethods[] = $method->getName(); continue; } /* @var \bitExpert\Disco\Annotations\Bean $beanAnnotation */ $beanAnnotation = $reader->getMethodAnnotation($method, Bean::class); if (null === $beanAnnotation) { throw new InvalidProxiedClassException(sprintf('Method "%s" on "%s" is missing the @Bean annotation!', $method->getName(), $originalClass->getName())); } // if alias is defined append it to the aliases list if ($beanAnnotation->getAlias() !== '' && !isset($aliases[$beanAnnotation->getAlias()])) { $aliases[$beanAnnotation->getAlias()] = $method->getName(); } /* @var \bitExpert\Disco\Annotations\Parameters $parametersAnnotation */ $parametersAnnotation = $reader->getMethodAnnotation($method, Parameters::class); if (null === $parametersAnnotation) { $parametersAnnotation = new Parameters(); } $beanType = $method->getReturnType(); if (null === $beanType) { throw new InvalidProxiedClassException(sprintf('Method "%s" on "%s" is missing the return typehint!', $method->getName(), $originalClass->getName())); } $beanType = (string) $beanType; if (!in_array($beanType, ['array', 'callable', 'bool', 'float', 'int', 'string']) && !class_exists($beanType) && !interface_exists($beanType)) { throw new InvalidProxiedClassException(sprintf('Return type of method "%s" on "%s" cannot be found! Did you use the full qualified name?', $method->getName(), $originalClass->getName())); } $methodReflection = new MethodReflection($method->class, $method->getName()); $proxyMethod = BeanMethod::generateMethod($methodReflection, $beanAnnotation, $parametersAnnotation, $beanType, $forceLazyInitProperty, $sessionBeansProperty, $postProcessorsProperty, $beanFactoryConfigurationProperty, $getParameterMethod, $wrapBeanAsLazyMethod); $classGenerator->addMethodFromGenerator($proxyMethod); } $aliasesProperty->setDefaultValue($aliases); $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $parameterValuesProperty, $sessionBeansProperty, $beanFactoryConfigurationProperty, $postProcessorsProperty, $postProcessorMethods)); $classGenerator->addMethodFromGenerator($wrapBeanAsLazyMethod); $classGenerator->addMethodFromGenerator($getParameterMethod); $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $sessionBeansProperty)); $classGenerator->addMethodFromGenerator(new GetAlias($originalClass, $aliasesProperty)); $classGenerator->addMethodFromGenerator(new HasAlias($originalClass, $aliasesProperty)); }