-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add snippet generator (WIP) #89
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
Draft
imfelixyeung
wants to merge
10
commits into
main
Choose a base branch
from
feat/snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
471cc3f
feat: add php file generator
imfelixyeung e851113
feat: alias base class with use keyword
imfelixyeung 3189e62
feat: make task generator generic to support both scheduled and adhoc…
imfelixyeung c3b7bca
refactor: move argument validation to constructor
imfelixyeung 1e6016a
feat: add cli command `make`
imfelixyeung f94dd46
fix: call method on instance instead of statically
imfelixyeung c060eb5
fix: php_class_info handles files directly in classes/
imfelixyeung ac37f30
fix: handle null component resolution in constructor
imfelixyeung 01ce611
feat: injects license at top of file
imfelixyeung 02af416
feat: add standard class file generator
imfelixyeung 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
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,77 @@ | ||
| <?php | ||
| // This file is part of Moodle - https://moodle.org/ | ||
| // | ||
| // Moodle is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Moodle is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| namespace local_devkit\local\cli\commands; | ||
|
|
||
| use local_devkit\local\format\base as format_base; | ||
| use local_devkit\local\format\phpcbf; | ||
| use local_devkit\local\format\pint; | ||
| use local_devkit\local\generators\snippets\base as snippet_base; | ||
| use local_devkit\local\generators\snippets\php_class; | ||
| use Symfony\Component\Console\Attribute\Argument; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| /** | ||
| * Command to generate boilerplates. | ||
| * | ||
| * @package local_devkit | ||
| * @copyright 2026 Felix | ||
| * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| */ | ||
| #[AsCommand( | ||
| name: 'make', | ||
| description: 'Make boilerplate.', | ||
| )] | ||
| class make extends Command { | ||
| /** | ||
| * Invoke. | ||
| */ | ||
| public function __invoke( | ||
| #[Argument('Path to new file.')] | ||
| string $path, | ||
| SymfonyStyle $io, | ||
| InputInterface $input, | ||
| OutputInterface $output, | ||
| ): int { | ||
| [$generatorclass, $formatterclassnames] = $this->get_generator_and_formatter_classnames(); | ||
|
|
||
| // The generators need the file to actually exist. | ||
| @mkdir(dirname($path)); | ||
| file_put_contents($path, ''); | ||
|
|
||
| $generator = new $generatorclass($path); | ||
| $contents = $generator->generate(); | ||
| file_put_contents($path, $contents); | ||
|
|
||
| foreach ($formatterclassnames as $formatterclassname) { | ||
| \core\di::get($formatterclassname)::format($path); | ||
| } | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * Summary of get_generator_and_formatters | ||
| * @return array{class-string<snippet_base>, class-string<format_base>[]} | ||
| */ | ||
| private function get_generator_and_formatter_classnames() { | ||
| return [php_class::class, [pint::class, phpcbf::class]]; | ||
| } | ||
| } |
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
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,154 @@ | ||
| <?php | ||
| // This file is part of Moodle - https://moodle.org/ | ||
| // | ||
| // Moodle is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Moodle is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| namespace local_devkit\local\generators\snippets; | ||
|
|
||
| use local_devkit\local\component; | ||
| use local_devkit\local\generators\boilerplate; | ||
| use local_devkit\local\utils; | ||
| use Nette\PhpGenerator\ClassType; | ||
| use Nette\PhpGenerator\PhpFile; | ||
| use Nette\PhpGenerator\PhpNamespace; | ||
| use Nette\PhpGenerator\Printer; | ||
|
|
||
| /** | ||
| * Class base | ||
| * | ||
| * @package local_devkit | ||
| * @copyright 2026 Felix | ||
| * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| */ | ||
| abstract class base { | ||
| /** @var string */ | ||
| protected readonly string $filepath; | ||
| /** @var string */ | ||
| protected readonly string $component; | ||
| /** @var string */ | ||
| protected readonly string $copyright; | ||
| /** @var string|null */ | ||
| protected ?string $category = null; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| */ | ||
| public function __construct(string $filepath) { | ||
| $this->filepath = utils::get_path_relative_to_moodle_root($filepath); | ||
| $component = component::resolve_component_from_path($this->filepath); | ||
| if ($component === null) { | ||
| throw new \InvalidArgumentException("Could not resolve component for path: $this->filepath"); | ||
| } | ||
| $this->component = $component; | ||
|
|
||
| $year = date("Y"); | ||
| $this->copyright = "$year Your name"; | ||
| } | ||
|
|
||
| /** | ||
| * Generates the snippet. | ||
| */ | ||
| abstract public function generate(): string; | ||
|
|
||
| /** | ||
| * Standard PHP File. | ||
| */ | ||
| protected function php_file(): PhpFile { | ||
| return new PhpFile(); | ||
| } | ||
|
|
||
| /** | ||
| * PHP Printer. | ||
| */ | ||
| protected function php_printer(): Printer { | ||
| $printer = new Printer(); | ||
| $printer->indentation = ' '; | ||
| $printer->bracesOnNextLine = false; | ||
| $printer->linesBetweenMethods = 1; | ||
| return $printer; | ||
| } | ||
|
|
||
| /** | ||
| * Prints a php file, injects license info at top of file. | ||
| */ | ||
| protected function print_php_file(PhpFile $file): string { | ||
| $printer = $this->php_printer(); | ||
|
|
||
| $phptag = '<?php'; | ||
| $license = boilerplate::generate_for_php(true); | ||
| $content = $printer->printFile($file); | ||
| return str_replace($phptag, "$phptag\n$license", $content); | ||
| } | ||
|
|
||
| /** | ||
| * Standard PHP File with class. | ||
| * @return array{PhpFile, PhpNamespace, ClassType} | ||
| */ | ||
| protected function php_file_with_namespaced_class(): array { | ||
| $file = $this->php_file(); | ||
| [$namespacename, $classname] = $this->php_class_info(); | ||
| $namespace = $file->addNamespace($namespacename); | ||
| $class = $namespace->addClass($classname); | ||
| $this->php_class_add_docblock_tags($class, $this->category); | ||
| return [$file, $namespace, $class]; | ||
| } | ||
|
|
||
| /** | ||
| * Adds docblock to class. | ||
| */ | ||
| protected function php_class_add_docblock_tags( | ||
| ClassType $class, | ||
| ?string $category = null, | ||
| ): ClassType { | ||
| $license = 'https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later'; | ||
|
|
||
| if (!$class->getComment()) { | ||
| $class->addComment("Class {$class->getName()}."); | ||
| } | ||
|
|
||
| $class->addComment(''); | ||
| $class->addComment("@package $this->component"); | ||
|
|
||
| if ($category) { | ||
| $class->addComment("@category $category"); | ||
| } | ||
|
|
||
| $class->addComment("@copyright $this->copyright"); | ||
| $class->addComment("@license $license"); | ||
| return $class; | ||
| } | ||
|
|
||
| /** | ||
| * Deduces the namespace and classname for a given path. | ||
| * @return array{string, string} | ||
| */ | ||
| protected function php_class_info(): array { | ||
| $dirpath = dirname($this->filepath); | ||
| $classname = basename($this->filepath, '.php'); | ||
| $componentpath = utils::get_path_relative_to_moodle_root( | ||
| \core_component::get_component_directory($this->component), | ||
| ); | ||
|
|
||
| $classesdir = "$componentpath/classes"; | ||
| if (str_starts_with($dirpath, $classesdir)) { | ||
| $namespace = substr($dirpath, strlen($classesdir) + 1); | ||
| } else { | ||
| $namespace = ''; | ||
| } | ||
| $namespace = str_replace('/', '\\', $namespace); | ||
| $namespace = rtrim("$this->component\\$namespace", '\\'); | ||
|
|
||
| return [$namespace, $classname]; | ||
| } | ||
| } |
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,32 @@ | ||
| <?php | ||
| // This file is part of Moodle - https://moodle.org/ | ||
| // | ||
| // Moodle is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Moodle is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| namespace local_devkit\local\generators\snippets; | ||
|
|
||
| /** | ||
| * Snippet for a standard PHP class file. | ||
| * | ||
| * @package local_devkit | ||
| * @copyright 2026 Felix | ||
| * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| */ | ||
| class php_class extends base { | ||
| #[\Override] | ||
| public function generate(): string { | ||
| [$file] = $this->php_file_with_namespaced_class(); | ||
| return $this->print_php_file($file); | ||
| } | ||
| } |
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,71 @@ | ||
| <?php | ||
| // This file is part of Moodle - https://moodle.org/ | ||
| // | ||
| // Moodle is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Moodle is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| namespace local_devkit\local\generators\snippets; | ||
|
|
||
| use Nette\PhpGenerator\ClassManipulator; | ||
|
|
||
| use function in_array; | ||
|
|
||
| /** | ||
| * Class task | ||
| * | ||
| * @package local_devkit | ||
| * @copyright 2026 Felix | ||
| * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| */ | ||
| class task extends php_class { | ||
| /** | ||
| * Constructor. | ||
| */ | ||
| public function __construct( | ||
| string $filepath, | ||
| /** @var 'scheduled'|'adhoc' $tasktype */ | ||
| private readonly string $tasktype = 'scheduled', | ||
| ) { | ||
| if (!in_array($tasktype, ['scheduled', 'adhoc'])) { | ||
| throw new \InvalidArgumentException('Invalid task type'); | ||
| } | ||
| parent::__construct($filepath); | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function generate(): string { | ||
| $this->category = 'task'; | ||
| [$file, $namespace, $class] = $this->php_file_with_namespaced_class(); | ||
|
|
||
| $baseclass = match ($this->tasktype) { | ||
| 'scheduled' => \core\task\scheduled_task::class, | ||
| 'adhoc' => \core\task\adhoc_task::class, | ||
| }; | ||
| $namespace->addUse($baseclass); | ||
|
|
||
| $class->setExtends($baseclass); | ||
| $manipulator = new ClassManipulator($class); | ||
|
|
||
| $getname = $manipulator->inheritMethod('get_name'); | ||
| $getname->removeComment(); | ||
| $getname->addAttribute('Override'); | ||
| $getname->addBody('return get_string(?, ?);', ["task:{$class->getName()}", $this->component]); | ||
|
|
||
| $execute = $manipulator->inheritMethod('execute'); | ||
| $execute->removeComment(); | ||
| $execute->addAttribute('Override'); | ||
| $class->setMethods([$getname, $execute]); | ||
|
|
||
| return $this->print_php_file($file); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.