diff --git a/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php b/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php index efee1b9..eef56db 100644 --- a/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php +++ b/core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php @@ -22,7 +22,30 @@ class ProxyBuilder { * The class name of the proxy. */ public static function buildProxyClassName($class_name) { - return str_replace('\\', '_', $class_name) . '_Proxy'; + $match = []; + preg_match('/([a-zA-Z0-9]+\\\\[a-zA-Z0-9]+)\\\\(.+)/', $class_name, $match); + $root_namespace = $match[1]; + $rest_fqcn = $match[2]; + $proxy_class_name = $root_namespace . '\\ProxyClass\\' . $rest_fqcn; + + return $proxy_class_name; + } + + /** + * Generates the used proxy namespace from a given class name. + * + * @param string $class_name + * The class name of the actual service. + * + * @return string + * The namespace name of the proxy. + */ + public static function buildProxyNamespace($class_name) { + $proxy_classname = static::buildProxyClassName($class_name); + + preg_match('/(.+)\\\\[a-zA-Z0-9]+/', $proxy_classname, $match); + $proxy_namespace = $match[1]; + return $proxy_namespace; } /** @@ -37,16 +60,29 @@ public static function buildProxyClassName($class_name) { public function build($class_name, $proxy_class_name = '') { $reflection = new \ReflectionClass($class_name); + if ($proxy_class_name) { + $proxy_class_reflection = new \ReflectionClass($proxy_class_name); + $proxy_namespace = $proxy_class_reflection->getNamespaceName(); + } + else { + $proxy_class_name = $this->buildProxyClassName($class_name); + $proxy_namespace = $this->buildProxyNamespace($class_name); + $proxy_class_shortname = str_replace($proxy_namespace . '\\', '', $proxy_class_name); + } + $output = ''; $class_documentation = <<<'EOS' -/** - * Provides a proxy class for \{{ class_name }}. - * - * @see \Drupal\Component\ProxyBuilder - */ + +namespace {{ namespace }}{ + + /** + * Provides a proxy class for \{{ class_name }}. + * + * @see \Drupal\Component\ProxyBuilder + */ EOS; - $class_start = 'class {{ proxy_class_name }}'; + $class_start = ' class {{ proxy_class_shortname }}'; // For cases in which the implemented interface is a child of another // interface, getInterfaceNames() also returns the parent. This causes a @@ -123,13 +159,14 @@ public function build($class_name, $proxy_class_name = '') { if ($value === '') { return $value; } - return " $value"; + return " $value"; }, explode("\n", $output))); - $final_output = $class_documentation . $class_start . "\n{\n\n" . $output . "\n}\n"; + $final_output = $class_documentation . $class_start . "\n {\n\n" . $output . "\n }\n\n}\n"; $final_output = str_replace('{{ class_name }}', $class_name, $final_output); - $final_output = str_replace('{{ proxy_class_name }}', $proxy_class_name ?: $this->buildProxyClassName($class_name), $final_output); + $final_output = str_replace('{{ namespace }}', $proxy_namespace ? $proxy_namespace . ' ' : '', $final_output); + $final_output = str_replace('{{ proxy_class_shortname }}', $proxy_class_shortname, $final_output); return $final_output; } @@ -144,7 +181,7 @@ protected function buildLazyLoadItselfMethod() { protected function lazyLoadItself() { if (!isset($this->service)) { - $method_name = 'get' . Container::camelize($this->serviceId) . 'Service'; + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; $this->service = $this->container->$method_name(false); } diff --git a/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php b/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php deleted file mode 100644 index 7126062..0000000 --- a/core/lib/Drupal/Component/ProxyBuilder/ProxyDumper.php +++ /dev/null @@ -1,78 +0,0 @@ -builder = $builder; - } - - /** - * {@inheritdoc} - */ - public function isProxyCandidate(Definition $definition) { - return $definition->isLazy() && ($class = $definition->getClass()) && class_exists($class); - } - - /** - * {@inheritdoc} - */ - public function getProxyFactoryCode(Definition $definition, $id) { - // Note: the specific get method is called initially with $lazyLoad=TRUE; - // When you want to retrieve the actual service, the code generated in - // ProxyBuilder calls the method with lazy loading disabled. - $output = <<<'EOS' - if ($lazyLoad) { - return $this->services['{{ id }}'] = new {{ class_name }}($this, '{{ id }}'); - } - -EOS; - $output = str_replace('{{ id }}', $id, $output); - $output = str_replace('{{ class_name }}', $this->builder->buildProxyClassName($definition->getClass()), $output); - - return $output; - } - - /** - * {@inheritdoc} - */ - public function getProxyCode(Definition $definition) { - // Maybe the same class is used in different services, which are both marked - // as lazy (just think about 2 database connections). - // In those cases we should not generate proxy code the second time. - if (!isset($this->buildClasses[$definition->getClass()])) { - $this->buildClasses[$definition->getClass()] = TRUE; - return $this->builder->build($definition->getClass()); - } - else { - return ''; - } - } - -} diff --git a/core/lib/Drupal/Core/Command/GenerateProxyClassApplication.php b/core/lib/Drupal/Core/Command/GenerateProxyClassApplication.php index 52132a4..c453eb4 100644 --- a/core/lib/Drupal/Core/Command/GenerateProxyClassApplication.php +++ b/core/lib/Drupal/Core/Command/GenerateProxyClassApplication.php @@ -8,9 +8,13 @@ namespace Drupal\Core\Command; use Drupal\Component\ProxyBuilder\ProxyBuilder; +use Drupal\Core\File\FileSystemInterface; use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputInterface; +/** + * Provides a console command to generate proxy classes. + */ class GenerateProxyClassApplication extends Application { /** @@ -21,13 +25,23 @@ class GenerateProxyClassApplication extends Application { protected $proxyBuilder; /** + * The file system. + * + * @var \Drupal\Core\File\FileSystemInterface + */ + protected $fileSystem; + + /** * Constructs a new GenerateProxyClassApplication instance. * * @param \Drupal\Component\ProxyBuilder\ProxyBuilder $proxy_builder * The proxy builder. + * @param \Drupal\Core\File\FileSystemInterface $file_system + * The file system. */ - public function __construct(ProxyBuilder $proxy_builder) { + public function __construct(ProxyBuilder $proxy_builder, FileSystemInterface $file_system) { $this->proxyBuilder = $proxy_builder; + $this->fileSystem = $file_system; parent::__construct(); } @@ -46,7 +60,7 @@ protected function getCommandName(InputInterface $input) { protected function getDefaultCommands() { // Even though this is a single command, keep the HelpCommand (--help). $default_commands = parent::getDefaultCommands(); - $default_commands[] = new GenerateProxyClassCommand($this->proxyBuilder); + $default_commands[] = new GenerateProxyClassCommand($this->proxyBuilder, $this->fileSystem); return $default_commands; } diff --git a/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php b/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php index 47da385..90b39c8 100644 --- a/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php +++ b/core/lib/Drupal/Core/Command/GenerateProxyClassCommand.php @@ -8,11 +8,15 @@ namespace Drupal\Core\Command; use Drupal\Component\ProxyBuilder\ProxyBuilder; +use Drupal\Core\File\FileSystemInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +/** + * Provides a console command to generate proxy classes. + */ class GenerateProxyClassCommand extends Command { /** @@ -23,18 +27,27 @@ class GenerateProxyClassCommand extends Command { protected $proxyBuilder; /** + * The file system. + * + * @var \Drupal\Core\File\FileSystemInterface + */ + protected $fileSystem; + + /** * Constructs a new GenerateProxyClassCommand instance. * * @param \Drupal\Component\ProxyBuilder\ProxyBuilder $proxy_builder * The proxy builder. + * @param \Drupal\Core\File\FileSystemInterface $file_system + * The file system. */ - public function __construct(ProxyBuilder $proxy_builder) { + public function __construct(ProxyBuilder $proxy_builder, FileSystemInterface $file_system) { parent::__construct(); $this->proxyBuilder = $proxy_builder; + $this->fileSystem = $file_system; } - /** * {@inheritdoc} */ @@ -63,9 +76,26 @@ protected function execute(InputInterface $input, OutputInterface $output) { $proxy_filename = $namespace_root . '/ProxyClass/' . str_replace('\\', '/', $rest_fqcn) . '.php'; $proxy_class_name = $root_namespace . '\\ProxyClass\\' . $rest_fqcn; - $proxy_class_string = $this->proxyBuilder->build($class_name, $proxy_class_name); - file_put_contents($proxy_filename, $proxy_class_string); + $proxy_class_string = $this->proxyBuilder->build($class_name); + + $file_string = <<<'EOS' +fileSystem->mkdir(dirname($proxy_filename), NULL, TRUE); + file_put_contents($proxy_filename, $file_string); $output->writeln(sprintf('Proxy of class %s written to %s', $class_name, $proxy_filename)); } diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php index 64a9aee..245db8f 100644 --- a/core/lib/Drupal/Core/CoreServiceProvider.php +++ b/core/lib/Drupal/Core/CoreServiceProvider.php @@ -10,6 +10,7 @@ use Drupal\Core\Cache\Context\CacheContextsPass; use Drupal\Core\Cache\ListCacheBinsPass; use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass; +use Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass; use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers; use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteFilters; use Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass; @@ -90,6 +91,8 @@ public function register(ContainerBuilder $container) { $container->addCompilerPass(new PluginManagerPass()); $container->addCompilerPass(new DependencySerializationTraitPass()); + + $container->addCompilerPass(new ProxyServicesPass()); } /** diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php new file mode 100644 index 0000000..f06722f --- /dev/null +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/ProxyServicesPass.php @@ -0,0 +1,33 @@ +getDefinitions() as $definition) { + if ($definition->isLazy()) { + $proxy_class = ProxyBuilder::buildProxyClassName($definition->getClass()); + if (class_exists($proxy_class)) { + $definition->setClass($proxy_class); + } + } + } + } + +} diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index b8eeb4f..bda91c5 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -8,7 +8,6 @@ namespace Drupal\Core; use Drupal\Component\FileCache\FileCacheFactory; -use Drupal\Component\ProxyBuilder\ProxyDumper; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; @@ -1180,7 +1179,6 @@ protected function dumpDrupalContainer(ContainerBuilder $container, $baseClass) } // Cache the container. $dumper = new PhpDumper($container); - $dumper->setProxyDumper(new ProxyDumper(new ProxyBuilder())); $class = $this->getClassName(); $namespace = $this->getClassNamespace(); $content = $dumper->dump([ diff --git a/core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php b/core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php new file mode 100644 index 0000000..7e1fcec --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php @@ -0,0 +1,84 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function load($id) + { + return $this->lazyLoadItself()->load($id); + } + + public function delete($id) + { + return $this->lazyLoadItself()->delete($id); + } + + public function update(array $batch) + { + return $this->lazyLoadItself()->update($batch); + } + + public function cleanup() + { + return $this->lazyLoadItself()->cleanup(); + } + + public function create(array $batch) + { + return $this->lazyLoadItself()->create($batch); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php b/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php new file mode 100644 index 0000000..78cfa04 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php @@ -0,0 +1,100 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function installDefaultConfig($type, $name) + { + return $this->lazyLoadItself()->installDefaultConfig($type, $name); + } + + public function installOptionalConfig(\Drupal\Core\Config\StorageInterface $storage = NULL, $dependency = array ( + )) + { + return $this->lazyLoadItself()->installOptionalConfig($storage, $dependency); + } + + public function installCollectionDefaultConfig($collection) + { + return $this->lazyLoadItself()->installCollectionDefaultConfig($collection); + } + + public function setSourceStorage(\Drupal\Core\Config\StorageInterface $storage) + { + return $this->lazyLoadItself()->setSourceStorage($storage); + } + + public function getSourceStorage() + { + return $this->lazyLoadItself()->getSourceStorage(); + } + + public function setSyncing($status) + { + return $this->lazyLoadItself()->setSyncing($status); + } + + public function isSyncing() + { + return $this->lazyLoadItself()->isSyncing(); + } + + public function checkConfigurationToInstall($type, $name) + { + return $this->lazyLoadItself()->checkConfigurationToInstall($type, $name); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Cron.php b/core/lib/Drupal/Core/ProxyClass/Cron.php new file mode 100644 index 0000000..28c1876 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Cron.php @@ -0,0 +1,64 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function run() + { + return $this->lazyLoadItself()->run(); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Entity/ContentUninstallValidator.php b/core/lib/Drupal/Core/ProxyClass/Entity/ContentUninstallValidator.php new file mode 100644 index 0000000..148afd2 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Entity/ContentUninstallValidator.php @@ -0,0 +1,69 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function validate($module) + { + return $this->lazyLoadItself()->validate($module); + } + + public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationInterface $translation) + { + return $this->lazyLoadItself()->setStringTranslation($translation); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php new file mode 100644 index 0000000..4e4f08c --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php @@ -0,0 +1,79 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function addUninstallValidator(\Drupal\Core\Extension\ModuleUninstallValidatorInterface $uninstall_validator) + { + return $this->lazyLoadItself()->addUninstallValidator($uninstall_validator); + } + + public function install(array $module_list, $enable_dependencies = true) + { + return $this->lazyLoadItself()->install($module_list, $enable_dependencies); + } + + public function uninstall(array $module_list, $uninstall_dependents = true) + { + return $this->lazyLoadItself()->uninstall($module_list, $uninstall_dependents); + } + + public function validateUninstall(array $module_list) + { + return $this->lazyLoadItself()->validateUninstall($module_list); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/File/MimeType/ExtensionMimeTypeGuesser.php b/core/lib/Drupal/Core/ProxyClass/File/MimeType/ExtensionMimeTypeGuesser.php new file mode 100644 index 0000000..e8d559f --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/File/MimeType/ExtensionMimeTypeGuesser.php @@ -0,0 +1,69 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function guess($path) + { + return $this->lazyLoadItself()->guess($path); + } + + public function setMapping(array $mapping = NULL) + { + return $this->lazyLoadItself()->setMapping($mapping); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php b/core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php new file mode 100644 index 0000000..25439bd --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php @@ -0,0 +1,74 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function guess($path) + { + return $this->lazyLoadItself()->guess($path); + } + + public function addGuesser(\Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $guesser, $priority = 0) + { + return $this->lazyLoadItself()->addGuesser($guesser, $priority); + } + + public static function registerWithSymfonyGuesser(\Symfony\Component\DependencyInjection\ContainerInterface $container) + { + \Drupal\Core\File\MimeType\MimeTypeGuesser::registerWithSymfonyGuesser($container); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php b/core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php new file mode 100644 index 0000000..9666c0a --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php @@ -0,0 +1,89 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function acquire($name, $timeout = 30) + { + return $this->lazyLoadItself()->acquire($name, $timeout); + } + + public function lockMayBeAvailable($name) + { + return $this->lazyLoadItself()->lockMayBeAvailable($name); + } + + public function release($name) + { + return $this->lazyLoadItself()->release($name); + } + + public function releaseAll($lock_id = NULL) + { + return $this->lazyLoadItself()->releaseAll($lock_id); + } + + public function wait($name, $delay = 30) + { + return $this->lazyLoadItself()->wait($name, $delay); + } + + public function getLockId() + { + return $this->lazyLoadItself()->getLockId(); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Lock/PersistentDatabaseLockBackend.php b/core/lib/Drupal/Core/ProxyClass/Lock/PersistentDatabaseLockBackend.php new file mode 100644 index 0000000..acd8996 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Lock/PersistentDatabaseLockBackend.php @@ -0,0 +1,89 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function acquire($name, $timeout = 30) + { + return $this->lazyLoadItself()->acquire($name, $timeout); + } + + public function lockMayBeAvailable($name) + { + return $this->lazyLoadItself()->lockMayBeAvailable($name); + } + + public function release($name) + { + return $this->lazyLoadItself()->release($name); + } + + public function releaseAll($lock_id = NULL) + { + return $this->lazyLoadItself()->releaseAll($lock_id); + } + + public function wait($name, $delay = 30) + { + return $this->lazyLoadItself()->wait($name, $delay); + } + + public function getLockId() + { + return $this->lazyLoadItself()->getLockId(); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/PageCache/ChainResponsePolicy.php b/core/lib/Drupal/Core/ProxyClass/PageCache/ChainResponsePolicy.php new file mode 100644 index 0000000..2776ef2 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/PageCache/ChainResponsePolicy.php @@ -0,0 +1,69 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function check(\Symfony\Component\HttpFoundation\Response $response, \Symfony\Component\HttpFoundation\Request $request) + { + return $this->lazyLoadItself()->check($response, $request); + } + + public function addPolicy(\Drupal\Core\PageCache\ResponsePolicyInterface $policy) + { + return $this->lazyLoadItself()->addPolicy($policy); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/ParamConverter/AdminPathConfigEntityConverter.php b/core/lib/Drupal/Core/ProxyClass/ParamConverter/AdminPathConfigEntityConverter.php new file mode 100644 index 0000000..451c5c1 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/ParamConverter/AdminPathConfigEntityConverter.php @@ -0,0 +1,69 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function convert($value, $definition, $name, array $defaults) + { + return $this->lazyLoadItself()->convert($value, $definition, $name, $defaults); + } + + public function applies($definition, $name, \Symfony\Component\Routing\Route $route) + { + return $this->lazyLoadItself()->applies($definition, $name, $route); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php b/core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php new file mode 100644 index 0000000..b9b194c --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php @@ -0,0 +1,69 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function convert($value, $definition, $name, array $defaults) + { + return $this->lazyLoadItself()->convert($value, $definition, $name, $defaults); + } + + public function applies($definition, $name, \Symfony\Component\Routing\Route $route) + { + return $this->lazyLoadItself()->applies($definition, $name, $route); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Plugin/CachedDiscoveryClearer.php b/core/lib/Drupal/Core/ProxyClass/Plugin/CachedDiscoveryClearer.php new file mode 100644 index 0000000..b7647e2 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Plugin/CachedDiscoveryClearer.php @@ -0,0 +1,69 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function addCachedDiscovery(\Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface $cached_discovery) + { + return $this->lazyLoadItself()->addCachedDiscovery($cached_discovery); + } + + public function clearCachedDefinitions() + { + return $this->lazyLoadItself()->clearCachedDefinitions(); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Render/BareHtmlPageRenderer.php b/core/lib/Drupal/Core/ProxyClass/Render/BareHtmlPageRenderer.php new file mode 100644 index 0000000..ca9ae49 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Render/BareHtmlPageRenderer.php @@ -0,0 +1,65 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function renderBarePage(array $content, $title, $page_theme_property, array $page_additions = array ( + )) + { + return $this->lazyLoadItself()->renderBarePage($content, $title, $page_theme_property, $page_additions); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php b/core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php new file mode 100644 index 0000000..03a274d --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Routing/MatcherDumper.php @@ -0,0 +1,75 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function addRoutes(\Symfony\Component\Routing\RouteCollection $routes) + { + return $this->lazyLoadItself()->addRoutes($routes); + } + + public function dump(array $options = array ( + )) + { + return $this->lazyLoadItself()->dump($options); + } + + public function getRoutes() + { + return $this->lazyLoadItself()->getRoutes(); + } + + } + +} diff --git a/core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php b/core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php new file mode 100644 index 0000000..51f57d8 --- /dev/null +++ b/core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php @@ -0,0 +1,79 @@ +container = $container; + $this->serviceId = $serviceId; + } + + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } + + public function setRebuildNeeded() + { + return $this->lazyLoadItself()->setRebuildNeeded(); + } + + public function rebuild() + { + return $this->lazyLoadItself()->rebuild(); + } + + public function rebuildIfNeeded() + { + return $this->lazyLoadItself()->rebuildIfNeeded(); + } + + public function destruct() + { + return $this->lazyLoadItself()->destruct(); + } + + } + +} diff --git a/core/scripts/generate-proxy-class.php b/core/scripts/generate-proxy-class.php index 974c41c..764f092 100644 --- a/core/scripts/generate-proxy-class.php +++ b/core/scripts/generate-proxy-class.php @@ -1,9 +1,7 @@ #!/usr/bin/env php boot(); // Run the database dump command. -$application = new GenerateProxyClassApplication(new ProxyBuilder()); +$application = new GenerateProxyClassApplication(new ProxyBuilder(), \Drupal::service('file_system')); $application->run(); diff --git a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php index a0f57fe..95069f8 100644 --- a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php +++ b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php @@ -37,7 +37,15 @@ protected function setUp() { */ public function testBuildProxyClassName() { $class_name = $this->proxyBuilder->buildProxyClassName('Drupal\Tests\Component\ProxyBuilder\TestServiceNoMethod'); - $this->assertEquals('Drupal_Tests_Component_ProxyBuilder_TestServiceNoMethod_Proxy', $class_name); + $this->assertEquals('Drupal\Tests\ProxyClass\Component\ProxyBuilder\TestServiceNoMethod', $class_name); + } + + /** + * @covers ::buildProxyNamespace + */ + public function testBuildProxyNamespace() { + $class_name = $this->proxyBuilder->buildProxyNamespace('Drupal\Tests\Component\ProxyBuilder\TestServiceNoMethod'); + $this->assertEquals('Drupal\Tests\ProxyClass\Component\ProxyBuilder', $class_name); } /** @@ -65,10 +73,10 @@ public function testBuildSimpleMethod() { $method_body = <<<'EOS' - public function method() - { - return $this->lazyLoadItself()->method(); - } +public function method() +{ + return $this->lazyLoadItself()->method(); +} EOS; $this->assertEquals($this->buildExpectedClass($class, $method_body), $result); @@ -86,10 +94,10 @@ public function testBuildMethodWithParameter() { $method_body = <<<'EOS' - public function methodWithParameter($parameter) - { - return $this->lazyLoadItself()->methodWithParameter($parameter); - } +public function methodWithParameter($parameter) +{ + return $this->lazyLoadItself()->methodWithParameter($parameter); +} EOS; $this->assertEquals($this->buildExpectedClass($class, $method_body), $result); @@ -108,11 +116,11 @@ public function testBuildComplexMethod() { // @todo Solve the silly linebreak for array() $method_body = <<<'EOS' - public function complexMethod($parameter, callable $function, \Drupal\Tests\Component\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array ( - )) - { - return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements); - } +public function complexMethod($parameter, callable $function, \Drupal\Tests\Component\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array ( +)) +{ + return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements); +} EOS; @@ -131,10 +139,10 @@ public function testBuildReturnReference() { // @todo Solve the silly linebreak for array() $method_body = <<<'EOS' - public function &returnReference() - { - return $this->lazyLoadItself()->returnReference(); - } +public function &returnReference() +{ + return $this->lazyLoadItself()->returnReference(); +} EOS; @@ -153,10 +161,10 @@ public function testBuildWithInterface() { $method_body = <<<'EOS' - public function testMethod($parameter) - { - return $this->lazyLoadItself()->testMethod($parameter); - } +public function testMethod($parameter) +{ + return $this->lazyLoadItself()->testMethod($parameter); +} EOS; @@ -189,10 +197,10 @@ public function testBuildWithProtectedAndPrivateMethod() { $method_body = <<<'EOS' - public function testMethod($parameter) - { - return $this->lazyLoadItself()->testMethod($parameter); - } +public function testMethod($parameter) +{ + return $this->lazyLoadItself()->testMethod($parameter); +} EOS; @@ -212,10 +220,10 @@ public function testBuildWithPublicStaticMethod() { // Ensure that the static method is not wrapped. $method_body = <<<'EOS' - public static function testMethod($parameter) - { - \Drupal\Tests\Component\ProxyBuilder\TestServiceWithPublicStaticMethod::testMethod($parameter); - } +public static function testMethod($parameter) +{ + \Drupal\Tests\Component\ProxyBuilder\TestServiceWithPublicStaticMethod::testMethod($parameter); +} EOS; @@ -232,53 +240,70 @@ public static function testMethod($parameter) * The code of the entire proxy. */ protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') { - $proxy_class = $this->proxyBuilder->buildProxyClassName($class); + $namespace = ProxyBuilder::buildProxyNamespace($class); + $reflection = new \ReflectionClass($class); + $proxy_class = $reflection->getShortName(); + $expected_string = <<<'EOS' -/** - * Provides a proxy class for \{{ class }}. - * - * @see \Drupal\Component\ProxyBuilder - */ -class {{ proxy_class }}{{ interface_string }} -{ - /** - * @var string - */ - protected $serviceId; +namespace {{ namespace }} { /** - * @var \{{ class }} - */ - protected $service; - - /** - * The service container. + * Provides a proxy class for \{{ class }}. * - * @var \Symfony\Component\DependencyInjection\ContainerInterface + * @see \Drupal\Component\ProxyBuilder */ - protected $container; - - public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $serviceId) + class {{ proxy_class }}{{ interface_string }} { - $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); + /** + * @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; } - return $this->service; - } + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } {{ expected_methods_body }} + } + } EOS; + + $expected_methods_body = implode("\n", array_map(function ($value) { + if ($value === '') { + return $value; + } + return " $value"; + }, explode("\n", $expected_methods_body))); + $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string); + $expected_string = str_replace('{{ namespace }}', $namespace, $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); diff --git a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php b/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php deleted file mode 100644 index 8285306..0000000 --- a/core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyDumperTest.php +++ /dev/null @@ -1,129 +0,0 @@ -proxyBuilder = $this->getMockBuilder('Drupal\Component\ProxyBuilder\ProxyBuilder') - ->disableOriginalConstructor() - ->setMethods(['build']) - ->getMock(); - $this->proxyDumper = new ProxyDumper($this->proxyBuilder); - } - - /** - * @dataProvider providerTestIsProxyCandidate - * @covers ::isProxyCandidate - */ - public function testIsProxyCandidate(Definition $definition, $expected) { - $this->assertSame($expected, $this->proxyDumper->isProxyCandidate($definition)); - } - - public function providerTestIsProxyCandidate() { - // Not lazy service. - $data = []; - $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestService'); - $data[] = [$definition, FALSE]; - // Not existing service. - $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestNotExistingService'); - $definition->setLazy(TRUE); - $data[] = [$definition, FALSE]; - // Existing and lazy service. - $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestService'); - $definition->setLazy(TRUE); - $data[] = [$definition, TRUE]; - - return $data; - } - - public function testGetProxyFactoryCode() { - $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestService'); - $definition->setLazy(TRUE); - - $result = $this->proxyDumper->getProxyFactoryCode($definition, 'test_service'); - - $expected = <<<'EOS' - if ($lazyLoad) { - return $this->services['test_service'] = new Drupal_Tests_Component_ProxyBuilder_TestService_Proxy($this, 'test_service'); - } - -EOS; - - $this->assertEquals($expected, $result); - } - - /** - * @covers ::getProxyCode - */ - public function testGetProxyCode() { - $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestService'); - $definition->setLazy(TRUE); - - $class = 'class Drupal_Tests_Component_ProxyBuilder_TestService_Proxy {}'; - $this->proxyBuilder->expects($this->once()) - ->method('build') - ->with('Drupal\Tests\Component\ProxyBuilder\TestService') - ->willReturn($class); - - $result = $this->proxyDumper->getProxyCode($definition); - $this->assertEquals($class, $result); - } - - /** - * @covers ::getProxyCode - */ - public function testGetProxyCodeWithSameClassMultipleTimes() { - $definition = new Definition('Drupal\Tests\Component\ProxyBuilder\TestService'); - $definition->setLazy(TRUE); - - $class = 'class Drupal_Tests_Component_ProxyBuilder_TestService_Proxy {}'; - $this->proxyBuilder->expects($this->once()) - ->method('build') - ->with('Drupal\Tests\Component\ProxyBuilder\TestService') - ->willReturn($class); - - $result = $this->proxyDumper->getProxyCode($definition); - $this->assertEquals($class, $result); - - $result = $this->proxyDumper->getProxyCode($definition); - $this->assertEquals('', $result); - } - -} - -class TestService { - -} diff --git a/core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php b/core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php index f04b75a..1c39d7a 100644 --- a/core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php @@ -44,11 +44,11 @@ public function testBuildComplexMethod() { // @todo Solve the silly linebreak for array() $method_body = <<<'EOS' - public function complexMethod($parameter, callable $function, \Drupal\Tests\Core\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array ( - )) - { - return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements); - } +public function complexMethod($parameter, callable $function, \Drupal\Tests\Core\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array ( +)) +{ + return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements); +} EOS; @@ -65,55 +65,71 @@ public function complexMethod($parameter, callable $function, \Drupal\Tests\Core * The code of the entire proxy. */ protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') { - $proxy_class = $this->proxyBuilder->buildProxyClassName($class); + $reflection = new \ReflectionClass($class); + $namespace = ProxyBuilder::buildProxyNamespace($class); + $proxy_class = $reflection->getShortName(); $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; +namespace {{ namespace }} { /** - * @var \{{ class }} - */ - protected $service; - - /** - * The service container. + * Provides a proxy class for \{{ class }}. * - * @var \Symfony\Component\DependencyInjection\ContainerInterface + * @see \Drupal\Component\ProxyBuilder */ - protected $container; - - public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $serviceId) + class {{ proxy_class }}{{ interface_string }} { - $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); + 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; } - return $this->service; - } + protected function lazyLoadItself() + { + if (!isset($this->service)) { + $method_name = 'get' . \Symfony\Component\DependencyInjection\Container::camelize($this->serviceId) . 'Service'; + $this->service = $this->container->$method_name(false); + } + + return $this->service; + } {{ expected_methods_body }} + } + } EOS; + + $expected_methods_body = implode("\n", array_map(function ($value) { + if ($value === '') { + return $value; + } + return " $value"; + }, explode("\n", $expected_methods_body))); + $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string); + $expected_string = str_replace('{{ namespace }}', $namespace, $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);