Source for file RobotLoader.php

Documentation is available at RobotLoader.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Loaders
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Loaders/AutoLoader.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Framework.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Nette auto loader is responsible for loading classes and interfaces.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Loaders
  34. 34:  */
  35. 35: class RobotLoader extends AutoLoader
  36. 36: {
  37. 37:     /** @var array */
  38. 38:     public $scanDirs;
  39. 39:  
  40. 40:     /** @var string  comma separated wildcards */
  41. 41:     public $ignoreDirs = '.*, *.old, *.bak, *.tmp, temp';
  42. 42:  
  43. 43:     /** @var string  comma separated wildcards */
  44. 44:     public $acceptFiles = '*.php, *.php5';
  45. 45:  
  46. 46:     /** @var bool */
  47. 47:     public $autoRebuild;
  48. 48:  
  49. 49:     /** @var array */
  50. 50:     private $list array();
  51. 51:  
  52. 52:     /** @var array */
  53. 53:     private $timestamps;
  54. 54:  
  55. 55:     /** @var bool */
  56. 56:     private $rebuilded FALSE;
  57. 57:  
  58. 58:     /** @var string */
  59. 59:     private $acceptMask;
  60. 60:  
  61. 61:     /** @var string */
  62. 62:     private $ignoreMask;
  63. 63:  
  64. 64:  
  65. 65:  
  66. 66:     /**
  67. 67:      */
  68. 68:     public function __construct()
  69. 69:     {
  70. 70:         if (!extension_loaded('tokenizer')) {
  71. 71:             throw new Exception("PHP extension Tokenizer is not loaded.");
  72. 72:         }
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Register autoloader.
  79. 79:      * @return void 
  80. 80:      */
  81. 81:     public function register()
  82. 82:     {
  83. 83:         $cache $this->getCache();
  84. 84:         $key $this->getKey();
  85. 85:         if (isset($cache[$key])) {
  86. 86:             $this->list $cache[$key];
  87. 87:         else {
  88. 88:             $this->rebuild();
  89. 89:         }
  90. 90:  
  91. 91:         if (isset($this->list[strtolower(__CLASS__)]&& class_exists('NetteLoader'FALSE)) {
  92. 92:             NetteLoader::getInstance()->unregister();
  93. 93:         }
  94. 94:  
  95. 95:         parent::register();
  96. 96:     }
  97. 97:  
  98. 98:  
  99. 99:  
  100. 100:     /**
  101. 101:      * Handles autoloading of classes or interfaces.
  102. 102:      * @param  string 
  103. 103:      * @return void 
  104. 104:      */
  105. 105:     public function tryLoad($type)
  106. 106:     {
  107. 107:         $type strtolower($type);
  108. 108:         
  109. 109:         if (isset($this->list[$type])) {
  110. 110:             if ($this->list[$type!== FALSE{
  111. 111:                 LimitedScope::load($this->list[$type]);
  112. 112:                 self::$count++;
  113. 113:             }
  114. 114:  
  115. 115:         else {
  116. 116:             $this->list[$typeFALSE;
  117. 117:  
  118. 118:             if ($this->autoRebuild === NULL{
  119. 119:                 $this->autoRebuild = !$this->isProduction();
  120. 120:             }
  121. 121:  
  122. 122:             if ($this->autoRebuild{
  123. 123:                 $this->rebuild(FALSE);
  124. 124:             }
  125. 125:  
  126. 126:             if ($this->list[$type!== FALSE{
  127. 127:                 LimitedScope::load($this->list[$type]);
  128. 128:                 self::$count++;
  129. 129:             }
  130. 130:         }
  131. 131:     }
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Rebuilds class list cache.
  137. 137:      * @param  bool 
  138. 138:      * @return void 
  139. 139:      */
  140. 140:     public function rebuild($force TRUE)
  141. 141:     {
  142. 142:         $cache $this->getCache();
  143. 143:         $key $this->getKey();
  144. 144:         $this->acceptMask self::wildcards2re($this->acceptFiles);
  145. 145:         $this->ignoreMask self::wildcards2re($this->ignoreDirs);
  146. 146:         $this->timestamps $cache[$key 'ts'];
  147. 147:  
  148. 148:         if ($force || !$this->rebuilded{
  149. 149:             foreach (array_unique($this->scanDirsas $dir{
  150. 150:                 $this->scanDirectory($dir);
  151. 151:             }
  152. 152:         }
  153. 153:  
  154. 154:         $this->rebuilded TRUE;
  155. 155:         $cache[$key$this->list;
  156. 156:         $cache[$key 'ts'$this->timestamps;
  157. 157:         $this->timestamps NULL;
  158. 158:     }
  159. 159:  
  160. 160:  
  161. 161:  
  162. 162:     /**
  163. 163:      * Add directory (or directories) to list.
  164. 164:      * @param  string|array
  165. 165:      * @return void 
  166. 166:      * @throws DirectoryNotFoundException if path is not found
  167. 167:      */
  168. 168:     public function addDirectory($path)
  169. 169:     {
  170. 170:         foreach ((array) $path as $val{
  171. 171:             $real realpath($val);
  172. 172:             if ($real === FALSE{
  173. 173:                 throw new DirectoryNotFoundException("Directory '$val' not found.");
  174. 174:             }
  175. 175:             $this->scanDirs[$real;
  176. 176:         }
  177. 177:     }
  178. 178:  
  179. 179:  
  180. 180:  
  181. 181:     /**
  182. 182:      * Add class and file name to the list.
  183. 183:      * @param  string 
  184. 184:      * @param  string 
  185. 185:      * @return void 
  186. 186:      */
  187. 187:     public function addClass($class$file)
  188. 188:     {
  189. 189:         $class strtolower($class);
  190. 190:         if (!empty($this->list[$class]&& $this->list[$class!== $file{
  191. 191:             spl_autoload_call($class)// hack: enables exceptions
  192. 192:             throw new InvalidStateException("Ambiguous class '$class' resolution; defined in $file and in $this->list[$class".");
  193. 193:         }
  194. 194:         $this->list[$class$file;
  195. 195:     }
  196. 196:  
  197. 197:  
  198. 198:  
  199. 199:     /**
  200. 200:      * Scan a directory for PHP files, subdirectories and 'netterobots.txt' file.
  201. 201:      * @param  string 
  202. 202:      * @return void 
  203. 203:      */
  204. 204:     private function scanDirectory($dir)
  205. 205:     {
  206. 206:         $iterator dir($dir);
  207. 207:         if (!$iteratorreturn;
  208. 208:  
  209. 209:         $disallow array();
  210. 210:         if (is_file($dir '/netterobots.txt')) {
  211. 211:             foreach (file($dir '/netterobots.txt'as $s{
  212. 212:                 if (preg_match('#^disallow\\s*:\\s*(\\S+)#i'$s$m)) {
  213. 213:                     $disallow[trim($m[1]'/')TRUE;
  214. 214:                 }
  215. 215:             }
  216. 216:             if (isset($disallow[''])) return;
  217. 217:         }
  218. 218:  
  219. 219:         while (FALSE !== ($entry $iterator->read())) {
  220. 220:             if ($entry == '.' || $entry == '..' || isset($disallow[$entry])) continue;
  221. 221:  
  222. 222:             $path $dir DIRECTORY_SEPARATOR $entry;
  223. 223:  
  224. 224:             // process subdirectories
  225. 225:             if (is_dir($path)) {
  226. 226:                 // check ignore mask
  227. 227:                 if (!preg_match($this->ignoreMask$entry)) {
  228. 228:                     $this->scanDirectory($path);
  229. 229:                 }
  230. 230:                 continue;
  231. 231:             }
  232. 232:  
  233. 233:             if (is_file($path&& preg_match($this->acceptMask$entry)) {
  234. 234:                 $time filemtime($path);
  235. 235:                 if (!isset($this->timestamps[$path]|| $this->timestamps[$path!== $time{
  236. 236:                     $this->timestamps[$path$time;
  237. 237:                     $this->scanScript($path);
  238. 238:                 }
  239. 239:             }
  240. 240:         }
  241. 241:  
  242. 242:         $iterator->close();
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Analyse PHP file.
  249. 249:      * @param  string 
  250. 250:      * @return void 
  251. 251:      */
  252. 252:     private function scanScript($file)
  253. 253:     {
  254. 254:         if (!defined('T_NAMESPACE')) {
  255. 255:             define('T_NAMESPACE'-1);
  256. 256:             define('T_NS_SEPARATOR'-1);
  257. 257:         }
  258. 258:  
  259. 259:         $expected FALSE;
  260. 260:         $namespace '';
  261. 261:         $level 0;
  262. 262:         $s file_get_contents($file);
  263. 263:  
  264. 264:         if (preg_match('#//nette'.'loader=(\S*)#'$s$matches)) {
  265. 265:             foreach (explode(','$matches[1]as $name{
  266. 266:                 $this->addClass($name$file);
  267. 267:             }
  268. 268:             return;
  269. 269:         }
  270. 270:  
  271. 271:         foreach (token_get_all($sas $token)
  272. 272:         {
  273. 273:             if (is_array($token)) {
  274. 274:                 switch ($token[0]{
  275. 275:                 case T_NAMESPACE:
  276. 276:                 case T_CLASS:
  277. 277:                 case T_INTERFACE:
  278. 278:                     $expected $token[0];
  279. 279:                     $name '';
  280. 280:                     continue 2;
  281. 281:  
  282. 282:                 case T_COMMENT:
  283. 283:                 case T_DOC_COMMENT:
  284. 284:                 case T_WHITESPACE:
  285. 285:                     continue 2;
  286. 286:  
  287. 287:                 case T_NS_SEPARATOR:
  288. 288:                 case T_STRING:
  289. 289:                     if ($expected{
  290. 290:                         $name .= $token[1];
  291. 291:                     }
  292. 292:                     continue 2;
  293. 293:                 }
  294. 294:             }
  295. 295:  
  296. 296:             if ($expected{
  297. 297:                 if ($expected === T_NAMESPACE{
  298. 298:                     $namespace $name '\\';
  299. 299:                 elseif ($level === 0{
  300. 300:                     $this->addClass($namespace $name$file);
  301. 301:                 }
  302. 302:                 $expected FALSE;
  303. 303:             }
  304. 304:  
  305. 305:             if (is_array($token)) {
  306. 306:                 if ($token[0=== T_CURLY_OPEN || $token[0=== T_DOLLAR_OPEN_CURLY_BRACES{
  307. 307:                     $level++;
  308. 308:                 }
  309. 309:             elseif ($token === '{'{
  310. 310:                 $level++;
  311. 311:             elseif ($token === '}'{
  312. 312:                 $level--;
  313. 313:             }
  314. 314:         }
  315. 315:     }
  316. 316:  
  317. 317:  
  318. 318:  
  319. 319:     /**
  320. 320:      * Converts comma separated wildcards to regular expression.
  321. 321:      * @param  string 
  322. 322:      * @return string 
  323. 323:      */
  324. 324:     private static function wildcards2re($wildcards)
  325. 325:     {
  326. 326:         $mask array();
  327. 327:         foreach (explode(','$wildcardsas $wildcard{
  328. 328:             $wildcard trim($wildcard);
  329. 329:             $wildcard addcslashes($wildcard'.\\+[^]$(){}=!><|:#');
  330. 330:             $wildcard strtr($wildcardarray('*' => '.*''?' => '.'));
  331. 331:             $mask[$wildcard;
  332. 332:         }
  333. 333:         return '#^(' implode('|'$mask')$#i';
  334. 334:     }
  335. 335:  
  336. 336:  
  337. 337:  
  338. 338:     /********************* backend ****************d*g**/
  339. 339:  
  340. 340:  
  341. 341:  
  342. 342:     /**
  343. 343:      * @return Cache 
  344. 344:      */
  345. 345:     protected function getCache()
  346. 346:     {
  347. 347:         return Environment::getCache('Nette.RobotLoader');
  348. 348:     }
  349. 349:  
  350. 350:  
  351. 351:  
  352. 352:     /**
  353. 353:      * @return string 
  354. 354:      */
  355. 355:     protected function getKey()
  356. 356:     {
  357. 357:         return md5("$this->ignoreDirs|$this->acceptFiles|implode('|'$this->scanDirs));
  358. 358:     }
  359. 359:  
  360. 360:  
  361. 361:  
  362. 362:     /**
  363. 363:      * @return bool 
  364. 364:      */
  365. 365:     protected function isProduction()
  366. 366:     {
  367. 367:         return Environment::isProduction();
  368. 368:     }
  369. 369: