pattern-lab / patternengine-php-twig

Twig-based PatternEngine for Pattern Lab.
http://patternlab.io/
MIT License
78 stars 36 forks source link

Provide a way to register a Twig_ExtensionInterface #16

Closed aleksip closed 8 years ago

aleksip commented 8 years ago

Especially for use cases where many StarterKits use the same Twig extensions it would be good to be able to register a class implementing Twig_ExtensionInterface.

aleksip commented 8 years ago

A solution for this would also resolve #8!

dmolsen commented 8 years ago

Do we want to enforce a convention that Twig extensions must have a Twig_Extensions or TwigExtensions as part of the class name to be loaded? We could look at declared/autoloaded classes and cherry pick out those ones that match that convention and use addExtension.

Another much more complicated solution I thought of before the simpler idea above...

Is it worth doing a combo of either naming convention (a la PatternLabListener) or composer.json config to build out a listeners.json style list of Twig extensions? The list would be modified at install of the extensions. Then that list of classes/extensions could be loaded in the Twig pattern loader.

I know we've had load order issues but it might be worth exploring the following changes:

  1. In InstallerUtil reload listeners after they've been added so they're available for future package installs
  2. In InstallerUtil Dispatch event after package install that includes the path to the installed package as well as the extra config.
  3. In Twig PatternEngine add the install listener. It will take the package info to determine if it was an extension related to it (look at config or try to find the named file in the package) and to store it in it's own config like ./config/twig.json. This way each pattern engine could determine this kind of thing on their own and I don't bake anything specific into core.
  4. When the pattern loader is... well, loaded it should read in ./config/twig.json and use that to load them.

It seems like a slightly indirect solution. I think extensions would have to live in ./vendor which may be a nonstarter for you all. The class needs to be available via the autoloader.

Just providing a starting point for ideas.

dmolsen commented 8 years ago

Nix that last idea. Going with #22 and #23 to see if that can address this problem.

dmolsen commented 8 years ago

The Twig Extensions Plugin shows a way that this can be done.

aleksip commented 8 years ago

@dmolsen Excellent solution! This is so great, thanks!