| | 267 | ?><?php |
|---|
| | 268 | /** |
|---|
| | 269 | * Class registry for mapping of classes to star files. |
|---|
| | 270 | * |
|---|
| | 271 | * @author Frank Kleine <mikey@stubbles.net> |
|---|
| | 272 | * @author Stephan Schmidt <schst@stubbles.net> |
|---|
| | 273 | * @package star |
|---|
| | 274 | */ |
|---|
| | 275 | /** |
|---|
| | 276 | * Class registry for mapping of classes to star files. |
|---|
| | 277 | * |
|---|
| | 278 | * @package star |
|---|
| | 279 | */ |
|---|
| | 280 | class StarClassRegistry |
|---|
| | 281 | { |
|---|
| | 282 | /** |
|---|
| | 283 | * switch whether init has been done or not |
|---|
| | 284 | * |
|---|
| | 285 | * @var bool |
|---|
| | 286 | */ |
|---|
| | 287 | protected static $initDone = false; |
|---|
| | 288 | /** |
|---|
| | 289 | * path to star files |
|---|
| | 290 | * |
|---|
| | 291 | * @var string |
|---|
| | 292 | */ |
|---|
| | 293 | protected static $libPathes = array(); |
|---|
| | 294 | /** |
|---|
| | 295 | * list of classes and the file where they are in |
|---|
| | 296 | * |
|---|
| | 297 | * @var array<string,string> |
|---|
| | 298 | */ |
|---|
| | 299 | protected static $classes = array(); |
|---|
| | 300 | /** |
|---|
| | 301 | * list of files and the classes they contain |
|---|
| | 302 | * |
|---|
| | 303 | * @var array<string,array<string>> |
|---|
| | 304 | */ |
|---|
| | 305 | protected static $files = array(); |
|---|
| | 306 | |
|---|
| | 307 | /** |
|---|
| | 308 | * set the path to the star files |
|---|
| | 309 | * |
|---|
| | 310 | * @param string $libPath path to lib files |
|---|
| | 311 | * @param bool $recursive optional recurse into sub directories of lib path |
|---|
| | 312 | */ |
|---|
| | 313 | public static function addLibPath($libPath, $recursive = true) |
|---|
| | 314 | { |
|---|
| | 315 | self::$libPathes[$libPath] = $recursive; |
|---|
| | 316 | self::$initDone = false; |
|---|
| | 317 | } |
|---|
| | 318 | |
|---|
| | 319 | /** |
|---|
| | 320 | * returns the file where the given classes is stored in |
|---|
| | 321 | * |
|---|
| | 322 | * @param string $fqClassName the full qualified class name |
|---|
| | 323 | * @return string |
|---|
| | 324 | */ |
|---|
| | 325 | public static function getFileForClass($fqClassName) |
|---|
| | 326 | { |
|---|
| | 327 | if (false === self::$initDone) { |
|---|
| | 328 | self::init(); |
|---|
| | 329 | } |
|---|
| | 330 | |
|---|
| | 331 | if (isset(self::$classes[$fqClassName]) === true) { |
|---|
| | 332 | return self::$classes[$fqClassName]; |
|---|
| | 333 | } |
|---|
| | 334 | |
|---|
| | 335 | return null; |
|---|
| | 336 | } |
|---|
| | 337 | |
|---|
| | 338 | /** |
|---|
| | 339 | * returns the uri for the given class |
|---|
| | 340 | * |
|---|
| | 341 | * @param string $fqClassName the full qualified class name |
|---|
| | 342 | * @return string |
|---|
| | 343 | */ |
|---|
| | 344 | public static function getUriForClass($fqClassName) |
|---|
| | 345 | { |
|---|
| | 346 | if (false === self::$initDone) { |
|---|
| | 347 | self::init(); |
|---|
| | 348 | } |
|---|
| | 349 | |
|---|
| | 350 | if (isset(self::$classes[$fqClassName]) === true) { |
|---|
| | 351 | return 'star://' . self::$classes[$fqClassName] . '?' . $fqClassName; |
|---|
| | 352 | } |
|---|
| | 353 | |
|---|
| | 354 | return null; |
|---|
| | 355 | } |
|---|
| | 356 | |
|---|
| | 357 | /** |
|---|
| | 358 | * returns all uris for a given resource |
|---|
| | 359 | * |
|---|
| | 360 | * @param string $fileName file name of resource |
|---|
| | 361 | * @return array |
|---|
| | 362 | */ |
|---|
| | 363 | public static function getUrisForResource($resource) |
|---|
| | 364 | { |
|---|
| | 365 | if (false === self::$initDone) { |
|---|
| | 366 | self::init(); |
|---|
| | 367 | } |
|---|
| | 368 | |
|---|
| | 369 | $uris = array(); |
|---|
| | 370 | foreach (self::$files as $file => $contents) { |
|---|
| | 371 | foreach ($contents as $content) { |
|---|
| | 372 | if ($content === $resource) { |
|---|
| | 373 | $uris[] = 'star://' . $file . '?' . $resource; |
|---|
| | 374 | continue 2; |
|---|
| | 375 | } |
|---|
| | 376 | } |
|---|
| | 377 | } |
|---|
| | 378 | |
|---|
| | 379 | return $uris; |
|---|
| | 380 | } |
|---|
| | 381 | |
|---|
| | 382 | /** |
|---|
| | 383 | * returns a list of all classes within given file |
|---|
| | 384 | * |
|---|
| | 385 | * @param string $file name of file |
|---|
| | 386 | * @return array |
|---|
| | 387 | */ |
|---|
| | 388 | public static function getClassNamesFromFile($file) |
|---|
| | 389 | { |
|---|
| | 390 | if (false === self::$initDone) { |
|---|
| | 391 | self::init(); |
|---|
| | 392 | } |
|---|
| | 393 | |
|---|
| | 394 | if (isset(self::$files[$file]) === true) { |
|---|
| | 395 | return self::$files[$file]; |
|---|
| | 396 | } |
|---|
| | 397 | |
|---|
| | 398 | return array(); |
|---|
| | 399 | } |
|---|
| | 400 | |
|---|
| | 401 | /** |
|---|
| | 402 | * returns a list of all classes |
|---|
| | 403 | * |
|---|
| | 404 | * @return string |
|---|
| | 405 | */ |
|---|
| | 406 | public static function getClasses() |
|---|
| | 407 | { |
|---|
| | 408 | return array_keys(self::$classes); |
|---|
| | 409 | } |
|---|
| | 410 | |
|---|
| | 411 | /** |
|---|
| | 412 | * initialize the class registry |
|---|
| | 413 | */ |
|---|
| | 414 | protected static function init() |
|---|
| | 415 | { |
|---|
| | 416 | if (true === self::$initDone) { |
|---|
| | 417 | return; |
|---|
| | 418 | } |
|---|
| | 419 | |
|---|
| | 420 | if (count(self::$libPathes) == 0) { |
|---|
| | 421 | self::$libPathes[dirname(__FILE__)] = true; |
|---|
| | 422 | } |
|---|
| | 423 | |
|---|
| | 424 | foreach (self::$libPathes as $libPath => $recursive) { |
|---|
| | 425 | if (file_exists($libPath . '/.cache') === true) { |
|---|
| | 426 | $cache = unserialize(file_get_contents($libPath . '/.cache')); |
|---|
| | 427 | self::$files = array_merge(self::$files, $cache['files']); |
|---|
| | 428 | self::$classes = array_merge(self::$classes, $cache['classes']); |
|---|
| | 429 | self::$initDone = true; |
|---|
| | 430 | continue; |
|---|
| | 431 | } |
|---|
| | 432 | |
|---|
| | 433 | if (true === $recursive) { |
|---|
| | 434 | $dirIt = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($libPath)); |
|---|
| | 435 | } else { |
|---|
| | 436 | $dirIt = new DirectoryIterator($libPath); |
|---|
| | 437 | } |
|---|
| | 438 | |
|---|
| | 439 | $cache['files'] = array(); |
|---|
| | 440 | $cache['classes'] = array(); |
|---|
| | 441 | foreach ($dirIt as $file) { |
|---|
| | 442 | if ($file->isFile() === false || substr($file->getPathname(), -14) === 'starReader.php' || (substr($file->getPathname(), -5) !== '.star' && substr($file->getPathname(), -4) !== '.php')) { |
|---|
| | 443 | continue; |
|---|
| | 444 | } |
|---|
| | 445 | |
|---|
| | 446 | $archiveData = StarStreamWrapper::acquire($file->getPathname()); |
|---|
| | 447 | if (empty($archiveData) == true) { |
|---|
| | 448 | continue; |
|---|
| | 449 | } |
|---|
| | 450 | |
|---|
| | 451 | $classes = array_keys($archiveData['index']); |
|---|
| | 452 | self::$files[$file->getPathname()] = $classes; |
|---|
| | 453 | $cache['files'][$file->getPathname()] = $classes; |
|---|
| | 454 | |
|---|
| | 455 | foreach (array_keys($archiveData['index']) as $fqClassName) { |
|---|
| | 456 | self::$classes[$fqClassName] = $file->getPathname(); |
|---|
| | 457 | $cache['classes'][$fqClassName] = $file->getPathname(); |
|---|
| | 458 | } |
|---|
| | 459 | } |
|---|
| | 460 | |
|---|
| | 461 | $cacheFile = $libPath . '/.cache'; |
|---|
| | 462 | if (is_writable($libPath) === false && is_writable($cacheFile) === false) { |
|---|
| | 463 | throw new StarException("Unable to write starRegistry cache file to {$cacheFile}."); |
|---|
| | 464 | } |
|---|
| | 465 | |
|---|
| | 466 | file_put_contents($cacheFile, serialize($cache)); |
|---|
| | 467 | self::$initDone = true; |
|---|
| | 468 | } |
|---|
| | 469 | } |
|---|
| | 470 | } |
|---|