From aaf1715c957a3ae8ca5f1ccdaf968fe80c704e74 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 15 Jun 2018 11:53:20 -0400 Subject: [PATCH] Add delegator factory support to generator. --- .../Generator/GeneratorTools.php | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php b/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php index 9a29caee8f7..32c0d147cb6 100644 --- a/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php +++ b/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php @@ -82,6 +82,7 @@ class GeneratorTools // service or a class in a plugin manager. $cm = $container->get('ControllerManager'); $cpm = $container->get('ControllerPluginManager'); + $delegators = []; if ($container->has($class)) { $factory = $this->getFactoryFromContainer($container, $class); $configPath = ['service_manager']; @@ -94,6 +95,7 @@ class GeneratorTools $pmKey = $apmFactory->getConfigKey(get_class($pm)); $factory = $this->getFactoryFromContainer($pm, $class); $configPath = ['vufind', 'plugin_managers', $pmKey]; + $delegators = $this->getDelegatorsFromContainer($pm, $class); } // No factory found? Throw an error! @@ -106,8 +108,7 @@ class GeneratorTools // Create the custom factory only if requested. $newFactory = $extendFactory - ? $this->cloneFactory($factory, $target) - : $factory; + ? $this->cloneFactory($factory, $target) : $factory; // Finalize the local module configuration -- create a factory for the // new class, and set up the new class as an alias for the old class. @@ -118,6 +119,17 @@ class GeneratorTools // write operation is sufficient. $this->writeNewConfig($aliasPath, $newClass, $target, false); + // Clone/configure delegator factories as needed. + if (!empty($delegators)) { + $newDelegators = []; + foreach ($delegators as $delegator) { + $newDelegators[] = $extendFactory + ? $this->cloneFactory($delegator, $target) : $delegator; + } + $delegatorPath = array_merge($configPath, ['delegators', $newClass]); + $this->writeNewConfig($delegatorPath, $newDelegators, $target, false); + } + return true; } @@ -150,6 +162,36 @@ class GeneratorTools return $factories[$class] ?? null; } + /** + * Get a list of delegators in the provided container. + * + * @param ContainerInterface $container Container to inspect + * + * @return array + */ + protected function getAllDelegatorsFromContainer(ContainerInterface $container) + { + // There is no "getDelegators" method, so we need to use reflection: + $reflectionProperty = new \ReflectionProperty($container, 'delegators'); + $reflectionProperty->setAccessible(true); + return $reflectionProperty->getValue($container); + } + + /** + * Get delegators from the provided container (or empty array if undefined). + * + * @param ContainerInterface $container Container to inspect + * @param string $class Class whose delegators we want + * + * @return array + */ + protected function getDelegatorsFromContainer(ContainerInterface $container, + $class + ) { + $delegators = $this->getAllDelegatorsFromContainer($container); + return $delegators[$class] ?? []; + } + /** * Search all plugin managers for one containing the requested class (or return * null if none found). -- GitLab