-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
79 lines (69 loc) · 2.47 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace OmekaTwig;
require __DIR__ . '/vendor/autoload.php';
use Omeka\Module\AbstractModule;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\View\Exception\InvalidArgumentException;
use OmekaTwig\Renderer\TwigRenderer;
use Zend\Mvc\MvcEvent;
class Module extends AbstractModule
{
const MODULE_NAME = 'omeka_twig';
public function onBootstrap(MvcEvent $e)
{
// Remember to keep the init() method as lightweight as possible
$events = $e->getApplication()->getEventManager();
$events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'), -10000);
}
/**
* Listen to the bootstrap event
*
* @param \Zend\Mvc\MvcEvent|EventInterface $e
*
* @return array
*/
public function onRoute(MvcEvent $e)
{
$app = $e->getApplication();
$container = $app->getServiceManager();
/**
* @var \Twig_Environment $env
*/
$config = $container->get('Configuration');
$env = $container->get('Twig_Environment');
$name = static::MODULE_NAME;
$options = $envOptions = empty($config[$name]) ? [] : $config[$name];
$extensions = empty($options['extensions']) ? [] : $options['extensions'];
$renderer = $container->get(TwigRenderer::class);
// Setup extensions
foreach ($extensions as $extension) {
// Allows modules to override/remove extensions.
if (empty($extension)) {
continue;
} elseif (is_string($extension)) {
if ($container->has($extension)) {
$extension = $container->get($extension);
} else {
$extension = new $extension($container, $renderer);
}
} elseif (!is_object($extension)) {
throw new InvalidArgumentException('Extensions should be a string or object.');
}
if (!$env->hasExtension(get_class($extension))) {
$env->addExtension($extension);
}
}
$container->get(View\TwigStrategy::class)->setRenderer($renderer);
}
/**
* Returns configuration to merge with application configuration
*
* @return array|\Traversable
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}