sfConfigHandler.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * sfConfigHandler allows a developer to create a custom formatted configuration
  12. * file pertaining to any information they like and still have it auto-generate
  13. * PHP code.
  14. *
  15. * @package symfony
  16. * @subpackage config
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Sean Kerr <sean@code-box.org>
  19. * @version SVN: $Id: sfConfigHandler.class.php 9398 2008-06-02 00:30:31Z dwhittle $
  20. */
  21. abstract class sfConfigHandler
  22. {
  23. protected
  24. $parameterHolder = null;
  25. /**
  26. * Class constructor.
  27. *
  28. * @see initialize()
  29. */
  30. public function __construct($parameters = null)
  31. {
  32. $this->initialize($parameters);
  33. }
  34. /**
  35. * Initializes this configuration handler.
  36. *
  37. * @param array $parameters An associative array of initialization parameters
  38. *
  39. * @return bool true, if initialization completes successfully, otherwise false
  40. *
  41. * @throws <b>sfInitializationException</b> If an error occurs while initializing this ConfigHandler
  42. */
  43. public function initialize($parameters = null)
  44. {
  45. $this->parameterHolder = new sfParameterHolder();
  46. $this->parameterHolder->add($parameters);
  47. }
  48. /**
  49. * Executes this configuration handler
  50. *
  51. * @param array $configFiles An array of filesystem path to a configuration file
  52. *
  53. * @return string Data to be written to a cache file
  54. *
  55. * @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable
  56. * @throws <b>sfParseException</b> If a requested configuration file is improperly formatted
  57. */
  58. abstract public function execute($configFiles);
  59. /**
  60. * Replaces constant identifiers in a value.
  61. *
  62. * If the value is an array replacements are made recursively.
  63. *
  64. * @param mixed $value The value on which to run the replacement procedure
  65. *
  66. * @return string The new value
  67. */
  68. static public function replaceConstants($value)
  69. {
  70. if (is_array($value))
  71. {
  72. array_walk_recursive($value, create_function('&$value', '$value = sfToolkit::replaceConstants($value);'));
  73. }
  74. else
  75. {
  76. $value = sfToolkit::replaceConstants($value);
  77. }
  78. return $value;
  79. }
  80. /**
  81. * Replaces a relative filesystem path with an absolute one.
  82. *
  83. * @param string $path A relative filesystem path
  84. *
  85. * @return string The new path
  86. */
  87. static public function replacePath($path)
  88. {
  89. if (is_array($path))
  90. {
  91. array_walk_recursive($path, create_function('&$path', '$path = sfConfigHandler::replacePath($path);'));
  92. }
  93. else
  94. {
  95. if (!sfToolkit::isPathAbsolute($path))
  96. {
  97. // not an absolute path so we'll prepend to it
  98. $path = sfConfig::get('sf_app_dir').'/'.$path;
  99. }
  100. }
  101. return $path;
  102. }
  103. /**
  104. * Gets the parameter holder for this configuration handler.
  105. *
  106. * @return sfParameterHolder A sfParameterHolder instance
  107. */
  108. public function getParameterHolder()
  109. {
  110. return $this->parameterHolder;
  111. }
  112. /**
  113. * Returns the configuration for the current config handler.
  114. *
  115. * @param array $configFiles An array of ordered configuration files
  116. * @throws LogicException no matter what
  117. */
  118. static public function getConfiguration(array $configFiles)
  119. {
  120. throw new LogicException('You must call the ::getConfiguration() method on a concrete config handler class');
  121. }
  122. }