bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanMethod::generateMethod PHP Method

generateMethod() public static method

Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\BeanMethod}.
public static generateMethod ( Zend\Code\Reflection\MethodReflection $originalMethod, Bean $beanMetadata, Parameters $methodParameters, $beanType, ForceLazyInitProperty $forceLazyInitProperty, SessionBeansProperty $sessionBeansProperty, BeanPostProcessorsProperty $postProcessorsProperty, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty, GetParameter $parameterValuesMethod, WrapBeanAsLazy $wrapBeanAsLazy ) : BeanMethod | ProxyManager\Generator\MethodGenerator
$originalMethod Zend\Code\Reflection\MethodReflection
$beanMetadata bitExpert\Disco\Annotations\Bean
$methodParameters bitExpert\Disco\Annotations\Parameters
$beanType
$forceLazyInitProperty bitExpert\Disco\Proxy\Configuration\PropertyGenerator\ForceLazyInitProperty
$sessionBeansProperty bitExpert\Disco\Proxy\Configuration\PropertyGenerator\SessionBeansProperty
$postProcessorsProperty bitExpert\Disco\Proxy\Configuration\PropertyGenerator\BeanPostProcessorsProperty
$beanFactoryConfigurationProperty bitExpert\Disco\Proxy\Configuration\PropertyGenerator\BeanFactoryConfigurationProperty
$parameterValuesMethod GetParameter
$wrapBeanAsLazy WrapBeanAsLazy
return BeanMethod | ProxyManager\Generator\MethodGenerator
    public static function generateMethod(MethodReflection $originalMethod, Bean $beanMetadata, Parameters $methodParameters, $beanType, ForceLazyInitProperty $forceLazyInitProperty, SessionBeansProperty $sessionBeansProperty, BeanPostProcessorsProperty $postProcessorsProperty, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty, GetParameter $parameterValuesMethod, WrapBeanAsLazy $wrapBeanAsLazy)
    {
        $method = static::fromReflection($originalMethod);
        $methodParams = static::convertMethodParamsToString($methodParameters, $parameterValuesMethod);
        $beanId = $originalMethod->name;
        $body = '';
        if (in_array($beanType, ['array', 'callable', 'bool', 'float', 'int', 'string'], true)) {
            // return type is a primitive, simply call parent method and return immediately
            $body .= 'return parent::' . $beanId . '(' . $methodParams . ');' . PHP_EOL;
        } elseif (class_exists($beanType) || interface_exists($beanType)) {
            if ($beanMetadata->isLazy()) {
                $body = static::generateLazyBeanCode('', $beanId, $beanType, $beanMetadata, $methodParams, $forceLazyInitProperty, $sessionBeansProperty, $postProcessorsProperty, $beanFactoryConfigurationProperty);
            } else {
                $body = static::generateNonLazyBeanCode('', $beanId, $beanType, $beanMetadata, $methodParams, $forceLazyInitProperty, $sessionBeansProperty, $postProcessorsProperty, $wrapBeanAsLazy);
            }
        } else {
            // return type is unknown, throw an exception
            $body .= '$message = sprintf(' . PHP_EOL;
            $body .= '    \'Either return type declaration missing or unknown for bean with id "' . $beanId . '": %s\',' . PHP_EOL;
            $body .= '    $e->getMessage()' . PHP_EOL;
            $body .= ');' . PHP_EOL;
            $body .= 'throw new \\' . BeanException::class . '($message, 0, $e);' . PHP_EOL;
        }
        $method->setBody($body);
        $method->setDocBlock('{@inheritDoc}');
        return $method;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * {@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));
 }