WorkerDirectoryLoader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * @author Marc Morera <yuhu@mmoreram.com>
  6. * @since 2013
  7. */
  8. namespace Mmoreram\GearmanBundle\Module;
  9. use Symfony\Component\Config\FileLocator;
  10. use Symfony\Component\Config\Loader\FileLoader;
  11. use Symfony\Component\Config\Resource\DirectoryResource;
  12. /**
  13. * Worker directory loader class
  14. */
  15. class WorkerDirectoryLoader extends FileLoader
  16. {
  17. /**
  18. * Load files into path with .php extension.
  19. * This algorythm acts recursively and only return leaves.
  20. *
  21. * @param string $path Path to find
  22. * @param string $type Type
  23. *
  24. * @return Array
  25. */
  26. public function load($path, $type = null)
  27. {
  28. $dir = $this->locator->locate($path);
  29. $files = array();
  30. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  31. if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
  32. continue;
  33. }
  34. $class = $this->findClass($file);
  35. if ($class) {
  36. $files[] = array(
  37. 'path' => $file,
  38. 'class' => $class
  39. );
  40. }
  41. }
  42. return $files;
  43. }
  44. /**
  45. * Returns true if this class supports the given resource.
  46. *
  47. * @param mixed $resource A resource
  48. * @param string $type The resource type
  49. *
  50. * @return Boolean True if this class supports the given resource, false otherwise
  51. */
  52. public function supports($resource, $type = null)
  53. {
  54. return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
  55. }
  56. /**
  57. * Returns the full class name for the first class in the file.
  58. *
  59. * @param string $file A PHP file path
  60. *
  61. * @return string|false Full class name if found, false otherwise
  62. */
  63. protected function findClass($file)
  64. {
  65. $class = false;
  66. $namespace = false;
  67. $tokens = token_get_all(file_get_contents($file));
  68. for ($i = 0, $count = count($tokens); $i < $count; $i++) {
  69. $token = $tokens[$i];
  70. if (!is_array($token)) {
  71. continue;
  72. }
  73. if (true === $class && T_STRING === $token[0]) {
  74. return $namespace.'\\'.$token[1];
  75. }
  76. if (true === $namespace && T_STRING === $token[0]) {
  77. $namespace = '';
  78. do {
  79. $namespace .= $token[1];
  80. $token = $tokens[++$i];
  81. } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
  82. }
  83. if (T_CLASS === $token[0]) {
  84. $class = true;
  85. }
  86. if (T_NAMESPACE === $token[0]) {
  87. $namespace = true;
  88. }
  89. }
  90. return false;
  91. }
  92. }