diff --git a/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php b/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php index 0d9fa46..d75a897 100644 --- a/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php +++ b/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php @@ -53,6 +53,8 @@ public static function buildProxyNamespace($class_name) { * * @param string $class_name * The class name of the actual service. + * @param string $proxy_class_name + * (optional) The class name of the proxy service. * * @return string * The full string with namespace class and methods. @@ -113,11 +115,15 @@ public function build($class_name, $proxy_class_name = '') { // The actual class; $properties = <<<'EOS' /** + * The id of the original proxied service. + * * @var string */ -protected $serviceId; +protected $drupalProxyOriginalServiceId; /** + * The real proxied service, after it was lazy loaded. + * * @var \{{ class_name }} */ protected $service; @@ -178,10 +184,16 @@ public function build($class_name, $proxy_class_name = '') { */ protected function buildLazyLoadItselfMethod() { $output = <<<'EOS' +/** + * Lazy loads the real service from the container. + * + * @return object + * Returns the constructed real service. + */ protected function lazyLoadItself() { if (!isset($this->service)) { - $this->service = $this->container->get($this->serviceId); + $this->service = $this->container->get($this->drupalProxyOriginalServiceId); } return $this->service; @@ -213,11 +225,19 @@ protected function buildMethod(\ReflectionMethod $reflection_method) { if ($reflection_method->returnsReference()) { $reference = '&'; } + + $signature_line = <<<'EOS' +/** + * {@inheritdoc} + */ + +EOS; + if ($reflection_method->isStatic()) { - $signature_line = 'public static function ' . $reference . $function_name . '('; + $signature_line .= 'public static function ' . $reference . $function_name . '('; } else { - $signature_line = 'public function ' . $reference . $function_name . '('; + $signature_line .= 'public function ' . $reference . $function_name . '('; } $signature_line .= implode(', ', $parameters); @@ -305,10 +325,18 @@ protected function buildMethodBody(\ReflectionMethod $reflection_method) { */ protected function buildConstructorMethod() { $output = <<<'EOS' -public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $serviceId) +/** + * Constructs a ProxyClass Drupal proxy object. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container. + * @param string $drupal_proxy_original_service_id + * The service ID of the original service. + */ +public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id) { $this->container = $container; - $this->serviceId = $serviceId; + $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id; } EOS; diff --git a/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php b/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php index 9ee0a32..4fbe3e3 100644 --- a/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php +++ b/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php @@ -44,9 +44,12 @@ protected function configure() { $this->setName('generate-proxy-class') ->setDefinition([ new InputArgument('class_name', InputArgument::REQUIRED, 'The class to be proxied'), - new InputArgument('namespace_root', InputArgument::REQUIRED, 'The filepath to the root of the namespace.'), + new InputArgument('namespace_root_path', InputArgument::REQUIRED, 'The filepath to the root of the namespace.'), ]) - ->setDescription('Dumps a generated proxy class into its appropriate namespace.'); + ->setDescription('Dumps a generated proxy class into its appropriate namespace.') + ->addUsage('\'Drupal\Core\Batch\BatchStorage\' "core/lib/Drupal/Core"') + ->addUsage('\'Drupal\block\BlockRepository\' "core/modules/block/src"') + ->addUsage('\'Drupal\mymodule\MyClass\' "modules/contrib/mymodule/src"'); } /** @@ -54,7 +57,7 @@ protected function configure() { */ protected function execute(InputInterface $input, OutputInterface $output) { $class_name = $input->getArgument('class_name'); - $namespace_root = $input->getArgument('namespace_root'); + $namespace_root = $input->getArgument('namespace_root_path'); $match = []; preg_match('/([a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+)\\\\(.+)/', $class_name, $match); @@ -68,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $proxy_class_string = $this->proxyBuilder->build($class_name); - $file_string = <<<'EOS' + $file_string = <<setArguments([new Reference('service_container'), $new_service_id]); } else { - // @todo Replace this with an assertion once - // https://www.drupal.org/node/2408013 is in. - throw new InvalidArgumentException(sprintf('Missing proxy class %s for service %s', $proxy_class, $service_id)); + $class_name = $definition->getClass(); + + // Find the root namespace. + $match = []; + preg_match('/([a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+)\\\\(.+)/', $class_name, $match); + $root_namespace = $match[1]; + + // Find the root namespace path. + $root_namespace_dir = '[namespace_root_path]'; + + $namespaces = $container->getParameter('container.namespaces'); + + // Hardcode Drupal Core, because it is not registered. + $namespaces['Drupal\Core'] = 'core/lib/Drupal/Core'; + + if (isset($namespaces[$root_namespace])) { + $root_namespace_dir = $namespaces[$root_namespace]; + } + + $message =<<root . '/' . $paths; } - $this->classLoader->setPsr4($prefix . '\\', $paths); + $this->classLoader->addPsr4($prefix . '\\', $paths); } }