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