Updated the code with the fixes suggested in the comments. Tested on 2.0.2

World Wide Web Server 2012-07-04 16:03:22 -07:00
parent 15876963b9
commit 7c2e2c6490

@ -59,76 +59,76 @@ Just copy and paste both of the libraries below into your application/libraries
/**
* A very simple sitemap link generator
*
*
* This class uses the PHP5 Reflection class to find all the public methods
* in a CodeIgniter controller class and generate a list of links.
*
* @author Jonathon Hill <jonathon@compwright.com>
*
* @author Jonathon Hill <jonathon@compwright.com>
* @license CodeIgniter license
* @requires MY_Parser extended Parser class [added sparse() for parsing templates stored in a string var]
* @requires CodeIgniter 1.6 and PHP5
* @version 1.1
*
*
*/
class Sitemap {
/**
* CodeIgniter base object reference
*
* @var object
*/
private $CI;
/**
* Sitemap links template
*
* @var string
*/
private $template = '&lt;h2&gt;&lt;a href="{section_index}"&gt;{section_text}&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;{links}&lt;li&gt;&lt;a href="{link_url}"&gt;{link_text}&lt;/a&gt;&lt;/li&gt;{/links}&lt;/ul&gt;';
private $template = '<h2><a href="{section_index}">{section_text}</a></h2><ul>{links}<li><a href="{link_url}">{link_text}</a></li>{/links}</ul>';
private $template_file; // optional sitemap template file
/**
* Hide the index page by default
*
* @var boolean
*/
private $show_index = false;
/**
* Method names to ignore
*
* @var array
*/
private $ignore = array(
'*' =&gt; array(
'*' => array(
'get_instance',
'controller',
'ci_base'
)
);
/**
* Sitemap object initialization
*
*/
function __construct() {
$this-&gt;CI =& get_instance();
$this-&gt;CI-&gt;load-&gt;library('parser');
$this->CI =& get_instance();
$this->CI->load->library('parser');
}
/**
* Set configuration option(s).
*
*
* Usage:
* $this-&gt;sitemap-&gt;set_option($option, $value);
* $this-&gt;sitemap-&gt;set_option(array(
* 'option' =&gt; 'value',
* $this->sitemap->set_option($option, $value);
* $this->sitemap->set_option(array(
* 'option' => 'value',
* ...
* ));
*
@ -136,7 +136,7 @@ class Sitemap {
* template (string) template stored in string
* template_file &#40;string&#41; template stored in file
* show_index (bool) show or hide the index page
*
*
* @param mixed $option
* @param mixed $value
* @return boolean
@ -145,16 +145,16 @@ class Sitemap {
{
if(is_array($option))
{
foreach($option as $opt =&gt; $val)
foreach($option as $opt => $val)
{
if($opt == 'ignore') continue;
if(isset($this-&gt;$opt)) $this-&gt;$opt = $val;
if(isset($this->$opt)) $this->$opt = $val;
}
return true;
}
elseif(isset($this-&gt;$option) && $option != 'ignore')
elseif(isset($this->$option) && $option != 'ignore')
{
$this-&gt;$option = $value;
$this->$option = $value;
return true;
}
else
@ -162,8 +162,8 @@ class Sitemap {
return false;
}
}
/**
* Return a configuration option
*
@ -172,17 +172,17 @@ class Sitemap {
*/
function get_option($option)
{
return ($this-&gt;$option)? $this-&gt;option : false;
return ($this->$option)? $this->option : false;
}
/**
* Ignore a controller or specific pages in a controller
* Usage:
* $this-&gt;sitemap-&gt;ignore('controller', '*'); // completely ignore a controller
* $this-&gt;sitemap-&gt;ignore('controller', array('page1', 'page2')); // ignore certain pages
*
*
* $this->sitemap->ignore('controller', '*'); // completely ignore a controller
* $this->sitemap->ignore('controller', array('page1', 'page2')); // ignore certain pages
*
*
* @param string $controller
* @param mixed $pages
*/
@ -195,12 +195,12 @@ class Sitemap {
else {
$pages = strtolower($pages);
}
if(is_array($this-&gt;ignore[$controller])) $pages = array_merge($this-&gt;ignore[$controller], (array) $pages);
$this-&gt;ignore[$controller] = $pages;
if(isset($this->ignore[$controller]) AND is_array($this->ignore[$controller])) $pages = array_merge($this->ignore[$controller], (array) $pages);
$this->ignore[$controller] = $pages;
}
/**
* Build a list of pages in a controller
*
@ -211,44 +211,44 @@ class Sitemap {
{
// Use the PHP5 Reflection class to introspect the controller
$controller = new ReflectionClass($class);
$data['links'] = array();
$data['section_index'] = strtolower(site_url($class));
$data['section_text'] = ucwords(strtr($class, array('_'=&gt;' ')));
foreach($controller-&gt;getMethods() as $method)
$data['section_text'] = ucwords(strtr($class, array('_'=>' ')));
foreach($controller->getMethods() as $method)
{
// skip methods that begin with '_'
if(substr($method-&gt;name, 0, 1) == '_') continue;
if(substr($method->name, 0, 1) == '_') continue;
// skip globally ignored names
if(in_array(strtolower($method-&gt;name), $this-&gt;ignore['*'])) continue;
if(in_array(strtolower($method->name), $this->ignore['*'])) continue;
// skip ignored controller methods
if(in_array(strtolower($method-&gt;name), (array) $this-&gt;ignore[strtolower($class)])) continue;
if(isset($this->ignore[strtolower($class)]) AND in_array(strtolower($method->name), (array) $this->ignore[strtolower($class)])) continue;
// skip index page
if($method-&gt;name == 'index' && !$this-&gt;show_index) continue;
if($method->name == 'index' && !$this->show_index) continue;
// skip old-style constructor
if(strtolower($method-&gt;name) == strtolower($class)) continue;
if(strtolower($method->name) == strtolower($class)) continue;
// skip methods that aren't public
if(!$method-&gt;isPublic()) continue;
if(!$method->isPublic()) continue;
// build link data for parser class
$data['links'][] = array(
'link_url' =&gt; strtolower(site_url("$class/$method-&gt;name")),
'link_text'=&gt; ucwords(strtr($method-&gt;name, array('_'=&gt;' '))),
'link_url' => strtolower(site_url("$class/$method->name")),
'link_text'=> ucwords(strtr($method->name, array('_'=>' '))),
);
}
return ($this-&gt;template_file)?
$this-&gt;CI-&gt;parser-&gt;parse($this-&gt;template_file, $data, true) :
$this-&gt;CI-&gt;parser-&gt;sparse($this-&gt;template, $data, true);
return ($this->template_file)?
$this->CI->parser->parse($this->template_file, $data, true) :
$this->CI->parser->sparse($this->template, $data, true);
}
/**
* Build a complete sitemap from your CI application controllers
*
@ -256,22 +256,22 @@ class Sitemap {
*/
function generate()
{
$this-&gt;CI-&gt;load-&gt;helper('file');
$this->CI->load->helper('file');
$sitemap = '';
$controllers_path = APPPATH.'controllers/';
foreach(get_filenames($controllers_path, true) as $controller) {
list($class, $ext) = explode('.', ucfirst(basename($controller)));
if($ext != 'php') continue; // skip anything other than PHP files
if($this-&gt;ignore[strtolower($class)] == '*') continue; // skip controllers marked as 'ignore'
if(isset($this->ignore[strtolower($class)]) AND $this->ignore[strtolower($class)] == '*') continue; // skip controllers marked as 'ignore'
if(!class_exists($class)) include($controller); // include the class for access
$sitemap .= $this-&gt;get_links($class) . "\n";
$sitemap .= $this->get_links($class) . "\n";
}
return $sitemap;
}
/*
* Callback wrapper function for strtolower
* Has 2 args to prevent warnings from the strtolower() function
@ -280,8 +280,6 @@ class Sitemap {
}
?&gt;
[/code]
[h3]MY_Parser.php[/h3]