00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 class Varien_Autoload
00031 {
00032 const SCOPE_FILE_PREFIX = '__';
00033
00034 static protected $_instance;
00035 static protected $_scope = 'default';
00036
00037 protected $_isIncludePathDefined= null;
00038 protected $_collectClasses = false;
00039 protected $_collectPath = null;
00040 protected $_arrLoadedClasses = array();
00041
00042
00043
00044
00045 public function __construct()
00046 {
00047 $this->_isIncludePathDefined = defined('COMPILER_INCLUDE_PATH');
00048 if (defined('COMPILER_COLLECT_PATH')) {
00049 $this->_collectClasses = true;
00050 $this->_collectPath = COMPILER_COLLECT_PATH;
00051 }
00052 self::registerScope(self::$_scope);
00053 }
00054
00055
00056
00057
00058
00059
00060 static public function instance()
00061 {
00062 if (!self::$_instance) {
00063 self::$_instance = new Varien_Autoload();
00064 }
00065 return self::$_instance;
00066 }
00067
00068
00069
00070
00071 static public function register()
00072 {
00073 spl_autoload_register(array(self::instance(), 'autoload'));
00074 }
00075
00076
00077
00078
00079
00080
00081 public function autoload($class)
00082 {
00083 if ($this->_collectClasses) {
00084 $this->_arrLoadedClasses[self::$_scope][] = $class;
00085 }
00086 if ($this->_isIncludePathDefined) {
00087 $classFile = $class;
00088 } else {
00089 $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
00090 }
00091 $classFile.= '.php';
00092
00093 return include $classFile;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 static public function registerScope($code)
00104 {
00105 self::$_scope = $code;
00106 @include self::SCOPE_FILE_PREFIX.$code.'.php';
00107 }
00108
00109
00110
00111
00112
00113
00114 static public function getScope()
00115 {
00116 return self::$_scope;
00117 }
00118
00119
00120
00121
00122 public function __destruct()
00123 {
00124 if ($this->_collectClasses) {
00125 $this->_saveCollectedStat();
00126 }
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 protected function _saveCollectedStat()
00136 {
00137 if (!is_dir($this->_collectPath)) {
00138 @mkdir($this->_collectPath);
00139 @chmod($this->_collectPath, 0777);
00140 }
00141
00142 if (!is_writeable($this->_collectPath)) {
00143 return $this;
00144 }
00145
00146 foreach ($this->_arrLoadedClasses as $scope => $classes) {
00147 $file = $this->_collectPath.DIRECTORY_SEPARATOR.$scope.'.csv';
00148 $data = array();
00149 if (file_exists($file)) {
00150 $data = explode("\n", file_get_contents($file));
00151 foreach ($data as $index => $class) {
00152 $class = explode(':', $class);
00153 $searchIndex = array_search($class[0], $classes);
00154 if ($searchIndex !== false) {
00155 $class[1]+=1;
00156 unset($classes[$searchIndex]);
00157 }
00158 $data[$index] = $class[0].':'.$class[1];
00159 }
00160 }
00161 foreach ($classes as $class) {
00162 $data[] = $class . ':1';
00163 }
00164 file_put_contents($file, implode("\n", $data));
00165 }
00166 return $this;
00167 }
00168 }