Skip to content

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
wants to merge 6 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $

return $entityPath;
}
}
}
108 changes: 103 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -64,6 +77,11 @@ public function generateClass(string $className, string $templateName, array $va

/**
* Generate a normal file from a template.
*
* @param string $targetPath
Copy link
Contributor

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 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay Max

* @param string $templateName
* @param array $variables
* @throws \Exception
*/
public function generateFile(string $targetPath, string $templateName, array $variables = [])
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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));
Expand All @@ -173,6 +206,9 @@ private function addOperation(string $targetPath, string $templateName, array $v
];
}

/**
* @return bool
*/
public function hasPendingOperations(): bool
{
return !empty($this->pendingOperations);
Expand All @@ -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(
Expand All @@ -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 = [])
{
Expand All @@ -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;
}
}
46 changes: 46 additions & 0 deletions src/MakerDefaultTemplateRenderer.php
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;
}
}
36 changes: 36 additions & 0 deletions src/MakerRenderer.php
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));
}
}
23 changes: 23 additions & 0 deletions src/MakerTemplateRendererInterface.php
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;
}