예제 #1
0
    /**
     * Constructs the expected class output.
     *
     * @param string $expected_methods_body
     *   The expected body of decorated methods.
     *
     * @return string
     *   The code of the entire proxy.
     */
    protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '')
    {
        $proxy_class = $this->proxyBuilder->buildProxyClassName($class);
        $expected_string = <<<'EOS'
/**
 * Provides a proxy class for \{{ class }}.
 *
 * @see \Drupal\Component\ProxyBuilder
 */
class {{ proxy_class }}{{ interface_string }}
{

    use \Drupal\Core\DependencyInjection\DependencySerializationTrait;

    /**
     * @var string
     */
    protected $serviceId;

    /**
     * @var \{{ class }}
     */
    protected $service;

    /**
     * The service container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected $container;

    public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $serviceId)
    {
        $this->container = $container;
        $this->serviceId = $serviceId;
    }

    protected function lazyLoadItself()
    {
        if (!isset($this->service)) {
            $method_name = 'get' . Container::camelize($this->serviceId) . 'Service';
            $this->service = $this->container->$method_name(false);
        }

        return $this->service;
    }
{{ expected_methods_body }}
}

EOS;
        $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string);
        $expected_string = str_replace('{{ class }}', $class, $expected_string);
        $expected_string = str_replace('{{ expected_methods_body }}', $expected_methods_body, $expected_string);
        $expected_string = str_replace('{{ interface_string }}', $interface_string, $expected_string);
        return $expected_string;
    }