Source for file XMLLoader.php

Documentation is available at XMLLoader.php

  1. <?php
  2.  
  3. /**
  4. * MVCnPHP - XMLLoader.class.php
  5. *
  6. * This source file is subject to version 2.02 of the PHP license,
  7. * that is bundled with this package in the file LICENSE, and is
  8. * available at through the world-wide-web at
  9. * http://www.php.net/license/2_02.txt.
  10. * If you did not receive a copy of the PHP license and are unable to
  11. * obtain it through the world-wide-web, please send a note to
  12. * license@php.net so we can mail you a copy immediately.
  13. *
  14. * @author Tony Bibbs <tony@geeklog.net>
  15. * @copyright Tony Bibbs 2003
  16. * @package net.geeklog.mvc
  17. * @version $Id: XMLLoader.class.php,v 1.5 2004/03/23 02:39:50 tony Exp $
  18. *
  19. */
  20.  
  21. /**
  22. * Base Loader class which does most of the work
  23. */
  24. require_once 'BaseLoader.php';
  25.  
  26. /**
  27. * This loader creates a mapping from an XML file
  28. *
  29. * @author Tony Bibbs <tony@geeklog.net>
  30. * @package net.geeklog.mvc
  31. *
  32. */
  33. class MVCnPHP_XMLLoader extends MVCnPHP_BaseLoader {
  34. /**
  35. * Handle to an XML DOM
  36. * @access protected
  37. * @var array
  38. */
  39. protected $dom = null;
  40. /**
  41. * Holds array representation of XML config file
  42. * @access protected
  43. * @var array
  44. */
  45. protected $arrayData = null;
  46. const VIEWS = 'VIEW';
  47. const COMMANDS = 'COMMAND';
  48. /**
  49. * Constructor
  50. *
  51. * Initializes object properties and, if the XML has been supplied it will parse it immediately
  52. * instead of waiting for an explicit call to parseXML
  53. *
  54. * @author Tony Bibbs <tony@geeklog.net>
  55. * @access public
  56. * @param string $xmlString XML to parse
  57. *
  58. */
  59. public function __construct()
  60. {
  61. $this->dom = new DomDocument();
  62. }
  63. /**
  64. * Gets config data for given object in the form of
  65. * a mapping object
  66. *
  67. * @author Tony Bibbs <tony@geeklog.net>
  68. * @access public
  69. * @param string $name Name of object to get mapping for
  70. * @param array $arrayData Array of configuration data
  71. * @return object Mapping object
  72. * @todo should probably scan $this->arrayData for potentially harmful PHP
  73. *
  74. */
  75. public function getMapping($name, $viewsDir, $commandsDir, $baseURL, $arrayData = '')
  76. {
  77. if (!file_exists($arrayData) AND !is_array($this->arrayData)) {
  78. return false;
  79. }
  80. if (empty($this->arrayData)) {
  81. $xmlData = file_get_contents($arrayData);
  82. $arrayFileName = dirname($arrayData);
  83. $arrayFileName = $arrayFileName . '/compiled_mvcConfig.php';
  84. $needsCompile = false;
  85. if (file_exists($arrayFileName)) {
  86. if (filemtime($arrayFileName) < filemtime($arrayData)) {
  87. $needsCompile = true;
  88. }
  89. } else {
  90. $needsCompile = true;
  91. }
  92.  
  93. if ($needsCompile) {
  94. $this->dom->loadXML($xmlData);
  95. if (!empty($xmlData)) {
  96. $this->xmlToArray();
  97. }
  98. file_put_contents($arrayFileName, '<?php $this->arrayData = ' . var_export($this->arrayData, true) . '; ?>');
  99. } else {
  100. require_once "$arrayFileName";
  101. }
  102. }
  103. return parent::getMapping($name, $viewsDir, $commandsDir, $baseURL, $this->arrayData);
  104. }
  105. /**
  106. * Converts the MVC XML configuration into an array
  107. *
  108. * @author Tony Bibbs <tony@geeklog.net>
  109. * @access public
  110. *
  111. */
  112. public function xmlToArray()
  113. {
  114. $arrayData = array();
  115. $arrayData['views'] = $this->parseSection(MVCnPHP_XMLLoader::VIEWS);
  116. $arrayData['commands'] = $this->parseSection(MVCnPHP_XMLLoader::COMMANDS);
  117. /*$fp = fopen($glConf['path'] . 'compiled_mvcconfig.php', 'w');
  118. fwrite($fp, '<?php $this->arrayData = */
  119. $this->arrayData = $arrayData;
  120. }
  121. /**
  122. * Parses a section of the XML configuration
  123. *
  124. * @author Tony Bibbs <tony@geeklog.net>
  125. * @access private
  126. * @param string $section Use one of the two class constants for this value
  127. * @return array Array representation of the XML for the given section
  128. *
  129. */
  130. private function parseSection($section)
  131. {
  132. $xpath = new DOMXPath($this->dom);
  133. $tagList = $xpath->query("/MVC_CONFIGURATION/$section");
  134. $tagArray = array();
  135. foreach ($tagList as $curTag) {
  136. $tagID = $curTag->getAttribute('ID');
  137. $tagName = $curTag->getAttribute('NAME');
  138. $tagDefault = $curTag->getAttribute('DEFAULT');
  139. $queryStr = sprintf("/MVC_CONFIGURATION/%s[@ID='%s']/FORWARD", $section, $tagID);
  140. $forwardList = $xpath->query($queryStr);
  141. $forwardArray = array();
  142. foreach ($forwardList as $curForward) {
  143. $forwardID = $curForward->getAttribute('ID');
  144. $forwardType = $curForward->getAttribute('TYPE');
  145. $forwardTarget = $curForward->textContent;
  146. $forwardArray[$forwardID] = array('target'=>$forwardTarget,'type'=>$forwardType);
  147. }
  148. if (strtolower($tagDefault) == 'true') {
  149. if (count($forwardArray) > 0) {
  150. $tagArray[$tagID] = array('name'=>$tagName, 'forwards'=>$forwardArray, 'default'=>1);
  151. } else {
  152. $tagArray[$tagID] = array('name'=>$tagName, 'default'=>1);
  153. }
  154. } else {
  155. if (count($forwardArray) > 0) {
  156. $tagArray[$tagID] = array('name'=>$tagName, 'forwards'=>$forwardArray);
  157. } else {
  158. $tagArray[$tagID] = array('name'=>$tagName);
  159. }
  160. }
  161. }
  162. return $tagArray;
  163. }
  164. /**
  165. * Gets the array representation of MVC XML configuration
  166. *
  167. * @author Tony Bibbs <tony@geeklog.net>
  168. * @access pubic
  169. * @return array Array representation of MVC configuration
  170. *
  171. */
  172. public function getArrayData()
  173. {
  174. return $this->arrayData;
  175. }
  176. }
  177.  
  178. ?>

Documentation generated on Mon, 7 Mar 2005 22:36:23 -0600 by phpDocumentor 1.3.0RC3