Skip to content
Snippets Groups Projects
Commit 8ac89fb6 authored by Demian Katz's avatar Demian Katz
Browse files

Add --extendfactory flag.

parent fc2c7731
No related merge requests found
......@@ -52,7 +52,7 @@ $config = [
$routes = [
'compile/theme' => 'compile theme [--force] [<source>] [<target>]',
'generate/dynamicroute' => 'generate dynamicroute [<name>] [<newController>] [<newAction>] [<module>]',
'generate/extendclass' => 'generate extendclass [<class>] [<target>]',
'generate/extendclass' => 'generate extendclass [--extendfactory] [<class>] [<target>]',
'generate/extendservice' => 'generate extendservice [<source>] [<target>]',
'generate/nontabrecordaction' => 'generate nontabrecordaction [<newAction>] [<module>]',
'generate/recordroute' => 'generate recordroute [<base>] [<newController>] [<module>]',
......
......@@ -98,10 +98,16 @@ class GenerateController extends AbstractBase
$request = $this->getRequest();
$class = $request->getParam('class');
$target = $request->getParam('target');
$extendFactory = $request->getParam('extendfactory');
if (empty($class) || empty($target)) {
Console::writeLine(
'Usage: ' . $request->getScriptName() . ' generate extendclass'
. ' [class_name] [target_module]'
. ' [--extendfactory] [class_name] [target_module]'
);
Console::writeLine(
"\t--extendfactory - optional switch; when set, subclass "
. 'the factory; otherwise, use existing factory'
);
Console::writeLine(
"\tclass_name - the name of the class you wish to extend"
......@@ -113,8 +119,9 @@ class GenerateController extends AbstractBase
}
try {
$this->getGeneratorTools()
->extendClass($this->serviceLocator, $class, $target);
$this->getGeneratorTools()->extendClass(
$this->serviceLocator, $class, $target, $extendFactory
);
} catch (\Exception $e) {
Console::writeLine($e->getMessage());
return $this->getFailureResponse();
......
......@@ -66,16 +66,18 @@ class GeneratorTools
* Extend a class defined somewhere in the service manager or its child
* plugin managers.
*
* @param ContainerInterface $container Service manager
* @param string $class Class name to extend
* @param string $target Target module in which to create new
* @param ContainerInterface $container Service manager
* @param string $class Class name to extend
* @param string $target Target module in which to create new
* service
* @param bool $extendFactory Should we extend the factory?
*
* @return bool
* @throws \Exception
*/
public function extendClass(ContainerInterface $container, $class, $target)
{
public function extendClass(ContainerInterface $container, $class, $target,
$extendFactory = false
) {
// Set things up differently depending on whether this is a top-level
// service or a class in a plugin manager.
if ($container->has($class)) {
......@@ -97,10 +99,15 @@ class GeneratorTools
// Create the custom subclass.
$newClass = $this->createSubclassInModule($class, $target);
// Create the custom factory only if requested.
$newFactory = $extendFactory
? $this->createSubclassInModule($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.
$factoryPath = array_merge($configPath, ['factories', $newClass]);
$this->writeNewConfig($factoryPath, $factory, $target);
$this->writeNewConfig($factoryPath, $newFactory, $target);
$aliasPath = array_merge($configPath, ['aliases', $class]);
// Don't back up the config twice -- the first backup from the previous
// write operation is sufficient.
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment