Skip to content

Commit b9dbab3

Browse files
committed
Abstract common template generation code
1 parent a55fc7e commit b9dbab3

File tree

5 files changed

+117
-83
lines changed

5 files changed

+117
-83
lines changed

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
2323

2424
use MageTest\PhpSpec\MagentoExtension\Locator\Magento\BlockResource;
25-
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
2625
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
2726
use PhpSpec\Locator\ResourceInterface;
2827

@@ -34,7 +33,7 @@
3433
*
3534
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3635
*/
37-
class BlockGenerator extends PromptingGenerator implements GeneratorInterface
36+
class BlockGenerator extends MagentoObjectGenerator implements GeneratorInterface
3837
{
3938
/**
4039
* @param ResourceInterface $resource
@@ -71,39 +70,28 @@ protected function getFilePath(ResourceInterface $resource)
7170
*
7271
* @return string
7372
*/
74-
protected function renderTemplate(ResourceInterface $resource, $filepath)
73+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
7574
{
76-
$values = array(
77-
'%filepath%' => $filepath,
78-
'%name%' => $resource->getName(),
79-
'%extends%' => 'Mage_Core_Block_Abstract',
80-
'%namespace%' => $resource->getSrcNamespace(),
81-
'%namespace_block%' => '' !== $resource->getSrcNamespace()
82-
? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace())
83-
: '',
75+
return sprintf(
76+
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\n",
77+
$resource->getSrcClassname(),
78+
$filepath
8479
);
80+
}
8581

86-
if (!$content = $this->getTemplateRenderer()->render('mage_block', $values)) {
87-
$content = $this->getTemplateRenderer()->renderString(
88-
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
89-
);
90-
}
91-
92-
return $content;
82+
/**
83+
* @return string
84+
*/
85+
protected function getParentClass()
86+
{
87+
return 'Mage_Core_Block_Abstract';
9388
}
9489

9590
/**
96-
* @param ResourceInterface $resource
97-
* @param string $filepath
98-
*
9991
* @return string
10092
*/
101-
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
93+
protected function getTemplateName()
10294
{
103-
return sprintf(
104-
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\n",
105-
$resource->getSrcClassname(),
106-
$filepath
107-
);
95+
return 'mage_block';
10896
}
10997
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
2323

2424
use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ControllerResource;
25-
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
2625
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
2726
use PhpSpec\Locator\ResourceInterface;
2827
/**
@@ -33,7 +32,7 @@
3332
*
3433
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3534
*/
36-
class ControllerGenerator extends PromptingGenerator implements GeneratorInterface
35+
class ControllerGenerator extends MagentoObjectGenerator implements GeneratorInterface
3736
{
3837
/**
3938
* @param ResourceInterface $resource
@@ -105,4 +104,20 @@ protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
105104
$filepath
106105
);
107106
}
107+
108+
/**
109+
* @return string
110+
*/
111+
protected function getParentClass()
112+
{
113+
return 'Mage_Core_Controller_Front_Action';
114+
}
115+
116+
/**
117+
* @return string
118+
*/
119+
protected function getTemplateName()
120+
{
121+
return 'mage_controller';
122+
}
108123
}

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
2323

2424
use MageTest\PhpSpec\MagentoExtension\Locator\Magento\HelperResource;
25-
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
2625
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
2726
use PhpSpec\Locator\ResourceInterface;
2827
/**
@@ -33,7 +32,7 @@
3332
*
3433
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3534
*/
36-
class HelperGenerator extends PromptingGenerator implements GeneratorInterface
35+
class HelperGenerator extends MagentoObjectGenerator implements GeneratorInterface
3736
{
3837
/**
3938
* @param ResourceInterface $resource
@@ -70,39 +69,28 @@ protected function getFilePath(ResourceInterface $resource)
7069
*
7170
* @return string
7271
*/
73-
protected function renderTemplate(ResourceInterface $resource, $filepath)
72+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
7473
{
75-
$values = array(
76-
'%filepath%' => $filepath,
77-
'%name%' => $resource->getName(),
78-
'%extends%' => 'Mage_Core_Helper_Abstract',
79-
'%namespace%' => $resource->getSrcNamespace(),
80-
'%namespace_block%' => '' !== $resource->getSrcNamespace()
81-
? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace())
82-
: '',
74+
return sprintf(
75+
"<info>Magento helper <value>%s</value> created in <value>'%s'</value>.</info>\n",
76+
$resource->getSrcClassname(),
77+
$filepath
8378
);
79+
}
8480

85-
if (!$content = $this->getTemplateRenderer()->render('mage_helper', $values)) {
86-
$content = $this->getTemplateRenderer()->renderString(
87-
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
88-
);
89-
}
90-
91-
return $content;
81+
/**
82+
* @return string
83+
*/
84+
protected function getParentClass()
85+
{
86+
return 'Mage_Core_Helper_Abstract';
9287
}
9388

9489
/**
95-
* @param ResourceInterface $resource
96-
* @param string $filepath
97-
*
9890
* @return string
9991
*/
100-
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
92+
protected function getTemplateName()
10193
{
102-
return sprintf(
103-
"<info>Magento helper <value>%s</value> created in <value>'%s'</value>.</info>\n",
104-
$resource->getSrcClassname(),
105-
$filepath
106-
);
94+
return 'mage_helper';
10795
}
10896
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
4+
5+
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
6+
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
7+
use PhpSpec\Locator\ResourceInterface;
8+
9+
abstract class MagentoObjectGenerator extends PromptingGenerator implements GeneratorInterface
10+
{
11+
/**
12+
* @param ResourceInterface $resource
13+
* @param string $filepath
14+
*
15+
* @return string
16+
*/
17+
protected function renderTemplate(ResourceInterface $resource, $filepath)
18+
{
19+
$values = array(
20+
'%filepath%' => $filepath,
21+
'%name%' => $resource->getName(),
22+
'%extends%' => $this->getParentClass(),
23+
'%namespace%' => $resource->getSrcNamespace(),
24+
'%namespace_block%' => '' !== $resource->getSrcNamespace()
25+
? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace())
26+
: '',
27+
);
28+
29+
if (!$content = $this->getTemplateRenderer()->render($this->getTemplateName(), $values)) {
30+
$content = $this->getTemplateRenderer()->renderString(
31+
file_get_contents(__DIR__ . $this->getTemplateFile()), $values
32+
);
33+
}
34+
35+
return $content;
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
abstract protected function getParentClass();
42+
43+
/**
44+
* @return string
45+
*/
46+
abstract protected function getTemplateName();
47+
48+
/**
49+
* @return string
50+
*/
51+
protected function getTemplateFile()
52+
{
53+
return '/templates/generic_class.template';
54+
}
55+
}

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator;
2323

2424
use MageTest\PhpSpec\MagentoExtension\Locator\Magento\ModelResource;
25-
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
2625
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
2726
use PhpSpec\Locator\ResourceInterface;
2827
/**
@@ -33,7 +32,7 @@
3332
*
3433
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3534
*/
36-
class ModelGenerator extends PromptingGenerator implements GeneratorInterface
35+
class ModelGenerator extends MagentoObjectGenerator implements GeneratorInterface
3736
{
3837
/**
3938
* @param ResourceInterface $resource
@@ -70,39 +69,28 @@ protected function getFilePath(ResourceInterface $resource)
7069
*
7170
* @return string
7271
*/
73-
protected function renderTemplate(ResourceInterface $resource, $filepath)
72+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
7473
{
75-
$values = array(
76-
'%filepath%' => $filepath,
77-
'%name%' => $resource->getName(),
78-
'%extends%' => 'Mage_Core_Model_Abstract',
79-
'%namespace%' => $resource->getSrcNamespace(),
80-
'%namespace_block%' => '' !== $resource->getSrcNamespace()
81-
? sprintf("\n\nnamespace %s;", $resource->getSrcNamespace())
82-
: '',
74+
return sprintf(
75+
"<info>Magento model <value>%s</value> created in <value>'%s'</value>.</info>\n",
76+
$resource->getSrcClassname(),
77+
$filepath
8378
);
79+
}
8480

85-
if (!$content = $this->getTemplateRenderer()->render('mage_model', $values)) {
86-
$content = $this->getTemplateRenderer()->renderString(
87-
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
88-
);
89-
}
90-
91-
return $content;
81+
/**
82+
* @return string
83+
*/
84+
protected function getParentClass()
85+
{
86+
return 'Mage_Core_Model_Abstract';
9287
}
9388

9489
/**
95-
* @param ResourceInterface $resource
96-
* @param string $filepath
97-
*
9890
* @return string
9991
*/
100-
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
92+
protected function getTemplateName()
10193
{
102-
return sprintf(
103-
"<info>Magento model <value>%s</value> created in <value>'%s'</value>.</info>\n",
104-
$resource->getSrcClassname(),
105-
$filepath
106-
);
94+
return 'mage_model';
10795
}
10896
}

0 commit comments

Comments
 (0)