LimeRegistration.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimeRegistration
  12. {
  13. protected
  14. $files = array(),
  15. $extension = '.php',
  16. $baseDir = '';
  17. public function setBaseDir($baseDir)
  18. {
  19. $this->baseDir = $baseDir;
  20. }
  21. public function setExtension($extension)
  22. {
  23. $this->extension = $extension;
  24. }
  25. public function getFiles()
  26. {
  27. return $this->files;
  28. }
  29. public function register($filesOrDirectories)
  30. {
  31. foreach ((array) $filesOrDirectories as $fileOrDirectory)
  32. {
  33. if (is_file($fileOrDirectory))
  34. {
  35. $this->files[] = realpath($fileOrDirectory);
  36. }
  37. elseif (is_dir($fileOrDirectory))
  38. {
  39. $this->registerDir($fileOrDirectory);
  40. }
  41. else
  42. {
  43. throw new Exception(sprintf('The file or directory "%s" does not exist.', $fileOrDirectory));
  44. }
  45. }
  46. }
  47. public function registerGlob($glob)
  48. {
  49. if ($dirs = glob($glob))
  50. {
  51. foreach ($dirs as $file)
  52. {
  53. $this->files[] = realpath($file);
  54. }
  55. }
  56. }
  57. public function registerDir($directory)
  58. {
  59. if (!is_dir($directory))
  60. {
  61. throw new Exception(sprintf('The directory "%s" does not exist.', $directory));
  62. }
  63. $files = array();
  64. $currentDir = opendir($directory);
  65. while ($entry = readdir($currentDir))
  66. {
  67. if ($entry == '.' || $entry == '..') continue;
  68. if (is_dir($entry))
  69. {
  70. $this->registerDir($entry);
  71. }
  72. elseif (preg_match('#'.$this->extension.'$#', $entry))
  73. {
  74. $files[] = realpath($directory.DIRECTORY_SEPARATOR.$entry);
  75. }
  76. }
  77. $this->files = array_merge($this->files, $files);
  78. }
  79. protected function getRelativeFile($file)
  80. {
  81. return str_replace(DIRECTORY_SEPARATOR, '/', str_replace(array(realpath($this->baseDir).DIRECTORY_SEPARATOR, $this->extension), '', $file));
  82. }
  83. }