diff --git a/src/Doctrine/EntityClassGenerator.php b/src/Doctrine/EntityClassGenerator.php index 57757199d..53b149499 100644 --- a/src/Doctrine/EntityClassGenerator.php +++ b/src/Doctrine/EntityClassGenerator.php @@ -60,4 +60,4 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $ return $entityPath; } -} +} \ No newline at end of file diff --git a/src/Generator.php b/src/Generator.php index b56377639..d403acd11 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -18,6 +18,7 @@ /** * @author Javier Eguiluz * @author Ryan Weaver + * @editor jonathan Kablan */ class Generator { @@ -25,12 +26,24 @@ class Generator 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; + } } diff --git a/src/MakerDefaultTemplateRenderer.php b/src/MakerDefaultTemplateRenderer.php new file mode 100644 index 000000000..e28819da4 --- /dev/null +++ b/src/MakerDefaultTemplateRenderer.php @@ -0,0 +1,46 @@ +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; + } +} \ No newline at end of file diff --git a/src/MakerRenderer.php b/src/MakerRenderer.php new file mode 100644 index 000000000..d26058b57 --- /dev/null +++ b/src/MakerRenderer.php @@ -0,0 +1,36 @@ +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)); + } +} \ No newline at end of file diff --git a/src/MakerTemplateRendererInterface.php b/src/MakerTemplateRendererInterface.php new file mode 100644 index 000000000..e2c58385a --- /dev/null +++ b/src/MakerTemplateRendererInterface.php @@ -0,0 +1,23 @@ + + */ +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; +} \ No newline at end of file