From b9dbab3e9d2b32dd816526bfd9c71922b0d819e3 Mon Sep 17 00:00:00 2001 From: Shane Auckland Date: Fri, 26 Jun 2015 12:51:52 +0100 Subject: [PATCH] Abstract common template generation code --- .../Generator/BlockGenerator.php | 42 +++++--------- .../Generator/ControllerGenerator.php | 19 ++++++- .../Generator/HelperGenerator.php | 42 +++++--------- .../Generator/MagentoObjectGenerator.php | 55 +++++++++++++++++++ .../Generator/ModelGenerator.php | 42 +++++--------- 5 files changed, 117 insertions(+), 83 deletions(-) create mode 100644 src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/MagentoObjectGenerator.php diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php index e1d1e32..cc70e5c 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php @@ -22,7 +22,6 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\BlockResource; -use PhpSpec\CodeGenerator\Generator\PromptingGenerator; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; use PhpSpec\Locator\ResourceInterface; @@ -34,7 +33,7 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class BlockGenerator extends PromptingGenerator implements GeneratorInterface +class BlockGenerator extends MagentoObjectGenerator implements GeneratorInterface { /** * @param ResourceInterface $resource @@ -71,39 +70,28 @@ protected function getFilePath(ResourceInterface $resource) * * @return string */ - protected function renderTemplate(ResourceInterface $resource, $filepath) + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) { - $values = array( - '%filepath%' => $filepath, - '%name%' => $resource->getName(), - '%extends%' => 'Mage_Core_Block_Abstract', - '%namespace%' => $resource->getSrcNamespace(), - '%namespace_block%' => '' !== $resource->getSrcNamespace() - ? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace()) - : '', + return sprintf( + "Magento block %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath ); + } - if (!$content = $this->getTemplateRenderer()->render('mage_block', $values)) { - $content = $this->getTemplateRenderer()->renderString( - file_get_contents(__DIR__ . '/templates/generic_class.template'), $values - ); - } - - return $content; + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Block_Abstract'; } /** - * @param ResourceInterface $resource - * @param string $filepath - * * @return string */ - protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + protected function getTemplateName() { - return sprintf( - "Magento block %s created in '%s'.\n", - $resource->getSrcClassname(), - $filepath - ); + return 'mage_block'; } } diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php index 03e3ee1..30ced3f 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php @@ -22,7 +22,6 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ControllerResource; -use PhpSpec\CodeGenerator\Generator\PromptingGenerator; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; use PhpSpec\Locator\ResourceInterface; /** @@ -33,7 +32,7 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class ControllerGenerator extends PromptingGenerator implements GeneratorInterface +class ControllerGenerator extends MagentoObjectGenerator implements GeneratorInterface { /** * @param ResourceInterface $resource @@ -105,4 +104,20 @@ protected function getGeneratedMessage(ResourceInterface $resource, $filepath) $filepath ); } + + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Controller_Front_Action'; + } + + /** + * @return string + */ + protected function getTemplateName() + { + return 'mage_controller'; + } } diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php index 28eac0c..b2ee97b 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php @@ -22,7 +22,6 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\HelperResource; -use PhpSpec\CodeGenerator\Generator\PromptingGenerator; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; use PhpSpec\Locator\ResourceInterface; /** @@ -33,7 +32,7 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class HelperGenerator extends PromptingGenerator implements GeneratorInterface +class HelperGenerator extends MagentoObjectGenerator implements GeneratorInterface { /** * @param ResourceInterface $resource @@ -70,39 +69,28 @@ protected function getFilePath(ResourceInterface $resource) * * @return string */ - protected function renderTemplate(ResourceInterface $resource, $filepath) + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) { - $values = array( - '%filepath%' => $filepath, - '%name%' => $resource->getName(), - '%extends%' => 'Mage_Core_Helper_Abstract', - '%namespace%' => $resource->getSrcNamespace(), - '%namespace_block%' => '' !== $resource->getSrcNamespace() - ? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace()) - : '', + return sprintf( + "Magento helper %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath ); + } - if (!$content = $this->getTemplateRenderer()->render('mage_helper', $values)) { - $content = $this->getTemplateRenderer()->renderString( - file_get_contents(__DIR__ . '/templates/generic_class.template'), $values - ); - } - - return $content; + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Helper_Abstract'; } /** - * @param ResourceInterface $resource - * @param string $filepath - * * @return string */ - protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + protected function getTemplateName() { - return sprintf( - "Magento helper %s created in '%s'.\n", - $resource->getSrcClassname(), - $filepath - ); + return 'mage_helper'; } } diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/MagentoObjectGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/MagentoObjectGenerator.php new file mode 100644 index 0000000..b9c8c62 --- /dev/null +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/MagentoObjectGenerator.php @@ -0,0 +1,55 @@ + $filepath, + '%name%' => $resource->getName(), + '%extends%' => $this->getParentClass(), + '%namespace%' => $resource->getSrcNamespace(), + '%namespace_block%' => '' !== $resource->getSrcNamespace() + ? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace()) + : '', + ); + + if (!$content = $this->getTemplateRenderer()->render($this->getTemplateName(), $values)) { + $content = $this->getTemplateRenderer()->renderString( + file_get_contents(__DIR__ . $this->getTemplateFile()), $values + ); + } + + return $content; + } + + /** + * @return string + */ + abstract protected function getParentClass(); + + /** + * @return string + */ + abstract protected function getTemplateName(); + + /** + * @return string + */ + protected function getTemplateFile() + { + return '/templates/generic_class.template'; + } +} diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ModelGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ModelGenerator.php index 768bfcc..c7be50d 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ModelGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ModelGenerator.php @@ -22,7 +22,6 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ModelResource; -use PhpSpec\CodeGenerator\Generator\PromptingGenerator; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; use PhpSpec\Locator\ResourceInterface; /** @@ -33,7 +32,7 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class ModelGenerator extends PromptingGenerator implements GeneratorInterface +class ModelGenerator extends MagentoObjectGenerator implements GeneratorInterface { /** * @param ResourceInterface $resource @@ -70,39 +69,28 @@ protected function getFilePath(ResourceInterface $resource) * * @return string */ - protected function renderTemplate(ResourceInterface $resource, $filepath) + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) { - $values = array( - '%filepath%' => $filepath, - '%name%' => $resource->getName(), - '%extends%' => 'Mage_Core_Model_Abstract', - '%namespace%' => $resource->getSrcNamespace(), - '%namespace_block%' => '' !== $resource->getSrcNamespace() - ? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace()) - : '', + return sprintf( + "Magento model %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath ); + } - if (!$content = $this->getTemplateRenderer()->render('mage_model', $values)) { - $content = $this->getTemplateRenderer()->renderString( - file_get_contents(__DIR__ . '/templates/generic_class.template'), $values - ); - } - - return $content; + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Model_Abstract'; } /** - * @param ResourceInterface $resource - * @param string $filepath - * * @return string */ - protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + protected function getTemplateName() { - return sprintf( - "Magento model %s created in '%s'.\n", - $resource->getSrcClassname(), - $filepath - ); + return 'mage_model'; } }