/** * @param MetadataInterface $metadata * @param array $configuration * @param string $path * @param string $actionName * @param array $methods * * @return Route */ private function createRoute(MetadataInterface $metadata, array $configuration, $path, $actionName, array $methods, $isApi = false) { $defaults = ['_controller' => $metadata->getServiceId('controller') . sprintf(':%sAction', $actionName)]; if ($isApi && 'index' === $actionName) { $defaults['_sylius']['serialization_groups'] = ['Default']; } if ($isApi && in_array($actionName, ['show', 'create', 'update'])) { $defaults['_sylius']['serialization_groups'] = ['Default', 'Detailed']; } if (isset($configuration['grid']) && 'index' === $actionName) { $defaults['_sylius']['grid'] = $configuration['grid']; } if (isset($configuration['form']) && in_array($actionName, ['create', 'update'])) { $defaults['_sylius']['form'] = $configuration['form']; } if (isset($configuration['section'])) { $defaults['_sylius']['section'] = $configuration['section']; } if (isset($configuration['templates']) && in_array($actionName, ['show', 'index', 'create', 'update'])) { $defaults['_sylius']['template'] = sprintf('%s:%s.html.twig', $configuration['templates'], $actionName); } if (isset($configuration['redirect']) && in_array($actionName, ['create', 'update'])) { $defaults['_sylius']['redirect'] = $this->getRouteName($metadata, $configuration, $configuration['redirect']); } if (isset($configuration['vars']['all'])) { $defaults['_sylius']['vars'] = $configuration['vars']['all']; } if (isset($configuration['vars'][$actionName])) { $vars = isset($configuration['vars']['all']) ? $configuration['vars']['all'] : []; $defaults['_sylius']['vars'] = array_merge($vars, $configuration['vars'][$actionName]); } return $this->routeFactory->createRoute($path, $defaults, [], [], '', [], $methods); }
/** * @param MetadataInterface $metadata * @param array $configuration * @param string $path * @param string $actionName * @param array $methods * * @return Route */ private function createRoute(MetadataInterface $metadata, array $configuration, $path, $actionName, array $methods) { $defaults = ['_controller' => $metadata->getServiceId('controller') . sprintf(':%sAction', $actionName)]; if (isset($configuration['form']) && in_array($actionName, ['create', 'update'])) { $defaults['_sylius']['form'] = $configuration['form']; } if (isset($configuration['section'])) { $defaults['_sylius']['section'] = $configuration['section']; } if (isset($configuration['templates']) && in_array($actionName, ['show', 'index', 'create', 'update'])) { $defaults['_sylius']['template'] = sprintf('%s:%s.html.twig', $configuration['templates'], $actionName); } if (isset($configuration['redirect']) && in_array($actionName, ['create', 'update'])) { $defaults['_sylius']['redirect'] = $this->getRouteName($metadata, $configuration, $configuration['redirect']); } return $this->routeFactory->createRoute($path, $defaults, [], [], '', [], $methods); }
function it_generates_routing_with_custom_variables(RegistryInterface $resourceRegistry, MetadataInterface $metadata, RouteFactoryInterface $routeFactory, RouteCollection $routeCollection, Route $showRoute, Route $indexRoute, Route $createRoute, Route $updateRoute, Route $deleteRoute) { $resourceRegistry->get('sylius.product')->willReturn($metadata); $metadata->getApplicationName()->willReturn('sylius'); $metadata->getName()->willReturn('product'); $metadata->getPluralName()->willReturn('products'); $metadata->getServiceId('controller')->willReturn('sylius.controller.product'); $routeFactory->createRouteCollection()->willReturn($routeCollection); $configuration = <<<EOT alias: sylius.product vars: all: foo: bar create: bar: foo update: abc: xyz EOT; $showDefaults = ['_controller' => 'sylius.controller.product:showAction', '_sylius' => ['vars' => ['foo' => 'bar']]]; $routeFactory->createRoute('/products/{id}', $showDefaults, [], [], '', [], ['GET'])->willReturn($showRoute); $routeCollection->add('sylius_product_show', $showRoute)->shouldBeCalled(); $indexDefaults = ['_controller' => 'sylius.controller.product:indexAction', '_sylius' => ['vars' => ['foo' => 'bar']]]; $routeFactory->createRoute('/products/', $indexDefaults, [], [], '', [], ['GET'])->willReturn($indexRoute); $routeCollection->add('sylius_product_index', $indexRoute)->shouldBeCalled(); $createDefaults = ['_controller' => 'sylius.controller.product:createAction', '_sylius' => ['vars' => ['foo' => 'bar', 'bar' => 'foo']]]; $routeFactory->createRoute('/products/new', $createDefaults, [], [], '', [], ['GET', 'POST'])->willReturn($createRoute); $routeCollection->add('sylius_product_create', $createRoute)->shouldBeCalled(); $updateDefaults = ['_controller' => 'sylius.controller.product:updateAction', '_sylius' => ['vars' => ['foo' => 'bar', 'abc' => 'xyz']]]; $routeFactory->createRoute('/products/{id}/edit', $updateDefaults, [], [], '', [], ['GET', 'PUT', 'PATCH'])->willReturn($updateRoute); $routeCollection->add('sylius_product_update', $updateRoute)->shouldBeCalled(); $deleteDefaults = ['_controller' => 'sylius.controller.product:deleteAction', '_sylius' => ['vars' => ['foo' => 'bar']]]; $routeFactory->createRoute('/products/{id}', $deleteDefaults, [], [], '', [], ['DELETE'])->willReturn($deleteRoute); $routeCollection->add('sylius_product_delete', $deleteRoute)->shouldBeCalled(); $this->load($configuration, 'sylius.resource')->shouldReturn($routeCollection); }
function it_generates_api_routing_based_on_resource_configuration(RegistryInterface $resourceRegistry, MetadataInterface $metadata, RouteFactoryInterface $routeFactory, RouteCollection $routeCollection, Route $showRoute, Route $indexRoute, Route $createRoute, Route $updateRoute, Route $deleteRoute) { $resourceRegistry->get('sylius.product')->willReturn($metadata); $metadata->getApplicationName()->willReturn('sylius'); $metadata->getName()->willReturn('product'); $metadata->getPluralName()->willReturn('products'); $metadata->getServiceId('controller')->willReturn('sylius.controller.product'); $routeFactory->createRouteCollection()->willReturn($routeCollection); $configuration = <<<EOT alias: sylius.product EOT; $showDefaults = ['_controller' => 'sylius.controller.product:showAction']; $routeFactory->createRoute('/products/{id}', $showDefaults, [], [], '', [], ['GET'])->willReturn($showRoute); $routeCollection->add('sylius_product_show', $showRoute)->shouldBeCalled(); $indexDefaults = ['_controller' => 'sylius.controller.product:indexAction']; $routeFactory->createRoute('/products/', $indexDefaults, [], [], '', [], ['GET'])->willReturn($indexRoute); $routeCollection->add('sylius_product_index', $indexRoute)->shouldBeCalled(); $createDefaults = ['_controller' => 'sylius.controller.product:createAction']; $routeFactory->createRoute('/products/', $createDefaults, [], [], '', [], ['POST'])->willReturn($createRoute); $routeCollection->add('sylius_product_create', $createRoute)->shouldBeCalled(); $updateDefaults = ['_controller' => 'sylius.controller.product:updateAction']; $routeFactory->createRoute('/products/{id}', $updateDefaults, [], [], '', [], ['PUT', 'PATCH'])->willReturn($updateRoute); $routeCollection->add('sylius_product_update', $updateRoute)->shouldBeCalled(); $deleteDefaults = ['_controller' => 'sylius.controller.product:deleteAction']; $routeFactory->createRoute('/products/{id}', $deleteDefaults, [], [], '', [], ['DELETE'])->willReturn($deleteRoute); $routeCollection->add('sylius_product_delete', $deleteRoute)->shouldBeCalled(); $this->load($configuration, 'sylius.resource_api')->shouldReturn($routeCollection); }