Skip to content

Commit

Permalink
Merge pull request #106 from MageTest/feature/reduce-generator-duplic…
Browse files Browse the repository at this point in the history
…ation

Reduce duplication in code generators
  • Loading branch information
James Cowie committed Jul 3, 2015
2 parents d3d1a6b + b9dbab3 commit c3a3de9
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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(
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\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(
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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(
"<info>Magento controller <value>%s</value> created in <value>'%s'</value>.</info>\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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,74 @@

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(),
'%namespace%' => $resource->getSpecNamespace(),
'%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(
"<info>ControllerSpecification for <value>%s</value> created in <value>'%s'</value>.</info>\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(
"<info>ControllerSpecification for <value>%s</value> created in <value>'%s'</value>.</info>\n",
$resource->getSrcClassname(),
$filepath
);
}
}
Loading

0 comments on commit c3a3de9

Please sign in to comment.