diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php index 51b9ef3..cc70e5c 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php @@ -22,10 +22,7 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\BlockResource; -use PhpSpec\Console\IO; -use PhpSpec\CodeGenerator\TemplateRenderer; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; -use PhpSpec\Util\Filesystem; use PhpSpec\Locator\ResourceInterface; /** @@ -36,66 +33,65 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class BlockGenerator implements GeneratorInterface +class BlockGenerator extends MagentoObjectGenerator implements GeneratorInterface { - private $io; - private $templates; - private $filesystem; - - public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null) - { - $this->io = $io; - $this->templates = $templates; - $this->filesystem = $filesystem ?: new Filesystem; - } - + /** + * @param ResourceInterface $resource + * @param string $generation + * @param array $data + * @return bool + */ public function supports(ResourceInterface $resource, $generation, array $data) { return 'class' === $generation && $resource instanceof BlockResource; } - public function generate(ResourceInterface $resource, array $data = array()) + /** + * @return int + */ + public function getPriority() { - $filepath = $resource->getSrcFilename(); - if ($this->filesystem->pathExists($filepath)) { - $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath)); - if (!$this->io->askConfirmation($message, false)) { - return; - } - - $this->io->writeln(); - } + return 30; + } - $path = dirname($filepath); - if (!$this->filesystem->isDirectory($path)) { - $this->filesystem->makeDirectory($path); - } + /** + * @param ResourceInterface $resource + * + * @return string + */ + protected function getFilePath(ResourceInterface $resource) + { + return $resource->getSrcFilename(); + } - $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()) - : '', + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + { + return sprintf( + "Magento block %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath ); + } - if (!$content = $this->templates->render('mage_block', $values)) { - $content = $this->templates->renderString( - file_get_contents(__DIR__ . '/templates/generic_class.template'), $values - ); - } - - $this->filesystem->putFileContents($filepath, $content); - $this->io->writeln(sprintf( - "Magento block %s created in '%s'.\n", - $resource->getSrcClassname(), $filepath - )); + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Block_Abstract'; } - public function getPriority() + /** + * @return string + */ + protected function getTemplateName() { - return 30; + return 'mage_block'; } } diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php index 94433c2..30ced3f 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php @@ -22,10 +22,7 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ControllerResource; -use PhpSpec\Console\IO; -use PhpSpec\CodeGenerator\TemplateRenderer; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; -use PhpSpec\Util\Filesystem; use PhpSpec\Locator\ResourceInterface; /** * ControllerGenerator @@ -35,41 +32,45 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class ControllerGenerator implements GeneratorInterface +class ControllerGenerator extends MagentoObjectGenerator implements GeneratorInterface { - private $io; - private $templates; - private $filesystem; - - public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null) - { - $this->io = $io; - $this->templates = $templates; - $this->filesystem = $filesystem ?: new Filesystem; - } - + /** + * @param ResourceInterface $resource + * @param string $generation + * @param array $data + * @return bool + */ public function supports(ResourceInterface $resource, $generation, array $data) { return 'class' === $generation && $resource instanceof ControllerResource; } - public function generate(ResourceInterface $resource, array $data = array()) + /** + * @return int + */ + public function getPriority() { - $filepath = $resource->getSrcFilename(); - if ($this->filesystem->pathExists($filepath)) { - $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath)); - if (!$this->io->askConfirmation($message, false)) { - return; - } - - $this->io->writeln(); - } + return 10; + } - $path = dirname($filepath); - if (!$this->filesystem->isDirectory($path)) { - $this->filesystem->makeDirectory($path); - } + /** + * @param ResourceInterface $resource + * + * @return string + */ + protected function getFilePath(ResourceInterface $resource) + { + return $resource->getSrcFilename(); + } + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function renderTemplate(ResourceInterface $resource, $filepath) + { $values = array( '%filepath%' => $filepath, '%name%' => $resource->getName(), @@ -80,21 +81,43 @@ public function generate(ResourceInterface $resource, array $data = array()) : '', ); - if (!$content = $this->templates->render('mage_controller', $values)) { - $content = $this->templates->renderString( + if (!$content = $this->getTemplateRenderer()->render('mage_controller', $values)) { + $content = $this->getTemplateRenderer()->renderString( file_get_contents(__DIR__ . '/templates/generic_class.template'), $values ); } - $this->filesystem->putFileContents($filepath, $content); - $this->io->writeln(sprintf( + return $content; + } + + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + { + return sprintf( "Magento controller %s created in '%s'.\n", - $resource->getSrcClassname(), $filepath - )); + $resource->getSrcClassname(), + $filepath + ); } - public function getPriority() + /** + * @return string + */ + protected function getParentClass() { - return 10; + return 'Mage_Core_Controller_Front_Action'; + } + + /** + * @return string + */ + protected function getTemplateName() + { + return 'mage_controller'; } } diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerSpecificationGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerSpecificationGenerator.php index 9b938ee..439022a 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerSpecificationGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerSpecificationGenerator.php @@ -2,47 +2,46 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; -use PhpSpec\Console\IO; -use PhpSpec\CodeGenerator\TemplateRenderer; -use PhpSpec\Util\Filesystem; +use PhpSpec\CodeGenerator\Generator\PromptingGenerator; use PhpSpec\Locator\ResourceInterface; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; -class ControllerSpecificationGenerator implements GeneratorInterface +class ControllerSpecificationGenerator extends PromptingGenerator implements GeneratorInterface { - private $io; - private $templates; - private $filesystem; - - public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null) - { - $this->io = $io; - $this->templates = $templates; - $this->filesystem = $filesystem ?: new Filesystem; - } - + /** + * @param ResourceInterface $resource + * @param string $generation + * @param array $data + * @return bool + */ public function supports(ResourceInterface $resource, $generation, array $data) { return 'controller_specification' === $generation; } - public function generate(ResourceInterface $resource, array $data = array()) + public function getPriority() { - $filepath = $resource->getSpecFilename(); - if ($this->filesystem->pathExists($filepath)) { - $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath)); - if (!$this->io->askConfirmation($message, false)) { - return; - } - - $this->io->writeln(); - } + return 0; + } - $path = dirname($filepath); - if (!$this->filesystem->isDirectory($path)) { - $this->filesystem->makeDirectory($path); - } + /** + * @param ResourceInterface $resource + * + * @return string + */ + protected function getFilePath(ResourceInterface $resource) + { + return $resource->getSpecFilename(); + } + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function renderTemplate(ResourceInterface $resource, $filepath) + { $values = array( '%filepath%' => $filepath, '%name%' => $resource->getSpecName(), @@ -50,21 +49,27 @@ public function generate(ResourceInterface $resource, array $data = array()) '%subject%' => $resource->getSrcClassname() ); - if (!$content = $this->templates->render('controller_specification', $values)) { - $content = $this->templates->renderString( + if (!$content = $this->getTemplateRenderer()->render('controller_specification', $values)) { + $content = $this->getTemplateRenderer()->renderString( file_get_contents(__DIR__ . '/templates/controller_spec.template'), $values ); } - $this->filesystem->putFileContents($filepath, $content); - $this->io->writeln(sprintf( - "ControllerSpecification for %s created in '%s'.\n", - $resource->getSrcClassname(), $filepath - )); + return $content; } - public function getPriority() + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) { - return 0; + sprintf( + "ControllerSpecification for %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath + ); } } diff --git a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php index 377846d..b2ee97b 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/HelperGenerator.php @@ -22,10 +22,7 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\HelperResource; -use PhpSpec\Console\IO; -use PhpSpec\CodeGenerator\TemplateRenderer; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; -use PhpSpec\Util\Filesystem; use PhpSpec\Locator\ResourceInterface; /** * HelperGenerator @@ -35,66 +32,65 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class HelperGenerator implements GeneratorInterface +class HelperGenerator extends MagentoObjectGenerator implements GeneratorInterface { - private $io; - private $templates; - private $filesystem; - - public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null) - { - $this->io = $io; - $this->templates = $templates; - $this->filesystem = $filesystem ?: new Filesystem; - } - + /** + * @param ResourceInterface $resource + * @param string $generation + * @param array $data + * @return bool + */ public function supports(ResourceInterface $resource, $generation, array $data) { return 'class' === $generation && $resource instanceof HelperResource; } - public function generate(ResourceInterface $resource, array $data = array()) + /** + * @return int + */ + public function getPriority() { - $filepath = $resource->getSrcFilename(); - if ($this->filesystem->pathExists($filepath)) { - $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath)); - if (!$this->io->askConfirmation($message, false)) { - return; - } - - $this->io->writeln(); - } + return 20; + } - $path = dirname($filepath); - if (!$this->filesystem->isDirectory($path)) { - $this->filesystem->makeDirectory($path); - } + /** + * @param ResourceInterface $resource + * + * @return string + */ + protected function getFilePath(ResourceInterface $resource) + { + return $resource->getSrcFilename(); + } - $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()) - : '', + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + { + return sprintf( + "Magento helper %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath ); + } - if (!$content = $this->templates->render('mage_helper', $values)) { - $content = $this->templates->renderString( - file_get_contents(__DIR__ . '/templates/generic_class.template'), $values - ); - } - - $this->filesystem->putFileContents($filepath, $content); - $this->io->writeln(sprintf( - "Magento helper %s created in '%s'.\n", - $resource->getSrcClassname(), $filepath - )); + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Helper_Abstract'; } - public function getPriority() + /** + * @return string + */ + protected function getTemplateName() { - return 20; + 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 6cabaab..c7be50d 100644 --- a/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ModelGenerator.php +++ b/src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ModelGenerator.php @@ -22,10 +22,7 @@ namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator; use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ModelResource; -use PhpSpec\Console\IO; -use PhpSpec\CodeGenerator\TemplateRenderer; use PhpSpec\CodeGenerator\Generator\GeneratorInterface; -use PhpSpec\Util\Filesystem; use PhpSpec\Locator\ResourceInterface; /** * ModelGenerator @@ -35,66 +32,65 @@ * * @author MageTest team (https://github.com/MageTest/MageSpec/contributors) */ -class ModelGenerator implements GeneratorInterface +class ModelGenerator extends MagentoObjectGenerator implements GeneratorInterface { - private $io; - private $templates; - private $filesystem; - - public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null) - { - $this->io = $io; - $this->templates = $templates; - $this->filesystem = $filesystem ?: new Filesystem; - } - + /** + * @param ResourceInterface $resource + * @param string $generation + * @param array $data + * @return bool + */ public function supports(ResourceInterface $resource, $generation, array $data) { return 'class' === $generation && $resource instanceof ModelResource; } - public function generate(ResourceInterface $resource, array $data = array()) + /** + * @return int + */ + public function getPriority() { - $filepath = $resource->getSrcFilename(); - if ($this->filesystem->pathExists($filepath)) { - $message = sprintf('File "%s" already exists. Overwrite?', basename($filepath)); - if (!$this->io->askConfirmation($message, false)) { - return; - } - - $this->io->writeln(); - } + return 42; + } - $path = dirname($filepath); - if (!$this->filesystem->isDirectory($path)) { - $this->filesystem->makeDirectory($path); - } + /** + * @param ResourceInterface $resource + * + * @return string + */ + protected function getFilePath(ResourceInterface $resource) + { + return $resource->getSrcFilename(); + } - $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()) - : '', + /** + * @param ResourceInterface $resource + * @param string $filepath + * + * @return string + */ + protected function getGeneratedMessage(ResourceInterface $resource, $filepath) + { + return sprintf( + "Magento model %s created in '%s'.\n", + $resource->getSrcClassname(), + $filepath ); + } - if (!$content = $this->templates->render('mage_model', $values)) { - $content = $this->templates->renderString( - file_get_contents(__DIR__ . '/templates/generic_class.template'), $values - ); - } - - $this->filesystem->putFileContents($filepath, $content); - $this->io->writeln(sprintf( - "Magento model %s created in '%s'.\n", - $resource->getSrcClassname(), $filepath - )); + /** + * @return string + */ + protected function getParentClass() + { + return 'Mage_Core_Model_Abstract'; } - public function getPriority() + /** + * @return string + */ + protected function getTemplateName() { - return 42; + return 'mage_model'; } }