Completed
Push — refonte ( 72d345...9e5238 )
by Arnaud
02:08
created

RoutingLoader::load()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.0008
c 0
b 0
f 0
cc 5
nc 6
nop 2
1
<?php
2
3
namespace LAG\AdminBundle\Routing;
4
5
use Exception;
6
use LAG\AdminBundle\Controller\HomeAction;
7
use LAG\AdminBundle\Factory\ConfigurationFactory;
8
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
9
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
10
use LAG\AdminBundle\Resource\ResourceCollection;
11
use RuntimeException;
12
use Symfony\Component\Config\Loader\LoaderInterface;
13
use Symfony\Component\Config\Loader\LoaderResolverInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\Routing\Route;
16
use Symfony\Component\Routing\RouteCollection;
17
18
/**
19
 * RoutingLoader.
20
 *
21
 * Creates routing for configured entities
22
 */
23
class RoutingLoader implements LoaderInterface
24
{
25
    /**
26
     * @var bool
27
     */
28
    private $loaded = false;
29
30
    /**
31
     * @var ResourceCollection
32
     */
33
    private $resourceCollection;
34
35
    /**
36
     * @var EventDispatcherInterface
37
     */
38
    private $eventDispatcher;
39
40
    /**
41
     * @var ApplicationConfiguration
42
     */
43
    private $applicationConfiguration;
44
45
    /**
46
     * @var ConfigurationFactory
47
     */
48
    private $configurationFactory;
49
50
    /**
51
     * RoutingLoader constructor.
52
     *
53
     * @param ResourceCollection              $resourceCollection
54
     * @param EventDispatcherInterface        $eventDispatcher
55
     * @param ApplicationConfigurationStorage $applicationConfigurationStorage
56
     * @param ConfigurationFactory            $configurationFactory
57
     */
58
    public function __construct(
59
        ResourceCollection $resourceCollection,
60
        EventDispatcherInterface $eventDispatcher,
61
        ApplicationConfigurationStorage $applicationConfigurationStorage,
62
        ConfigurationFactory $configurationFactory
63
    ) {
64
        $this->resourceCollection = $resourceCollection;
65
        $this->eventDispatcher = $eventDispatcher;
66
        $this->applicationConfiguration = $applicationConfigurationStorage->getConfiguration();
67
        $this->configurationFactory = $configurationFactory;
68
    }
69
70
    /**
71
     * @param string $adminName
72
     * @param string $actionName
73
     * @param string $routingPattern
74
     *
75
     * @return string
76
     */
77
    public static function generateRouteName(string $adminName, string $actionName, string $routingPattern)
78
    {
79
        // generate the route name using the configured pattern
80
        $routeName = str_replace(
81
            '{admin}',
82
            strtolower($adminName),
83
            $routingPattern
84
        );
85
        $routeName = str_replace(
86
            '{action}',
87
            $actionName,
88
            $routeName
89
        );
90
91
        return $routeName;
92
    }
93
94
    /**
95
     * Load the Admin's route.
96
     *
97
     * @param mixed  $resource
98
     * @param string $type
99
     *
100
     * @return RouteCollection
101
     *
102
     * @throws Exception
103
     */
104
    public function load($resource, $type = null)
105
    {
106
        if (true === $this->loaded) {
107
            throw new RuntimeException('Do not add the Admin "extra" loader twice');
108
        }
109
        $routes = new RouteCollection();
110
111
        foreach ($this->resourceCollection->all() as $name => $resource) {
112
            $configuration = $this
113
                ->configurationFactory
114
                ->createAdminConfiguration(
115
                    $resource->getName(),
116
                    $resource->getConfiguration(),
117
                    $this->applicationConfiguration
118
                )
119
            ;
120
121
            foreach ($configuration->getParameter('actions') as $actionName => $actionData) {
122
                $actionConfiguration = $this
123
                    ->configurationFactory
124
                    ->createActionConfiguration($actionName, $actionData, $name, $configuration)
125
                ;
126
                $route = new Route(
127
                    $actionConfiguration->getParameter('route_path'),
128
                    $actionConfiguration->getParameter('route_defaults'),
129
                    $actionConfiguration->getParameter('route_requirements')
130
                );
131
                $routes->add($actionConfiguration->getParameter('route'), $route);
132
            }
133
134
            if ($this->applicationConfiguration->getParameter('enable_homepage')) {
135
                $route = new Route('/', ['_controller' => HomeAction::class,], []);
136
                $routes->add('lag.admin.homepage', $route);
137
            }
138
        }
139
140
        return $routes;
141
    }
142
143
    /**
144
     * Return true for the extra resource.
145
     *
146
     * @param mixed $resource
147
     * @param string $type
148
     *
149
     * @return bool
150
     */
151
    public function supports($resource, $type = null)
152
    {
153
        return 'extra' === $type;
154
    }
155
    
156
    public function getResolver()
157
    {
158
    }
159
160
    /**
161
     * @param LoaderResolverInterface $resolver
162
     */
163
    public function setResolver(LoaderResolverInterface $resolver)
164
    {
165
    }
166
}
167