Skip to content

Commit

Permalink
Added support for multiple search templates
Browse files Browse the repository at this point in the history
  • Loading branch information
setola committed Dec 18, 2012
1 parent 9bb5cbf commit 22575df
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions ClassAutoloader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,57 @@
*
*/
class ClassAutoloader {
var $loading_template;
const WORDPRESS_THEME_UTILS_CLASS_DIR = 'classes';
public $loading_template;

public function __construct() {
$this->set_loading_template(dirname(__FILE__) . '/%classname%.class.php');
spl_autoload_register(array($this, 'loader'));
$this->add_loading_template(
WORDPRESS_THEME_UTILS_PATH.'/'.self::WORDPRESS_THEME_UTILS_CLASS_DIR.'/%classname%.class.php'
);
//spl_autoload_register(array($this, 'loader'));
}

/**
* Set the loading template
* @param string $tpl the template
* @return ClassAutoloader $this for chainability
*/
public function set_loading_template($tpl){
$this->loading_template = $tpl;
public function add_loading_template($tpl){
$this->loading_template[] = $tpl;
return $this;
}

/**
* Hook to the WordPress init
*/
public function hook(){
add_action('init', array($this, 'register_autoload'));
}

/**
* Register the autoload function to PHP
*/
public function register_autoload(){
spl_autoload_register(array($this, 'loader'));
}

/**
* Autoload needed classes
* @param String $className the name of the class
* @return ClassAutoloader $this for chainability
*/
private function loader($className) {
$filename = str_replace(
foreach($this->loading_template as $tpl){
$filename = str_replace(
array('%classname%'),
array($className),
$this->loading_template
);
if(file_exists($filename)) include_once $filename;
$tpl
);
if(file_exists($filename)) {
include_once $filename;
return $this;
}
}
return $this;
}
}

0 comments on commit 22575df

Please sign in to comment.