src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php
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 unknown 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;
    }
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 unknown 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;
    }
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