/** * @param OutputInterface $output */ private function listResources(OutputInterface $output) { $resources = $this->registry->getAll(); ksort($resources); $table = new Table($output); $table->setHeaders(['Alias']); foreach ($resources as $resource) { $table->addRow([$resource->getAlias()]); } $table->render(); }
/** * @param ClassMetadataInfo $metadata */ private function convertToDocumentIfNeeded(ClassMetadataInfo $metadata) { foreach ($this->resourceRegistry->getAll() as $alias => $resourceMetadata) { if ($metadata->getName() !== $resourceMetadata->getClass('model')) { continue; } if ($resourceMetadata->hasClass('repository')) { $metadata->setCustomRepositoryClass($resourceMetadata->getClass('repository')); } $metadata->isMappedSuperclass = false; } }
/** * It should display the metadata for a given resource alias. */ public function testDebugResource() { $this->registry->get('metadata.one')->willReturn($this->createMetadata('one')); $this->tester->execute(['resource' => 'metadata.one']); $display = $this->tester->getDisplay(); $this->assertEquals(<<<'EOT' +------------------------------+-----------------+ | name | one | | application | sylius | | driver | doctrine/foobar | | classes.foo | bar | | classes.bar | foo | | whatever.something.elephants | camels | +------------------------------+-----------------+ EOT , $display); }
/** * {@inheritdoc} */ public function load($resource, $type = null) { $processor = new Processor(); $configurationDefinition = new Configuration(); $configuration = Yaml::parse($resource); $configuration = $processor->processConfiguration($configurationDefinition, ['routing' => $configuration]); if (!empty($configuration['only']) && !empty($configuration['except'])) { throw new \InvalidArgumentException('You can configure only one of "except" & "only" options.'); } $routesToGenerate = ['show', 'index', 'create', 'update', 'delete']; if (!empty($configuration['only'])) { $routesToGenerate = $configuration['only']; } if (!empty($configuration['except'])) { $routesToGenerate = array_diff($routesToGenerate, $configuration['except']); } $isApi = $type === 'sylius.resource_api'; /** @var MetadataInterface $metadata */ $metadata = $this->resourceRegistry->get($configuration['alias']); $routes = $this->routeFactory->createRouteCollection(); $rootPath = sprintf('/%s/', isset($configuration['path']) ? $configuration['path'] : Urlizer::urlize($metadata->getPluralName())); if (in_array('index', $routesToGenerate)) { $indexRoute = $this->createRoute($metadata, $configuration, $rootPath, 'index', ['GET'], $isApi); $routes->add($this->getRouteName($metadata, $configuration, 'index'), $indexRoute); } if (in_array('create', $routesToGenerate)) { $createRoute = $this->createRoute($metadata, $configuration, $isApi ? $rootPath : $rootPath . 'new', 'create', $isApi ? ['POST'] : ['GET', 'POST'], $isApi); $routes->add($this->getRouteName($metadata, $configuration, 'create'), $createRoute); } if (in_array('update', $routesToGenerate)) { $updateRoute = $this->createRoute($metadata, $configuration, $isApi ? $rootPath . '{id}' : $rootPath . '{id}/edit', 'update', $isApi ? ['PUT', 'PATCH'] : ['GET', 'PUT', 'PATCH'], $isApi); $routes->add($this->getRouteName($metadata, $configuration, 'update'), $updateRoute); } if (in_array('show', $routesToGenerate)) { $showRoute = $this->createRoute($metadata, $configuration, $rootPath . '{id}', 'show', ['GET'], $isApi); $routes->add($this->getRouteName($metadata, $configuration, 'show'), $showRoute); } if (in_array('delete', $routesToGenerate)) { $deleteRoute = $this->createRoute($metadata, $configuration, $rootPath . '{id}', 'delete', ['DELETE'], $isApi); $routes->add($this->getRouteName($metadata, $configuration, 'delete'), $deleteRoute); } return $routes; }
/** * Add mapping data to a translation entity. * * @param ClassMetadata $metadata */ private function mapTranslation(ClassMetadata $metadata) { $className = $metadata->name; try { $resourceMetadata = $this->resourceMetadataRegistry->getByClass($className); } catch (\InvalidArgumentException $exception) { return; } /** @var MetadataInterface $translatableResourceMetadata */ $translatableResourceMetadata = $this->resourceMetadataRegistry->get(str_replace('_translation', '', $resourceMetadata->getAlias())); $metadata->mapManyToOne(['fieldName' => 'translatable', 'targetEntity' => $translatableResourceMetadata->getClass('model'), 'inversedBy' => 'translations', 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE', 'nullable' => false]]]); if (!$metadata->hasField('locale')) { $metadata->mapField(['fieldName' => 'locale', 'type' => 'string', 'nullable' => false]); } // Map unique index. $columns = [$metadata->getSingleAssociationJoinColumnName('translatable'), 'locale']; if (!$this->hasUniqueConstraint($metadata, $columns)) { $constraints = isset($metadata->table['uniqueConstraints']) ? $metadata->table['uniqueConstraints'] : []; $constraints[$metadata->getTableName() . '_uniq_trans'] = ['columns' => $columns]; $metadata->setPrimaryTable(['uniqueConstraints' => $constraints]); } }
function it_does_not_set_custom_repository_class_if_registry_does_not_have_class(LoadClassMetadataEventArgs $event, RegistryInterface $registry, ClassMetadata $classMetadata) { $registry->getByClass('Foo')->willThrow(\InvalidArgumentException::class); $classMetadata->setCustomRepositoryClass(Argument::any())->shouldNotBeCalled(); $this->loadClassMetadata($event); }
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); }