Mark V
1: <?php
2: namespace MOC\V\Component\Template\Component\Bridge\Repository;
3:
4: use MOC\V\Component\Template\Component\Bridge\Bridge;
5: use MOC\V\Component\Template\Component\IBridgeInterface;
6: use MOC\V\Component\Template\Component\Parameter\Repository\FileParameter;
7: use MOC\V\Core\AutoLoader\AutoLoader;
8: use Umpirsky\Twig\Extension\PhpFunctionExtension;
9:
10: /**
11: * Class TwigTemplate
12: *
13: * @package MOC\V\Component\Template\Component\Bridge
14: */
15: class TwigTemplate extends Bridge implements IBridgeInterface
16: {
17:
18: /** @var null|\Twig_Environment $Instance */
19: private $Instance = null;
20: /** @var null|\Twig_Template $Template */
21: private $Template = null;
22: /** @var null|\Twig_LoaderInterface */
23: private $Loader = null;
24:
25: /**
26: *
27: */
28: public function __construct()
29: {
30:
31: require_once( __DIR__.'/../../../Vendor/Twig/lib/Twig/Autoloader.php' );
32: \Twig_Autoloader::register();
33:
34: AutoLoader::getNamespaceAutoLoader(
35: 'Umpirsky\Twig\Extension',
36: __DIR__.'/../../../Vendor/TwigExtension/TwigPHPFunction/0.0.0/src'
37: );
38:
39: $this->Loader = new \Twig_Loader_String();
40: }
41:
42: /**
43: * @param string $String
44: * @param bool $Reload
45: *
46: * @return IBridgeInterface
47: */
48: public function loadString($String, $Reload = false)
49: {
50:
51: $this->Loader = new \Twig_Loader_String();
52: $this->createInstance($Reload);
53: $this->Template = $this->Instance->loadTemplate($String);
54: return $this;
55: }
56:
57: /**
58: * @param bool|false $Reload
59: *
60: * @return \Twig_Environment
61: */
62: public function createInstance($Reload = false)
63: {
64:
65: $this->Instance = new \Twig_Environment(
66: $this->Loader,
67: array('auto_reload' => $Reload, 'autoescape' => false, 'cache' => realpath(__DIR__.'/TwigTemplate'))
68: );
69: $this->Instance->addFilter(new \Twig_SimpleFilter('utf8_encode', 'utf8_encode'));
70: $this->Instance->addFilter(new \Twig_SimpleFilter('utf8_decode', 'utf8_decode'));
71: $this->Instance->addExtension(new PhpFunctionExtension());
72: return $this->Instance;
73: }
74:
75: /**
76: * @param FileParameter $Location
77: * @param bool $Reload
78: *
79: * @return IBridgeInterface
80: */
81: public function loadFile(FileParameter $Location, $Reload = false)
82: {
83:
84: $this->Loader = new \Twig_Loader_Filesystem(array(dirname($Location->getFile())));
85: $this->createInstance($Reload);
86: $this->Template = $this->Instance->loadTemplate(basename($Location->getFile()));
87: return $this;
88: }
89:
90: /**
91: * @return string
92: */
93: public function getContent()
94: {
95:
96: return $this->Template->render($this->VariableList);
97: }
98:
99: }
100: