fvsch / kirby-twig

Twig templating support for Kirby CMS 2. For Kirby 3, use https://github.com/amteich/kirby-twig
MIT License
70 stars 8 forks source link

Complete install with Composer #7

Closed fvsch closed 7 years ago

fvsch commented 8 years ago

For projects that are set up with Composer, could we install this plugin that way?

Dual install method: check if Twig_Environment class exists (it should if we installed the plugin through Composer), try to require plugin/twig/vendor/autoload.php otherwise.

fvsch commented 8 years ago

In 1.3 partial support for Composer was removed, and the Twig library copied in this project.

It might be interesting to provide a Composer-only install, but perhaps in a separate branch which doesn't have the Twig code.

thasmo commented 8 years ago

It should be possible to install your plugin via Composer. But I highly recommend to not put the Twig source into your repository. It should just installed via Composer as well.

fvsch commented 8 years ago

Yeah, I’m using this technique to install it as well. But it’s a bit complex as you have to manually manage version numbers in two places in a big composer.json. I might document this technique in the repo though.

For not bundling the Twig source in this repo: I disagree. I know that’s standard practice in most PHP developer communities, but the Kirby community is overwhelmingly made of beginners, designers, HTML tinkerers, non-developers, and a few PHP devs. Most do not know Composer at all and have no interest in it. Plugins are usually installed by downloading a zip file. Depending on Composer for installing a Kirby plugin restricts the audience of that plugin greatly.

If Kirby and/or the Kirby ecosystem move to a Composer-based install, I’ll update accordingly. :)

thasmo commented 8 years ago

Understandable. It would be nice if the Kirby CLI could wrap Composer and it's complexity so it's easier for people to install plugins - or any other way to enjoy the advantages of both worlds.

fvsch commented 8 years ago

I’ve added a composer.json and published fvsch/kirby-twig on Packagist. https://packagist.org/packages/fvsch/kirby-twig

The plugin itself is not autoloaded (it would probably cause errors, unless Kirby’s classes are autoloaded too). This mostly lets Composer download the plugin’s code to vendor/fvsch/kirby-twig. Then, in a project I’m loading it like this:

<?php
// This could be situated at site/plugins/vendor.php for example,
// or in your `site.php` at the website’s root.

/**
 * Include main plugin files from Composer's vendor dir.
 * We’re copying the plugin loading logic from Kirby::plugin.
 */

$root = $kirby->roots()->index . '/vendor';

$plugins = [
    'staticbuilder' => $root . '/fvsch/kirby-staticbuilder/staticbuilder.php',
    'twig' => $root . '/fvsch/kirby-twig/twig.php',
    'typography' => $root . '/fabianmichael/kirby-typography/typography.php'
];

foreach ($plugins as $name=>$file) {
    if (!isset($kirby->plugins[$name]) && file_exists($file)) {
        $kirby->plugins[$name] = include_once($file);
    }
}

(A simple include would work too, of course.)

This works well, and I’d like to enable this kind of installation and benefit from Composer more fully (pulling twig/twig as a dependency). But if I want to keep the simple zip install too, I would need to do that on a separate branch.

thasmo commented 8 years ago

You could upload a pre-packaged zip-file which includes all dependencies, for every release so people can download the pre-packaged release or install it via Composer.

fvsch commented 8 years ago

Yeah, something like:

  1. Maintain main code on master branch, with composer.json requiring twig/twig.
  2. Maintain a separate classic-install branch with the Twig library files.
  3. Make releases from the master branch, adding a zip from the classic-install branch as a binary.
fvsch commented 8 years ago

I’ll work on this once Kirby core can be installed with Composer from Packagist, which should happen for Kirby 2.4. I’m waiting for a 2.4 beta, at least, to be able to test it.

fvsch commented 7 years ago

So we already have Composer install support, but what needs to be improved is:

  1. Avoid downloading the lib/Twig folder altogether, relying on Composer for the Twig dependency instead. I plan to achieve that using a separate branch which won’t have the lib folder at all. Git version tags will point to this branch rather than to master, since they’re only used by Composer/Packagist anyway.

  2. The plugin will need to be registered by users, with: Kirby\Twig\Plugin::register(); Still need to figure out if this needs to happen in site.php or if site/config/config.php is alright (I would prefer that).

fvsch commented 7 years ago

@thasmo If you have some time to try out v3.0 beta, it should deal will the issues mentioned here. https://github.com/fvsch/kirby-twig/releases/tag/v3.0.0-beta1