Skip to content

Commit c3a3de9

Browse files
author
James Cowie
committed
Merge pull request #106 from MageTest/feature/reduce-generator-duplication
Reduce duplication in code generators
2 parents d3d1a6b + b9dbab3 commit c3a3de9

File tree

6 files changed

+296
-225
lines changed

6 files changed

+296
-225
lines changed

src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/BlockGenerator.php

+46-50
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
2323

2424
use MageTest\PhpSpec\MagentoExtension\Locator\Magento\BlockResource;
25-
use PhpSpec\Console\IO;
26-
use PhpSpec\CodeGenerator\TemplateRenderer;
2725
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
28-
use PhpSpec\Util\Filesystem;
2926
use PhpSpec\Locator\ResourceInterface;
3027

3128
/**
@@ -36,66 +33,65 @@
3633
*
3734
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3835
*/
39-
class BlockGenerator implements GeneratorInterface
36+
class BlockGenerator extends MagentoObjectGenerator implements GeneratorInterface
4037
{
41-
private $io;
42-
private $templates;
43-
private $filesystem;
44-
45-
public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null)
46-
{
47-
$this->io = $io;
48-
$this->templates = $templates;
49-
$this->filesystem = $filesystem ?: new Filesystem;
50-
}
51-
38+
/**
39+
* @param ResourceInterface $resource
40+
* @param string $generation
41+
* @param array $data
42+
* @return bool
43+
*/
5244
public function supports(ResourceInterface $resource, $generation, array $data)
5345
{
5446
return 'class' === $generation && $resource instanceof BlockResource;
5547
}
5648

57-
public function generate(ResourceInterface $resource, array $data = array())
49+
/**
50+
* @return int
51+
*/
52+
public function getPriority()
5853
{
59-
$filepath = $resource->getSrcFilename();
60-
if ($this->filesystem->pathExists($filepath)) {
61-
$message = sprintf('File "%s" already exists. Overwrite?', basename($filepath));
62-
if (!$this->io->askConfirmation($message, false)) {
63-
return;
64-
}
65-
66-
$this->io->writeln();
67-
}
54+
return 30;
55+
}
6856

69-
$path = dirname($filepath);
70-
if (!$this->filesystem->isDirectory($path)) {
71-
$this->filesystem->makeDirectory($path);
72-
}
57+
/**
58+
* @param ResourceInterface $resource
59+
*
60+
* @return string
61+
*/
62+
protected function getFilePath(ResourceInterface $resource)
63+
{
64+
return $resource->getSrcFilename();
65+
}
7366

74-
$values = array(
75-
'%filepath%' => $filepath,
76-
'%name%' => $resource->getName(),
77-
'%extends%' => 'Mage_Core_Block_Abstract',
78-
'%namespace%' => $resource->getSrcNamespace(),
79-
'%namespace_block%' => '' !== $resource->getSrcNamespace()
80-
? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace())
81-
: '',
67+
/**
68+
* @param ResourceInterface $resource
69+
* @param string $filepath
70+
*
71+
* @return string
72+
*/
73+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
74+
{
75+
return sprintf(
76+
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\n",
77+
$resource->getSrcClassname(),
78+
$filepath
8279
);
80+
}
8381

84-
if (!$content = $this->templates->render('mage_block', $values)) {
85-
$content = $this->templates->renderString(
86-
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
87-
);
88-
}
89-
90-
$this->filesystem->putFileContents($filepath, $content);
91-
$this->io->writeln(sprintf(
92-
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\n",
93-
$resource->getSrcClassname(), $filepath
94-
));
82+
/**
83+
* @return string
84+
*/
85+
protected function getParentClass()
86+
{
87+
return 'Mage_Core_Block_Abstract';
9588
}
9689

97-
public function getPriority()
90+
/**
91+
* @return string
92+
*/
93+
protected function getTemplateName()
9894
{
99-
return 30;
95+
return 'mage_block';
10096
}
10197
}

src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerGenerator.php

+60-37
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
2323

2424
use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ControllerResource;
25-
use PhpSpec\Console\IO;
26-
use PhpSpec\CodeGenerator\TemplateRenderer;
2725
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
28-
use PhpSpec\Util\Filesystem;
2926
use PhpSpec\Locator\ResourceInterface;
3027
/**
3128
* ControllerGenerator
@@ -35,41 +32,45 @@
3532
*
3633
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3734
*/
38-
class ControllerGenerator implements GeneratorInterface
35+
class ControllerGenerator extends MagentoObjectGenerator implements GeneratorInterface
3936
{
40-
private $io;
41-
private $templates;
42-
private $filesystem;
43-
44-
public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null)
45-
{
46-
$this->io = $io;
47-
$this->templates = $templates;
48-
$this->filesystem = $filesystem ?: new Filesystem;
49-
}
50-
37+
/**
38+
* @param ResourceInterface $resource
39+
* @param string $generation
40+
* @param array $data
41+
* @return bool
42+
*/
5143
public function supports(ResourceInterface $resource, $generation, array $data)
5244
{
5345
return 'class' === $generation && $resource instanceof ControllerResource;
5446
}
5547

56-
public function generate(ResourceInterface $resource, array $data = array())
48+
/**
49+
* @return int
50+
*/
51+
public function getPriority()
5752
{
58-
$filepath = $resource->getSrcFilename();
59-
if ($this->filesystem->pathExists($filepath)) {
60-
$message = sprintf('File "%s" already exists. Overwrite?', basename($filepath));
61-
if (!$this->io->askConfirmation($message, false)) {
62-
return;
63-
}
64-
65-
$this->io->writeln();
66-
}
53+
return 10;
54+
}
6755

68-
$path = dirname($filepath);
69-
if (!$this->filesystem->isDirectory($path)) {
70-
$this->filesystem->makeDirectory($path);
71-
}
56+
/**
57+
* @param ResourceInterface $resource
58+
*
59+
* @return string
60+
*/
61+
protected function getFilePath(ResourceInterface $resource)
62+
{
63+
return $resource->getSrcFilename();
64+
}
7265

66+
/**
67+
* @param ResourceInterface $resource
68+
* @param string $filepath
69+
*
70+
* @return string
71+
*/
72+
protected function renderTemplate(ResourceInterface $resource, $filepath)
73+
{
7374
$values = array(
7475
'%filepath%' => $filepath,
7576
'%name%' => $resource->getName(),
@@ -80,21 +81,43 @@ public function generate(ResourceInterface $resource, array $data = array())
8081
: '',
8182
);
8283

83-
if (!$content = $this->templates->render('mage_controller', $values)) {
84-
$content = $this->templates->renderString(
84+
if (!$content = $this->getTemplateRenderer()->render('mage_controller', $values)) {
85+
$content = $this->getTemplateRenderer()->renderString(
8586
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
8687
);
8788
}
8889

89-
$this->filesystem->putFileContents($filepath, $content);
90-
$this->io->writeln(sprintf(
90+
return $content;
91+
}
92+
93+
/**
94+
* @param ResourceInterface $resource
95+
* @param string $filepath
96+
*
97+
* @return string
98+
*/
99+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
100+
{
101+
return sprintf(
91102
"<info>Magento controller <value>%s</value> created in <value>'%s'</value>.</info>\n",
92-
$resource->getSrcClassname(), $filepath
93-
));
103+
$resource->getSrcClassname(),
104+
$filepath
105+
);
94106
}
95107

96-
public function getPriority()
108+
/**
109+
* @return string
110+
*/
111+
protected function getParentClass()
97112
{
98-
return 10;
113+
return 'Mage_Core_Controller_Front_Action';
114+
}
115+
116+
/**
117+
* @return string
118+
*/
119+
protected function getTemplateName()
120+
{
121+
return 'mage_controller';
99122
}
100123
}

src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/ControllerSpecificationGenerator.php

+43-38
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,74 @@
22

33
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
44

5-
use PhpSpec\Console\IO;
6-
use PhpSpec\CodeGenerator\TemplateRenderer;
7-
use PhpSpec\Util\Filesystem;
5+
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
86
use PhpSpec\Locator\ResourceInterface;
97
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
108

11-
class ControllerSpecificationGenerator implements GeneratorInterface
9+
class ControllerSpecificationGenerator extends PromptingGenerator implements GeneratorInterface
1210
{
13-
private $io;
14-
private $templates;
15-
private $filesystem;
16-
17-
public function __construct(IO $io, TemplateRenderer $templates, Filesystem $filesystem = null)
18-
{
19-
$this->io = $io;
20-
$this->templates = $templates;
21-
$this->filesystem = $filesystem ?: new Filesystem;
22-
}
23-
11+
/**
12+
* @param ResourceInterface $resource
13+
* @param string $generation
14+
* @param array $data
15+
* @return bool
16+
*/
2417
public function supports(ResourceInterface $resource, $generation, array $data)
2518
{
2619
return 'controller_specification' === $generation;
2720
}
2821

29-
public function generate(ResourceInterface $resource, array $data = array())
22+
public function getPriority()
3023
{
31-
$filepath = $resource->getSpecFilename();
32-
if ($this->filesystem->pathExists($filepath)) {
33-
$message = sprintf('File "%s" already exists. Overwrite?', basename($filepath));
34-
if (!$this->io->askConfirmation($message, false)) {
35-
return;
36-
}
37-
38-
$this->io->writeln();
39-
}
24+
return 0;
25+
}
4026

41-
$path = dirname($filepath);
42-
if (!$this->filesystem->isDirectory($path)) {
43-
$this->filesystem->makeDirectory($path);
44-
}
27+
/**
28+
* @param ResourceInterface $resource
29+
*
30+
* @return string
31+
*/
32+
protected function getFilePath(ResourceInterface $resource)
33+
{
34+
return $resource->getSpecFilename();
35+
}
4536

37+
/**
38+
* @param ResourceInterface $resource
39+
* @param string $filepath
40+
*
41+
* @return string
42+
*/
43+
protected function renderTemplate(ResourceInterface $resource, $filepath)
44+
{
4645
$values = array(
4746
'%filepath%' => $filepath,
4847
'%name%' => $resource->getSpecName(),
4948
'%namespace%' => $resource->getSpecNamespace(),
5049
'%subject%' => $resource->getSrcClassname()
5150
);
5251

53-
if (!$content = $this->templates->render('controller_specification', $values)) {
54-
$content = $this->templates->renderString(
52+
if (!$content = $this->getTemplateRenderer()->render('controller_specification', $values)) {
53+
$content = $this->getTemplateRenderer()->renderString(
5554
file_get_contents(__DIR__ . '/templates/controller_spec.template'), $values
5655
);
5756
}
5857

59-
$this->filesystem->putFileContents($filepath, $content);
60-
$this->io->writeln(sprintf(
61-
"<info>ControllerSpecification for <value>%s</value> created in <value>'%s'</value>.</info>\n",
62-
$resource->getSrcClassname(), $filepath
63-
));
58+
return $content;
6459
}
6560

66-
public function getPriority()
61+
/**
62+
* @param ResourceInterface $resource
63+
* @param string $filepath
64+
*
65+
* @return string
66+
*/
67+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
6768
{
68-
return 0;
69+
sprintf(
70+
"<info>ControllerSpecification for <value>%s</value> created in <value>'%s'</value>.</info>\n",
71+
$resource->getSrcClassname(),
72+
$filepath
73+
);
6974
}
7075
}

0 commit comments

Comments
 (0)