PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/config/sfFactoryConfigHandler.class.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 256 lines | 176 code | 34 blank | 46 comment | 22 complexity | ed4e6c09645a025c71159a6b1fba1af7 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfFactoryConfigHandler allows you to specify which factory implementation the
  12. * system will use.
  13. *
  14. * @package symfony
  15. * @subpackage config
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @author Sean Kerr <sean@code-box.org>
  18. * @version SVN: $Id: sfFactoryConfigHandler.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  19. */
  20. class sfFactoryConfigHandler extends sfYamlConfigHandler
  21. {
  22. /**
  23. * Executes this configuration handler.
  24. *
  25. * @param array $configFiles An array of absolute filesystem path to a configuration file
  26. *
  27. * @return string Data to be written to a cache file
  28. *
  29. * @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable
  30. * @throws <b>sfParseException</b> If a requested configuration file is improperly formatted
  31. */
  32. public function execute($configFiles)
  33. {
  34. // parse the yaml
  35. $config = self::getConfiguration($configFiles);
  36. // init our data and includes arrays
  37. $includes = array();
  38. $instances = array();
  39. // available list of factories
  40. $factories = array('view_cache_manager', 'logger', 'i18n', 'controller', 'request', 'response', 'routing', 'storage', 'user', 'view_cache', 'mailer');
  41. // let's do our fancy work
  42. foreach ($factories as $factory)
  43. {
  44. // see if the factory exists for this controller
  45. $keys = $config[$factory];
  46. if (!isset($keys['class']))
  47. {
  48. // missing class key
  49. throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $factory));
  50. }
  51. $class = $keys['class'];
  52. if (isset($keys['file']))
  53. {
  54. // we have a file to include
  55. if (!is_readable($keys['file']))
  56. {
  57. // factory file doesn't exist
  58. throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $class, $keys['file']));
  59. }
  60. // append our data
  61. $includes[] = sprintf("require_once('%s');", $keys['file']);
  62. }
  63. // parse parameters
  64. $parameters = array();
  65. if (isset($keys['param']))
  66. {
  67. if (!is_array($keys['param']))
  68. {
  69. throw new InvalidArgumentException(sprintf('The "param" key for the "%s" factory must be an array (in %s).', $class, $configFiles[0]));
  70. }
  71. $parameters = $keys['param'];
  72. }
  73. // append new data
  74. switch ($factory)
  75. {
  76. case 'controller':
  77. $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_controller', '%s');\n \$this->factories['controller'] = new \$class(\$this);", $class);
  78. break;
  79. case 'request':
  80. $parameters['no_script_name'] = sfConfig::get('sf_no_script_name');
  81. $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_request', '%s');\n \$this->factories['request'] = new \$class(\$this->dispatcher, array(), array(), sfConfig::get('sf_factory_request_parameters', %s), sfConfig::get('sf_factory_request_attributes', array()));", $class, var_export($parameters, true));
  82. break;
  83. case 'response':
  84. $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_response', '%s');\n \$this->factories['response'] = new \$class(\$this->dispatcher, sfConfig::get('sf_factory_response_parameters', array_merge(array('http_protocol' => isset(\$_SERVER['SERVER_PROTOCOL']) ? \$_SERVER['SERVER_PROTOCOL'] : null), %s)));", $class, var_export($parameters, true));
  85. // TODO: this is a bit ugly, as it only works for sfWebRequest & sfWebResponse combination. see #3397
  86. $instances[] = sprintf(" if (\$this->factories['request'] instanceof sfWebRequest \n && \$this->factories['response'] instanceof sfWebResponse \n && 'HEAD' == \$this->factories['request']->getMethod())\n { \n \$this->factories['response']->setHeaderOnly(true);\n }\n");
  87. break;
  88. case 'storage':
  89. $defaultParameters = array();
  90. $defaultParameters[] = sprintf("'auto_shutdown' => false, 'session_id' => \$this->getRequest()->getParameter('%s'),", $parameters['session_name']);
  91. if (is_subclass_of($class, 'sfDatabaseSessionStorage'))
  92. {
  93. $defaultParameters[] = sprintf("'database' => \$this->getDatabaseManager()->getDatabase('%s'),", isset($parameters['database']) ? $parameters['database'] : 'default');
  94. unset($parameters['database']);
  95. }
  96. $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_storage', '%s');\n \$this->factories['storage'] = new \$class(array_merge(array(\n%s\n), sfConfig::get('sf_factory_storage_parameters', %s)));", $class, implode("\n", $defaultParameters), var_export($parameters, true));
  97. break;
  98. case 'user':
  99. $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_user', '%s');\n \$this->factories['user'] = new \$class(\$this->dispatcher, \$this->factories['storage'], array_merge(array('auto_shutdown' => false, 'culture' => \$this->factories['request']->getParameter('sf_culture')), sfConfig::get('sf_factory_user_parameters', %s)));", $class, var_export($parameters, true));
  100. break;
  101. case 'view_cache':
  102. $instances[] = sprintf("\n if (sfConfig::get('sf_cache'))\n {\n".
  103. " \$class = sfConfig::get('sf_factory_view_cache', '%s');\n".
  104. " \$cache = new \$class(sfConfig::get('sf_factory_view_cache_parameters', %s));\n".
  105. " \$this->factories['viewCacheManager'] = new %s(\$this, \$cache, %s);\n".
  106. " }\n".
  107. " else\n".
  108. " {\n".
  109. " \$this->factories['viewCacheManager'] = null;\n".
  110. " }\n",
  111. $class, var_export($parameters, true), $config['view_cache_manager']['class'], var_export($config['view_cache_manager']['param'], true));
  112. break;
  113. case 'i18n':
  114. if (isset($parameters['cache']))
  115. {
  116. $cache = sprintf(" \$cache = new %s(%s);\n", $parameters['cache']['class'], var_export($parameters['cache']['param'], true));
  117. unset($parameters['cache']);
  118. }
  119. else
  120. {
  121. $cache = " \$cache = null;\n";
  122. }
  123. $instances[] = sprintf("\n if (sfConfig::get('sf_i18n'))\n {\n".
  124. " \$class = sfConfig::get('sf_factory_i18n', '%s');\n".
  125. "%s".
  126. " \$this->factories['i18n'] = new \$class(\$this->configuration, \$cache, %s);\n".
  127. " sfWidgetFormSchemaFormatter::setTranslationCallable(array(\$this->factories['i18n'], '__'));\n".
  128. " }\n"
  129. , $class, $cache, var_export($parameters, true)
  130. );
  131. break;
  132. case 'routing':
  133. if (isset($parameters['cache']))
  134. {
  135. $cache = sprintf(" \$cache = new %s(%s);\n", $parameters['cache']['class'], var_export($parameters['cache']['param'], true));
  136. unset($parameters['cache']);
  137. }
  138. else
  139. {
  140. $cache = " \$cache = null;\n";
  141. }
  142. $instances[] = sprintf(" \$class = sfConfig::get('sf_factory_routing', '%s');\n".
  143. " %s\n".
  144. "\$this->factories['routing'] = new \$class(\$this->dispatcher, \$cache, array_merge(array('auto_shutdown' => false, 'context' => \$this->factories['request']->getRequestContext()), sfConfig::get('sf_factory_routing_parameters', %s)));\n".
  145. "if (\$parameters = \$this->factories['routing']->parse(\$this->factories['request']->getPathInfo()))\n".
  146. "{\n".
  147. " \$this->factories['request']->addRequestParameters(\$parameters);\n".
  148. "}\n",
  149. $class, $cache, var_export($parameters, true)
  150. );
  151. break;
  152. case 'logger':
  153. $loggers = '';
  154. if (isset($parameters['loggers']))
  155. {
  156. foreach ($parameters['loggers'] as $name => $keys)
  157. {
  158. if (isset($keys['enabled']) && !$keys['enabled'])
  159. {
  160. continue;
  161. }
  162. if (!isset($keys['class']))
  163. {
  164. // missing class key
  165. throw new sfParseException(sprintf('Configuration file "%s" specifies logger "%s" with missing class key.', $configFiles[0], $name));
  166. }
  167. $condition = true;
  168. if (isset($keys['param']['condition']))
  169. {
  170. $condition = $keys['param']['condition'];
  171. unset($keys['param']['condition']);
  172. }
  173. if ($condition)
  174. {
  175. // create logger instance
  176. $loggers .= sprintf("\n\$logger = new %s(\$this->dispatcher, array_merge(array('auto_shutdown' => false), %s));\n\$this->factories['logger']->addLogger(\$logger);\n",
  177. $keys['class'],
  178. isset($keys['param']) ? var_export($keys['param'], true) : 'array()'
  179. );
  180. }
  181. }
  182. unset($parameters['loggers']);
  183. }
  184. $instances[] = sprintf(
  185. " \$class = sfConfig::get('sf_factory_logger', '%s');\n \$this->factories['logger'] = new \$class(\$this->dispatcher, array_merge(array('auto_shutdown' => false), sfConfig::get('sf_factory_logger_parameters', %s)));\n".
  186. " %s"
  187. , $class, var_export($parameters, true), $loggers);
  188. break;
  189. case 'mailer':
  190. $instances[] = sprintf(
  191. "require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/classes/Swift.php';\n".
  192. "Swift::registerAutoload();\n".
  193. "sfMailer::initialize();\n".
  194. "\$this->setMailerConfiguration(array_merge(array('class' => sfConfig::get('sf_factory_mailer', '%s')), sfConfig::get('sf_factory_mailer_parameters', %s)));\n"
  195. , $class, var_export($parameters, true));
  196. break;
  197. }
  198. }
  199. // compile data
  200. $retval = sprintf("<?php\n".
  201. "// auto-generated by sfFactoryConfigHandler\n".
  202. "// date: %s\n%s\n%s\n",
  203. date('Y/m/d H:i:s'), implode("\n", $includes),
  204. implode("\n", $instances));
  205. return $retval;
  206. }
  207. /**
  208. * @see sfConfigHandler
  209. */
  210. static public function getConfiguration(array $configFiles)
  211. {
  212. $config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
  213. foreach ($config as $factory => $values)
  214. {
  215. if (isset($values['file']))
  216. {
  217. $config[$factory]['file'] = self::replacePath($values['file']);
  218. }
  219. }
  220. return $config;
  221. }
  222. }