123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace Mmoreramerino\GearmanBundle\Module;
- use Symfony\Component\Config\FileLocator;
- use Symfony\Component\Config\Loader\FileLoader;
- use Symfony\Component\Config\Resource\DirectoryResource;
- /**
- * Worker directory loader class
- *
- * @author Marc Morera <marc@ulabox.com>
- */
- class WorkerDirectoryLoader extends FileLoader
- {
- /**
- * Load files into path with .php extension.
- * This algorythm acts recursively and only return leaves.
- *
- * @param string $path Path to find
- * @param string $type Type
- *
- * @return Array
- */
- public function load($path, $type = null)
- {
- $dir = $this->locator->locate($path);
- $files = array();
- foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
- if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
- continue;
- }
- $class = $this->findClass($file);
- if ($class) {
- $files[] = array(
- 'path' => $file,
- 'class' => $class
- );
- }
- }
- return $files;
- }
- /**
- * Returns true if this class supports the given resource.
- *
- * @param mixed $resource A resource
- * @param string $type The resource type
- *
- * @return Boolean True if this class supports the given resource, false otherwise
- */
- public function supports($resource, $type = null)
- {
- return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
- }
- /**
- * Returns the full class name for the first class in the file.
- *
- * @param string $file A PHP file path
- *
- * @return string|false Full class name if found, false otherwise
- */
- protected function findClass($file)
- {
- $class = false;
- $namespace = false;
- $tokens = token_get_all(file_get_contents($file));
- for ($i = 0, $count = count($tokens); $i < $count; $i++) {
- $token = $tokens[$i];
- if (!is_array($token)) {
- continue;
- }
- if (true === $class && T_STRING === $token[0]) {
- return $namespace.'\\'.$token[1];
- }
- if (true === $namespace && T_STRING === $token[0]) {
- $namespace = '';
- do {
- $namespace .= $token[1];
- $token = $tokens[++$i];
- } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
- }
- if (T_CLASS === $token[0]) {
- $class = true;
- }
- if (T_NAMESPACE === $token[0]) {
- $namespace = true;
- }
- }
- return false;
- }
- }
|