function it_defines_resource_options(OptionsResolver $resolver, MetadataInterface $metadata) { $metadata->getDriver()->willReturn(SyliusResourceBundle::DRIVER_DOCTRINE_ORM); $resolver->setDefaults(['class' => null])->shouldBeCalled()->willReturn($resolver); $resolver->setNormalizer('class', Argument::type('callable'))->shouldBeCalled()->willReturn($resolver); $this->configureOptions($resolver); }
/** * {@inheritdoc} */ public function build(MetadataInterface $metadata, FormBuilderInterface $formBuilder, array $options) { $classMetadata = $this->entityManager->getClassMetadata($metadata->getClass('model')); if (1 < count($classMetadata->identifier)) { throw new \RuntimeException('The default form factory does not support entity classes with multiple primary keys.'); } $fields = (array) $classMetadata->fieldNames; if (!$classMetadata->isIdentifierNatural()) { $fields = array_diff($fields, $classMetadata->identifier); } foreach ($fields as $fieldName) { $options = []; if (in_array($fieldName, ['createdAt', 'updatedAt', 'deletedAt'])) { continue; } if (Type::DATETIME === $classMetadata->getTypeOfField($fieldName)) { $options = ['widget' => 'single_text']; } $formBuilder->add($fieldName, null, $options); } foreach ($classMetadata->getAssociationMappings() as $fieldName => $associationMapping) { if (ClassMetadataInfo::ONE_TO_MANY !== $associationMapping['type']) { $formBuilder->add($fieldName, null, ['property' => 'id']); } } }
/** * @param MetadataInterface $metadata * * @return string */ protected function getObjectManagerName(MetadataInterface $metadata) { $objectManagerName = 'default'; if ($metadata->hasParameter('options') && isset($metadata->getParameter('options')['object_manager'])) { $objectManagerName = $metadata->getParameter('options')['object_manager']; } return $objectManagerName; }
/** * {@inheritdoc} */ protected function addDefaultForm(ContainerBuilder $container, MetadataInterface $metadata) { $defaultFormBuilderDefinition = new Definition(DefaultFormBuilder::class); $defaultFormBuilderDefinition->setArguments([new Reference($metadata->getServiceId('manager'))]); $definition = new Definition(DefaultResourceType::class); $definition->setArguments([$this->getMetdataDefinition($metadata), $defaultFormBuilderDefinition])->addTag('form.type', ['alias' => sprintf('%s_%s', $metadata->getApplicationName(), $metadata->getName())]); $container->setDefinition(sprintf('%s.form.type.%s', $metadata->getApplicationName(), $metadata->getName()), $definition); }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $repositoryClass = new Parameter('sylius.phpcr_odm.repository.class'); if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $definition = new Definition($repositoryClass); $definition->setArguments(array(new Reference($metadata->getServiceId('manager')), $this->getClassMetadataDefinition($metadata))); $container->setDefinition($metadata->getServiceId('repository'), $definition); }
function it_returns_specific_metadata_by_model_class(MetadataInterface $metadata1, MetadataInterface $metadata2) { $metadata1->getAlias()->willReturn('app.product'); $metadata1->getClass('model')->willReturn('App\\Model\\Product'); $metadata2->getAlias()->willReturn('app.order'); $metadata2->getClass('model')->willReturn('App\\Model\\Order'); $this->add($metadata1); $this->add($metadata2); $this->getByClass('App\\Model\\Order')->shouldReturn($metadata2); }
function it_uses_data_provider_to_create_a_view_with_data_and_definition(DataProviderInterface $dataProvider, ParametersParserInterface $parametersParser, Grid $grid, Parameters $parameters, MetadataInterface $resourceMetadata, Request $request, RequestConfiguration $requestConfiguration) { $expectedResourceGridView = new ResourceGridView(['foo', 'bar'], $grid->getWrappedObject(), $parameters->getWrappedObject(), $resourceMetadata->getWrappedObject(), $requestConfiguration->getWrappedObject()); $requestConfiguration->getRequest()->willReturn($request); $parametersParser->parseRequestValues(['repository' => ['method' => 'createByCustomerQueryBuilder', 'arguments' => ['$customerId']]], $request)->willReturn(['repository' => ['method' => 'createByCustomerQueryBuilder', 'arguments' => [5]]]); $grid->getDriverConfiguration()->willReturn(['repository' => ['method' => 'createByCustomerQueryBuilder', 'arguments' => ['$customerId']]]); $grid->setDriverConfiguration(['repository' => ['method' => 'createByCustomerQueryBuilder', 'arguments' => [5]]])->shouldBeCalled(); $dataProvider->getData($grid, $parameters)->willReturn(['foo', 'bar']); $this->create($grid, $parameters, $resourceMetadata, $requestConfiguration)->shouldBeSameResourceGridViewAs($expectedResourceGridView); }
function it_adds_custom_message(SessionInterface $session, TranslatorBagInterface $translator, MessageCatalogueInterface $messageCatalogue, FlashBagInterface $flashBag, MetadataInterface $metadata, RequestConfiguration $requestConfiguration, ResourceInterface $resource) { $metadata->getApplicationName()->willReturn('app'); $metadata->getHumanizedName()->willReturn('book'); $requestConfiguration->getMetadata()->willReturn($metadata); $requestConfiguration->getFlashMessage('send')->willReturn('app.book.send'); $translator->getCatalogue('en')->willReturn($messageCatalogue); $messageCatalogue->has('app.book.send', 'flashes')->willReturn(true); $session->getBag('flashes')->willReturn($flashBag); $flashBag->add('success', 'app.book.send')->shouldBeCalled(); $this->addSuccessFlash($requestConfiguration, 'send', $resource); }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $modelClass = $metadata->getClass('model'); $repositoryClass = in_array(TranslatableInterface::class, class_implements($modelClass)) ? TranslatableRepository::class : new Parameter('sylius.mongodb.odm.repository.class'); if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $unitOfWorkDefinition = new Definition('Doctrine\\ODM\\MongoDB\\UnitOfWork'); $unitOfWorkDefinition->setFactory([new Reference($this->getManagerServiceId($metadata)), 'getUnitOfWork'])->setPublic(false); $definition = new Definition($repositoryClass); $definition->setArguments([new Reference($metadata->getServiceId('manager')), $unitOfWorkDefinition, $this->getClassMetadataDefinition($metadata)]); $container->setDefinition($metadata->getServiceId('repository'), $definition); }
function it_uses_default_translation_if_message_is_not_translated(SessionInterface $session, FlashBagInterface $flashBag, TranslatorInterface $translator, MetadataInterface $metadata, RequestConfiguration $requestConfiguration, ResourceInterface $resource) { $metadata->getApplicationName()->willReturn('sylius'); $metadata->getName()->willReturn('product'); $metadata->getHumanizedName()->willReturn('product'); $requestConfiguration->getMetadata()->willReturn($metadata); $requestConfiguration->isHtmlRequest()->willReturn(true); $session->getBag('flashes')->willReturn($flashBag); $requestConfiguration->getFlashMessage(ResourceActions::CREATE)->willReturn('sylius.product.create'); $translator->trans('sylius.product.create', ['%resource%' => 'Product'], 'flashes')->willReturn('sylius.product.create'); $translator->trans('sylius.resource.create', ['%resource%' => 'Product'], 'flashes')->willReturn('Product has been successfully created.'); $flashBag->add('success', 'Product has been successfully created.')->shouldBeCalled(); $this->addSuccessFlash($requestConfiguration, ResourceActions::CREATE, $resource); }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $reflection = new \ReflectionClass($metadata->getClass('model')); $translatableInterface = 'Sylius\\Component\\Translation\\Model\\TranslatableInterface'; $translatable = interface_exists($translatableInterface) && $reflection->implementsInterface($translatableInterface); $repositoryClassParameterName = sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName()); $repositoryClass = $translatable ? 'Sylius\\Bundle\\TranslationBundle\\Doctrine\\ORM\\TranslatableResourceRepository' : 'Sylius\\Bundle\\ResourceBundle\\Doctrine\\ORM\\EntityRepository'; if ($container->hasParameter($repositoryClassParameterName)) { $repositoryClass = $container->getParameter($repositoryClassParameterName); } if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $definition = new Definition($repositoryClass); $definition->setArguments(array(new Reference($metadata->getServiceId('manager')), $this->getClassMetadataDefinition($metadata))); if ($metadata->hasParameter('translation')) { $repositoryReflection = new \ReflectionClass($repositoryClass); $translatableRepositoryInterface = 'Sylius\\Component\\Translation\\Repository\\TranslatableResourceRepositoryInterface'; $translationConfig = $metadata->getParameter('translation'); if (interface_exists($translatableRepositoryInterface) && $repositoryReflection->implementsInterface($translatableRepositoryInterface)) { if (isset($translationConfig['fields'])) { $definition->addMethodCall('setTranslatableFields', array($translationConfig['fields'])); } } } $container->setDefinition($metadata->getServiceId('repository'), $definition); }
/** * @param MetadataInterface $metadata * * @return DriverInterface * * @throws UnknownDriverException */ public static function get(MetadataInterface $metadata) { $type = $metadata->getDriver(); if (isset(self::$drivers[$type])) { return self::$drivers[$type]; } switch ($type) { case SyliusResourceBundle::DRIVER_DOCTRINE_ORM: return self::$drivers[$type] = new DoctrineORMDriver(); case SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM: return self::$drivers[$type] = new DoctrineODMDriver(); case SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM: return self::$drivers[$type] = new DoctrinePHPCRDriver(); } throw new UnknownDriverException($type); }
/** * @param Request $request * * @return Response */ public function updateAction(Request $request) { $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); $this->isGrantedOr403($configuration, ResourceActions::UPDATE); $resource = $this->findOr404($configuration); $form = $this->resourceFormFactory->create($configuration, $resource); if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && $form->handleRequest($request)->isValid()) { $resource = $form->getData(); $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); if ($event->isStopped() && !$configuration->isHtmlRequest()) { throw new HttpException($event->getErrorCode(), $event->getMessage()); } if ($event->isStopped()) { $this->flashHelper->addFlashFromEvent($configuration, $event); return $this->redirectHandler->redirectToResource($configuration, $resource); } if ($configuration->hasStateMachine()) { $this->stateMachine->apply($configuration, $resource); } $this->manager->flush(); $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); if (!$configuration->isHtmlRequest()) { return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT)); } $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource); return $this->redirectHandler->redirectToResource($configuration, $resource); } if (!$configuration->isHtmlRequest()) { return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); } $view = View::create()->setData(['configuration' => $configuration, 'metadata' => $this->metadata, 'resource' => $resource, $this->metadata->getName() => $resource, 'form' => $form->createView()])->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html')); return $this->viewHandler->handle($configuration, $view); }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $modelClass = $metadata->getClass('model'); $reflection = new \ReflectionClass($modelClass); $translatableInterface = 'Sylius\\Component\\Translation\\Model\\TranslatableInterface'; $translatable = interface_exists($translatableInterface) && $reflection->implementsInterface($translatableInterface); $repositoryClass = $translatable ? 'Sylius\\Bundle\\TranslationBundle\\Doctrine\\ODM\\MongoDB\\TranslatableResourceRepository' : new Parameter('sylius.mongodb_odm.repository.class'); if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $unitOfWorkDefinition = new Definition('Doctrine\\ODM\\MongoDB\\UnitOfWork'); $unitOfWorkDefinition->setFactory(array(new Reference($this->getManagerServiceId($metadata)), 'getUnitOfWork'))->setPublic(false); $definition = new Definition($repositoryClass); $definition->setArguments(array(new Reference($metadata->getServiceId('manager')), $unitOfWorkDefinition, $this->getClassMetadataDefinition($modelClass))); return $definition; }
/** * @param Request $request * * @return Response */ public function updateAction(Request $request) { $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); $this->isGrantedOr403($configuration, ResourceActions::UPDATE); $resource = $this->findOr404($configuration); $form = $this->resourceFormFactory->create($configuration, $resource); if (in_array($request->getMethod(), array('POST', 'PUT', 'PATCH')) && $form->submit($request, !$request->isMethod('PATCH'))->isValid()) { $resource = $form->getData(); $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource); if ($event->isStopped() && !$configuration->isHtmlRequest()) { throw new HttpException($event->getErrorCode(), $event->getMessage()); } if ($event->isStopped()) { $this->flashHelper->addFlashFromEvent($configuration, $event); return $this->redirectHandler->redirectToResource($configuration, $resource); } $this->manager->flush(); $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource); if (!$configuration->isHtmlRequest()) { return $this->viewHandler->handle($configuration, View::create($resource, 204)); } $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource); return $this->redirectHandler->redirectToResource($configuration, $resource); } if (!$configuration->isHtmlRequest()) { return $this->viewHandler->handle($configuration, View::create($form, 400)); } $view = View::create()->setData(array('metadata' => $this->metadata, 'resource' => $resource, $this->metadata->getName() => $resource, 'form' => $form->createView()))->setTemplate($configuration->getTemplate(ResourceActions::UPDATE)); return $this->viewHandler->handle($configuration, $view); }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $modelClass = $metadata->getClass('model'); $reflection = new \ReflectionClass($modelClass); $translatableInterface = TranslatableInterface::class; $translatable = interface_exists($translatableInterface) && $reflection->implementsInterface($translatableInterface); $repositoryClass = $translatable ? TranslatableResourceRepository::class : new Parameter('sylius.mongodb_odm.repository.class'); if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $repositoryReflection = new \ReflectionClass($repositoryClass); $unitOfWorkDefinition = new Definition('Doctrine\\ODM\\MongoDB\\UnitOfWork'); $unitOfWorkDefinition->setFactory([new Reference($this->getManagerServiceId($metadata)), 'getUnitOfWork'])->setPublic(false); $definition = new Definition($repositoryClass); $definition->setArguments([new Reference($metadata->getServiceId('manager')), $unitOfWorkDefinition, $this->getClassMetadataDefinition($metadata)]); $definition->setLazy(!$repositoryReflection->isFinal()); $container->setDefinition($metadata->getServiceId('repository'), $definition); }
/** * @param string $name * * @return string * * @throws \LogicException */ public function getPermission($name) { if (!$this->hasPermission()) { throw new \LogicException('Current action does not require any authorization.'); } if (!$this->parameters->has('permission')) { return sprintf('%s.%s.%s', $this->metadata->getApplicationName(), $this->metadata->getName(), $name); } return $this->parameters->get('permission'); }
/** * {@inheritdoc} */ public function build(MetadataInterface $metadata, FormBuilderInterface $formBuilder, array $options) { $classMetadata = $this->documentManager->getClassMetadata($metadata->getClass('model')); // the field mappings should only contain standard value mappings foreach ($classMetadata->fieldMappings as $fieldName) { if ($fieldName === $classMetadata->uuidFieldName) { continue; } if ($fieldName === $classMetadata->nodename) { continue; } $options = []; $mapping = $classMetadata->mappings[$fieldName]; if ($mapping['nullable'] === false) { $options['required'] = true; } $formBuilder->add($fieldName, null, $options); } }
/** * @param string $name * * @return string * * @throws \LogicException */ public function getPermission($name) { $permission = $this->parameters->get('permission'); if (null === $permission) { throw new \LogicException('Current action does not require any authorization.'); } if (true === $permission) { return sprintf('%s.%s.%s', $this->metadata->getApplicationName(), $this->metadata->getName(), $name); } return $permission; }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $repositoryClass = new Parameter('sylius.phpcr_odm.repository.class'); if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $repositoryReflection = new \ReflectionClass($repositoryClass); $definition = new Definition($repositoryClass); $definition->setArguments([new Reference($metadata->getServiceId('manager')), $this->getClassMetadataDefinition($metadata)]); $definition->setLazy(!$repositoryReflection->isFinal()); if ($metadata->hasParameter('translation')) { $translationConfig = $metadata->getParameter('translation'); if (in_array(TranslatableRepositoryInterface::class, class_implements($repositoryClass))) { if (isset($translationConfig['fields'])) { $definition->addMethodCall('setTranslatableFields', [$translationConfig['fields']]); } } } $container->setDefinition($metadata->getServiceId('repository'), $definition); }
/** * {@inheritdoc} */ protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) { $repositoryClassParameterName = sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName()); $repositoryClass = EntityRepository::class; if ($container->hasParameter($repositoryClassParameterName)) { $repositoryClass = $container->getParameter($repositoryClassParameterName); } if ($metadata->hasClass('repository')) { $repositoryClass = $metadata->getClass('repository'); } $definition = new Definition($repositoryClass); $definition->setArguments([new Reference($metadata->getServiceId('manager')), $this->getClassMetadataDefinition($metadata)]); $container->setDefinition($metadata->getServiceId('repository'), $definition); }
/** * @param Request $request * @param string $transition * @param string $graph * * @return RedirectResponse */ public function updateStateAction(Request $request, $transition, $graph) { $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); $resource = $this->findOr404($configuration); $stateMachine = $this->get('sm.factory')->get($resource, $graph); if (!$stateMachine->can($transition)) { throw new NotFoundHttpException(sprintf('The requested transition %s cannot be applied on the given %s with graph %s.', $transition, $this->metadata->getName(), $graph)); } $stateMachine->apply($transition); $this->manager->flush(); if (!$configuration->isHtmlRequest()) { return $this->viewHandler->handle($configuration, View::create($resource, 204)); } $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource); return $this->redirectHandler->redirectToReferer($configuration); }
function it_has_a_name(MetadataInterface $metadata) { $metadata->getName()->willReturn('taxon'); $metadata->getApplicationName()->willReturn('sylius'); $this->getName()->shouldReturn('sylius_taxon_to_identifier'); }
/** * @param string $flashMessage * @param MetadataInterface $metadata * * @return string */ private function translateMessage($flashMessage, MetadataInterface $metadata) { return $this->translator->trans($flashMessage, ['%resource%' => ucfirst($metadata->getHumanizedName())], 'flashes'); }
/** * @param MetadataInterface $metadata * * @return Definition */ protected function getMetadataDefinition(MetadataInterface $metadata) { $definition = new Definition(Metadata::class); $definition->setFactory([new Reference('sylius.resource_registry'), 'get'])->setArguments([$metadata->getAlias()]); return $definition; }
function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(MetadataInterface $metadata, RequestConfigurationFactoryInterface $requestConfigurationFactory, RequestConfiguration $configuration, AuthorizationCheckerInterface $authorizationChecker, RepositoryInterface $repository, SingleResourceProviderInterface $singleResourceProvider, ResourceInterface $resource, FlashHelperInterface $flashHelper, EventDispatcherInterface $eventDispatcher, ResourceControllerEvent $event, Request $request) { $metadata->getApplicationName()->willReturn('sylius'); $metadata->getName()->willReturn('product'); $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); $configuration->hasPermission()->willReturn(true); $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); $singleResourceProvider->get($configuration, $repository)->willReturn($resource); $configuration->isHtmlRequest()->willReturn(false); $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); $event->isStopped()->willReturn(true); $event->getMessage()->willReturn('Cannot delete this product.'); $event->getErrorCode()->willReturn(500); $repository->remove($resource)->shouldNotBeCalled(); $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled(); $this->shouldThrow(new HttpException(500, 'Cannot delete this product.'))->during('deleteAction', array($request)); }
/** * {@inheritdoc} */ public function getBlockPrefix() { return sprintf('%s_%s_choice', $this->metadata->getApplicationName(), $this->metadata->getName()); }
/** * @param MetadataInterface $metadata * @param OutputInterface $output */ private function debugResource(MetadataInterface $metadata, OutputInterface $output) { $table = new Table($output); $information = ['name' => $metadata->getName(), 'application' => $metadata->getApplicationName(), 'driver' => $metadata->getDriver()]; $parameters = $this->flattenParameters($metadata->getParameters()); foreach ($parameters as $key => $value) { $information[$key] = $value; } foreach ($information as $key => $value) { $table->addRow([$key, $value]); } $table->render(); }
function it_generates_permission_name(MetadataInterface $metadata, Parameters $parameters) { $metadata->getApplicationName()->willReturn('sylius'); $metadata->getName()->willReturn('product'); $parameters->has('permission')->willReturn(false); $this->getPermission('index')->shouldReturn('sylius.product.index'); }
/** * {@inheritdoc} */ public function getName() { return sprintf('%s_%s', $this->metadata->getApplicationName(), $this->metadata->getName()); }