Source for file Crafty_Config.class.php

Documentation is available at Crafty_Config.class.php

  1. <?php
  2.  
  3. /**
  4. * Implementation file of the Crafty_Config class.
  5. *
  6. * <pre>
  7. * PROJECT : Crafty
  8. * Template Engine.
  9. * AUTHOR : Crafty Team <crafty@zulan.net>
  10. * COPYRIGHT : (c) Thomas Ilsche, 2004
  11. *
  12. * FILE : [ROOT]\core\Crafty_Config.class.php
  13. * DESCRIPTION: Implementation file of the Crafty_Config class.
  14. * </pre>
  15. *
  16. * This library is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2.1 of the License, or (at your option) any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public
  27. * License along with this library; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. * @package Crafty_Compiler
  31. * @author Crafty Team <crafty@zulan.net>
  32. * @copyright Copyright (c) Thomas Ilsche 2004
  33. * @version 1.0.0.2004.09.15
  34. * @link http://crafty.zulan.net
  35. * @license http://www.gnu.org/licenses/lgpl.html
  36. * GNU Lesser General Public License Version 2.1
  37. ***/
  38.  
  39. /**
  40. * Crafty_Config class
  41. *
  42. * This class uses simplexml to parse the config xml file and features a
  43. * structured object tree similar to the xml structure.<br/>
  44. * Aswell it allows navigation in this tree by {@link $parent} and
  45. * {@link $top}
  46. *
  47. * @package Crafty_Compiler
  48. * @author Crafty Team <crafty@zulan.net>
  49. * @copyright Copyright (c) Thomas Ilsche
  50. * @version 1.0.0.2004.09.15
  51. * @link http://crafty.zulan.net
  52. * @license http://www.gnu.org/licenses/lgpl.html
  53. * GNU Lesser General Public License Version 2.1
  54. ***/
  55. class Crafty_Config
  56. {
  57.  
  58. /**
  59. * Parent node object handle in the xml tree
  60. *
  61. * @var Crafty_Config
  62. ***/
  63. public $parent;
  64.  
  65. /**
  66. * Top node object handle in the xml tree
  67. *
  68. * @var Crafty_Config
  69. ***/
  70. public $top;
  71.  
  72. /**
  73. * Object handle of the compiler object
  74. *
  75. * @var Crafty_Compiler
  76. ***/
  77. private $_compiler;
  78.  
  79. /**
  80. * Initiates a Crafty_Config object and the corresponding object/XML tree
  81. *
  82. * When called for the first time this only needs the first parameter,
  83. * then it will recursively parse the XML tree and create a new Crafty_Config
  84. * objects for every XML tag.
  85. *
  86. * @param string $xml
  87. * @param Crafty_Compiler $compiler [optional,default=NULL]
  88. * @param Crafty_Config $parent [optional,default=NULL]
  89. * @param Crafty_Config $top [optional,default=NULL]
  90. ***/
  91. public function __construct($xml, $compiler = NULL, $parent = NULL, $top = NULL)
  92. {
  93. if (empty($xml)) {
  94. $xml = array();
  95. } elseif (is_string($xml)) {
  96. $xml = simplexml_load_file($xml);
  97. } elseif (!is_object($xml)) {
  98. throw new Crafty_Exception('Unknown error when reading the config.');
  99. }
  100.  
  101. if ($parent == NULL) {
  102. $this->parent = NULL;
  103. $this->top = $this;
  104. if (!($compiler instanceof crafty_compiler)) {
  105. throw new Crafty_Exception('No crafty compiler handed over to top
  106. config.');
  107. }
  108. } elseif ($top == NULL) {
  109. $this->parent = $parent;
  110. $this->top = $parent;
  111. } else {
  112. $this->parent = $parent;
  113. $this->top = $top;
  114. }
  115.  
  116. $this->_compiler = $compiler;
  117. foreach ($xml as $key => $value) {
  118. $value_str = (string)$value;
  119. /* We do not want the whitespace only non leaf tags in the object... */
  120. $value_str = preg_replace('/^\s+$/', '', $value_str);
  121. if (empty($value_str)) {
  122. $value = new Crafty_Config( $value, NULL , $this, $this->top);
  123. } else {
  124. $value = $value_str;
  125. $value = $this->top->_compiler->booleanize($value);
  126. }
  127. if (isset($this->$key)) {
  128. if (is_array($this->$key)) {
  129. array_push($this->$key, $value);
  130. } else {
  131. $this->$key = array($this->$key, $value);
  132. }
  133. }
  134. else {
  135. $this->$key = $value;
  136. }
  137. }
  138. }
  139. }
  140. ?>

Documentation generated on Sat, 19 Feb 2005 01:43:41 +0100 by phpDocumentor 1.3.0RC3