Skip to content

Commit 11233a8

Browse files
committed
add customizable templates path
1 parent 81eb8d9 commit 11233a8

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<argument /> <!-- root namespace -->
5858
<argument>null</argument> <!-- PhpCompatUtil -->
5959
<argument type="service" id="maker.template_component_generator" />
60+
<argument /> <!-- templates_folders -->
6061
</service>
6162

6263
<service id="maker.entity_class_generator" class="Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator">

src/Generator.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(
3333
private string $namespacePrefix,
3434
?PhpCompatUtil $phpCompatUtil = null,
3535
private ?TemplateComponentGenerator $templateComponentGenerator = null,
36+
private array $templatesFolders = [],
3637
) {
3738
$this->twigHelper = new GeneratorTwigHelper($fileManager);
3839
$this->namespacePrefix = trim($namespacePrefix, '\\');
@@ -299,11 +300,7 @@ private function addOperation(string $targetPath, string $templateName, array $v
299300

300301
$templatePath = $templateName;
301302
if (!file_exists($templatePath)) {
302-
$templatePath = \sprintf('%s/templates/%s', \dirname(__DIR__), $templateName);
303-
304-
if (!file_exists($templatePath)) {
305-
$templatePath = $this->getTemplateFromLegacySkeletonPath($templateName);
306-
}
303+
$templatePath = $this->getTemplatePath($templatePath);
307304

308305
if (!file_exists($templatePath)) {
309306
throw new \Exception(\sprintf('Cannot find template "%s" in the templates/ dir.', $templateName));
@@ -316,6 +313,25 @@ private function addOperation(string $targetPath, string $templateName, array $v
316313
];
317314
}
318315

316+
private function getTemplatePath(string $templateName): string
317+
{
318+
foreach($this->templatesFolders as $folder) {
319+
$templatePath = \sprintf('%s/%s', $folder, $templateName);
320+
321+
if ($this->fileManager->fileExists($templatePath)) {
322+
return $templatePath;
323+
}
324+
}
325+
326+
$templatePath = \sprintf('%s/templates/%s', \dirname(__DIR__), $templateName);
327+
328+
if (!file_exists($templatePath)) {
329+
return $this->getTemplateFromLegacySkeletonPath($templateName);
330+
}
331+
332+
return $templatePath;
333+
}
334+
319335
/**
320336
* @legacy - Remove when public generate methods become "internal" to MakerBundle in v2
321337
*/

src/MakerBundle.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function configure(DefinitionConfigurator $definition): void
3535
->scalarNode('root_namespace')->defaultValue('App')->end()
3636
->booleanNode('generate_final_classes')->defaultTrue()->end()
3737
->booleanNode('generate_final_entities')->defaultFalse()->end()
38+
->arrayNode('templates_folders')
39+
->defaultValue([])
40+
->prototype('scalar')->end()
41+
->end()
3842
->end()
3943
;
4044
}
@@ -51,6 +55,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
5155
->arg(0, $rootNamespace)
5256
->get('maker.generator')
5357
->arg(1, $rootNamespace)
58+
->arg(4, $config['templates_folders'])
5459
->get('maker.doctrine_helper')
5560
->arg(0, \sprintf('%s\\Entity', $rootNamespace))
5661
->get('maker.template_component_generator')

0 commit comments

Comments
 (0)