mirror of
https://github.com/bcit-ci/CodeIgniter.git
synced 2025-02-20 11:13:29 +08:00
Updated the code with the fixes suggested in the comments. Tested on 2.0.2
parent
15876963b9
commit
7c2e2c6490
148
Sitemap.md
148
Sitemap.md
@ -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 = '<h2><a href="{section_index}">{section_text}</a></h2><ul>{links}<li><a href="{link_url}">{link_text}</a></li>{/links}</ul>';
|
||||
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(
|
||||
'*' => array(
|
||||
'*' => array(
|
||||
'get_instance',
|
||||
'controller',
|
||||
'ci_base'
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Sitemap object initialization
|
||||
*
|
||||
*/
|
||||
function __construct() {
|
||||
$this->CI =& get_instance();
|
||||
$this->CI->load->library('parser');
|
||||
$this->CI =& get_instance();
|
||||
$this->CI->load->library('parser');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set configuration option(s).
|
||||
*
|
||||
*
|
||||
* Usage:
|
||||
* $this->sitemap->set_option($option, $value);
|
||||
* $this->sitemap->set_option(array(
|
||||
* 'option' => '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 (string) 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 => $val)
|
||||
foreach($option as $opt => $val)
|
||||
{
|
||||
if($opt == 'ignore') continue;
|
||||
if(isset($this->$opt)) $this->$opt = $val;
|
||||
if(isset($this->$opt)) $this->$opt = $val;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
elseif(isset($this->$option) && $option != 'ignore')
|
||||
elseif(isset($this->$option) && $option != 'ignore')
|
||||
{
|
||||
$this->$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->$option)? $this->option : false;
|
||||
return ($this->$option)? $this->option : false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Ignore a controller or specific pages in a controller
|
||||
* Usage:
|
||||
* $this->sitemap->ignore('controller', '*'); // completely ignore a controller
|
||||
* $this->sitemap->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->ignore[$controller])) $pages = array_merge($this->ignore[$controller], (array) $pages);
|
||||
$this->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('_'=>' ')));
|
||||
|
||||
foreach($controller->getMethods() as $method)
|
||||
$data['section_text'] = ucwords(strtr($class, array('_'=>' ')));
|
||||
|
||||
foreach($controller->getMethods() as $method)
|
||||
{
|
||||
// skip methods that begin with '_'
|
||||
if(substr($method->name, 0, 1) == '_') continue;
|
||||
|
||||
if(substr($method->name, 0, 1) == '_') continue;
|
||||
|
||||
// skip globally ignored names
|
||||
if(in_array(strtolower($method->name), $this->ignore['*'])) continue;
|
||||
if(in_array(strtolower($method->name), $this->ignore['*'])) continue;
|
||||
|
||||
// skip ignored controller methods
|
||||
if(in_array(strtolower($method->name), (array) $this->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->name == 'index' && !$this->show_index) continue;
|
||||
|
||||
if($method->name == 'index' && !$this->show_index) continue;
|
||||
|
||||
// skip old-style constructor
|
||||
if(strtolower($method->name) == strtolower($class)) continue;
|
||||
|
||||
if(strtolower($method->name) == strtolower($class)) continue;
|
||||
|
||||
// skip methods that aren't public
|
||||
if(!$method->isPublic()) continue;
|
||||
|
||||
if(!$method->isPublic()) continue;
|
||||
|
||||
// build link data for parser class
|
||||
$data['links'][] = array(
|
||||
'link_url' => strtolower(site_url("$class/$method->name")),
|
||||
'link_text'=> ucwords(strtr($method->name, array('_'=>' '))),
|
||||
'link_url' => strtolower(site_url("$class/$method->name")),
|
||||
'link_text'=> ucwords(strtr($method->name, array('_'=>' '))),
|
||||
);
|
||||
}
|
||||
|
||||
return ($this->template_file)?
|
||||
$this->CI->parser->parse($this->template_file, $data, true) :
|
||||
$this->CI->parser->sparse($this->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->CI->load->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->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->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 {
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
[/code]
|
||||
|
||||
[h3]MY_Parser.php[/h3]
|
||||
|
Loading…
x
Reference in New Issue
Block a user