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