[Cs-content-commits] SF.net SVN: cs-content:[317] trunk/0.10
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-01-19 17:38:47
|
Revision: 317 http://cs-content.svn.sourceforge.net/cs-content/?rev=317&view=rev Author: crazedsanity Date: 2009-01-19 17:38:36 +0000 (Mon, 19 Jan 2009) Log Message: ----------- New cs_siteConfig{} class with unit tests. Modified Paths: -------------- trunk/0.10/tests/testOfCSContent.php Added Paths: ----------- trunk/0.10/cs_siteConfig.class.php Copied: trunk/0.10/cs_siteConfig.class.php (from rev 315, trunk/0.10/cs_phpDB.php) =================================================================== --- trunk/0.10/cs_siteConfig.class.php (rev 0) +++ trunk/0.10/cs_siteConfig.class.php 2009-01-19 17:38:36 UTC (rev 317) @@ -0,0 +1,163 @@ +<?php + +/* + * A class for handling configuration of database-driven web applications. + * + * NOTICE::: this class requires that cs-phpxml and cs-arraytopath are both available + * at the same directory level as cs-content; all projects are SourceForge.net projects, + * using their unix names ("cs-phpxml" and "cs-arrayToPath"). The cs-phpxml project + * requires cs-arrayToPath for parsing XML paths. + * + * SVN INFORMATION::: + * SVN Signature:::::::: $Id$ + * Last Committted Date: $Date$ + * Last Committed Path:: $HeadURL$ + * + */ + +require_once(dirname(__FILE__). '/../cs-phpxml/xmlParserClass.php'); +require_once(dirname(__FILE__) .'/../cs-phpxml/xmlBuilderClass.php'); + +class cs_siteConfig { + + private $xmlReader; + private $xmlWriter; + private $xmlBuilder; + private $fs; + private $readOnly; + private $configDirname; + + /** Active section of the full site configuration. */ + private $activeSection; + + /** The FULL configuration file, instead of just the active section. */ + private $fullConfig=array(); + + /** arrayToPath{} object. */ + private $a2p; + + + //------------------------------------------------------------------------- + /** + * Constructor. + * + * @$configFileLocation (str) URI for config file. + */ + public function __construct($configFileLocation, $section='MAIN', $setVarPrefix=null) { + + //TODO: don't use cs_globalFunctions{} if unneeded. + $this->gf = new cs_globalFunctions; + $this->gf->debugPrintOpt=1; + + $this->set_active_section($section); + + if(strlen($configFileLocation) && file_exists($configFileLocation)) { + + $this->configDirname = dirname($configFileLocation); + $this->fs = new cs_fileSystemClass($this->configDirname); + + $this->xmlReader = new XMLParser($this->fs->read($configFileLocation)); + + if($this->fs->is_writable($configFileLocation)) { + $this->readOnly = false; + } + else { + $this->readOnly = true; + } + } + else { + throw new exception(__METHOD__ .": invalid configuration file (". $configFileLocation .")"); + } + + if(strlen($section)) { + try { + $this->parse_config(); + $this->config = $this->get_section($section); + } + catch(exception $e) { + throw new exception(__METHOD__ .": invalid section (". $section ."), DETAILS::: ". $e->getMessage()); + } + } + else { + throw new exception(__METHOD__ .": no section given (". $section .")"); + } + }//end __construct() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function set_active_section($section) { + $this->activeSection = strtoupper($section); + }//end set_active_section($section) + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + private function parse_config() { + $data = $this->xmlReader->get_path($this->xmlReader->get_root_element()); + + $specialVars = array( + '_DIRNAMEOFFILE_' => $this->configDirname + ); + $parseThis = array(); + + foreach($data as $section=>$secData) { + //only handle UPPERCASE index names; lowercase indexes are special entries (i.e. "type" or "attributes" + if($section == strtoupper($section)) { + $this->gf->debug_print(__METHOD__ .": handling (". $section .")"); + foreach($secData as $itemName=>$itemValue) { + $itemValue = $itemValue['value']; + if(preg_match("/{/", $itemValue)) { + $origVal = $itemValue; + $itemValue = $this->gf->mini_parser($itemValue, $specialVars, '{', '}'); + $itemValue = $this->gf->mini_parser($itemValue, $parseThis, '{', '}'); + $itemValue = preg_replace("/[\/]{2,}/", "/", $itemValue); + } + $parseThis[$itemName] = $itemValue; + $parseThis[$section ."/". $itemName] = $itemValue; + $data[$section][$itemName]['value'] = $itemValue; + } + } + } + $this->a2p = new arrayToPath($data); + }//end parse_config() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function get_section($section) { + $this->gf->debug_print(__METHOD__ .": section=(". $section .")"); + $data = $this->a2p->get_data($section); + + if(is_array($data) && count($data) && $data['type'] == 'open') { + unset($data['type']); + $retval = $data; + } + else { + throw new exception(__METHOD__ .": invalid section or no data (". $data['type'] .")"); + } + + return($retval); + }//end get_section() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function get_value($index) { + if(preg_match("/\//", $index)) { + //section NOT given, assume they're looking for something in the active section. + $index = $this->activeSection ."/". $index; + } + $retval = $this->a2p->get_data($index .'/value'); + return($retval); + }//end get_value() + //------------------------------------------------------------------------- + + +}//end cs_siteConfig + +?> Property changes on: trunk/0.10/cs_siteConfig.class.php ___________________________________________________________________ Added: svn:eol + native Added: svn:keywords + Id HeadURL Date Revision Author Added: svn:mergeinfo + Added: svn:eol-style + native Modified: trunk/0.10/tests/testOfCSContent.php =================================================================== --- trunk/0.10/tests/testOfCSContent.php 2009-01-19 16:43:10 UTC (rev 316) +++ trunk/0.10/tests/testOfCSContent.php 2009-01-19 17:38:36 UTC (rev 317) @@ -14,15 +14,13 @@ - - - //============================================================================= class TestOfCSContent extends UnitTestCase { //------------------------------------------------------------------------- function __construct() { require_once(dirname(__FILE__) .'/../cs_globalFunctions.php'); + require_once(dirname(__FILE__) .'/../cs_siteConfig.class.php'); }//end __construct() //------------------------------------------------------------------------- @@ -176,6 +174,35 @@ + //------------------------------------------------------------------------- + public function test_siteConfig() { + $configFile = dirname(__FILE__) .'/files/sampleConfig.xml'; + $sc = new cs_siteConfig($configFile); + + //make sure that specifying the section "main" section works just like NOT specifying it. + $this->assertEqual($sc->get_value('SITEROOT'), $sc->get_value('MAIN/SITEROOT')); + $this->assertEqual($sc->get_value('SITEROOT'), $sc->get_value('siteroot')); + $this->assertEqual($sc->get_value('SITEROOT'), $sc->get_value('siteRoot')); + + //make sure if we request an index that doesn't exist, it is returned as null + $this->assertTrue(is_null($sc->get_value('NONExISTENT___'))); + + //make sure some values have been replaced. + $this->assertTrue(!preg_match("/{/", $sc->get_value('libdir'))); + $this->assertTrue( + preg_match("/^". preg_replace("/\//", "\/", $sc->get_value('siteroot')) ."/", $sc->get_value('libdir')), + "LIBDIR (". $sc->get_value('libdir') .") doesn't contain SITEROOT (". $sc->get_value('siteroot') .")" + ); + $this->assertEqual( + $sc->get_value('main/tmpldir'), + $sc->get_value('cs-content/tmpldir'), + "path replacement for cs-content/tmpldir (". $sc->get_value('cs-content/tmpldir') .") didn't match main/tmpldir (". $sc->get_value('main/tmpldir') .")" + ); + }//end test_siteConfig() + //------------------------------------------------------------------------- + + + }//end TestOfCSContent //============================================================================= ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |