/library/phpRack/Adapters/Config/Ini.php

https://github.com/ajohnstone/zf-boilerplate · PHP · 137 lines · 72 code · 5 blank · 60 comment · 3 complexity · 5b2ca1cb1d875dc143ef473182cf62e3 MD5 · raw file

  1. <?php
  2. /**
  3. * phpRack: Integration Testing Framework
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt. It is also available
  7. * through the world-wide-web at this URL: http://www.phprack.com/LICENSE.txt
  8. * If you did not receive a copy of the license and are unable to
  9. * obtain it through the world-wide-web, please send an email
  10. * to license@phprack.com so we can send you a copy immediately.
  11. *
  12. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  15. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  16. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  22. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. * POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * @copyright Copyright (c) phpRack.com
  26. * @version $Id: Ini.php 545 2010-05-04 09:40:46Z yegor256@yahoo.com $
  27. * @category phpRack
  28. */
  29. /**
  30. * @see phpRack_Adapters_Config
  31. */
  32. require_once PHPRACK_PATH . '/Adapters/Config.php';
  33. /**
  34. * Config adapter used for store test configuration loaded from INI file
  35. *
  36. * You can use it like this:
  37. *
  38. * <code>
  39. * // app.ini:
  40. * // [production]
  41. * // params.db.username = 'test'
  42. * $ini = new phpRack_Adapters_Config_Ini('app.ini', 'production');
  43. * assert($ini->params->db->username == 'test');
  44. * </code>
  45. *
  46. * @package Adapters
  47. */
  48. class phpRack_Adapters_Config_Ini extends phpRack_Adapters_Config
  49. {
  50. /**
  51. * Create config object and load selected section from INI file,
  52. * or all sections if $sectionName == null
  53. *
  54. * @param string Filename of INI file
  55. * @param string|null Section name to load, or null to load all
  56. * @return void
  57. * @throws Exception if INI file not exists
  58. * @throws Exception if section not exists in INI file
  59. * @see ConfigTest::testConfigIni() and other integration tests
  60. */
  61. public function __construct($filename, $sectionName = null)
  62. {
  63. $sections = $this->_loadSectionsFromIniFile($filename);
  64. // one section to return
  65. if ($sectionName) {
  66. if (!array_key_exists($sectionName, $sections)) {
  67. throw new Exception("Section '{$sectionName}' doesn't exist in INI file '{$filename}'");
  68. }
  69. $dataArray = $this->_sectionToArray($sections[$sectionName]);
  70. } else {
  71. // all sections to return
  72. foreach ($sections as $key => $section) {
  73. $dataArray[$key] = $this->_sectionToArray($section);
  74. }
  75. }
  76. parent::__construct($dataArray);
  77. }
  78. /**
  79. * Convert section with "key.subkey1.subkey2" values as keys
  80. * to multidimensional associative array
  81. *
  82. * @param array section from ini file
  83. * @return array
  84. * @see __construct()
  85. */
  86. protected function _sectionToArray($section)
  87. {
  88. $dataArray = array();
  89. foreach ($section as $key => $value) {
  90. $currentElement =& $dataArray;
  91. foreach (explode('.', $key) as $keyFragment) {
  92. $currentElement =& $currentElement[$keyFragment];
  93. }
  94. $currentElement = $value;
  95. }
  96. return $dataArray;
  97. }
  98. /**
  99. * Load config sections from INI file, taking into account section inheritance
  100. *
  101. * @param string INI file to load and parse
  102. * @return array
  103. * @throws Exception if config INI file not exists
  104. * @see __construct()
  105. */
  106. protected function _loadSectionsFromIniFile($filename)
  107. {
  108. if (!file_exists($filename)) {
  109. throw new Exception("INI file '{$filename}' doesn't exist");
  110. }
  111. $sections = array();
  112. $iniFileSections = parse_ini_file($filename, true);
  113. foreach ($iniFileSections as $sectionName => $data) {
  114. // divide section name to check it have some parent ([section : parent])
  115. $nameParts = explode(':', $sectionName);
  116. $thisSectionName = trim($nameParts[0]);
  117. // if section have parent
  118. if (isset($nameParts[1])) {
  119. $parentSectionName = trim($nameParts[1]);
  120. // merge current section values, with parent values
  121. $data = array_merge(
  122. $sections[$parentSectionName],
  123. $data
  124. );
  125. }
  126. $sections[$thisSectionName] = $data;
  127. }
  128. return $sections;
  129. }
  130. }