public function compress($html) { if (!$this->twig->isDebug() || $this->forceCompression) { return $this->parser->compress($html); } return $html; }
/** * Loads the Twig instance and registers the autoloader. */ public function configure() { parent::configure(); $this->configuration = $this->context->getConfiguration(); // template name $template = sfConfig::get('sf_template'); $templateDir = sfConfig::get('sf_template_dir') . '/' . $template; if (!file_exists($templateDir)) { throw new sfException(__METHOD__ . ": Couldn't find template " . $template); } // decorator template path $layoutTemplateDir = $templateDir . '/global'; if (is_readable($layoutTemplateDir . '/' . $this->getDecoratorTemplate())) { $this->setDecoratorDirectory($layoutTemplateDir); } // module template path $moduleTemplateDir = $templateDir . '/modules/' . $this->moduleName; if (is_readable($moduleTemplateDir . '/' . $this->getTemplate())) { $this->setDirectory($moduleTemplateDir); } // init twig engine // empty array becuase it changes based on the rendering context $this->loader = new Twig_Loader_Filesystem(array()); $this->twig = new sfTwigEnvironment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir') . '/' . $template, 'debug' => sfConfig::get('sf_debug', false), 'sf_context' => $this->context)); if ($this->twig->isDebug()) { $this->twig->setAutoReload(true); } $this->loadExtensions(); }
public function output(FieldDescriptionInterface $fieldDescription, $content) { if ($this->environment->isDebug()) { return sprintf("\n<!-- START - fieldName: %s, template: %s -->\n%s\n<!-- END - fieldName: %s -->", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content, $fieldDescription->getFieldName()); } return $content; }
/** * Инициализируется расширения, необходимые для работы */ private function initExtensions() { if ($this->engine->isDebug()) { $this->engine->addExtension(new \Twig_Extension_Debug()); } $this->engine->addExtension(new BitrixExtension()); $this->engine->addExtension(new CustomFunctionsExtension()); }
/** * Loads the Twig instance and registers the autoloader. * * @return void */ public function configure() { parent::configure(); $this->configuration = $this->context->getConfiguration(); //Empty array becuase it changes based on the rendering context $this->loader = new Twig_Loader_Filesystem(array()); $this->twig = new Twig_Environment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir'), 'debug' => sfConfig::get('sf_debug', false))); if ($this->twig->isDebug()) { $this->twig->setCache(null); $this->twig->setAutoReload(true); } $this->loadExtensions(); }
/** * Loads the Twig instance and registers the autoloader. */ public function configure() { parent::configure(); $this->configuration = $this->context->getConfiguration(); require_once sfConfig::get('sf_twig_lib_dir', dirname(__FILE__) . '/../lib/vendor/Twig/lib') . '/Twig/Autoloader.php'; Twig_Autoloader::register(); // empty array becuase it changes based on the rendering context $this->loader = new Twig_Loader_Filesystem(array()); $this->twig = new sfTwigEnvironment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir'), 'debug' => sfConfig::get('sf_debug', false), 'sf_context' => $this->context)); if ($this->twig->isDebug()) { $this->twig->enableAutoReload(); $this->twig->setCache(null); } $this->loadExtensions(); }
/** * {@inheritdoc} */ public function dump(\Twig_Environment $env, $context) { if (!$env->isDebug()) { return null; } if (func_num_args() === 2) { $vars = []; foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $vars[$key] = $value; } } $vars = [$vars]; } else { $vars = func_get_args(); unset($vars[0], $vars[1]); } $output = fopen('php://memory', 'r+b'); $prevOutput = $this->dumper->setOutput($output); foreach ($vars as $value) { $this->dumper->dump($this->cloner->cloneVar($value)); } $this->dumper->setOutput($prevOutput); rewind($output); return stream_get_contents($output); }
/** * {@inheritdoc} */ public function dump(\Twig_Environment $env, $context) { // Return if 'debug' is `false` in Twig, or there's no logged on user _and_ `debug_show_loggedoff` in // config.yml is `false`. if (!$env->isDebug() || $this->users->getCurrentUser() === null && !$this->debugShowLoggedoff) { return null; } if (func_num_args() === 2) { $vars = []; foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $vars[$key] = $value; } } $vars = [$vars]; } else { $vars = func_get_args(); unset($vars[0], $vars[1]); } $output = fopen('php://memory', 'r+b'); $prevOutput = $this->dumper->setOutput($output); foreach ($vars as $value) { $this->dumper->dump($this->cloner->cloneVar($value)); } $this->dumper->setOutput($prevOutput); rewind($output); return stream_get_contents($output); }
public function content($key, $type, array $arguments = array(), $default='') { $debugmessage = ''; if ($this->env->isDebug()) { $debugmessage .= "<!--debug IbrowsSimpleCMS\n"; $debugmessage .= "type=$type \n"; $debugmessage .= "key=$key \n"; $debugmessage .= "default=$default \n"; $debugmessage .= "arguments=" . print_r($arguments, true) . " \n"; $debugmessage .= '-->'; if ($default == '') { $default = "$key-$type"; } } $obj = $this->manager->find($type, $key); if ($obj) { $out = $debugmessage . $obj->toHTML($this, $arguments); } else { $out = $default; } $grant = $this->handler->isGranted('ibrows_simple_cms_content_edit_key', array('key'=> $key,'type'=>$type )); //$grant = $this->handler->isGranted('ibrows_simple_cms_content'); if(!$grant){ return $out; } return $this->wrapOutputForEdit($out, $key, $type, $arguments, $default); }
public function dump(\Twig_Environment $env, $context) { if (!$env->isDebug() || !$this->cloner) { return; } if (2 === func_num_args()) { $vars = array(); foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $vars[$key] = $value; } } $vars = array($vars); } else { $vars = func_get_args(); unset($vars[0], $vars[1]); } $html = ''; $dumper = new HtmlDumper(function ($line, $depth) use(&$html) { if (-1 !== $depth) { $html .= str_repeat(' ', $depth) . $line . "\n"; } }); foreach ($vars as $value) { $dumper->dump($this->cloner->cloneVar($value)); } return $html; }
public function dump(\Twig_Environment $env, $context) { if (!$env->isDebug()) { return; } if (2 === func_num_args()) { $vars = array(); foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $vars[$key] = $value; } } $vars = array($vars); } else { $vars = func_get_args(); unset($vars[0], $vars[1]); } $dump = fopen('php://memory', 'r+b'); $dumper = new HtmlDumper($dump); foreach ($vars as $value) { $dumper->dump($this->cloner->cloneVar($value)); } rewind($dump); return stream_get_contents($dump); }
private static function getInstance() { if (self::$instance) { return self::$instance; } $loader = new BitrixLoader($_SERVER['DOCUMENT_ROOT']); $c = Configuration::getInstance(); $config = $c->get('maximaster'); $twigConfig = (array) $config['tools']['twig']; $defaultConfig = array('debug' => false, 'charset' => SITE_CHARSET, 'cache' => $_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/maximaster/tools.twig', 'auto_reload' => isset($_GET['clear_cache']) && strtoupper($_GET['clear_cache']) == 'Y', 'autoescape' => false); $twigOptions = array_merge($defaultConfig, $twigConfig); $twig = new \Twig_Environment($loader, $twigOptions); if ($twig->isDebug()) { $twig->addExtension(new \Twig_Extension_Debug()); } $twig->addExtension(new BitrixExtension()); $twig->addExtension(new CustomFunctionsExtension()); $event = new Event('', 'onAfterTwigTemplateEngineInited', array($twig)); $event->send(); if ($event->getResults()) { foreach ($event->getResults() as $evenResult) { if ($evenResult->getType() == \Bitrix\Main\EventResult::SUCCESS) { $twig = current($evenResult->getParameters()); } } } return self::$instance = $twig; }
/** * @param FieldDescriptionInterface $fieldDescription * @param \Twig_Template $template * @param array $parameters * * @return string */ public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters = array()) { $content = $template->render($parameters); if ($this->environment->isDebug()) { return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $template->getTemplateName(), $content, $fieldDescription->getFieldName()); } return $content; }
/** * @param Twig_Environment $env * @param array $context * @return string */ function refFunction(Twig_Environment $env, array $context) { if (!$env->isDebug()) { return ''; } ob_start(); r(2 === func_num_args() ? $context : array_slice(func_get_args(), 2)); return ob_get_clean(); }
/** * Compiles the node to PHP. * * @param \Twig_Compiler A Twig_Compiler instance */ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); $location = $this->listener_directory . $this->getNode('expr')->getAttribute('name'); foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path) { $ext_namespace = str_replace('/', '_', $ext_namespace); if ($this->environment->isDebug()) { // If debug mode is enabled, lets check for new/removed EVENT // templates on page load rather than at compile. This is // slower, but makes developing extensions easier (no need to // purge the cache when a new event template file is added) $compiler->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n")->indent(); } if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) { $compiler->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n")->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n")->write("\$this->env->loadTemplate('@{$ext_namespace}/{$location}.html')->display(\$context);\n")->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n"); } if ($this->environment->isDebug()) { $compiler->outdent()->write("}\n\n"); } } }
public function contentCollection($key, $type, array $arguments = array(), $default = null, $noedit = false) { $debugmessage = ''; if ($this->env->isDebug()) { $debugmessage .= "<!--debug IbrowsSimpleCMS Collection\n"; $debugmessage .= "type={$type} \n"; $debugmessage .= "key={$key} \n"; $debugmessage .= "default={$default} \n"; $debugmessage .= "arguments=" . print_r($arguments, true) . " \n"; $debugmessage .= '-->'; if ($default == null) { $default = "{$key}-{$type}"; } } $objs = $this->manager->findAll($type, $key); $out = ''; $grant = $this->handler->isGranted('ibrows_simple_cms_content'); if ($noedit) { $grant = false; } $addkey = $this->manager->getNewGroupKey($key, $objs); if ($objs) { foreach ($objs as $objkey => $content) { /* @var $content \Ibrows\SimpleCMSBundle\Entity\ContentInterface */ $outobj = $debugmessage . $content->toHTML($this, $arguments); if ($grant && $this->handler->isGranted('ibrows_simple_cms_content_edit_key', array('key' => $content->getKeyword(), 'type' => $type))) { $outobj = $this->wrapOutputForEdit($outobj, $content->getKeyword(), $type, $arguments, $default); } $out .= $outobj; } } else { if (!$grant) { $out = $default; } } if (!$grant) { return $out; } $class = ''; if (isset($arguments['inline']) && $arguments['inline'] == true) { $class = 'inline'; } //addlink if ($this->handler->isGranted('ibrows_simple_cms_content_create', array('type' => $type))) { $editpath = $this->env->getExtension('routing')->getPath('ibrows_simple_cms_content_edit_key', array('key' => $addkey, 'type' => $type)); $editpath .= "?args=" . urlencode(serialize($arguments)); $editpath .= "&default=" . $default; $outadd = '<a href="' . $editpath . '" class="simplecms-editlink simplecms-addlink" > </a> ADD ' . $default . ''; $outadd = "<div class=\"simplcms-{$addkey}-{$type} simplecms-edit simplecms-add {$class}\" id=\"simplcms-{$addkey}-{$type}\" >{$outadd}</div>"; } return "<div class=\"simplcms-collection-{$key}-{$type} simplecms-edit-collection {$class}\" id=\"simplcms-collection-{$key}-{$type}\" >{$out}{$outadd}</div>"; }
public function dump(\Twig_Environment $environment, $context) { if (!$environment->isDebug()) { return ''; } $arguments = func_get_args(); array_shift($arguments); array_shift($arguments); $count = count($arguments); if ($count === 0) { $arguments = $context; } if ($count === 1) { $arguments = array_shift($arguments); } return $this->doDump($arguments); }
function twig_var_dump(Twig_Environment $env, $context) { if (!$env->isDebug()) { return; } ob_start(); $count = func_num_args(); if (2 === $count) { $vars = []; foreach ($context as $key => $value) { if (!$value instanceof Twig_Template) { $vars[$key] = $value; } } var_dump($vars); } else { for ($i = 2; $i < $count; $i++) { var_dump(func_get_arg($i)); } } return ob_get_clean(); }
function twig_vo_debug(\Twig_Environment $env, $context) { if (!$env->isDebug()) { return; } ob_start(); $count = func_num_args(); $voDebug = new VoDebug(); $voDebug->setDumpers(new Dumpers\StringDumper(), new Dumpers\NumberDumper(), new Dumpers\ArrayDumper($voDebug), new Dumpers\ObjectDumper($voDebug)); if (2 === $count) { $vars = array(); foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $vars[$key] = $value; } } echo $voDebug->dump($vars); } else { for ($i = 2; $i < $count; ++$i) { echo $voDebug->dump(func_get_arg($i)); } } return ob_get_clean(); }
/** * Based on Twig_Extension_Debug / twig_var_dump * (c) 2011 Fabien Potencier * * @param \Twig_Environment $env * @param $context */ public function dump(\Twig_Environment $env, $context) { if (!$env->isDebug() || !$this->debugger) { return; } $count = func_num_args(); if (2 === $count) { $data = []; foreach ($context as $key => $value) { if (is_object($value)) { if (method_exists($value, 'toArray')) { $data[$key] = $value->toArray(); } else { $data[$key] = "Object (" . get_class($value) . ")"; } } else { $data[$key] = $value; } } $this->debugger->addMessage($data, 'debug'); } else { for ($i = 2; $i < $count; $i++) { $this->debugger->addMessage(func_get_arg($i), 'debug'); } } }
/** * Provides Kint function to Twig templates. * * Handles 0, 1, or multiple arguments. * * Code derived from https://github.com/barelon/CgKintBundle. * * @param Twig_Environment $env * The twig environment instance. * @param array $context * An array of parameters passed to the template. */ public function kint(\Twig_Environment $env, array $context) { // Don't do anything unless twig_debug is enabled. This reads from the Twig // environment, not Drupal Settings, so a container rebuild is necessary // when toggling twig_debug on and off. We can consider injecting Settings. if (!$env->isDebug()) { return; } kint_require(); // Don't display where Kint was called from. // @todo Can we add information about which template Kint was called from? \Kint::$displayCalledFrom = FALSE; $output = ''; if (func_num_args() === 2) { // No arguments passed to kint(), display full Twig context. $kint_variable = array(); foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $kint_variable[$key] = $value; } } $result = @\Kint::dump($kint_variable); $output = str_replace('$kint_variable', 'Twig context', $result); } else { // Try to get the names of variables from the Twig template. $trace = debug_backtrace(); $callee = $trace[0]; $lines = file($callee['file']); $source = $lines[$callee['line'] - 1]; preg_match('/kint\\((.+)\\);/', $source, $matches); $parameters = $matches[1]; $parameters = preg_replace('/\\$this->getContext\\(\\$context, "(.+)"\\)/U', "\$1", $parameters); $parameters = preg_replace('/\\(isset\\(\\$context\\["(.+)"\\]\\) \\? \\$context\\["(.+)"\\] : null\\)/U', "\$1", $parameters); do { $parameters = preg_replace('/\\$this->getAttribute\\((.+), "(.+)"\\)/U', "\$1.\$2", $parameters, 1, $found); } while ($found); $parameters = explode(', ', $parameters); foreach ($parameters as $index => $parameter) { // Remove bad entries from the parameters array. Maybe we can avoid this // by doing more with the regular expressions. // This only seems to be needed for cases like: // {{ my_array['#hash_index'] }} if (in_array($parameter, array('array()', '"array'))) { unset($parameters[$index]); continue; } // Trim parens and quotes from the parameter strings. $parameters[$index] = trim($parameter, '()"'); } // Don't include $env and $context arguments in $args and $parameters. $args = array_slice(func_get_args(), 2); $parameters = array_slice($parameters, 2); // If there is only one argument, pass to Kint without too much hassle. if (count($args) == 1) { $kint_variable = reset($args); $result = @\Kint::dump($kint_variable); // Replace $kint_variable with the name of the variable in the Twig // template. $output = str_replace('$kint_variable', reset($parameters), $result); } else { // Build an array of variable to pass to Kint. // @todo Can we just call_user_func_array while still retaining the // variable names? foreach ($args as $index => $arg) { // Prepend a unique index to allow debugging the same variable more // than once in the same Kint dump. $kint_args['_index_' . $index . '_' . $parameters[$index]] = $arg; } $result = @\Kint::dump($kint_args); // Display a comma separated list of the variables contained in this group. $output = str_replace('$kint_args', implode(', ', $parameters), $result); // Remove unique indexes from output. $output = preg_replace('/_index_([0-9]+)_/', '', $output); } } return $output; }
/** * @param FieldDescriptionInterface $fieldDescription * @param \Twig_Template $template * @param array $parameters * * @return string */ public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters, \Twig_Environment $environment) { $content = $template->render($parameters); if ($environment->isDebug()) { $commentTemplate = <<<EOT <!-- START fieldName: %s template: %s compiled template: %s --> %s <!-- END - fieldName: %s --> EOT; return sprintf($commentTemplate, $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $template->getTemplateName(), $content, $fieldDescription->getFieldName()); } return $content; }
/** * Gets the minified asset * * @param \Twig_Environment $env The twig environment * @param array $pathinfo The pathinfo for the asset * * @return bool|string The minified rev'd asset if available, else false */ public function minify($env, $pathinfo) { $min = sprintf("%s/%s.min.%s", $pathinfo['dirname'], $pathinfo['filename'], $pathinfo['extension']); return in_array($pathinfo['extension'], self::$minify_exts) && isset($this->assets[$min]) && $this->minified && !$env->isDebug() ? $this->assets[$min] : false; }
/** * Provides debug function to Twig templates. * * Handles 0, 1, or multiple arguments. * * @param \Twig_Environment $env * The twig environment instance. * @param array $context * An array of parameters passed to the template. * @param array $args * An array of parameters passed the function. * * @return void * * @see \Drupal\devel\DevelDumperManager::message() */ public function message(\Twig_Environment $env, array $context, array $args = []) { if (!$env->isDebug()) { return; } // No arguments passed, display full Twig context. if (empty($args)) { $context_variables = $this->getContextVariables($context); $this->dumper->message($context_variables, 'Twig context'); } else { foreach ($args as $variable) { $this->dumper->message($variable); } } }