<?php
namespace Moto\Twig\Extension; use Moto; use Twig_Markup; class AbstractExtension extends \Twig_Extension { protected $_name = 'motoAbstract'; protected $_options = array(); protected $_environment = null; protected $_loader = null; public function initRuntime(\Twig_Environment $environment) { $this->setEnvironment($environment); $this->_onInitRuntime(); } public function setEnvironment($environment) { if ($environment instanceof \Twig_Environment) { $this->_environment = $environment; $this->_loader = $environment->getLoader(); } return $this; } protected function _onInitRuntime() { } public function getEnvironment() { return $this->_environment; } public function getName() { return $this->_name; } public function setOptions($options) { if (!is_array($options) && !$options instanceof \Traversable) { return $this; } foreach ($options as $name => $value) { $this->setOption($name, $value); } return $this; } public function getOptions() { return $this->_options; } public function getOption($name, $default = null) { return (array_key_exists($name, $this->_options) ? $this->_options[$name] : $default); } public function issetOption($name) { return array_key_exists($name, $this->_options); } public function setOption($name, $value) { $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); if (method_exists($this, $setter)) { $this->{$setter}($value); } else { $this->_options[$name] = $value; } return $this; } public function getGlobal($name, $default = null) { if (null == $this->_environment) { return $default; } $globals = $this->_environment->getGlobals(); return (array_key_exists($name, $globals) ? $globals[$name] : $default); } public function isPreRenderMode() { return $this->getGlobal('isPreRender', false); } public function isPreviewMode() { return $this->getGlobal('isPreview', false); } protected function _returnTemplate($code) { return new Twig_Markup($code, 'UTF-8'); } } 