Created
February 13, 2016 18:54
-
-
Save mnapoli/e4acd8ae51285f2fce13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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