sfCacheConfigHandler.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfCacheConfigHandler allows you to configure cache.
  11. *
  12. * @package symfony
  13. * @subpackage config
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCacheConfigHandler.class.php 9085 2008-05-20 01:53:23Z Carl.Vondrick $
  16. */
  17. class sfCacheConfigHandler extends sfYamlConfigHandler
  18. {
  19. protected
  20. $cacheConfig = array();
  21. /**
  22. * Executes this configuration handler.
  23. *
  24. * @param array $configFiles An array of absolute filesystem path to a configuration file
  25. *
  26. * @return string Data to be written to a cache file
  27. *
  28. * @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable
  29. * @throws <b>sfParseException</b> If a requested configuration file is improperly formatted
  30. * @throws <b>sfInitializationException</b> If a cache.yml key check fails
  31. */
  32. public function execute($configFiles)
  33. {
  34. // parse the yaml
  35. $this->yamlConfig = self::getConfiguration($configFiles);
  36. // iterate through all action names
  37. $data = array();
  38. $first = true;
  39. foreach ($this->yamlConfig as $actionName => $values)
  40. {
  41. if ($actionName == 'all')
  42. {
  43. continue;
  44. }
  45. $data[] = $this->addCache($actionName);
  46. $first = false;
  47. }
  48. // general cache configuration
  49. $data[] = $this->addCache('DEFAULT');
  50. // compile data
  51. $retval = sprintf("<?php\n".
  52. "// auto-generated by sfCacheConfigHandler\n".
  53. "// date: %s\n%s\n",
  54. date('Y/m/d H:i:s'), implode('', $data));
  55. return $retval;
  56. }
  57. /**
  58. * Returns a single addCache statement.
  59. *
  60. * @param string $actionName The action name
  61. *
  62. * @return string PHP code for the addCache statement
  63. */
  64. protected function addCache($actionName = '')
  65. {
  66. $data = array();
  67. // enabled?
  68. $enabled = $this->getConfigValue('enabled', $actionName);
  69. // cache with or without loayout
  70. $withLayout = $this->getConfigValue('with_layout', $actionName) ? 'true' : 'false';
  71. // lifetime
  72. $lifeTime = !$enabled ? '0' : $this->getConfigValue('lifetime', $actionName, '0');
  73. // client_lifetime
  74. $clientLifetime = !$enabled ? '0' : $this->getConfigValue('client_lifetime', $actionName, $lifeTime, '0');
  75. // contextual
  76. $contextual = $this->getConfigValue('contextual', $actionName) ? 'true' : 'false';
  77. // vary
  78. $vary = $this->getConfigValue('vary', $actionName, array());
  79. if (!is_array($vary))
  80. {
  81. $vary = array($vary);
  82. }
  83. // add cache information to cache manager
  84. $data[] = sprintf("\$this->addCache(\$moduleName, '%s', array('withLayout' => %s, 'lifeTime' => %s, 'clientLifeTime' => %s, 'contextual' => %s, 'vary' => %s));\n",
  85. $actionName, $withLayout, $lifeTime, $clientLifetime, $contextual, str_replace("\n", '', var_export($vary, true)));
  86. return implode("\n", $data);
  87. }
  88. /**
  89. * @see sfConfigHandler
  90. */
  91. static public function getConfiguration(array $configFiles)
  92. {
  93. return self::flattenConfiguration(self::parseYamls($configFiles));
  94. }
  95. }