From 8464c8edac07905198f23b127598affcf24dce98 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Thu, 24 Nov 2011 19:22:12 +0400 Subject: [PATCH] modified Autoloader for folders exclusion --- Load.php | 34 +++++++++++++++++++++++++--------- util/AutoloadBuilder.php | 20 +++++++++++++++++++- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Load.php b/Load.php index 432cd2c..f2085ad 100644 --- a/Load.php +++ b/Load.php @@ -11,10 +11,27 @@ class Load { - + static protected $file; + static protected $autoload; + static protected $exclude = array('/.git', '/lib/core/tests', '/lib/core/.git'); + + /** + * Add exclude path for autoload. Should be called before setAutoloadFrom + * @static + * @param array $exclude list of relative path (from project root) + * @return void + */ + static public function setExclude($exclude = array()) + { + if(!is_array($exclude)) { + $exclude = array($exclude); + } + self::$exclude = array_merge(self::$exclude, $exclude); + } + static public function setAutoloadFrom($file) { self::$file = $file; @@ -23,7 +40,7 @@ class Load } self::$autoload = require(self::$file); } - + static public function autoload($class) { if (isset(self::$autoload[$class])) { @@ -35,16 +52,16 @@ class Load self::buildAutoload(); } if (isset(self::$autoload[$class])) { - require(PATH . self::$autoload[$class]); + require(PATH . self::$autoload[$class]); } } } - + static public function getFilePath($class) { return self::$autoload[$class]; } - + static protected function buildAutoload() { ignore_user_abort(true); @@ -52,12 +69,11 @@ class Load if (!file_exists($dir) && !mkdir($dir)) { trigger_error('Can\'t create directory: "' . $dir . '"', E_USER_ERROR); } - + $scan = array(PATH . '/' . APP . '/src', PATH . '/lib'); - require_once(PATH . '/lib/core/util/AutoloadBuilder.php'); - - $builder = new AutoloadBuilder(self::$file, $scan); + + $builder = new AutoloadBuilder(self::$file, $scan, self::$exclude); $builder->build(); ignore_user_abort(false); } diff --git a/util/AutoloadBuilder.php b/util/AutoloadBuilder.php index 4e4ff74..3f64ef6 100644 --- a/util/AutoloadBuilder.php +++ b/util/AutoloadBuilder.php @@ -20,14 +20,17 @@ class AutoloadBuilder protected $dirs; + protected $exclude; + protected $handler; protected $regex = '/(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/'; - public function __construct($autoload, $dirs = array()) + public function __construct($autoload, $dirs = array(), $exclude = array()) { $this->autoload = $autoload; $this->dirs = $dirs; + $this->exclude = $exclude; } public function build() @@ -42,6 +45,10 @@ class AutoloadBuilder ); foreach ($iterator as $file) { + + if($this->isExcluded($file->getRealPath())) { + continue; + } // skip non php files $e = explode('.', $file->getFileName()); if ((end($e) !== 'php') || $this->rightSubstr($file->getFileName(), 8) == 'Test.php') { @@ -68,6 +75,17 @@ class AutoloadBuilder $this->closeAutoload(); } + protected function isExcluded($file) + { + foreach ($this->exclude as $dir) { + if (stripos($file, PATH . $dir) === 0) { + return true; + } + } + return false; + } + + protected function openAutoload() { $this->write("