Skip to content

Commit a55fc7e

Browse files
committed
Implement upstream PromptingGenerator abstract
1 parent 818d550 commit a55fc7e

File tree

5 files changed

+227
-190
lines changed

5 files changed

+227
-190
lines changed

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
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;
25+
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
2726
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
28-
use PhpSpec\Util\Filesystem;
2927
use PhpSpec\Locator\ResourceInterface;
3028

3129
/**
@@ -36,41 +34,45 @@
3634
*
3735
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3836
*/
39-
class BlockGenerator implements GeneratorInterface
37+
class BlockGenerator extends PromptingGenerator implements GeneratorInterface
4038
{
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-
39+
/**
40+
* @param ResourceInterface $resource
41+
* @param string $generation
42+
* @param array $data
43+
* @return bool
44+
*/
5245
public function supports(ResourceInterface $resource, $generation, array $data)
5346
{
5447
return 'class' === $generation && $resource instanceof BlockResource;
5548
}
5649

57-
public function generate(ResourceInterface $resource, array $data = array())
50+
/**
51+
* @return int
52+
*/
53+
public function getPriority()
5854
{
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-
}
55+
return 30;
56+
}
6857

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

68+
/**
69+
* @param ResourceInterface $resource
70+
* @param string $filepath
71+
*
72+
* @return string
73+
*/
74+
protected function renderTemplate(ResourceInterface $resource, $filepath)
75+
{
7476
$values = array(
7577
'%filepath%' => $filepath,
7678
'%name%' => $resource->getName(),
@@ -81,21 +83,27 @@ public function generate(ResourceInterface $resource, array $data = array())
8183
: '',
8284
);
8385

84-
if (!$content = $this->templates->render('mage_block', $values)) {
85-
$content = $this->templates->renderString(
86+
if (!$content = $this->getTemplateRenderer()->render('mage_block', $values)) {
87+
$content = $this->getTemplateRenderer()->renderString(
8688
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
8789
);
8890
}
8991

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-
));
92+
return $content;
9593
}
9694

97-
public function getPriority()
95+
/**
96+
* @param ResourceInterface $resource
97+
* @param string $filepath
98+
*
99+
* @return string
100+
*/
101+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
98102
{
99-
return 30;
103+
return sprintf(
104+
"<info>Magento block <value>%s</value> created in <value>'%s'</value>.</info>\n",
105+
$resource->getSrcClassname(),
106+
$filepath
107+
);
100108
}
101109
}

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
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;
25+
use PhpSpec\CodeGenerator\Generator\PromptingGenerator;
2726
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
28-
use PhpSpec\Util\Filesystem;
2927
use PhpSpec\Locator\ResourceInterface;
3028
/**
3129
* ControllerGenerator
@@ -35,41 +33,45 @@
3533
*
3634
* @author MageTest team (https://github.com/MageTest/MageSpec/contributors)
3735
*/
38-
class ControllerGenerator implements GeneratorInterface
36+
class ControllerGenerator extends PromptingGenerator implements GeneratorInterface
3937
{
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-
38+
/**
39+
* @param ResourceInterface $resource
40+
* @param string $generation
41+
* @param array $data
42+
* @return bool
43+
*/
5144
public function supports(ResourceInterface $resource, $generation, array $data)
5245
{
5346
return 'class' === $generation && $resource instanceof ControllerResource;
5447
}
5548

56-
public function generate(ResourceInterface $resource, array $data = array())
49+
/**
50+
* @return int
51+
*/
52+
public function getPriority()
5753
{
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-
}
54+
return 10;
55+
}
6756

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

67+
/**
68+
* @param ResourceInterface $resource
69+
* @param string $filepath
70+
*
71+
* @return string
72+
*/
73+
protected function renderTemplate(ResourceInterface $resource, $filepath)
74+
{
7375
$values = array(
7476
'%filepath%' => $filepath,
7577
'%name%' => $resource->getName(),
@@ -80,21 +82,27 @@ public function generate(ResourceInterface $resource, array $data = array())
8082
: '',
8183
);
8284

83-
if (!$content = $this->templates->render('mage_controller', $values)) {
84-
$content = $this->templates->renderString(
85+
if (!$content = $this->getTemplateRenderer()->render('mage_controller', $values)) {
86+
$content = $this->getTemplateRenderer()->renderString(
8587
file_get_contents(__DIR__ . '/templates/generic_class.template'), $values
8688
);
8789
}
8890

89-
$this->filesystem->putFileContents($filepath, $content);
90-
$this->io->writeln(sprintf(
91-
"<info>Magento controller <value>%s</value> created in <value>'%s'</value>.</info>\n",
92-
$resource->getSrcClassname(), $filepath
93-
));
91+
return $content;
9492
}
9593

96-
public function getPriority()
94+
/**
95+
* @param ResourceInterface $resource
96+
* @param string $filepath
97+
*
98+
* @return string
99+
*/
100+
protected function getGeneratedMessage(ResourceInterface $resource, $filepath)
97101
{
98-
return 10;
102+
return sprintf(
103+
"<info>Magento controller <value>%s</value> created in <value>'%s'</value>.</info>\n",
104+
$resource->getSrcClassname(),
105+
$filepath
106+
);
99107
}
100108
}

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

Lines changed: 43 additions & 38 deletions
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)