Documentation is available at XMLLoader.php
- <?php
- /**
- * MVCnPHP - XMLLoader.class.php
- *
- * This source file is subject to version 2.02 of the PHP license,
- * that is bundled with this package in the file LICENSE, and is
- * available at through the world-wide-web at
- * http://www.php.net/license/2_02.txt.
- * If you did not receive a copy of the PHP license and are unable to
- * obtain it through the world-wide-web, please send a note to
- * license@php.net so we can mail you a copy immediately.
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @copyright Tony Bibbs 2003
- * @package net.geeklog.mvc
- * @version $Id: XMLLoader.class.php,v 1.5 2004/03/23 02:39:50 tony Exp $
- *
- */
- /**
- * Base Loader class which does most of the work
- */
- require_once 'BaseLoader.php';
- /**
- * This loader creates a mapping from an XML file
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @package net.geeklog.mvc
- *
- */
- class MVCnPHP_XMLLoader extends MVCnPHP_BaseLoader {
- /**
- * Handle to an XML DOM
- * @access protected
- * @var array
- */
- protected $dom = null;
- /**
- * Holds array representation of XML config file
- * @access protected
- * @var array
- */
- protected $arrayData = null;
- const VIEWS = 'VIEW';
- const COMMANDS = 'COMMAND';
- /**
- * Constructor
- *
- * Initializes object properties and, if the XML has been supplied it will parse it immediately
- * instead of waiting for an explicit call to parseXML
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access public
- * @param string $xmlString XML to parse
- *
- */
- public function __construct()
- {
- $this->dom = new DomDocument();
- }
- /**
- * Gets config data for given object in the form of
- * a mapping object
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access public
- * @param string $name Name of object to get mapping for
- * @param array $arrayData Array of configuration data
- * @return object Mapping object
- * @todo should probably scan $this->arrayData for potentially harmful PHP
- *
- */
- public function getMapping($name, $viewsDir, $commandsDir, $baseURL, $arrayData = '')
- {
- if (!file_exists($arrayData) AND !is_array($this->arrayData)) {
- return false;
- }
- if (empty($this->arrayData)) {
- $xmlData = file_get_contents($arrayData);
- $arrayFileName = dirname($arrayData);
- $arrayFileName = $arrayFileName . '/compiled_mvcConfig.php';
- $needsCompile = false;
- if (file_exists($arrayFileName)) {
- if (filemtime($arrayFileName) < filemtime($arrayData)) {
- $needsCompile = true;
- }
- } else {
- $needsCompile = true;
- }
- if ($needsCompile) {
- $this->dom->loadXML($xmlData);
- if (!empty($xmlData)) {
- $this->xmlToArray();
- }
- file_put_contents($arrayFileName, '<?php $this->arrayData = ' . var_export($this->arrayData, true) . '; ?>');
- } else {
- require_once "$arrayFileName";
- }
- }
- return parent::getMapping($name, $viewsDir, $commandsDir, $baseURL, $this->arrayData);
- }
- /**
- * Converts the MVC XML configuration into an array
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access public
- *
- */
- public function xmlToArray()
- {
- $arrayData = array();
- $arrayData['views'] = $this->parseSection(MVCnPHP_XMLLoader::VIEWS);
- $arrayData['commands'] = $this->parseSection(MVCnPHP_XMLLoader::COMMANDS);
- /*$fp = fopen($glConf['path'] . 'compiled_mvcconfig.php', 'w');
- fwrite($fp, '<?php $this->arrayData = */
- $this->arrayData = $arrayData;
- }
- /**
- * Parses a section of the XML configuration
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access private
- * @param string $section Use one of the two class constants for this value
- * @return array Array representation of the XML for the given section
- *
- */
- private function parseSection($section)
- {
- $xpath = new DOMXPath($this->dom);
- $tagList = $xpath->query("/MVC_CONFIGURATION/$section");
- $tagArray = array();
- foreach ($tagList as $curTag) {
- $tagID = $curTag->getAttribute('ID');
- $tagName = $curTag->getAttribute('NAME');
- $tagDefault = $curTag->getAttribute('DEFAULT');
- $queryStr = sprintf("/MVC_CONFIGURATION/%s[@ID='%s']/FORWARD", $section, $tagID);
- $forwardList = $xpath->query($queryStr);
- $forwardArray = array();
- foreach ($forwardList as $curForward) {
- $forwardID = $curForward->getAttribute('ID');
- $forwardType = $curForward->getAttribute('TYPE');
- $forwardTarget = $curForward->textContent;
- $forwardArray[$forwardID] = array('target'=>$forwardTarget,'type'=>$forwardType);
- }
- if (strtolower($tagDefault) == 'true') {
- if (count($forwardArray) > 0) {
- $tagArray[$tagID] = array('name'=>$tagName, 'forwards'=>$forwardArray, 'default'=>1);
- } else {
- $tagArray[$tagID] = array('name'=>$tagName, 'default'=>1);
- }
- } else {
- if (count($forwardArray) > 0) {
- $tagArray[$tagID] = array('name'=>$tagName, 'forwards'=>$forwardArray);
- } else {
- $tagArray[$tagID] = array('name'=>$tagName);
- }
- }
- }
- return $tagArray;
- }
- /**
- * Gets the array representation of MVC XML configuration
- *
- * @author Tony Bibbs <tony@geeklog.net>
- * @access pubic
- * @return array Array representation of MVC configuration
- *
- */
- public function getArrayData()
- {
- return $this->arrayData;
- }
- }
- ?>
Documentation generated on Mon, 7 Mar 2005 22:36:23 -0600 by phpDocumentor 1.3.0RC3