|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
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 Sylius\Bundle\ResourceBundle\Routing; |
|
13
|
|
|
|
|
14
|
|
|
use Gedmo\Sluggable\Util\Urlizer; |
|
15
|
|
|
use Sylius\Component\Resource\Metadata\MetadataInterface; |
|
16
|
|
|
use Sylius\Component\Resource\Metadata\RegistryInterface; |
|
17
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
18
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
|
19
|
|
|
use Symfony\Component\Config\Loader\LoaderResolverInterface; |
|
20
|
|
|
use Symfony\Component\Routing\Route; |
|
21
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ResourceLoader implements LoaderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var RegistryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $resourceRegistry; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var RouteFactoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $routeFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param RegistryInterface $resourceRegistry |
|
40
|
|
|
* @param RouteFactoryInterface $routeFactory |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(RegistryInterface $resourceRegistry, RouteFactoryInterface $routeFactory) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->resourceRegistry = $resourceRegistry; |
|
45
|
|
|
$this->routeFactory = $routeFactory; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function load($resource, $type = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$processor = new Processor(); |
|
54
|
|
|
$configurationDefinition = new Configuration(); |
|
55
|
|
|
|
|
56
|
|
|
$configuration = Yaml::parse($resource); |
|
57
|
|
|
$configuration = $processor->processConfiguration($configurationDefinition, ['routing' => $configuration]); |
|
58
|
|
|
|
|
59
|
|
|
if (!empty($configuration['only']) && !empty($configuration['except'])) { |
|
60
|
|
|
throw new \InvalidArgumentException('You can configure only one of "except" & "only" options.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$routesToGenerate = ['show', 'index', 'create', 'update', 'delete']; |
|
64
|
|
|
|
|
65
|
|
|
if (!empty($configuration['only'])) { |
|
66
|
|
|
$routesToGenerate = $configuration['only']; |
|
67
|
|
|
} |
|
68
|
|
|
if (!empty($configuration['except'])) { |
|
69
|
|
|
$routesToGenerate = array_diff($routesToGenerate, $configuration['except']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$isApi = $type === 'sylius.resource_api'; |
|
73
|
|
|
|
|
74
|
|
|
$metadata = $this->resourceRegistry->get($configuration['alias']); |
|
75
|
|
|
$routes = $this->routeFactory->createRouteCollection(); |
|
76
|
|
|
|
|
77
|
|
|
$rootPath = sprintf('/%s/', isset($configuration['path']) ? $configuration['path'] : Urlizer::urlize($metadata->getPluralName())); |
|
78
|
|
|
|
|
79
|
|
|
if (in_array('index', $routesToGenerate)) { |
|
80
|
|
|
$indexRoute = $this->createRoute($metadata, $configuration, $rootPath, 'index', ['GET']); |
|
81
|
|
|
$routes->add($this->getRouteName($metadata, $configuration, 'index'), $indexRoute); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (in_array('create', $routesToGenerate)) { |
|
85
|
|
|
$createRoute = $this->createRoute($metadata, $configuration, $isApi ? $rootPath : $rootPath.'new', 'create', $isApi ? ['POST'] : ['GET', 'POST']); |
|
86
|
|
|
$routes->add($this->getRouteName($metadata, $configuration, 'create'), $createRoute); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (in_array('update', $routesToGenerate)) { |
|
90
|
|
|
$updateRoute = $this->createRoute($metadata, $configuration, $isApi ? $rootPath.'{id}' : $rootPath.'{id}/edit', 'update', $isApi ? ['PUT', 'PATCH'] : ['GET', 'PUT', 'PATCH']); |
|
91
|
|
|
$routes->add($this->getRouteName($metadata, $configuration, 'update'), $updateRoute); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (in_array('show', $routesToGenerate)) { |
|
95
|
|
|
$showRoute = $this->createRoute($metadata, $configuration, $rootPath.'{id}', 'show', ['GET']); |
|
96
|
|
|
$routes->add($this->getRouteName($metadata, $configuration, 'show'), $showRoute); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (in_array('delete', $routesToGenerate)) { |
|
100
|
|
|
$deleteRoute = $this->createRoute($metadata, $configuration, $rootPath.'{id}', 'delete', ['DELETE']); |
|
101
|
|
|
$routes->add($this->getRouteName($metadata, $configuration, 'delete'), $deleteRoute); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $routes; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function supports($resource, $type = null) |
|
111
|
|
|
{ |
|
112
|
|
|
return 'sylius.resource' === $type || 'sylius.resource_api' === $type; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getResolver() |
|
119
|
|
|
{ |
|
120
|
|
|
// Intentionally left blank. |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setResolver(LoaderResolverInterface $resolver) |
|
127
|
|
|
{ |
|
128
|
|
|
// Intentionally left blank. |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param MetadataInterface $metadata |
|
133
|
|
|
* @param array $configuration |
|
134
|
|
|
* @param string $path |
|
135
|
|
|
* @param string $actionName |
|
136
|
|
|
* @param array $methods |
|
137
|
|
|
* |
|
138
|
|
|
* @return Route |
|
139
|
|
|
*/ |
|
140
|
|
|
private function createRoute(MetadataInterface $metadata, array $configuration, $path, $actionName, array $methods) |
|
141
|
|
|
{ |
|
142
|
|
|
$defaults = [ |
|
143
|
|
|
'_controller' => $metadata->getServiceId('controller').sprintf(':%sAction', $actionName), |
|
144
|
|
|
]; |
|
145
|
|
|
|
|
146
|
|
|
if (isset($configuration['form']) && in_array($actionName, ['create', 'update'])) { |
|
147
|
|
|
$defaults['_sylius']['form'] = $configuration['form']; |
|
148
|
|
|
} |
|
149
|
|
|
if (isset($configuration['section'])) { |
|
150
|
|
|
$defaults['_sylius']['section'] = $configuration['section']; |
|
151
|
|
|
} |
|
152
|
|
|
if (isset($configuration['templates']) && in_array($actionName, ['show', 'index', 'create', 'update'])) { |
|
153
|
|
|
$defaults['_sylius']['template'] = sprintf('%s:%s.html.twig', $configuration['templates'], $actionName); |
|
154
|
|
|
} |
|
155
|
|
|
if (isset($configuration['redirect']) && in_array($actionName, ['create', 'update'])) { |
|
156
|
|
|
$defaults['_sylius']['redirect'] = $this->getRouteName($metadata, $configuration, $configuration['redirect']); |
|
157
|
|
|
} |
|
158
|
|
|
if (isset($configuration['vars']['all'])) { |
|
159
|
|
|
$defaults['_sylius']['vars'] = $configuration['vars']['all']; |
|
160
|
|
|
} |
|
161
|
|
|
if (isset($configuration['vars'][$actionName])) { |
|
162
|
|
|
$vars = isset($configuration['vars']['all']) ? $configuration['vars']['all'] : []; |
|
163
|
|
|
$defaults['_sylius']['vars'] = array_merge($vars, $configuration['vars'][$actionName]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this->routeFactory->createRoute($path, $defaults, [], [], '', [], $methods); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param MetadataInterface $metadata |
|
171
|
|
|
* @param array $configuration |
|
172
|
|
|
* @param string $actionName |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
private function getRouteName(MetadataInterface $metadata, array $configuration, $actionName) |
|
177
|
|
|
{ |
|
178
|
|
|
$sectionPrefix = isset($configuration['section']) ? $configuration['section'].'_' : ''; |
|
179
|
|
|
|
|
180
|
|
|
return sprintf('%s_%s%s_%s', $metadata->getApplicationName(), $sectionPrefix, $metadata->getName(), $actionName); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|