Skip to content

Commit 155e04c

Browse files
committed
Added support for custom templates
1 parent 4497b50 commit 155e04c

9 files changed

Lines changed: 58 additions & 13 deletions

File tree

Helper/Dir.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ public function template(): string
4242
{
4343
return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Template';
4444
}
45+
46+
/** Gets a filesystem path of the custom templates directory*/
47+
public function customTemplate(): ?string
48+
{
49+
$template = getenv('GEN_CLI_TEMPLATE');
50+
if ($template === false) {
51+
return null;
52+
}
53+
return rtrim($template, '/\\');
54+
}
4555
}

Helper/File.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class File extends \Magento\Framework\App\Helper\AbstractHelper
1616
public function __construct(
1717
Context $context,
1818
private FileDriver $fileDriver,
19+
private Dir $dir,
1920
) {
2021
parent::__construct($context);
2122
}
@@ -28,9 +29,30 @@ public function copy(string $source, string $destination): bool
2829

2930
/** Copy template from source to destination and replace the placeholders */
3031
public function copyTemplate(string $source, string $destination, array $placeholders = []): bool
32+
{
33+
$sourcePath = $this->join($this->dir->template(), $source);
34+
35+
// Check if file exists in custom templates directory
36+
if ($this->dir->customTemplate() !== null) {
37+
$customTemplatePath = $this->join($this->dir->customTemplate(), $source);
38+
if ($this->exists($customTemplatePath)) {
39+
$sourcePath = $customTemplatePath;
40+
}
41+
}
42+
43+
// Check if file exists
44+
if (!$this->exists($sourcePath)) {
45+
return false;
46+
}
47+
48+
return $this->copyAndReplaceTemplate($sourcePath, $destination, $placeholders);
49+
}
50+
51+
/** Copy template from source to destination and replace the placeholders */
52+
private function copyAndReplaceTemplate(string $absSource, string $destination, array $placeholders = []): bool
3153
{
3254
// Read file content
33-
$content = $this->read($source);
55+
$content = $this->read($absSource);
3456
// Replace placeholders
3557
foreach ($placeholders as $placeholder => $value) {
3658
$content = str_replace($placeholder, $value, $content);

Model/Block.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function exists(): bool
7171
public function copy(): bool
7272
{
7373
if (!$this->file->copyTemplate(
74-
$this->file->join($this->dir->template(), 'Block', 'Section', 'Action.php.template'),
74+
$this->file->join('Block', 'Section', 'Action.php.template'),
7575
$this->blockPath,
7676
[
7777
'{{ vendor }}' => $this->module->vendor(),
@@ -85,7 +85,7 @@ public function copy(): bool
8585
}
8686

8787
if (!$this->file->copyTemplate(
88-
$this->file->join($this->dir->template(), 'view', 'frontend', 'layout', 'vendor_module_section_action.xml.template'),
88+
$this->file->join('view', 'frontend', 'layout', 'vendor_module_section_action.xml.template'),
8989
$this->layoutPath,
9090
[
9191
'{{ module_name }}' => $this->module->moduleName(),
@@ -102,7 +102,7 @@ public function copy(): bool
102102
}
103103

104104
if (!$this->file->copyTemplate(
105-
$this->file->join($this->dir->template(), 'view', 'frontend', 'templates', 'section', 'action.phtml.template'),
105+
$this->file->join('view', 'frontend', 'templates', 'section', 'action.phtml.template'),
106106
$this->templatePath,
107107
[
108108
'{{ vendor }}' => $this->module->vendor(),

Model/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function exists(): bool
5959
public function copy(): bool
6060
{
6161
if (!$this->file->copyTemplate(
62-
$this->file->join($this->dir->template(), 'Console', 'Command', 'Command.php.template'),
62+
$this->file->join('Console', 'Command', 'Command.php.template'),
6363
$this->commandPath,
6464
[
6565
'{{ vendor }}' => $this->module->vendor(),
@@ -75,7 +75,7 @@ public function copy(): bool
7575

7676
if (!$this->file->exists($this->diXmlPath)) {
7777
if (!$this->file->copyTemplate(
78-
$this->file->join($this->dir->template(), 'etc', 'di.xml.template'),
78+
$this->file->join('etc', 'di.xml.template'),
7979
$this->diXmlPath,
8080
[],
8181
)) {

Model/Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function exists(): bool
5959
public function copy(): bool
6060
{
6161
if (!$this->file->copyTemplate(
62-
$this->file->join($this->dir->template(), 'Controller', 'Section', 'Action.php.template'),
62+
$this->file->join('Controller', 'Section', 'Action.php.template'),
6363
$this->controllerPath,
6464
[
6565
'{{ vendor }}' => $this->module->vendor(),
@@ -74,7 +74,7 @@ public function copy(): bool
7474

7575
if (!$this->file->exists($this->frontendXmlPath)) {
7676
if (!$this->file->copyTemplate(
77-
$this->file->join($this->dir->template(), 'etc', 'frontend', 'routes.xml.template'),
77+
$this->file->join('etc', 'frontend', 'routes.xml.template'),
7878
$this->frontendXmlPath,
7979
[
8080
'{{ module_name }}' => $this->module->moduleName(),

Model/Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function exists(): bool
5454
public function copy(): bool
5555
{
5656
if (!$this->file->copyTemplate(
57-
$this->file->join($this->dir->template(), 'Helper', 'Data.php.template'),
57+
$this->file->join('Helper', 'Data.php.template'),
5858
$this->helperPath,
5959
[
6060
'{{ vendor }}' => $this->module->vendor(),

Model/Module.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function copy(): bool
101101
// Source: https://experienceleague.adobe.com/en/docs/commerce-learn/tutorials/backend-development/create-module
102102

103103
if (!$this->file->copyTemplate(
104-
$this->file->join($this->dir->template(), 'composer.json.template'),
104+
'composer.json.template',
105105
$this->file->join($this->modulePath, 'composer.json'),
106106
[
107107
'{{ vendor }}' => $this->vendor(),
@@ -115,7 +115,7 @@ public function copy(): bool
115115
}
116116

117117
if (!$this->file->copyTemplate(
118-
$this->file->join($this->dir->template(), 'registration.php.template'),
118+
'registration.php.template',
119119
$this->file->join($this->modulePath, 'registration.php'),
120120
['{{ module_name }}' => $this->moduleName],
121121
)) {
@@ -124,7 +124,7 @@ public function copy(): bool
124124
}
125125

126126
if (!$this->file->copyTemplate(
127-
$this->file->join($this->dir->template(), 'etc', 'module.xml.template'),
127+
$this->file->join('etc', 'module.xml.template'),
128128
$this->file->join($this->modulePath, 'etc', 'module.xml'),
129129
['{{ module_name }}' => $this->moduleName],
130130
)) {

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
**Content**
44
- [Commands](#commands)
55
- [Usage](#usage)
6+
- [Custom templates](#custom-templates)
67
- [Installation](#installation)
78
- [Updating to latest version](#updating-to-latest-version)
89

@@ -30,6 +31,18 @@ Generating files...
3031
Module 'Magento/Sales' was created.
3132
```
3233

34+
## Custom templates
35+
Don't like the files the commands create?
36+
With custom templates you can create a folder structure like the `Template` directory of this module.
37+
You can just place the custom templates for the files you want to change.
38+
Otherwise the default templates from this module will be used.
39+
40+
The module will take the path from the environment variable `GEN_CLI_TEMPLATE`.
41+
42+
```bash
43+
$ export GEN_CLI_TEMPLATE="/var/www/html/app/code/My/Templates"
44+
```
45+
3346
## Installation
3447
This Magento2 module can be installed using composer:
3548
`> composer require masterzydra/magento2-gen-cli`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": "^8.1",
88
"magento/framework": "*"
99
},
10-
"version": "1.3.0",
10+
"version": "1.4.0",
1111
"license": "MIT",
1212
"autoload": {
1313
"files": [

0 commit comments

Comments
 (0)