-
Notifications
You must be signed in to change notification settings - Fork 132
CLI command for adding a template generator to an add-on #4624
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
Merged
TomJaeger
merged 3 commits into
release/7.6.0
from
feature/7.dev/add-cli-generator-for-addon-template-generators
Mar 5, 2026
+409
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
72 changes: 72 additions & 0 deletions
72
system/ee/ExpressionEngine/Cli/Commands/CommandMakeTemplateGenerator.php
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,72 @@ | ||
| <?php | ||
| /** | ||
| * This source file is part of the open source project | ||
| * ExpressionEngine (https://expressionengine.com) | ||
| * | ||
| * @link https://expressionengine.com/ | ||
| * @copyright Copyright (c) 2003-2023, Packet Tide, LLC (https://www.packettide.com) | ||
| * @license https://expressionengine.com/license Licensed under Apache License, Version 2.0 | ||
| */ | ||
|
|
||
| namespace ExpressionEngine\Cli\Commands; | ||
|
|
||
| use ExpressionEngine\Cli\Cli; | ||
|
|
||
| /** | ||
| * Command to make action files for addons | ||
| */ | ||
| class CommandMakeTemplateGenerator extends Cli | ||
| { | ||
| /** | ||
| * name of command | ||
| * @var string | ||
| */ | ||
| public $name = 'Create Template Generator for Add-on'; | ||
|
|
||
| /** | ||
| * signature of command | ||
| * @var string | ||
| */ | ||
| public $signature = 'make:template-generator'; | ||
|
|
||
| /** | ||
| * How to use command | ||
| * @var string | ||
| */ | ||
| public $usage = 'php eecli.php make:template-generator --addon=my_existing_addon'; | ||
|
|
||
| /** | ||
| * options available for use in command | ||
| * @var array | ||
| */ | ||
| public $commandOptions = [ | ||
| 'addon,a:' => 'command_make_template_generator_option_addon', | ||
| ]; | ||
|
|
||
| protected $data = []; | ||
|
|
||
| /** | ||
| * Run the command | ||
| * @return mixed | ||
| */ | ||
| public function handle() | ||
| { | ||
| $this->info('command_make_template_generator_lets_build_template_generator'); | ||
|
|
||
| // Gather all the template generator information | ||
| $this->data['name'] = $this->getFirstUnnamedArgument("command_make_template_generator_ask_name", null, true); | ||
| $this->data['addon'] = $this->getOptionOrAskAddon('--addon', "command_make_template_generator_ask_addon"); | ||
|
|
||
| $this->info('command_make_template_generator_building_template_generator'); | ||
|
|
||
| try { | ||
| // Build the action | ||
| $service = ee('TemplateGeneratorGenerator', $this->data); | ||
| $service->build(); | ||
| } catch (\Exception $e) { | ||
| $this->fail(addslashes($e->getMessage())); | ||
| } | ||
|
|
||
| $this->info('command_make_template_generator_created_successfully'); | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
system/ee/ExpressionEngine/Service/Generator/TemplateGeneratorGenerator.php
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,129 @@ | ||
| <?php | ||
| /** | ||
| * This source file is part of the open source project | ||
| * ExpressionEngine (https://expressionengine.com) | ||
| * | ||
| * @link https://expressionengine.com/ | ||
| * @copyright Copyright (c) 2003-2023, Packet Tide, LLC (https://www.packettide.com) | ||
| * @license https://expressionengine.com/license Licensed under Apache License, Version 2.0 | ||
| */ | ||
|
|
||
| namespace ExpressionEngine\Service\Generator; | ||
|
|
||
| use ExpressionEngine\Library\Filesystem\Filesystem; | ||
| use ExpressionEngine\Library\Filesystem\FilesystemException; | ||
| use ExpressionEngine\Library\String\Str; | ||
|
|
||
| class TemplateGeneratorGenerator | ||
| { | ||
| public $name; | ||
| public $addon; | ||
| protected $filesystem; | ||
| protected $str; | ||
| protected $addonPath; | ||
| protected $stubPath; | ||
| protected $templateGeneratorName; | ||
| protected $templateGeneratorsPath; | ||
| protected $namespace; | ||
|
|
||
| public function __construct(Filesystem $filesystem, Str $str, array $data) | ||
| { | ||
| // Set FS and String library | ||
| $this->filesystem = $filesystem; | ||
| $this->str = $str; | ||
|
|
||
| // // Set required data for generator to use | ||
| $this->templateGeneratorName = $this->str->studly($data['name']); | ||
| $this->addon = $this->str->snakecase($data['addon']); | ||
|
|
||
| // Set up addon path, generator path, and stub path | ||
| $this->init(); | ||
|
|
||
| $addonSetupArray = require $this->addonPath . 'addon.setup.php'; | ||
| $this->namespace = $addonSetupArray['namespace']; | ||
| } | ||
|
|
||
| private function init() | ||
| { | ||
| $this->stubPath = SYSPATH . 'ee/ExpressionEngine/Service/Generator/stubs/MakeAddon/TemplateGenerators/'; | ||
| $this->addonPath = SYSPATH . 'user/addons/' . $this->addon . '/'; | ||
| $this->templateGeneratorsPath = SYSPATH . 'user/addons/' . $this->addon . '/'; | ||
|
|
||
| // Make sure the addon exists | ||
| if (! ee('Addon')->get($this->addon)) { | ||
| throw new \Exception(lang('cli_error_the_specified_addon_does_not_exist'), 1); | ||
| } | ||
| } | ||
|
|
||
| public function build() | ||
| { | ||
| // Build the template generator | ||
| $templateGeneratorStub = $this->filesystem->read($this->stub('TemplateGenerator.php')); | ||
| $templateGeneratorStub = $this->write('namespace', ucfirst($this->namespace), $templateGeneratorStub); | ||
| $templateGeneratorStub = $this->write('TemplateGeneratorName', $this->templateGeneratorName, $templateGeneratorStub); | ||
|
|
||
| $this->putFile('TemplateGenerators/' . $this->templateGeneratorName . '.php', $templateGeneratorStub); | ||
|
|
||
| // Build the stub template | ||
| $generatedTemplateStub = $this->filesystem->read($this->stub('GeneratedTemplate.php')); | ||
| $generatedTemplateStub = $this->write('addon_shortname', $this->addon, $generatedTemplateStub); | ||
|
|
||
| $this->putFile('stubs/' . $this->templateGeneratorName . '/index.php', $generatedTemplateStub); | ||
|
|
||
| // add the template generator to the addon.setup.php file | ||
| $this->addTemplateGeneratorToAddonSetup(); | ||
| } | ||
|
|
||
| private function stub($file) | ||
| { | ||
| return $this->stubPath . $file; | ||
| } | ||
|
|
||
| private function write($key, $value, $file) | ||
| { | ||
| return str_replace('{{' . $key . '}}', $value, $file); | ||
| } | ||
|
|
||
| private function putFile($name, $contents, $path = null) | ||
| { | ||
| if ($path) { | ||
| $path = trim($path, '/') . '/'; | ||
| } else { | ||
| $path = ''; | ||
| } | ||
|
|
||
| if (!$this->filesystem->exists($this->addonPath . $path . $name)) { | ||
| $this->filesystem->write($this->addonPath . $path . $name, $contents); | ||
| } | ||
| } | ||
|
|
||
| private function addTemplateGeneratorToAddonSetup() | ||
| { | ||
| try { | ||
| $addonSetupFile = $this->filesystem->read($this->addonPath . 'addon.setup.php'); | ||
| } catch (FilesystemException $e) { | ||
| return false; | ||
| } catch (\Exception $e) { | ||
| return false; | ||
| } | ||
|
|
||
| $addonSetupArray = require $this->addonPath . 'addon.setup.php'; | ||
|
|
||
| // Parse TemplateGenerator Stub | ||
| $templateGeneratorNameString = " '" . $this->templateGeneratorName . "',"; | ||
| $templateGeneratorAddonSetupStub = $this->filesystem->read($this->stub('template_generator.addon.php')); | ||
| $templateGeneratorAddonSetupStub = $this->write('generator_name', $templateGeneratorNameString, $templateGeneratorAddonSetupStub); | ||
|
|
||
| // The add-on setup has the templateGenerators array | ||
| if (array_key_exists('templateGenerators', $addonSetupArray)) { | ||
| $pattern = "/(templateGenerators)([^=]+)(=>\s)(array\(|\[)([^\S]*)([\s])([\s\S]*)$/"; | ||
| $addonSetupFile = preg_replace($pattern, "$1$2$3$4\n$templateGeneratorNameString$5$6$7", $addonSetupFile); | ||
| $this->filesystem->write($this->addonPath . 'addon.setup.php', $addonSetupFile, true); | ||
| } else { | ||
| // The add-on setup does not have the templateGenerators array | ||
| $pattern = '/(,)([^,]+)$/'; | ||
| $addonSetupFile = preg_replace($pattern, ",\n $templateGeneratorAddonSetupStub $2", $addonSetupFile); | ||
| $this->filesystem->write($this->addonPath . 'addon.setup.php', $addonSetupFile, true); | ||
| } | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
...pressionEngine/Service/Generator/stubs/MakeAddon/TemplateGenerators/GeneratedTemplate.php
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,4 @@ | ||
| {!-- Write your tag logic here, accessing PHP variables from the getVariables() function of the generator --} | ||
| {exp:{{addon_shortname}}:tag channel="<?=$channel?>" numbers="<?=implode('|', $numbers);?>" color="<?=$color?>"} | ||
| {title} - {path=<?=$template_group?>/entry/{url_title}} | ||
| {/exp:{{addon_shortname}}:tag} |
Oops, something went wrong.
Oops, something went wrong.
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.
trying to remember if we moved away form studly for this?