示例#1
0
 /**
  * Apply converter on request based on the given configuration.
  *
  * @param Request                $request
  * @param ConfigurationInterface $configuration
  */
 protected function applyConverter(Request $request, ConfigurationInterface $configuration)
 {
     $value = $request->attributes->get($configuration->getName());
     $className = $configuration->getClass();
     // If the value is already an instance of the class we are trying to convert it into
     // we should continue as no conversion is required
     if (is_object($value) && $value instanceof $className) {
         return;
     }
     if ($converterName = $configuration->getConverter()) {
         if (!isset($this->namedConverters[$converterName])) {
             throw new \RuntimeException(sprintf("No converter named '%s' found for conversion of parameter '%s'.", $converterName, $configuration->getName()));
         }
         $converter = $this->namedConverters[$converterName];
         if (!$converter->supports($configuration)) {
             throw new \RuntimeException(sprintf("Converter '%s' does not support conversion of parameter '%s'.", $converterName, $configuration->getName()));
         }
         $converter->apply($request, $configuration);
         return;
     }
     foreach ($this->all() as $converter) {
         if ($converter->supports($configuration)) {
             if ($converter->apply($request, $configuration)) {
                 return;
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  *
  * @throws \LogicException       When unable to guess how to get a Doctrine instance from the request information
  * @throws NotFoundHttpException When object not found
  */
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $name = $configuration->getName();
     $class = $configuration->getClass();
     $options = $this->getOptions($configuration);
     if (null === $request->attributes->get($name, false)) {
         $configuration->setIsOptional(true);
     }
     // find by identifier?
     if (false === ($object = $this->find($class, $request, $options, $name))) {
         // find by criteria
         if (false === ($object = $this->findOneBy($class, $request, $options))) {
             if ($configuration->isOptional()) {
                 $object = null;
             } else {
                 throw new \LogicException('Unable to guess how to get a Doctrine instance from the request information.');
             }
         }
     }
     if (null === $object && false === $configuration->isOptional()) {
         throw new NotFoundHttpException(sprintf('%s object not found.', $class));
     }
     $request->attributes->set($name, $object);
     return true;
 }
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     if ($request->attributes->has($configuration->getName())) {
         return;
     }
     $class = $configuration->getClass();
     if (false === ($object = $this->find($class, $request))) {
         if (false === ($object = $this->findOneBy($class, $request))) {
             throw new \LogicException('Unable to guess how to get a Doctrine instance from the request information.');
         }
     }
     if (null === $object && false === $configuration->isOptional()) {
         throw new NotFoundHttpException(sprintf('%s object not found.', $class));
     }
     $request->attributes->set($configuration->getName(), $object);
 }
 /**
  * @param Request                $request
  * @param ConfigurationInterface $configuration
  *
  * @return bool
  *
  * @throws \LogicException
  * @throws NotFoundHttpException
  * @throws \Exception
  */
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $classQuery = $configuration->getClass() . 'Query';
     $classPeer = $configuration->getClass() . 'Peer';
     $this->filters = array();
     $this->exclude = array();
     if (!class_exists($classQuery)) {
         throw new \Exception(sprintf('The %s Query class does not exist', $classQuery));
     }
     $tableMap = $classPeer::getTableMap();
     $pkColumns = $tableMap->getPrimaryKeyColumns();
     if (count($pkColumns) == 1) {
         $this->pk = strtolower($pkColumns[0]->getName());
     }
     $options = $configuration->getOptions();
     // Check route options for converter options, if there are non provided.
     if (empty($options) && $request->attributes->has('_route') && $this->router && $configuration instanceof ParamConverter) {
         $converterOption = $this->router->getRouteCollection()->get($request->attributes->get('_route'))->getOption('propel_converter');
         if (!empty($converterOption[$configuration->getName()])) {
             $options = $converterOption[$configuration->getName()];
         }
     }
     if (isset($options['mapping'])) {
         // We use the mapping for calling findPk or filterBy
         foreach ($options['mapping'] as $routeParam => $column) {
             if ($request->attributes->has($routeParam)) {
                 if ($this->pk === $column) {
                     $this->pk = $routeParam;
                 } else {
                     $this->filters[$column] = $request->attributes->get($routeParam);
                 }
             }
         }
     } else {
         $this->exclude = isset($options['exclude']) ? $options['exclude'] : array();
         $this->filters = $request->attributes->all();
     }
     $this->withs = isset($options['with']) ? is_array($options['with']) ? $options['with'] : array($options['with']) : array();
     // find by Pk
     if (false === ($object = $this->findPk($classQuery, $request))) {
         // find by criteria
         if (false === ($object = $this->findOneBy($classQuery, $request))) {
             if ($configuration->isOptional()) {
                 //we find nothing but the object is optional
                 $object = null;
             } else {
                 throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
             }
         }
     }
     if (null === $object && false === $configuration->isOptional()) {
         throw new NotFoundHttpException(sprintf('%s object not found.', $configuration->getClass()));
     }
     $request->attributes->set($configuration->getName(), $object);
     return true;
 }
 public function supports(ConfigurationInterface $configuration)
 {
     if (!$configuration->getName()) {
         return false;
     }
     if (!$configuration->getClass()) {
         return false;
     }
     return $configuration->getClass() === 'Civix\\CoreBundle\\Model\\Comment\\CommentModelInterface';
 }
示例#6
0
 function apply(Request $request, ConfigurationInterface $configuration)
 {
     // On récupère l'entité Site correspondante
     $site = $this->repository->findOneByHostname($request->getHost());
     // On définit ensuite un attribut de requête du nom de $conf->getName()
     // et contenant notre entité Site
     $request->attributes->set($configuration->getName(), $site);
     // On retourne true pour qu'aucun autre ParamConverter ne soit utilisé sur cet argument
     // Je pense notamment au ParamConverter de Doctrine qui risque de vouloir s'appliquer !
     return true;
 }
 /**
  * @param \Symfony\Component\HttpFoundation\Request                                $request
  * @param \Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface $configuration
  *
  * @return bool
  */
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $name = $configuration->getName();
     $class = $configuration->getClass();
     $options = $this->getOptions($configuration);
     $document = $this->findOneBy($class, $request, $options);
     if (!$document) {
         return false;
     }
     $request->attributes->set($name, $document);
     return true;
 }
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $param = $configuration->getName();
     if (!$request->attributes->has($param)) {
         return false;
     }
     $options = $configuration->getOptions();
     $value = $request->attributes->get($param);
     $date = isset($options['format']) ? DateTime::createFromFormat($options['format'], $value) : new DateTime($value);
     if (!$date) {
         throw new NotFoundHttpException('Invalid date given.');
     }
     $request->attributes->set($param, $date);
     return true;
 }
示例#9
0
 /**
  * Apply the converter
  *
  * @param  \Symfony\Component\HttpFoundation\Request                                $request
  * @param  \Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface $configuration
  * @return boolean
  * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  */
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $name = $configuration->getName();
     $options = $this->getOptions($configuration);
     $id = $this->getIdentifier($request, $options, $name);
     if (is_array($id)) {
         $id = implode(":", $id);
     }
     try {
         return parent::apply($request, $configuration);
     } catch (NotFoundHttpException $exeption) {
         throw new NotFoundHttpException(sprintf($options['error_message'], $id));
     }
     return true;
 }
 public function supports(ConfigurationInterface $configuration)
 {
     if (!$configuration->getName()) {
         return false;
     }
     if (!($options = $configuration->getOptions())) {
         return false;
     }
     if (!isset($options['entityId']) || !isset($options['typeEntity'])) {
         return false;
     }
     if (!$configuration->getClass()) {
         return false;
     }
     return $configuration->getClass() === 'Civix\\CoreBundle\\Model\\Answer\\AnswerModelInterface';
 }
 function apply(Request $request, ConfigurationInterface $configuration)
 {
     if ($configuration->getClass() == 'Amisure\\P4SApiBundle\\Entity\\User\\BeneficiaryUser') {
         $user = $this->accessor->getBeneficiaryProfile($request->get('beneficiaryId'));
         if (null == $user) {
             throw new NotFoundResourceException('BeneficiaryUser[id=' . $request->get('beneficiaryId') . '] inexistant');
         }
     } else {
         $user = $this->em->getRepository('Amisure\\P4SApiBundle\\Entity\\User\\SessionUser')->findOneBy(array('username' => $request->get('beneficiaryId')));
         if (null == $user) {
             throw new NotFoundResourceException('SessionUser[id=' . $request->get('beneficiaryId') . '] inexistant');
         }
     }
     $request->attributes->set($configuration->getName(), $user);
     return true;
     // Don't use an other ParamConverter after this one
 }
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $classQuery = $configuration->getClass() . 'Query';
     if (!class_exists($classQuery)) {
         throw new \Exception(sprintf('The %s Query class does not exist', $classQuery));
     }
     $options = $configuration->getOptions();
     $exclude = isset($options['exclude']) ? $options['exclude'] : array();
     // find by Pk
     if (in_array('id', $exclude) || false === ($object = $this->findPk($classQuery, $request))) {
         // find by criteria
         if (false === ($object = $this->findOneBy($classQuery, $request, $exclude))) {
             throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
         }
     }
     if (null === $object && false === $configuration->isOptional()) {
         throw new NotFoundHttpException(sprintf('%s object not found.', $configuration->getClass()));
     }
     $request->attributes->set($configuration->getName(), $object);
 }
 /**
  * {@inheritDoc}
  */
 public function apply(Request $request, ConfigurationInterface $configuration)
 {
     $options = (array) $configuration->getOptions();
     if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) {
         $context = array_merge($this->context, $options['deserializationContext']);
     } else {
         $context = $this->context;
     }
     if ($this->serializer instanceof SerializerInterface) {
         $context = $this->configureDeserializationContext($this->getDeserializationContext(), $context);
     }
     try {
         $object = $this->serializer->deserialize($request->getContent(), $configuration->getClass(), $request->getContentType(), $context);
     } catch (UnsupportedFormatException $e) {
         throw new HttpException(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, $e->getMessage());
     } catch (JMSSerializerException $e) {
         throw new HttpException(Codes::HTTP_BAD_REQUEST, $e->getMessage());
     } catch (SymfonySerializerException $e) {
         throw new HttpException(Codes::HTTP_BAD_REQUEST, $e->getMessage());
     }
     $request->attributes->set($configuration->getName(), $object);
     if (null !== $this->validator) {
         $validatorOptions = $this->getValidatorOptions($options);
         $request->attributes->set($this->validationErrorsArgument, $this->validator->validate($object, $validatorOptions['groups'], $validatorOptions['traverse'], $validatorOptions['deep']));
     }
     return true;
 }