Skip to content

Commit

Permalink
Abstract common template generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
shanethehat committed Jun 26, 2015
1 parent a55fc7e commit b9dbab3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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(
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\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(
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\n",
$resource->getSrcClassname(),
$filepath
);
return 'mage_block';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -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
Expand Down Expand Up @@ -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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -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
Expand Down Expand Up @@ -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(
"<info>Magento helper <value>%s</value> created in <value>'%s'</value>.</info>\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(
"<info>Magento helper <value>%s</value> created in <value>'%s'</value>.</info>\n",
$resource->getSrcClassname(),
$filepath
);
return 'mage_helper';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;

use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
use PhpSpec\Locator\ResourceInterface;

abstract class MagentoObjectGenerator extends PromptingGenerator implements GeneratorInterface
{
/**
* @param ResourceInterface $resource
* @param string $filepath
*
* @return string
*/
protected function renderTemplate(ResourceInterface $resource, $filepath)
{
$values = array(
'%filepath%' => $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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -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
Expand Down Expand Up @@ -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(
"<info>Magento model <value>%s</value> created in <value>'%s'</value>.</info>\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(
"<info>Magento model <value>%s</value> created in <value>'%s'</value>.</info>\n",
$resource->getSrcClassname(),
$filepath
);
return 'mage_model';
}
}

0 comments on commit b9dbab3

Please sign in to comment.