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 $timestamps;
  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]);
  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:                 $this->rebuild(FALSE);
  110. 110:             }
  111. 111:  
  112. 112:             if ($this->list[$type!== FALSE{
  113. 113:                 LimitedScope::load($this->list[$type]);
  114. 114:                 self::$count++;
  115. 115:             }
  116. 116:         }
  117. 117:     }
  118. 118:  
  119. 119:  
  120. 120:  
  121. 121:     /**
  122. 122:      * Rebuilds class list cache.
  123. 123:      * @param  bool 
  124. 124:      * @return void 
  125. 125:      */
  126. 126:     public function rebuild($force TRUE)
  127. 127:     {
  128. 128:         $cache $this->getCache();
  129. 129:         $key $this->getKey();
  130. 130:         $this->acceptMask self::wildcards2re($this->acceptFiles);
  131. 131:         $this->ignoreMask self::wildcards2re($this->ignoreDirs);
  132. 132:         $this->timestamps $cache[$key 'ts'];
  133. 133:  
  134. 134:         if ($force || !$this->rebuilded{
  135. 135:             foreach (array_unique($this->scanDirsas $dir{
  136. 136:                 $this->scanDirectory($dir);
  137. 137:             }
  138. 138:         }
  139. 139:  
  140. 140:         $this->rebuilded TRUE;
  141. 141:         $cache[$key$this->list;
  142. 142:         $cache[$key 'ts'$this->timestamps;
  143. 143:         $this->timestamps NULL;
  144. 144:     }
  145. 145:  
  146. 146:  
  147. 147:  
  148. 148:     /**
  149. 149:      * Add directory (or directories) to list.
  150. 150:      * @param  string|array
  151. 151:      * @return void 
  152. 152:      * @throws DirectoryNotFoundException if path is not found
  153. 153:      */
  154. 154:     public function addDirectory($path)
  155. 155:     {
  156. 156:         foreach ((array) $path as $val{
  157. 157:             $real realpath($val);
  158. 158:             if ($real === FALSE{
  159. 159:                 throw new DirectoryNotFoundException("Directory '$val' not found.");
  160. 160:             }
  161. 161:             $this->scanDirs[$real;
  162. 162:         }
  163. 163:     }
  164. 164:  
  165. 165:  
  166. 166:  
  167. 167:     /**
  168. 168:      * Add class and file name to the list.
  169. 169:      * @param  string 
  170. 170:      * @param  string 
  171. 171:      * @return void 
  172. 172:      */
  173. 173:     public function addClass($class$file)
  174. 174:     {
  175. 175:         $class strtolower($class);
  176. 176:         if (!empty($this->list[$class]&& $this->list[$class!== $file{
  177. 177:             spl_autoload_call($class)// hack: enables exceptions
  178. 178:             throw new InvalidStateException("Ambiguous class '$class' resolution; defined in $file and in $this->list[$class".");
  179. 179:         }
  180. 180:         $this->list[$class$file;
  181. 181:     }
  182. 182:  
  183. 183:  
  184. 184:  
  185. 185:     /**
  186. 186:      * Scan a directory for PHP files, subdirectories and 'netterobots.txt' file.
  187. 187:      * @param  string 
  188. 188:      * @return void 
  189. 189:      */
  190. 190:     private function scanDirectory($dir)
  191. 191:     {
  192. 192:         $iterator dir($dir);
  193. 193:         if (!$iteratorreturn;
  194. 194:  
  195. 195:         $disallow array();
  196. 196:         if (is_file($dir '/netterobots.txt')) {
  197. 197:             foreach (file($dir '/netterobots.txt'as $s{
  198. 198:                 if (preg_match('#^disallow\\s*:\\s*(\\S+)#i'$s$m)) {
  199. 199:                     $disallow[trim($m[1]'/')TRUE;
  200. 200:                 }
  201. 201:             }
  202. 202:             if (isset($disallow[''])) return;
  203. 203:         }
  204. 204:  
  205. 205:         while (FALSE !== ($entry $iterator->read())) {
  206. 206:             if ($entry == '.' || $entry == '..' || isset($disallow[$entry])) continue;
  207. 207:  
  208. 208:             $path $dir DIRECTORY_SEPARATOR $entry;
  209. 209:  
  210. 210:             // process subdirectories
  211. 211:             if (is_dir($path)) {
  212. 212:                 // check ignore mask
  213. 213:                 if (!preg_match($this->ignoreMask$entry)) {
  214. 214:                     $this->scanDirectory($path);
  215. 215:                 }
  216. 216:                 continue;
  217. 217:             }
  218. 218:  
  219. 219:             if (is_file($path&& preg_match($this->acceptMask$entry)) {
  220. 220:                 $time filemtime($path);
  221. 221:                 if (!isset($this->timestamps[$path]|| $this->timestamps[$path!== $time{
  222. 222:                     $this->timestamps[$path$time;
  223. 223:                     $this->scanScript($path);
  224. 224:                 }
  225. 225:             }
  226. 226:         }
  227. 227:  
  228. 228:         $iterator->close();
  229. 229:     }
  230. 230:  
  231. 231:  
  232. 232:  
  233. 233:     /**
  234. 234:      * Analyse PHP file.
  235. 235:      * @param  string 
  236. 236:      * @return void 
  237. 237:      */
  238. 238:     private function scanScript($file)
  239. 239:     {
  240. 240:         if (!defined('T_NAMESPACE')) {
  241. 241:             define('T_NAMESPACE'-1);
  242. 242:             define('T_NS_SEPARATOR'-1);
  243. 243:         }
  244. 244:  
  245. 245:         $expected FALSE;
  246. 246:         $namespace '';
  247. 247:         $level 0;
  248. 248:         $s file_get_contents($file);
  249. 249:  
  250. 250:         if (preg_match('#//nette'.'loader=(\S*)#'$s$matches)) {
  251. 251:             foreach (explode(','$matches[1]as $name{
  252. 252:                 $this->addClass($name$file);
  253. 253:             }
  254. 254:             return;
  255. 255:         }
  256. 256:  
  257. 257:         foreach (token_get_all($sas $token)
  258. 258:         {
  259. 259:             if (is_array($token)) {
  260. 260:                 switch ($token[0]{
  261. 261:                 case T_COMMENT:
  262. 262:                 case T_DOC_COMMENT:
  263. 263:                 case T_WHITESPACE:
  264. 264:                     continue 2;
  265. 265:  
  266. 266:                 case T_NS_SEPARATOR:
  267. 267:                 case T_STRING:
  268. 268:                     if ($expected{
  269. 269:                         $name .= $token[1];
  270. 270:                     }
  271. 271:                     continue 2;
  272. 272:  
  273. 273:                 case T_NAMESPACE:
  274. 274:                 case T_CLASS:
  275. 275:                 case T_INTERFACE:
  276. 276:                     $expected $token[0];
  277. 277:                     $name '';
  278. 278:                     continue 2;
  279. 279:                 case T_CURLY_OPEN:
  280. 280:                 case T_DOLLAR_OPEN_CURLY_BRACES:
  281. 281:                     $level++;
  282. 282:                 }
  283. 283:             }
  284. 284:  
  285. 285:             if ($expected{
  286. 286:                 switch ($expected{
  287. 287:                 case T_CLASS:
  288. 288:                 case T_INTERFACE:
  289. 289:                     if ($level === 0{
  290. 290:                         $this->addClass($namespace $name$file);
  291. 291:                     }
  292. 292:                     break;
  293. 293:  
  294. 294:                 case T_NAMESPACE:
  295. 295:                     $namespace $name '\\';
  296. 296:                 }
  297. 297:  
  298. 298:                 $expected NULL;
  299. 299:             }
  300. 300:  
  301. 301:             if ($token === '{'{
  302. 302:                 $level++;
  303. 303:             elseif ($token === '}'{
  304. 304:                 $level--;
  305. 305:             }
  306. 306:         }
  307. 307:     }
  308. 308:  
  309. 309:  
  310. 310:  
  311. 311:     /**
  312. 312:      * Converts comma separated wildcards to regular expression.
  313. 313:      * @param  string 
  314. 314:      * @return string 
  315. 315:      */
  316. 316:     private static function wildcards2re($wildcards)
  317. 317:     {
  318. 318:         $mask array();
  319. 319:         foreach (explode(','$wildcardsas $wildcard{
  320. 320:             $wildcard trim($wildcard);
  321. 321:             $wildcard addcslashes($wildcard'.\\+[^]$(){}=!><|:#');
  322. 322:             $wildcard strtr($wildcardarray('*' => '.*''?' => '.'));
  323. 323:             $mask[$wildcard;
  324. 324:         }
  325. 325:         return '#^(' implode('|'$mask')$#i';
  326. 326:     }
  327. 327:  
  328. 328:  
  329. 329:  
  330. 330:     /********************* backend ****************d*g**/
  331. 331:  
  332. 332:  
  333. 333:  
  334. 334:     /**
  335. 335:      * @return Cache 
  336. 336:      */
  337. 337:     protected function getCache()
  338. 338:     {
  339. 339:         return Environment::getCache('Nette.RobotLoader');
  340. 340:     }
  341. 341:  
  342. 342:  
  343. 343:  
  344. 344:     /**
  345. 345:      * @return string 
  346. 346:      */
  347. 347:     protected function getKey()
  348. 348:     {
  349. 349:         return md5("$this->ignoreDirs|$this->acceptFiles|implode('|'$this->scanDirs));
  350. 350:     }
  351. 351:  
  352. 352:  
  353. 353:  
  354. 354:     /**
  355. 355:      * @return bool 
  356. 356:      */
  357. 357:     protected function isProduction()
  358. 358:     {
  359. 359:         return Environment::isProduction();
  360. 360:     }
  361. 361: