Completed
Push — dev ( 4306de...25864c )
by Arnaud
02:52
created

RoutingLoader::setResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Routing;
4
5
use LAG\AdminBundle\Action\Action;
6
use LAG\AdminBundle\Action\ActionInterface;
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use Exception;
9
use LAG\AdminBundle\Admin\Factory\AdminFactory;
10
use RuntimeException;
11
use Symfony\Component\Config\Loader\LoaderInterface;
12
use Symfony\Component\Config\Loader\LoaderResolverInterface;
13
use Symfony\Component\Routing\Route;
14
use Symfony\Component\Routing\RouteCollection;
15
16
/**
17
 * RoutingLoader.
18
 *
19
 * Creates routing for configured entities
20
 */
21
class RoutingLoader implements LoaderInterface
22
{
23
    protected $loaded = false;
24
25
    /**
26
     * @var AdminFactory
27
     */
28
    protected $adminFactory;
29
30
    /**
31
     * RoutingLoader constructor.
32
     * 
33
     * @param AdminFactory $adminFactory
34
     */
35
    public function __construct(AdminFactory $adminFactory)
36
    {
37
        $this->adminFactory = $adminFactory;
38
    }
39
40
    /**
41
     * @param mixed $resource
42
     * @param null $type
43
     * @return RouteCollection
44
     * @throws Exception
45
     */
46
    public function load($resource, $type = null)
47
    {
48
        if (true === $this->loaded) {
49
            throw new RuntimeException('Do not add the "extra" loader twice');
50
        }
51
        $routes = new RouteCollection();
52
        $this->adminFactory->init();
53
        $admins = $this
54
            ->adminFactory
55
            ->getAdmins();
56
        
57
        // creating a route by admin and action
58
        /** @var AdminInterface $admin */
59
        foreach ($admins as $admin) {
60
            $actions = $admin->getActions();
61
            
62
            // by default, actions are create, edit, delete, list
63
            /** @var Action $action */
64
            foreach ($actions as $action) {
65
                // load route into collection
66
                $this->loadRouteForAction($admin, $action, $routes);
67
            }
68
        }
69
        // loader is loaded
70
        $this->loaded = true;
71
72
        return $routes;
73
    }
74
75
    /**
76
     * @param mixed $resource
77
     * @param null $type
78
     * @return bool
79
     */
80
    public function supports($resource, $type = null)
81
    {
82
        return 'extra' === $type;
83
    }
84
85
    /**
86
     * 
87
     */
88
    public function getResolver()
89
    {
90
    }
91
92
    /**
93
     * @param LoaderResolverInterface $resolver
94
     */
95
    public function setResolver(LoaderResolverInterface $resolver)
96
    {
97
    }
98
99
    /**
100
     * Add a Route to the RouteCollection according to an Admin an an Action.
101
     *
102
     * @param AdminInterface $admin
103
     * @param ActionInterface $action
104
     * @param RouteCollection $routeCollection
105
     *
106
     * @throws Exception
107
     */
108
    protected function loadRouteForAction(AdminInterface $admin, ActionInterface $action, RouteCollection $routeCollection)
109
    {
110
        $routingUrlPattern = $admin
111
            ->getConfiguration()
112
            ->getParameter('routing_url_pattern');
113
114
        // routing pattern should contains {admin} and {action}
115
        if (strpos($routingUrlPattern, '{admin}') == -1 || strpos($routingUrlPattern, '{action}') == -1) {
116
            throw new Exception('Admin routing pattern should contains {admin} and {action} placeholder');
117
        }
118
        // route path by entity name and action name
119
        $path = str_replace('{admin}', $admin->getName(), $routingUrlPattern);
120
        $path = str_replace('{action}', $action->getName(), $path);
121
122
        // by default, generic controller
123
        $defaults = [
124
            '_controller' => $admin->getConfiguration()->getParameter('controller').':'.$action->getName(),
125
            '_admin' => $admin->getName(),
126
            '_action' => $action->getName(),
127
        ];
128
        // by default, no requirements
129
        $requirements = [];
130
131
        // for delete and edit action, an id is required
132
        if (in_array($action->getName(), ['delete', 'edit'])) {
133
            $path .= '/{id}';
134
            $requirements = [
135
                'id' => '\d+',
136
            ];
137
        }
138
        // creating new route
139
        $route = new Route($path, $defaults, $requirements);
140
        $routeName = $admin->generateRouteName($action->getName());
141
142
        // replace action route configuration
143
        $actionConfiguration = $action
144
            ->getConfiguration()
145
            ->getParameters();
146
147
        $actionConfiguration['route'] = $routeName;
148
        $actionConfiguration['parameters'] = $requirements;
149
150
        $action
151
            ->getConfiguration()
152
            ->setParameters($actionConfiguration);
153
154
        // adding route to symfony collection
155
        $routeCollection->add($routeName, $route);
156
    }
157
158
    /**
159
     * @param $pattern
160
     * @param $adminName
161
     * @param $actionName
162
     * @return mixed
163
     */
164
    protected function replaceInRoute($pattern, $adminName, $actionName)
165
    {
166
        $pattern = str_replace('{admin}', $adminName, $pattern);
167
        $pattern = str_replace('{action}', $actionName, $pattern);
168
169
        return $pattern;
170
    }
171
172
    /**
173
     * Return entity path for routing (for example, MyNamespace\EntityName => entityName).
174
     *
175
     * @param $namespace
176
     *
177
     * @return string
178
     */
179
    protected function getEntityPath($namespace)
180
    {
181
        $array = explode('\\', $namespace);
182
        $path = array_pop($array);
183
        $path = strtolower(substr($path, 0, 1)).substr($path, 1);
184
185
        return $path;
186
    }
187
}
188