From 884a2f4a9ed898eace7bceab6fc5b1d38171dbb5 Mon Sep 17 00:00:00 2001 From: rMalie Date: Mon, 8 Aug 2011 14:24:32 +0000 Subject: [PATCH] // Add some comments in new autoload class --- classes/Autoload.php | 151 +++++++++++++++++++++++++++---------------- 1 file changed, 97 insertions(+), 54 deletions(-) diff --git a/classes/Autoload.php b/classes/Autoload.php index 48b89a686..8ba0d331a 100644 --- a/classes/Autoload.php +++ b/classes/Autoload.php @@ -1,25 +1,63 @@ +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ class Autoload { + /** + * File where classes index is stored + */ const INDEX_FILE = 'cache/class_index.php'; + /** + * @var Autoload + */ protected static $instance; + + /** + * @var string Root directory + */ protected $root_dir; /** - * array('classname' => 'path/to/override', 'classnamecore' => 'path/to/class/core') + * @var array array('classname' => 'path/to/override', 'classnamecore' => 'path/to/class/core') */ public $index = array(); - protected function __construct() { $this->root_dir = dirname(dirname(__FILE__)).'/'; + if (file_exists($this->root_dir.Autoload::INDEX_FILE)) + $this->index = include_once($this->root_dir.Autoload::INDEX_FILE); } /** - * + * Get instance of autoload (singleton) + * + * @return Autoload */ public static function getInstance() { @@ -28,54 +66,10 @@ class Autoload return Autoload::$instance; } - - public function generateIndex() - { - $classes = array_merge( - $this->getClassesFromDir('classes/', true), - $this->getClassesFromDir('override/classes/', false) - ); - $content = 'root_dir.Autoload::INDEX_FILE; - - if ((file_exists($filename) && is_writable($filename)) || is_writable(dirname($filename))) - file_put_contents($filename, $content); - else - throw new Exception($filename.' is not writable!'); - - $this->index = include_once($this->root_dir.Autoload::INDEX_FILE); - } - - protected function getClassesFromDir($path, $is_core) - { - $classes = array(); - - foreach (scandir($this->root_dir.$path) as $file) - { - if ($file[0] != '.') - { - if (is_dir($this->root_dir.$path.$file)) - $classes = array_merge($classes, $this->getClassesFromDir($path.$file.'/', $is_core)); - else if (substr($file, -4) == '.php') - { - $content = file_get_contents($this->root_dir.$path.$file); - if (preg_match('#\W((abstract\s+)?class|interface)\s+(?P'.basename($file, '.php').'(Core)?)(\s+(extends|implements)\s+[a-z][a-z0-9_]*)?\s*\{#i', $content, $m)) - { - $classes[$m['classname']] = $path.$file; - if (substr($m['classname'], -4) == 'Core') - $classes[substr($m['classname'], 0, -4)] = ''; - } - } - } - } - - return $classes; - } - + /** - * - * + * Retrive informations about a class in classes index and load it + * * @param string $classname */ public function load($classname) @@ -83,10 +77,8 @@ class Autoload if (!isset($this->index[$classname])) $this->generateIndex(); - $is_core = (substr($classname, -4) == 'Core'); - // If $classname has not core suffix (E.g. Shop, Product) - if (!$is_core) + if (substr($classname, -4) == 'Core') { // If requested class does not exist, load associated core class if (isset($this->index[$classname]) && !$this->index[$classname]) @@ -121,5 +113,56 @@ class Autoload throw new Exception('Class not found: '.$classname); } -} + /** + * Generate classes index + */ + public function generateIndex() + { + $classes = array_merge( + $this->getClassesFromDir('classes/', true), + $this->getClassesFromDir('override/classes/', false) + ); + $content = 'root_dir.Autoload::INDEX_FILE; + if ((file_exists($filename) && is_writable($filename)) || is_writable(dirname($filename))) + file_put_contents($filename, $content); + else + throw new Exception($filename.' is not writable!'); + + $this->index = $classes; + } + + /** + * Retrive recursively all classes in a directory and its subdirectories + * + * @param unknown_type $path Relativ path from root to the directory + * @return array + */ + protected function getClassesFromDir($path) + { + $classes = array(); + + foreach (scandir($this->root_dir.$path) as $file) + { + if ($file[0] != '.') + { + if (is_dir($this->root_dir.$path.$file)) + $classes = array_merge($classes, $this->getClassesFromDir($path.$file.'/')); + else if (substr($file, -4) == '.php') + { + $content = file_get_contents($this->root_dir.$path.$file); + if (preg_match('#\W((abstract\s+)?class|interface)\s+(?P'.basename($file, '.php').'(Core)?)(\s+(extends|implements)\s+[a-z][a-z0-9_]*)?\s*\{#i', $content, $m)) + { + $classes[$m['classname']] = $path.$file; + if (substr($m['classname'], -4) == 'Core') + $classes[substr($m['classname'], 0, -4)] = ''; + } + } + } + } + + return $classes; + } +}