You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

  1. <?php
  2. /**
  3. * Generates array of classes for autoload
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage util
  9. * @since 2010-02-24
  10. * @version SVN: $Id$
  11. * @filesource $URL$
  12. */
  13. class AutoloadBuilder
  14. {
  15. protected $autoload;
  16. protected $dirs;
  17. protected $handler;
  18. protected $regex = '/(class|interface) ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
  19. public function __construct($autoload, $dirs = array())
  20. {
  21. $this->autoload = $autoload;
  22. $this->dirs = $dirs;
  23. }
  24. public function build()
  25. {
  26. $this->openAutoload();
  27. echo "parsing started...\n";
  28. // for dublicates check
  29. $classes = array();
  30. foreach ($this->dirs as $dir) {
  31. $iterator = new RecursiveIteratorIterator(
  32. new RecursiveDirectoryIterator($dir),
  33. RecursiveIteratorIterator::LEAVES_ONLY
  34. );
  35. foreach ($iterator as $file) {
  36. // skip non php files
  37. $e = explode('.', $file->getFileName());
  38. if ((end($e) !== 'php') || strstr((string)$file, 'test')) {
  39. continue;
  40. }
  41. $content = file_get_contents($file->getRealPath());
  42. $matches = array();
  43. $relative_path = substr($file->getRealPath(), strlen(PATH));
  44. if (preg_match_all($this->regex, $content, $matches, PREG_SET_ORDER)) {
  45. foreach ($matches as $match) {
  46. echo "found {$match[0]}...";
  47. if (array_key_exists($match[2], $classes)) {
  48. echo " FAULT\n{$match[0]} also found in ".
  49. $file->getRealPath(). ", skipped.\n";
  50. continue;
  51. }
  52. echo " OK\n";
  53. $string = "'{$match[2]}'=>'" . $relative_path . "',\n";
  54. $this->write($string);
  55. $classes[$match[2]] = $file->getRealPath();
  56. }
  57. }
  58. }
  59. }
  60. echo "done.\n";
  61. $this->closeAutoload();
  62. }
  63. protected function openAutoload()
  64. {
  65. $this->write("<?php\n// This file is autogenerated by \n// " . __FILE__ . " script.\nreturn array(\n");
  66. }
  67. protected function closeAutoload()
  68. {
  69. if ($this->write(");\n")) {
  70. fclose($this->handler);
  71. }
  72. }
  73. protected function write($string)
  74. {
  75. if (! $this->handler) {
  76. if (! $this->handler = fopen($this->autoload, 'w')) {
  77. echo "{$this->autoload} is not writable\n";
  78. die;
  79. }
  80. }
  81. return (bool) fwrite($this->handler, $string);
  82. }
  83. }