123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfViewConfigHandler allows you to configure views.
- *
- * @package symfony
- * @subpackage config
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfViewConfigHandler.class.php 9085 2008-05-20 01:53:23Z Carl.Vondrick $
- */
- class sfViewConfigHandler extends sfYamlConfigHandler
- {
- /**
- * Executes this configuration handler.
- *
- * @param array $configFiles An array of absolute filesystem path to a configuration file
- *
- * @return string Data to be written to a cache file
- *
- * @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable
- * @throws <b>sfParseException</b> If a requested configuration file is improperly formatted
- * @throws <b>sfInitializationException</b> If a view.yml key check fails
- */
- public function execute($configFiles)
- {
- // parse the yaml
- $this->yamlConfig = self::getConfiguration($configFiles);
- // init our data array
- $data = array();
- $data[] = "\$response = \$this->context->getResponse();\n\n";
- // first pass: iterate through all view names to determine the real view name
- $first = true;
- foreach ($this->yamlConfig as $viewName => $values)
- {
- if ($viewName == 'all')
- {
- continue;
- }
- $data[] = ($first ? '' : 'else ')."if (\$this->actionName.\$this->viewName == '$viewName')\n".
- "{\n";
- $data[] = $this->addTemplate($viewName);
- $data[] = "}\n";
- $first = false;
- }
- // general view configuration
- $data[] = ($first ? '' : "else\n{")."\n";
- $data[] = $this->addTemplate($viewName);
- $data[] = ($first ? '' : "}")."\n\n";
- // second pass: iterate through all real view names
- $first = true;
- foreach ($this->yamlConfig as $viewName => $values)
- {
- if ($viewName == 'all')
- {
- continue;
- }
- $data[] = ($first ? '' : 'else ')."if (\$templateName.\$this->viewName == '$viewName')\n".
- "{\n";
- $data[] = $this->addLayout($viewName);
- $data[] = $this->addComponentSlots($viewName);
- $data[] = $this->addHtmlHead($viewName);
- $data[] = $this->addEscaping($viewName);
- $data[] = $this->addHtmlAsset($viewName);
- $data[] = "}\n";
- $first = false;
- }
- // general view configuration
- $data[] = ($first ? '' : "else\n{")."\n";
- $data[] = $this->addLayout();
- $data[] = $this->addComponentSlots();
- $data[] = $this->addHtmlHead();
- $data[] = $this->addEscaping();
- $data[] = $this->addHtmlAsset();
- $data[] = ($first ? '' : "}")."\n";
- // compile data
- $retval = sprintf("<?php\n".
- "// auto-generated by sfViewConfigHandler\n".
- "// date: %s\n%s\n",
- date('Y/m/d H:i:s'), implode('', $data));
- return $retval;
- }
- /**
- * Adds a component slot statement to the data.
- *
- * @param string $viewName The view name
- *
- * @return string The PHP statement
- */
- protected function addComponentSlots($viewName = '')
- {
- $data = '';
- $components = $this->mergeConfigValue('components', $viewName);
- foreach ($components as $name => $component)
- {
- if (!is_array($component) || count($component) < 1)
- {
- $component = array(null, null);
- }
- $data .= " \$this->setComponentSlot('$name', '{$component[0]}', '{$component[1]}');\n";
- $data .= " if (sfConfig::get('sf_logging_enabled')) \$this->context->getEventDispatcher()->notify(new sfEvent(\$this, 'application.log', array(sprintf('Set component \"%s\" (%s/%s)', '$name', '{$component[0]}', '{$component[1]}'))));\n";
- }
- return $data;
- }
- /**
- * Adds a template setting statement to the data.
- *
- * @param string $viewName The view name
- *
- * @return string The PHP statement
- */
- protected function addTemplate($viewName = '')
- {
- $data = '';
- $templateName = $this->getConfigValue('template', $viewName);
- $defaultTemplateName = $templateName ? "'$templateName'" : '$this->actionName';
- $data .= " \$templateName = sfConfig::get('symfony.view.'.\$this->moduleName.'_'.\$this->actionName.'_template', $defaultTemplateName);\n";
- $data .= " \$this->setTemplate(\$templateName.\$this->viewName.\$this->getExtension());\n";
- return $data;
- }
- /**
- * Adds a layout statement statement to the data.
- *
- * @param string $viewName The view name
- *
- * @return string The PHP statement
- */
- protected function addLayout($viewName = '')
- {
- // true if the user set 'has_layout' to true or set a 'layout' name for this specific action
- $hasLocalLayout = isset($this->yamlConfig[$viewName]['layout']) || (isset($this->yamlConfig[$viewName]) && array_key_exists('has_layout', $this->yamlConfig[$viewName]));
- // the layout value
- $layout = $this->getConfigValue('has_layout', $viewName) ? $this->getConfigValue('layout', $viewName) : false;
- // the user set a decorator in the action
- $data = <<<EOF
- if (!is_null(\$layout = sfConfig::get('symfony.view.'.\$this->moduleName.'_'.\$this->actionName.'_layout')))
- {
- \$this->setDecoratorTemplate(false === \$layout ? false : \$layout.\$this->getExtension());
- }
- EOF;
- if ($hasLocalLayout)
- {
- // the user set a decorator in view.yml for this action
- $data .= <<<EOF
- else
- {
- \$this->setDecoratorTemplate('' == '$layout' ? false : '$layout'.\$this->getExtension());
- }
- EOF;
- }
- else
- {
- // no specific configuration
- // set the layout to the 'all' view.yml value except if:
- // * the decorator template has already been set by "someone" (via view.configure_format for example)
- // * the request is an XMLHttpRequest request
- $data .= <<<EOF
- else if (is_null(\$this->getDecoratorTemplate()) && !\$this->context->getRequest()->isXmlHttpRequest())
- {
- \$this->setDecoratorTemplate('' == '$layout' ? false : '$layout'.\$this->getExtension());
- }
- EOF;
- }
- return $data;
- }
- /**
- * Adds http metas and metas statements to the data.
- *
- * @param string $viewName The view name
- *
- * @return string The PHP statement
- */
- protected function addHtmlHead($viewName = '')
- {
- $data = array();
- foreach ($this->mergeConfigValue('http_metas', $viewName) as $httpequiv => $content)
- {
- $data[] = sprintf(" \$response->addHttpMeta('%s', '%s', false);", $httpequiv, str_replace('\'', '\\\'', $content));
- }
- foreach ($this->mergeConfigValue('metas', $viewName) as $name => $content)
- {
- $data[] = sprintf(" \$response->addMeta('%s', '%s', false, false);", $name, str_replace('\'', '\\\'', preg_replace('/&(?=\w+;)/', '&', htmlspecialchars($content, ENT_QUOTES, sfConfig::get('sf_charset')))));
- }
- return implode("\n", $data)."\n";
- }
- /**
- * Adds stylesheets and javascripts statements to the data.
- *
- * @param string $viewName The view name
- *
- * @return string The PHP statement
- */
- protected function addHtmlAsset($viewName = '')
- {
- $data = array();
- $omit = array();
- $delete = array();
- $delete_all = false;
- // Merge the current view's stylesheets with the app's default stylesheets
- $stylesheets = $this->mergeConfigValue('stylesheets', $viewName);
- $tmp = array();
- foreach ((array) $stylesheets as $css)
- {
- $position = '';
- if (is_array($css))
- {
- $key = key($css);
- $options = $css[$key];
- if (isset($options['position']))
- {
- $position = $options['position'];
- unset($options['position']);
- }
- }
- else
- {
- $key = $css;
- $options = array();
- }
- if ('-*' == $key)
- {
- $tmp = array();
- }
- else if ('-' == $key[0])
- {
- unset($tmp[substr($key, 1)]);
- }
- else
- {
- $tmp[$key] = sprintf(" \$response->addStylesheet('%s', '%s', %s);", $key, $position, str_replace("\n", '', var_export($options, true)));
- }
- }
- $data = array_merge($data, array_values($tmp));
- $omit = array();
- $delete_all = false;
- // Merge the current view's javascripts with the app's default javascripts
- $javascripts = $this->mergeConfigValue('javascripts', $viewName);
- $tmp = array();
- foreach ((array) $javascripts as $js)
- {
- $position = '';
- if (is_array($js))
- {
- $key = key($js);
- $options = $js[$key];
- if (isset($options['position']))
- {
- $position = $options['position'];
- unset($options['position']);
- }
- }
- else
- {
- $key = $js;
- $options = array();
- }
- if ('-*' == $key)
- {
- $tmp = array();
- }
- else if ('-' == $key[0])
- {
- unset($tmp[substr($key, 1)]);
- }
- else
- {
- $tmp[$key] = sprintf(" \$response->addJavascript('%s', '%s', %s);", $key, $position, str_replace("\n", '', var_export($options, true)));
- }
- }
- $data = array_merge($data, array_values($tmp));
- return implode("\n", $data)."\n";
- }
- /**
- * Adds an escaping statement to the data.
- *
- * @param string $viewName The view name
- *
- * @return string The PHP statement
- */
- protected function addEscaping($viewName = '')
- {
- $data = array();
- $escaping = $this->getConfigValue('escaping', $viewName);
- if (isset($escaping['method']))
- {
- $data[] = sprintf(" \$this->getAttributeHolder()->setEscapingMethod(%s);", var_export($escaping['method'], true));
- }
- return implode("\n", $data)."\n";
- }
- /**
- * @see sfConfigHandler
- */
- static public function getConfiguration(array $configFiles)
- {
- return self::mergeConfig(self::parseYamls($configFiles));
- }
- static protected function mergeConfig($config)
- {
- // merge javascripts and stylesheets
- $config['all']['stylesheets'] = array_merge(isset($config['default']['stylesheets']) && is_array($config['default']['stylesheets']) ? $config['default']['stylesheets'] : array(), isset($config['all']['stylesheets']) && is_array($config['all']['stylesheets']) ? $config['all']['stylesheets'] : array());
- unset($config['default']['stylesheets']);
- $config['all']['javascripts'] = array_merge(isset($config['default']['javascripts']) && is_array($config['default']['javascripts']) ? $config['default']['javascripts'] : array(), isset($config['all']['javascripts']) && is_array($config['all']['javascripts']) ? $config['all']['javascripts'] : array());
- unset($config['default']['javascripts']);
- // merge default and all
- $config['all'] = sfToolkit::arrayDeepMerge(
- isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
- isset($config['all']) && is_array($config['all']) ? $config['all'] : array()
- );
- unset($config['default']);
- return self::replaceConstants($config);
- }
- }
|