1 | <?php |
||
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) |
||
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) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getResolver() |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function setResolver(LoaderResolverInterface $resolver) |
||
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) |
||
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) |
||
182 | } |
||
183 |