/** * 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); }
/** * 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"); }
/** * Helper method to generate the method body for managing lazy bean instances. * * @param string $padding * @param string $beanId * @param string $beanType * @param Bean $beanMetadata * @param string $methodParams * @param ForceLazyInitProperty $forceLazyInitProperty * @param SessionBeansProperty $sessionBeansProperty * @param BeanPostProcessorsProperty $postProcessorsProperty * @param BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty * @return string */ protected static function generateLazyBeanCode(string $padding, string $beanId, string $beanType, Bean $beanMetadata, string $methodParams, ForceLazyInitProperty $forceLazyInitProperty, SessionBeansProperty $sessionBeansProperty, BeanPostProcessorsProperty $postProcessorsProperty, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty) : string { $content = ''; if ($beanMetadata->isSession()) { $content .= $padding . 'if($this->' . $sessionBeansProperty->getName() . '->has("' . $beanId . '")) {' . PHP_EOL; if ($beanMetadata->isSingleton()) { $content .= $padding . ' $sessionInstance = clone $this->' . $sessionBeansProperty->getName() . '->get("' . $beanId . '");' . PHP_EOL; } else { $content .= $padding . ' $sessionInstance = $this->' . $sessionBeansProperty->getName() . '->get("' . $beanId . '");' . PHP_EOL; } $content .= $padding . ' return $sessionInstance;' . PHP_EOL; $content .= $padding . '}' . PHP_EOL; } if ($beanMetadata->isSingleton()) { $content .= $padding . 'static $instance = null;' . PHP_EOL; $content .= $padding . 'if ($instance !== null) {' . PHP_EOL; $content .= $padding . ' return $instance;' . PHP_EOL; $content .= $padding . '}' . PHP_EOL; } $content .= $padding . '$factory = new \\' . LazyBeanFactory::class . '("' . $beanId . '", $this->' . $beanFactoryConfigurationProperty->getName() . '->getProxyManagerConfiguration());' . PHP_EOL; $content .= $padding . '$initializer = function (&$instance, \\' . LazyLoadingInterface::class . ' $proxy, $method, array $parameters, &$initializer) {' . PHP_EOL; $content .= $padding . ' try {' . PHP_EOL; $content .= $padding . ' $initializer = null;' . PHP_EOL; if ($beanMetadata->isSession()) { $content .= $padding . ' $backupForceLazyInit = $this->' . $forceLazyInitProperty->getName() . ';' . PHP_EOL; $content .= $padding . ' $this->' . $forceLazyInitProperty->getName() . ' = true;' . PHP_EOL; } $content .= $padding . self::generateBeanCreationCode($padding . ' ', $beanId, $methodParams, $postProcessorsProperty); if ($beanMetadata->isSession()) { $content .= $padding . ' $this->' . $forceLazyInitProperty->getName() . ' = $backupForceLazyInit;' . PHP_EOL; } $content .= $padding . ' } catch (\\Throwable $e) {' . PHP_EOL; $content .= $padding . ' $message = sprintf(' . PHP_EOL; $content .= $padding . ' \'Either return type declaration missing or unkown for bean with id "' . $beanId . '": %s\',' . PHP_EOL; $content .= $padding . ' $e->getMessage()' . PHP_EOL; $content .= $padding . ' );' . PHP_EOL; $content .= $padding . ' throw new \\' . BeanException::class . '($message, 0, $e);' . PHP_EOL; $content .= $padding . ' }' . PHP_EOL; $content .= $padding . ' return true;' . PHP_EOL; $content .= $padding . '};' . PHP_EOL; $content .= $padding . PHP_EOL; $content .= $padding . '$initializer->bindTo($this);' . PHP_EOL; $content .= $padding . '$instance = $factory->createProxy("' . $beanType . '", $initializer);' . PHP_EOL; if ($beanMetadata->isSession()) { $content .= $padding . '$this->' . $sessionBeansProperty->getName() . '->add("' . $beanId . '", $instance);' . PHP_EOL; } $content .= $padding . 'return $instance;' . PHP_EOL; return $content; }