Completed
Push — 8.x-1.x ( 411616...2574ac )
by
unknown
20:15
created

RouteEventSubscriber::getRoutePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Drupal\controller_annotations\EventSubscriber;
4
5
use Drupal\Core\Routing\RouteBuildEvent;
6
use Drupal\Core\Routing\RoutingEvents;
7
use Drupal\controller_annotations\Configuration\ConfigurationLoader;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
10
use Symfony\Component\Routing\Route;
11
12
class RouteEventSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var AnnotationDirectoryLoader
16
     */
17
    private $annotationDirectoryLoader;
18
19
    /**
20
     * @var string
21
     */
22
    private $rootPath;
23
24
    /**
25
     * @param AnnotationDirectoryLoader $annotationDirectoryLoader
26
     * @param string $rootPath
27
     */
28 9
    public function __construct(AnnotationDirectoryLoader $annotationDirectoryLoader, $rootPath)
29
    {
30 9
        ConfigurationLoader::load();
31
32 9
        $this->annotationDirectoryLoader = $annotationDirectoryLoader;
33 9
        $this->rootPath = $rootPath;
34 9
    }
35
36
    /**
37
     * @param RouteBuildEvent $event
38
     * @throws \Exception
39
     */
40 9
    public function onRoutes(RouteBuildEvent $event)
41
    {
42
        /**
43
         * @var $route Route
44
         */
45 9
        foreach ($event->getRouteCollection() as $name => $route) {
46 8
            if ($route->hasOption('type')
47 8
                && $route->getOption('type') === 'annotation'
48
            ) {
49 7
                $routeCollection = $this->annotationDirectoryLoader->load($this->rootPath . $this->getRoutePath($route));
50 6
                $routeCollection->addPrefix($route->getPath());
51
52 6
                $event->getRouteCollection()->addCollection($routeCollection);
53 7
                $event->getRouteCollection()->remove($name);
54
            }
55
        }
56 8
    }
57
58
    /**
59
     * @return array
60
     */
61 5
    public static function getSubscribedEvents()
62
    {
63
        return [
64
            RoutingEvents::DYNAMIC => [
65
                ['onRoutes', 0],
66
            ]
67 5
        ];
68
    }
69
70
    /**
71
     * @param Route $route
72
     * @return string
73
     * @throws \Exception
74
     */
75 7
    public function getRoutePath(Route $route)
76
    {
77 7
        if ($route->hasOption('path')) {
78 1
            $path = $route->getOption('path');
79 6
        } elseif ($route->hasOption('module')) {
80 5
            $path = sprintf('/%s/src/Controller', drupal_get_path('module', $route->getOption('module')));
81
        } else {
82 1
            throw new \Exception(
83 1
                'Either the "resource" or "module"  option is required to load from annotations'
84
            );
85
        }
86 6
        return $path;
87
    }
88
}
89