1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Routing\Loader; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
15
|
|
|
use Symfony\Component\Config\Loader\Loader; |
16
|
|
|
use Symfony\Component\Finder\Finder; |
17
|
|
|
use Symfony\Component\Routing\RouteCollection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Parse annotated controller classes from all files of a directory. |
21
|
|
|
* |
22
|
|
|
* @author Christian Flothmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class DirectoryRouteLoader extends Loader |
25
|
|
|
{ |
26
|
|
|
private $fileLocator; |
27
|
|
|
private $processor; |
28
|
|
|
|
29
|
17 |
|
public function __construct(FileLocatorInterface $fileLocator, RestRouteProcessor $processor) |
30
|
|
|
{ |
31
|
17 |
|
$this->fileLocator = $fileLocator; |
32
|
17 |
|
$this->processor = $processor; |
33
|
17 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
2 |
|
public function load($resource, $type = null) |
39
|
|
|
{ |
40
|
2 |
|
if (isset($resource[0]) && '@' === $resource[0]) { |
41
|
1 |
|
$resource = $this->fileLocator->locate($resource); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
2 |
|
if (!is_dir($resource)) { |
45
|
|
|
throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource)); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$collection = new RouteCollection(); |
49
|
|
|
|
50
|
2 |
|
$finder = new Finder(); |
51
|
|
|
|
52
|
2 |
|
foreach ($finder->in($resource)->name('*.php')->files() as $file) { |
53
|
2 |
|
if($class = ClassUtils::findClassInFile($file)) { |
54
|
2 |
|
$imported = $this->processor->importResource($this, $class, array(), null, null, 'rest'); |
55
|
2 |
|
$collection->addCollection($imported); |
56
|
2 |
|
} |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
return $collection; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
16 |
|
public function supports($resource, $type = null) |
66
|
|
|
{ |
67
|
16 |
|
if ('rest' !== $type || !is_string($resource)) { |
68
|
1 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
15 |
|
if (isset($resource[0]) && '@' === $resource[0]) { |
72
|
1 |
|
$resource = $this->fileLocator->locate($resource); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
15 |
|
return is_dir($resource); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|