diff --git a/README.md b/README.md index 2eac6ff..dcff2ff 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,11 @@ Testing in dummy IDE ```$xslt $ ./gradlew runide ``` + +Generating config from PHP sources +------------ +To generate config of this plugin with custom filters, functions and tags from your Latte extensions, use [contrib/CustomSources](contrib/CustomSources/). + +* In `contrib/CustomSources/`, run `composer i`. +* Edit `generate.php` to use your Latte extensions and run it. +* It will generate `latte.xml` which you can store in the `.idea/` directory of your project. diff --git a/contrib/CustomSources/.gitignore b/contrib/CustomSources/.gitignore new file mode 100644 index 0000000..d1502b0 --- /dev/null +++ b/contrib/CustomSources/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock diff --git a/contrib/CustomSources/composer.json b/contrib/CustomSources/composer.json new file mode 100644 index 0000000..a5f3ae8 --- /dev/null +++ b/contrib/CustomSources/composer.json @@ -0,0 +1,10 @@ +{ + "autoload": { + "psr-4": { + "IdeaLatteSupport\\": ["src/"] + } + }, + "require": { + "latte/latte": "^3.0" + } +} diff --git a/contrib/CustomSources/generate.php b/contrib/CustomSources/generate.php new file mode 100644 index 0000000..9a8bb2e --- /dev/null +++ b/contrib/CustomSources/generate.php @@ -0,0 +1,11 @@ +#!/usr/bin/env php +getFilters(); + $functions += $extension->getFunctions(); + $tags += $extension->getTags(); + } + + $params = [ + 'filters' => self::getCallables($filters), + 'functions' => self::getCallables($functions), + 'macros' => [], + ]; + + foreach ($tags as $name => $tag) { + $name = preg_replace('~^n:~', '', $name, count: $count) ?? $name; + $pair = null; + if (is_callable($tag)) { + $r = new \ReflectionFunction(\Closure::fromCallable($tag)); + $pair = (string) $r->getReturnType() === 'Generator' ? 'PAIR' : null; + } + $params['macros'][$name] = $count === 1 ? 'ATTR_ONLY' : $pair; + } + + $latte = new \Latte\Engine(); + return $latte->renderToString(__DIR__ . '/idea.latte', $params); + } + + /** + * @param array $callables + * @return array + */ + private static function getCallables(array $callables): array + { + $return = []; + + foreach ($callables as $name => $callable) { + $r = new \ReflectionFunction(\Closure::fromCallable($callable)); + $return[$name] = [ + 'returns' => (string) $r->getReturnType(), + 'params' => implode(', ', array_column($r->getParameters(), 'name')), + ]; + } + + return $return; + } + +} diff --git a/contrib/CustomSources/src/MyLatteExtension.php b/contrib/CustomSources/src/MyLatteExtension.php new file mode 100644 index 0000000..2873f43 --- /dev/null +++ b/contrib/CustomSources/src/MyLatteExtension.php @@ -0,0 +1,34 @@ + function () {}, + 'n:attrOnly' => function () {}, + 'pair' => function (): \Generator {}, + ]; + } + + /** @inheritdoc */ + public function getFilters(): array + { + return [ + 'myFilter' => function ($param) {}, + ]; + } + + /** @inheritdoc */ + public function getFunctions(): array + { + return [ + 'myFunction' => function ($param): bool {}, + ]; + } + +} diff --git a/contrib/CustomSources/src/idea.latte b/contrib/CustomSources/src/idea.latte new file mode 100644 index 0000000..b475f7d --- /dev/null +++ b/contrib/CustomSources/src/idea.latte @@ -0,0 +1,21 @@ +{contentType xml} + + + + + + + + + + + + + + + + + + + +