[Cs-content-commits] SF.net SVN: cs-content:[369] trunk/1.0/contentSystem.class.php
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-03-03 19:07:53
|
Revision: 369 http://cs-content.svn.sourceforge.net/cs-content/?rev=369&view=rev Author: crazedsanity Date: 2009-03-03 19:07:51 +0000 (Tue, 03 Mar 2009) Log Message: ----------- Fix problems from last commit & minor bug with adding includes. /contentSystem.class.php: * parse_section(): -- when checking the size of the $myArr var, don't check the value of the first (0) index. -- added a TODO about possible problems from an empty array -- throw an exception if no array is retrieved from section (shouldn't ever happen). * validate_page(): -- NOTE::: this method was very much overhauled to make it simpler. -- checks for two template files that would indicate it is a valid page instead of doing so much cd()'ing and such with a lot of duplicated and confusing code (I was even confused, and I wrote it). * load_page_templates(): -- instead of using the last item in the internal sectionArr, it uses the internal "finalSection" variable. -- always load the final template list followed by loading items for the final section instead of doing an if/else statement (which could cause some frustrating & unexpected bahaviour). * load_main_templates(): -- cd() into the baseDir again (allows the final content template to be loaded again; previous commit broke this). * load_includes(): -- cd() to the internal "finalSection" and include the index and shared files (but only if the cd() succeeds). * add_include(): -- fix problems with extra slashes in the filename causing files to be included multiple times -- only load a new include if the result of array_search() isn't numeric: previously, if the requested include was already in index 0, it would get included again. Modified Paths: -------------- trunk/1.0/contentSystem.class.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-03-02 21:10:18 UTC (rev 368) +++ trunk/1.0/contentSystem.class.php 2009-03-03 19:07:51 UTC (rev 369) @@ -292,10 +292,15 @@ $myArr = split('/', $this->section); //if we've got something in the array, keep going. - if(is_array($myArr) && count($myArr) && ($myArr[0] !== 0)) { + if(is_array($myArr) && count($myArr) > 0) { + + //TODO: if there's only one section here, sectionArr becomes BLANK... does that cause unexpected behaviour? $this->baseDir = array_shift($myArr); $this->sectionArr = $myArr; } + else { + throw new exception(__METHOD__ .": failed to get an array from section (". $this->section .")"); + } }//end parse_section() //------------------------------------------------------------------------ @@ -400,84 +405,36 @@ /** * Ensures the page we're on would actually load, so other methods don't have to do * so much extra checking. + * + * TODO: the if & else should be consolidated as much as possible... */ private function validate_page() { - $valid = FALSE; - //if we've got a non-basedir page, (instead of "/whatever", we have "/whatever/x"), see - // if there are templates that make it good... or just check the base template. + + $this->tmplFs->cd('/'); + + $valid = false; + if((count($this->sectionArr) > 0) && !((count($this->sectionArr) == 1) && ($this->sectionArr[0] == 'index'))) { - //got more than just a baseDir url... see if the template is good. - $finalLink = $this->gfObj->string_from_array($this->sectionArr, NULL, '/'); - $this->tmplFs->cd('/'); $mySectionArr = $this->sectionArr; - $finalSection = array_pop($mySectionArr); - $this->finalSection = $finalSection; - if(count($mySectionArr) > 0) { - foreach($mySectionArr as $dir) { - if(!$this->tmplFs->cd($dir)) { - break; - } - } - } - - //check for the file & the directory... - $indexFilename = $finalSection ."/index.content.tmpl"; - if(!strlen($finalSection)) { - $indexFilename = 'index.content.tmpl'; - } - - $lsDir = $this->tmplFs->ls($indexFilename); - $lsDirVals = array_values($lsDir); - $lsFile = $this->tmplFs->ls("$finalSection.content.tmpl"); - - if(is_array(array_values($lsFile)) && is_array($lsFile[$finalSection .".content.tmpl"])) { - //it's the file ("{finalSection}.content.tmpl", like "mySection.content.tmpl") - $myIndex = $finalSection .".content.tmpl"; - } - elseif(is_array(array_values($lsDir)) && (is_array($lsDir[$indexFilename]))) { - $myIndex = $indexFilename; - } - else { - //nothin' doin'. - $myIndex = NULL; - } - - //check the index file for validity... this is kind of a dirty hack... but it works. - $checkMe = $this->tmplFs->ls($myIndex); - if(!is_array($checkMe[$myIndex])) { - unset($myIndex); - } - - if(isset($myIndex)) { - $valid = TRUE; - $this->tmplFs->cd('/'); - } - else { - $this->reason = __METHOD__ .": couldn't find page template for ". $this->section; - } + $this->finalSection = array_pop($mySectionArr); + $reasonText = "page template"; } else { - //if the baseDir is "help", this would try to use "/help/index.content.tmpl" - $myFile = $this->baseDir .'/index.content.tmpl'; - $sectionLsData = $this->tmplFs->ls($myFile); - - //if the baseDir is "help", this would try to use "/help.content.tmpl" - $sectionFile = $this->baseDir .'.content.tmpl'; - $lsData = $this->tmplFs->ls(); - - if(isset($lsData[$sectionFile]) && is_array($lsData[$sectionFile])) { - $valid = TRUE; - $this->finalSection = $this->baseDir; - } - elseif(isset($sectionLsData[$myFile]) && $sectionLsData[$myFile]['type'] == 'file') { - //we're good. - $valid = TRUE; - $this->finalSection = $this->baseDir; - } - else { - $this->reason = __METHOD__ .": couldn't find base template."; - } + $this->finalSection = $this->baseDir; + $reasonText = "base template"; } + + $tmplFile1 = $this->section .".content.tmpl"; + $tmplFile2 = $this->section ."/index.content.tmpl"; + + if(file_exists($this->tmplFs->realcwd ."/". $tmplFile2) || file_exists($this->tmplFs->realcwd ."/". $tmplFile1)) { + $valid = true; + $this->reason=null; + } + else { + $valid = false; + $this->reason=__METHOD__ .": couldn't find ". $reasonText; + } $this->isValid = $valid; return($valid); @@ -495,7 +452,7 @@ // looking for templates. $mySectionArr = $this->sectionArr; - $finalSection = $this->sectionArr[(count($this->sectionArr) -1)]; + $finalSection = $this->finalSection; foreach($mySectionArr as $index=>$value) { $tmplList = $this->arrange_directory_contents('name', 'section'); if(isset($tmplList[$value])) { @@ -509,20 +466,20 @@ } } - //load the final template(s). $finalTmplList = $this->arrange_directory_contents('name', 'section'); + foreach($finalTmplList as $mySection => $subArr) { + foreach($subArr as $internalSection => $myTmpl) { + $this->templateList[$mySection] = $myTmpl; + } + } + + //go through the final section, if set, so the templates defined there are used. if(isset($finalTmplList[$finalSection])) { foreach($finalTmplList[$finalSection] as $mySection => $myTmpl) { $this->templateList[$mySection] = $myTmpl; } } - elseif(is_array($finalTmplList)) { - foreach($finalTmplList as $mySection => $subArr) { - foreach($subArr as $internalSection => $myTmpl) { - $this->templateList[$mySection] = $myTmpl; - } - } - } + if($this->tmplFs->cd($finalSection)) { //load the index stuff. $tmplList = $this->arrange_directory_contents('name', 'section'); @@ -549,6 +506,7 @@ //check to see if the present section is valid. $this->tmplFs->cd('/'); $dirContents = $this->arrange_directory_contents('name', 'section'); + $this->tmplFs->cd($this->baseDir); if(is_array($dirContents)) { foreach($dirContents as $mySection => $subArr) { foreach($subArr as $subIndex=>$templateFilename) { @@ -694,13 +652,15 @@ } //include the final shared & index files. - $lsData = $this->incFs->ls(); - if(isset($lsData['shared.inc']) && is_array($lsData['shared.inc'])) { - $this->add_include('shared.inc'); + if($this->incFs->cd($this->finalSection)) { + $lsData = $this->incFs->ls(); + if(isset($lsData['shared.inc']) && is_array($lsData['shared.inc'])) { + $this->add_include('shared.inc'); + } + if(isset($lsData['index.inc']) && is_array($lsData['index.inc'])) { + $this->add_include('index.inc'); + } } - if(isset($lsData['index.inc']) && is_array($lsData['index.inc'])) { - $this->add_include('index.inc'); - } }//end load_includes() //------------------------------------------------------------------------ @@ -893,8 +853,8 @@ * Method that appends filenames to the list of include scripts. */ private final function add_include($file) { - $myFile = $this->incFs->realcwd .'/'. $file; - if(!array_search($myFile, $this->includesList)) { + $myFile = preg_replace('/\/{2,}/', '/', $this->incFs->realcwd .'/'. $file); + if(!is_numeric(array_search($myFile, $this->includesList))) { $this->includesList[] = $myFile; } }//end add_include() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |