-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract common template generation code
- Loading branch information
1 parent
a55fc7e
commit b9dbab3
Showing
5 changed files
with
117 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/MageTest/PhpSpec/MagentoExtension/CodeGenerator/Generator/MagentoObjectGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters