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