/** * @param ContainerBuilder $container * @param MetadataInterface $metadata */ protected function addResourceListeners(ContainerBuilder $container, MetadataInterface $metadata) { $defaultOptions = ['parent_path_default' => null, 'parent_path_autocreate' => false, 'parent_path_force' => false, 'name_filter' => true, 'name_resolver' => true]; $metadataOptions = $metadata->hasParameter('options') ? $metadata->getParameter('options') : []; if ($diff = array_diff(array_keys($metadataOptions), array_keys($defaultOptions))) { throw new InvalidArgumentException(sprintf('Unknown PHPCR-ODM configuration options: "%s"', implode('", "', $diff))); } $options = array_merge($defaultOptions, $metadataOptions); $createEventName = sprintf('%s.%s.pre_%s', $metadata->getApplicationName(), $metadata->getName(), 'create'); $updateEventName = sprintf('%s.%s.pre_%s', $metadata->getApplicationName(), $metadata->getName(), 'update'); if ($options['parent_path_default']) { $defaultPath = new Definition(DefaultParentListener::class); $defaultPath->setArguments([new Reference($metadata->getServiceId('manager')), $options['parent_path_default'], $options['parent_path_autocreate'], $options['parent_path_force']]); $defaultPath->addTag('kernel.event_listener', ['event' => $createEventName, 'method' => 'onPreCreate']); $container->setDefinition(sprintf('%s.resource.%s.doctrine.odm.phpcr.event_listener.default_path', $metadata->getApplicationName(), $metadata->getName()), $defaultPath); } if ($options['name_filter']) { $nameFilter = new Definition(NameFilterListener::class); $nameFilter->setArguments([new Reference($metadata->getServiceId('manager'))]); $nameFilter->addTag('kernel.event_listener', ['event' => $createEventName, 'method' => 'onEvent']); $nameFilter->addTag('kernel.event_listener', ['event' => $updateEventName, 'method' => 'onEvent']); $container->setDefinition(sprintf('%s.resource.%s.doctrine.odm.phpcr.event_listener.name_filter', $metadata->getApplicationName(), $metadata->getName()), $nameFilter); } if ($options['name_resolver']) { $nameResolver = new Definition(NameResolverListener::class); $nameResolver->setArguments([new Reference($metadata->getServiceId('manager'))]); $nameResolver->addTag('kernel.event_listener', ['event' => $createEventName, 'method' => 'onEvent']); $nameResolver->addTag('kernel.event_listener', ['event' => $updateEventName, 'method' => 'onEvent']); $container->setDefinition(sprintf('%s.resource.%s.doctrine.odm.phpcr.event_listener.name_resolver', $metadata->getApplicationName(), $metadata->getName()), $nameResolver); } }
/** * @param ContainerBuilder $container * @param MetadataInterface $metadata */ protected function setClassesParameters(ContainerBuilder $container, MetadataInterface $metadata) { if ($metadata->hasClass('model')) { $container->setParameter(sprintf('%s.model.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('model')); } if ($metadata->hasClass('controller')) { $container->setParameter(sprintf('%s.controller.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('controller')); } if ($metadata->hasClass('factory')) { $container->setParameter(sprintf('%s.factory.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('factory')); } if ($metadata->hasClass('repository')) { $container->setParameter(sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('repository')); } }
/** * @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) { $reflection = new \ReflectionClass($metadata->getClass('model')); $translatableInterface = TranslatableInterface::class; $translatable = interface_exists($translatableInterface) && $reflection->implementsInterface($translatableInterface); $repositoryClassParameterName = sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName()); $repositoryClass = $translatable ? TranslatableResourceRepository::class : EntityRepository::class; if ($container->hasParameter($repositoryClassParameterName)) { $repositoryClass = $container->getParameter($repositoryClassParameterName); } 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')) { $translatableRepositoryInterface = TranslatableResourceRepositoryInterface::class; $translationConfig = $metadata->getParameter('translation'); if (interface_exists($translatableRepositoryInterface) && $repositoryReflection->implementsInterface($translatableRepositoryInterface)) { if (isset($translationConfig['fields'])) { $definition->addMethodCall('setTranslatableFields', [$translationConfig['fields']]); } } } $container->setDefinition($metadata->getServiceId('repository'), $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(), ['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) { $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 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'); }
/** * @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; }
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) { $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('product'); $metadata->getApplicationName()->willReturn('sylius'); $this->getName()->shouldReturn('sylius_product_from_identifier'); }
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()); }
function let(MetadataInterface $metadata, DefaultFormBuilderInterface $defaultFormBuilder) { $metadata->getApplicationName()->willReturn('sylius'); $metadata->getName()->willReturn('order_item'); $this->beConstructedWith($metadata, $defaultFormBuilder); }
/** * @param ContainerBuilder $container * @param MetadataInterface $metadata */ protected function addForms(ContainerBuilder $container, MetadataInterface $metadata) { foreach ($metadata->getClass('form') as $formName => $formClass) { $suffix = 'default' === $formName ? '' : sprintf('_%s', $formName); $alias = sprintf('%s_%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix); $definition = new Definition($formClass); switch ($formName) { case 'choice': $definition->setArguments([$metadata->getClass('model'), $metadata->getDriver(), $alias]); break; default: $validationGroupsParameterName = sprintf('%s.validation_groups.%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix); $validationGroups = new Parameter($validationGroupsParameterName); if (!$container->hasParameter($validationGroupsParameterName)) { $validationGroups = ['Default']; } $definition->setArguments([$metadata->getClass('model'), $validationGroups]); break; } $definition->addTag('form.type', ['alias' => $alias]); $container->setParameter(sprintf('%s.form.type.%s%s.class', $metadata->getApplicationName(), $metadata->getName(), $suffix), $formClass); $container->setDefinition(sprintf('%s.form.type.%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix), $definition); } if (!$container->hasDefinition(sprintf('%s.form.type.%s', $metadata->getApplicationName(), $metadata->getName()))) { $this->addDefaultForm($container, $metadata); } }
/** * @param MetadataInterface $metadata * @param array $configuration * @param string $actionName * * @return string */ private function getRouteName(MetadataInterface $metadata, array $configuration, $actionName) { $sectionPrefix = isset($configuration['section']) ? $configuration['section'] . '_' : ''; return sprintf('%s_%s%s_%s', $metadata->getApplicationName(), $sectionPrefix, $metadata->getName(), $actionName); }
function it_has_a_valid_name(MetadataInterface $metadata) { $metadata->getName()->willReturn('country'); $metadata->getApplicationName()->willReturn('sylius'); $this->getName()->shouldReturn('sylius_country_choice'); }
function it_has_a_name(MetadataInterface $metadata) { $metadata->getName()->willReturn('taxon'); $metadata->getApplicationName()->willReturn('sylius'); $this->getName()->shouldReturn('sylius_taxon_to_identifier'); }
/** * {@inheritdoc} */ public function getBlockPrefix() { return sprintf('%s_%s_to_identifier', $this->metadata->getApplicationName(), $this->metadata->getName()); }