1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\ApplicationDI;
9:
10: use Nette;
11: use Latte;
12:
13:
14: 15: 16:
17: class LatteExtension extends Nette\DI\CompilerExtension
18: {
19: public $defaults = array(
20: 'xhtml' => FALSE,
21: 'macros' => array(),
22: );
23:
24:
25: private $debugMode;
26:
27:
28: private $tempDir;
29:
30:
31: public function __construct($tempDir, $debugMode = FALSE)
32: {
33: $this->tempDir = $tempDir;
34: $this->debugMode = $debugMode;
35: }
36:
37:
38: public function loadConfiguration()
39: {
40: if (!class_exists('Latte\Engine')) {
41: return;
42: }
43:
44: $config = $this->validateConfig($this->defaults);
45: $container = $this->getContainerBuilder();
46:
47: $latteFactory = $container->addDefinition($this->prefix('latteFactory'))
48: ->setClass('Latte\Engine')
49: ->addSetup('setTempDirectory', array($this->tempDir))
50: ->addSetup('setAutoRefresh', array($this->debugMode))
51: ->addSetup('setContentType', array($config['xhtml'] ? Latte\Compiler::CONTENT_XHTML : Latte\Compiler::CONTENT_HTML))
52: ->addSetup('Nette\Utils\Html::$xhtml = ?', array((bool) $config['xhtml']))
53: ->setImplement('Nette\Bridges\ApplicationLatte\ILatteFactory');
54:
55: $container->addDefinition($this->prefix('templateFactory'))
56: ->setClass('Nette\Application\UI\ITemplateFactory')
57: ->setFactory('Nette\Bridges\ApplicationLatte\TemplateFactory');
58:
59: $container->addDefinition('nette.latte')
60: ->setClass('Latte\Engine')
61: ->addSetup('::trigger_error', array('Service nette.latte is deprecated, implement Nette\Bridges\ApplicationLatte\ILatteFactory.', E_USER_DEPRECATED))
62: ->addSetup('setTempDirectory', array($this->tempDir))
63: ->addSetup('setAutoRefresh', array($this->debugMode))
64: ->addSetup('setContentType', array($config['xhtml'] ? Latte\Compiler::CONTENT_XHTML : Latte\Compiler::CONTENT_HTML))
65: ->addSetup('Nette\Utils\Html::$xhtml = ?', array((bool) $config['xhtml']))
66: ->setAutowired(FALSE);
67:
68: foreach ($config['macros'] as $macro) {
69: if (strpos($macro, '::') === FALSE && class_exists($macro)) {
70: $macro .= '::install';
71: }
72: $this->addMacro($macro);
73: }
74:
75: if (class_exists('Nette\Templating\FileTemplate')) {
76: $container->addDefinition('nette.template')
77: ->setFactory('Nette\Templating\FileTemplate')
78: ->addSetup('::trigger_error', array('Service nette.template is deprecated.', E_USER_DEPRECATED))
79: ->addSetup('registerFilter', array(new Nette\DI\Statement(array($latteFactory, 'create'))))
80: ->addSetup('registerHelperLoader', array('Nette\Templating\Helpers::loader'))
81: ->setAutowired(FALSE);
82: }
83:
84: if ($this->name === 'latte') {
85: $container->addAlias('nette.latteFactory', $this->prefix('latteFactory'));
86: $container->addAlias('nette.templateFactory', $this->prefix('templateFactory'));
87: }
88: }
89:
90:
91: 92: 93: 94:
95: public function addMacro($macro)
96: {
97: Nette\Utils\Validators::assert($macro, 'callable');
98:
99: $container = $this->getContainerBuilder();
100: $container->getDefinition('nette.latte')
101: ->addSetup('?->onCompile[] = function ($engine) { ' . $macro . '($engine->getCompiler()); }', array('@self'));
102:
103: $container->getDefinition($this->prefix('latteFactory'))
104: ->addSetup('?->onCompile[] = function ($engine) { ' . $macro . '($engine->getCompiler()); }', array('@self'));
105: }
106:
107: }
108: