Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Created February 13, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnapoli/e4acd8ae51285f2fce13 to your computer and use it in GitHub Desktop.
Save mnapoli/e4acd8ae51285f2fce13 to your computer and use it in GitHub Desktop.
<?php
namespace TwigModule;
use Interop\Container\ContainerInterface;
use Interop\Container\ServiceProvider\ServiceProvider;
use Puli\Repository\Api\ResourceRepository;
use Puli\TwigExtension\PuliExtension;
use Puli\TwigExtension\PuliTemplateLoader;
use Puli\UrlGenerator\Api\UrlGenerator;
use Twig_Environment;
class TwigServiceProvider implements ServiceProvider
{
public static function getServices()
{
return [
Twig_Environment::class => 'getTwig',
'twig.options' => 'getOptions',
'twig.extensions' => 'getExtensions',
'twig.loader' => 'getLoader',
// Puli integration
PuliExtension::class => 'getPuliExtension',
];
}
public function getTwig(ContainerInterface $container)
{
$loader = $container->get('twig.loader');
$options = $container->get('twig.options');
$twig = new Twig_Environment($loader, $options);
$twig->setExtensions($container->get('twig.extensions'));
return $twig;
}
/**
* Twig options
*
* Override the entry to set options.
* @see http://twig.sensiolabs.org/doc/api.html#environment-options
*/
public function getOptions()
{
return [];
}
/**
* Returns the list of Twig extensions to use.
*
* Override the entry to register additional extensions.
*
* @return \Twig_ExtensionInterface[]
*/
public function getExtensions(ContainerInterface $container)
{
return [
$container->get(PuliExtension::class),
];
}
/**
* @return \Twig_LoaderInterface
*/
public function getLoader(ContainerInterface $container)
{
$resourceRepository = $container->get(ResourceRepository::class);
return new PuliTemplateLoader($resourceRepository);
}
/**
* @return PuliExtension
*/
public function getPuliExtension(ContainerInterface $container)
{
$resourceRepository = $container->get(ResourceRepository::class);
$urlGenerator = null;
if ($container->has(UrlGenerator::class)) {
$urlGenerator = $container->get(UrlGenerator::class);
}
return new PuliExtension($resourceRepository, $urlGenerator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment