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
|
14 |
|
private $processor; |
28
|
|
|
|
29
|
14 |
|
public function __construct(FileLocatorInterface $fileLocator, RestRouteProcessor $processor) |
30
|
14 |
|
{ |
31
|
|
|
$this->fileLocator = $fileLocator; |
32
|
|
|
$this->processor = $processor; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
1 |
|
*/ |
38
|
|
|
public function load($resource, $type = null) |
39
|
|
|
{ |
40
|
|
View Code Duplication |
if (isset($resource[0]) && '@' === $resource[0]) { |
|
|
|
|
41
|
1 |
|
$resource = $this->fileLocator->locate($resource); |
42
|
|
|
} |
43
|
1 |
|
|
44
|
|
|
if (!is_dir($resource)) { |
45
|
1 |
|
throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource)); |
46
|
1 |
|
} |
47
|
1 |
|
|
48
|
1 |
|
$collection = new RouteCollection(); |
49
|
|
|
|
50
|
1 |
|
$finder = new Finder(); |
51
|
|
|
|
52
|
|
|
foreach ($finder->in($resource)->name('*.php')->files() as $file) { |
53
|
|
|
$imported = $this->processor->importResource($this, ClassUtils::findClassInFile($file), array(), null, null, 'rest'); |
54
|
|
|
$collection->addCollection($imported); |
55
|
|
|
} |
56
|
5 |
|
|
57
|
|
|
return $collection; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function supports($resource, $type = null) |
64
|
|
|
{ |
65
|
|
|
if ('rest' !== $type || !is_string($resource)) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
if (isset($resource[0]) && '@' === $resource[0]) { |
|
|
|
|
70
|
|
|
$resource = $this->fileLocator->locate($resource); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return is_dir($resource); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.