From 66d0409032b354b37dbcca66f83bc30c16a9433a Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 1 Oct 2024 14:57:59 +0200 Subject: [PATCH] Add support for OctoberCMS / WinterCMS (#27) * Update Configuration.php Add support for hint paths configuration * Update ComponentTokenParser.php Is enabled hint path support, prepare the hint path * update hint param handle style, add test * update readme for OctoberCMS and WinterCMS * update readme for OctoberCMS and WinterCMS * update readme for OctoberCMS and WinterCMS * fixes --------- Co-authored-by: Giorgio Pogliani --- README.md | 37 ++++++++++++++++++ src/Configuration.php | 10 ++++- src/TokenParser/ComponentTokenParser.php | 11 ++++-- tests/ComponentTokenParserTest.php | 50 ++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/ComponentTokenParserTest.php diff --git a/README.md b/README.md index 194ada2..5f9457e 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,44 @@ final class TwigEnvironmentConfigurator } ``` +### OctoberCMS / WinterCMS + +In OctoberCMS / WinterCMS you need to hook into ```cms.page.beforedisplay``` event inside your plugin's boot method in order to access twig instance. +Then you can use your plugin hint path to choose a views subfolder as component's folder. + ## Usage +es. +``` +plugins +|__namespace + |__pluginname + |__views + |__components + |__button.htm +``` + +```php + public function boot(): void + { + Event::Listen('cms.page.beforeDisplay', function ($controller, $url, $page) { + $twig = $controller->getTwig(); + + Configuration::make($twig) + ->setTemplatesPath('namespace.pluginname::components', hint: true) + ->useCustomTags() + ->setup(); + }); + } +``` + +then in your htm files + +``` +... +``` + +Alle features like subfolders are supported, like `````` will refer to ```plugins/namespace/pluginname/views/forms/input.htm``` + The components are just Twig templates in a folder of your choice (e.g. `components`) and can be used anywhere in your Twig templates. The slot variable is any content you will add between the opening and the close tag. diff --git a/src/Configuration.php b/src/Configuration.php index b2a40b6..4077aa5 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -18,6 +18,8 @@ class Configuration protected string $templatesPath = 'components'; + protected bool $needsHintPath = false; + protected bool $isUsingTemplatesExtension = true; protected string $templatesExtension = 'twig'; @@ -40,9 +42,10 @@ public static function make(Environment $twig): Configuration * @param string $path * @return Configuration */ - public function setTemplatesPath(string $path): self + public function setTemplatesPath(string $path, bool $hint = false): self { $this->templatesPath = rtrim($path, DIRECTORY_SEPARATOR); + $this->needsHintPath = $hint; return $this; } @@ -52,6 +55,11 @@ public function getTemplatesPath(): string return $this->templatesPath; } + public function getNeedsHintPath(): bool + { + return $this->needsHintPath; + } + public function useTemplatesExtension(bool $isUsing = true): self { $this->isUsingTemplatesExtension = $isUsing; diff --git a/src/TokenParser/ComponentTokenParser.php b/src/TokenParser/ComponentTokenParser.php index 49eee2e..5485e5c 100644 --- a/src/TokenParser/ComponentTokenParser.php +++ b/src/TokenParser/ComponentTokenParser.php @@ -31,8 +31,13 @@ public function getComponentPath(string $name) $componentPath = rtrim($this->configuration->getTemplatesPath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name; - if ($this->configuration->isUsingTemplatesExtension()) { - $componentPath .= '.' . $this->configuration->getTemplatesExtension(); + if ($this->configuration->getNeedsHintPath()) { + $componentPath = rtrim($componentPath, '.'); + $componentPath = str_replace('/', '.', $componentPath); + } else { + if ($this->configuration->isUsingTemplatesExtension()) { + $componentPath .= '.' . $this->configuration->getTemplatesExtension(); + } } return $componentPath; @@ -40,7 +45,7 @@ public function getComponentPath(string $name) public function parse(Token $token): Node { - list($variables, $name) = $this->parseArguments(); + [$variables, $name] = $this->parseArguments(); $slot = $this->parser->subparse([$this, 'decideBlockEnd'], true); diff --git a/tests/ComponentTokenParserTest.php b/tests/ComponentTokenParserTest.php new file mode 100644 index 0000000..0fad788 --- /dev/null +++ b/tests/ComponentTokenParserTest.php @@ -0,0 +1,50 @@ +setTemplatesPath('mynamespace.myplugin::components', hint: true) + ->setTemplatesExtension('twig') + ->useCustomTags(); + + $this->assertTrue($config->getNeedsHintPath()); + $this->assertEquals($config->getTemplatesPath(), 'mynamespace.myplugin::components'); + $this->assertEquals($config->getTemplatesExtension(), 'twig'); + + $parser = new ComponentTokenParser($config); + $componentPath = $parser->getComponentPath('test/component'); + $this->assertEquals('mynamespace.myplugin::components.test.component', $componentPath); + } + + public function testGetComponentPathWithoutHintPath() + { + $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates'); + $twig = new \Twig\Environment($loader); + + $config = Configuration::make($twig) + ->setTemplatesPath('_components', hint: false) + ->setTemplatesExtension('twig') + ->useCustomTags(); + + $this->assertFalse($config->getNeedsHintPath()); + $this->assertEquals($config->getTemplatesPath(), '_components'); + $this->assertEquals($config->getTemplatesExtension(), 'twig'); + + $parser = new ComponentTokenParser($config); + + $componentPath = $parser->getComponentPath('test/component'); + + $this->assertEquals('_components/test/component.twig', $componentPath); + } +}