-
-
Notifications
You must be signed in to change notification settings - Fork 424
function to change the name path of the template Template.tpl.php #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jonathanlight
wants to merge
6
commits into
symfony:1.x
Choose a base branch
from
Jonathanlight:feat/add-function-root-template-name
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28ed6b4
function to change the name path of the template Template.tpl.php
Jonathanlight 824e4ac
Update property in rootTemplateDirectory and removal (templatesNameEn…
Jonathanlight ac087dd
Merge branch 'master' into feat/add-function-root-template-name
Jonathanlight 9450147
new composant MakerRender Interface
Jonathanlight 29643e9
Merge branch 'feat/add-function-root-template-name' of https://github…
Jonathanlight 8a371ef
Create Class MakerDefaultTemplateRenderer
Jonathanlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -60,4 +60,4 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $ | |
|
||
return $entityPath; | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -18,19 +18,32 @@ | |
/** | ||
* @author Javier Eguiluz <[email protected]> | ||
* @author Ryan Weaver <[email protected]> | ||
* @editor jonathan Kablan <[email protected]> | ||
*/ | ||
class Generator | ||
{ | ||
private $fileManager; | ||
private $twigHelper; | ||
private $pendingOperations = []; | ||
private $namespacePrefix; | ||
private $templateNameEntity; | ||
private $templateNameRepository; | ||
private $rootTemplateName; | ||
|
||
/** | ||
* Generator constructor. | ||
* @param FileManager $fileManager | ||
* @param string $namespacePrefix | ||
*/ | ||
public function __construct(FileManager $fileManager, string $namespacePrefix) | ||
{ | ||
$this->fileManager = $fileManager; | ||
$this->twigHelper = new GeneratorTwigHelper($fileManager); | ||
$this->namespacePrefix = trim($namespacePrefix, '\\'); | ||
|
||
$this->rootTemplateName = __DIR__.'/Resources/skeleton/'; | ||
$this->templateNameEntity = 'doctrine/Entity.tpl.php'; | ||
$this->templateNameRepository = 'doctrine/Repository.tpl.php'; | ||
} | ||
|
||
/** | ||
|
@@ -64,6 +77,11 @@ public function generateClass(string $className, string $templateName, array $va | |
|
||
/** | ||
* Generate a normal file from a template. | ||
* | ||
* @param string $targetPath | ||
* @param string $templateName | ||
* @param array $variables | ||
* @throws \Exception | ||
*/ | ||
public function generateFile(string $targetPath, string $templateName, array $variables = []) | ||
{ | ||
|
@@ -120,9 +138,12 @@ public function getFileContentsForPendingOperation(string $targetPath): string | |
* // Cool\Stuff\BalloonController | ||
* $gen->createClassNameDetails('Cool\\Stuff\\Balloon', 'Controller', 'Controller'); | ||
* | ||
* @param string $name The short "name" that will be turned into the class name | ||
* @param string $namespacePrefix Recommended namespace where this class should live, but *without* the "App\\" part | ||
* @param string $suffix Optional suffix to guarantee is on the end of the class | ||
* @param string $name The short "name" that will be turned into the class name | ||
* @param string $namespacePrefix Recommended namespace where this class should live, but *without* the "App\\" part | ||
* @param string $suffix Optional suffix to guarantee is on the end of the class | ||
* @param string $validationErrorMessage | ||
* | ||
* @return ClassNameDetails | ||
*/ | ||
public function createClassNameDetails(string $name, string $namespacePrefix, string $suffix = '', string $validationErrorMessage = ''): ClassNameDetails | ||
{ | ||
|
@@ -145,22 +166,34 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st | |
return new ClassNameDetails($className, $fullNamespacePrefix, $suffix); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRootDirectory(): string | ||
{ | ||
return $this->fileManager->getRootDirectory(); | ||
} | ||
|
||
/** | ||
* @param string $targetPath | ||
* @param string $templateName | ||
* @param array $variables | ||
* @throws \Exception | ||
*/ | ||
private function addOperation(string $targetPath, string $templateName, array $variables) | ||
{ | ||
if ($this->fileManager->fileExists($targetPath)) { | ||
throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath))); | ||
throw new RuntimeCommandException(sprintf( | ||
'The file "%s" can\'t be generated because it already exists.', | ||
$this->fileManager->relativizePath($targetPath) | ||
)); | ||
} | ||
|
||
$variables['relative_path'] = $this->fileManager->relativizePath($targetPath); | ||
|
||
$templatePath = $templateName; | ||
if (!file_exists($templatePath)) { | ||
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName; | ||
$templatePath = $this->rootTemplateName.$templateName; | ||
|
||
if (!file_exists($templatePath)) { | ||
throw new \Exception(sprintf('Cannot find template "%s"', $templateName)); | ||
|
@@ -173,6 +206,9 @@ private function addOperation(string $targetPath, string $templateName, array $v | |
]; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasPendingOperations(): bool | ||
{ | ||
return !empty($this->pendingOperations); | ||
|
@@ -199,11 +235,21 @@ public function writeChanges() | |
$this->pendingOperations = []; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRootNamespace(): string | ||
{ | ||
return $this->namespacePrefix; | ||
} | ||
|
||
/** | ||
* @param string $controllerClassName | ||
* @param string $controllerTemplatePath | ||
* @param array $parameters | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
public function generateController(string $controllerClassName, string $controllerTemplatePath, array $parameters = []): string | ||
{ | ||
return $this->generateClass( | ||
|
@@ -218,6 +264,10 @@ public function generateController(string $controllerClassName, string $controll | |
|
||
/** | ||
* Generate a template file. | ||
* | ||
* @param string $targetPath | ||
* @param string $templateName | ||
* @param array $variables | ||
*/ | ||
public function generateTemplate(string $targetPath, string $templateName, array $variables = []) | ||
{ | ||
|
@@ -227,4 +277,52 @@ public function generateTemplate(string $targetPath, string $templateName, array | |
$variables | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getTemplateNameEntity(): string | ||
{ | ||
return $this->templateNameEntity; | ||
} | ||
|
||
/** | ||
* @param string $templateNameEntity | ||
*/ | ||
public function setTemplateNameEntity(string $templateNameEntity) | ||
{ | ||
$this->templateNameEntity = $templateNameEntity; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getTemplateNameRepository(): string | ||
{ | ||
return $this->templateNameRepository; | ||
} | ||
|
||
/** | ||
* @param string $templateNameRepository | ||
*/ | ||
public function setTemplateNameRepository(string $templateNameRepository) | ||
{ | ||
$this->templateNameRepository = $templateNameRepository; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRootTemplateName(): string | ||
{ | ||
return $this->rootTemplateName; | ||
} | ||
|
||
/** | ||
* @param string $rootTemplateName | ||
*/ | ||
public function setRootTemplateName(string $rootTemplateName) | ||
{ | ||
$this->rootTemplateName = $rootTemplateName; | ||
} | ||
} |
This file contains hidden or 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,46 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\MakerBundle; | ||
|
||
class MakerDefaultTemplateRenderer implements MakerTemplateRendererInterface | ||
{ | ||
/** | ||
* @var FileManager | ||
*/ | ||
private $fileManager; | ||
|
||
/** | ||
* @param FileManager $fileManager | ||
*/ | ||
public function __construct(FileManager $fileManager) | ||
{ | ||
$this->fileManager = $fileManager; | ||
} | ||
|
||
/** | ||
* @param string $templateName | ||
* @return bool | ||
*/ | ||
public function supports(string $templateName): bool | ||
{ | ||
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName; | ||
|
||
return file_exists($templatePath); | ||
} | ||
|
||
/** | ||
* @param string $templateName | ||
* @param array $variables | ||
* @return string | ||
*/ | ||
public function render(string $templateName, array $variables): string | ||
{ | ||
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName; | ||
|
||
// this is basically the current logic for rendering templates | ||
$contents = $this->fileManager->parseTemplate($templatePath, $variables); | ||
$this->fileManager->dumpFile($templatePath, $contents); | ||
|
||
return $templatePath; | ||
} | ||
} |
This file contains hidden or 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,36 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\MakerBundle; | ||
|
||
class MakerRenderer | ||
{ | ||
/** | ||
* @var MakerTemplateRendererInterface[] | ||
*/ | ||
private $templateRenderers; | ||
|
||
/** | ||
* @param array $templateRenderers | ||
*/ | ||
public function __construct(array $templateRenderers) | ||
{ | ||
$this->templateRenderers = $templateRenderers; | ||
} | ||
|
||
/** | ||
* @param string $templateName | ||
* @param array $variables | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
public function renderTemplate(string $templateName, array $variables) | ||
{ | ||
foreach ($this->templateRenderers as $templateRenderer) { | ||
if ($templateRenderer->supports($templateName)) { | ||
return $templateRenderer->render($templateName, $variables); | ||
} | ||
} | ||
|
||
throw new \Exception(sprintf('No template renderers for template "%s"', $templateName)); | ||
} | ||
} |
This file contains hidden or 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,23 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\MakerBundle; | ||
|
||
/** | ||
* | ||
* @author Jonathan Kablan <[email protected]> | ||
*/ | ||
interface MakerTemplateRendererInterface | ||
{ | ||
/** | ||
* @param string $templateName | ||
* @return bool | ||
*/ | ||
public function supports(string $templateName): bool; | ||
|
||
/** | ||
* @param string $templateName | ||
* @param array $variables | ||
* @return string | ||
*/ | ||
public function render(string $templateName, array $variables): string; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This information does not add value because we already have the type in the signature. You should delete them.
BTW, PHP-CS-Fixer should remove it 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay Max