vendor/bundles/Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/DoctrineParamConverter.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
       
     4 
       
     5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
       
     6 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
       
     7 use Symfony\Component\HttpFoundation\Request;
       
     8 use Symfony\Bundle\DoctrineBundle\Registry;
       
     9 
       
    10 use Doctrine\ORM\NoResultException;
       
    11 use Doctrine\ORM\Mapping\MappingException;
       
    12 
       
    13 /*
       
    14  * This file is part of the Symfony framework.
       
    15  *
       
    16  * (c) Fabien Potencier <fabien@symfony.com>
       
    17  *
       
    18  * This source file is subject to the MIT license that is bundled
       
    19  * with this source code in the file LICENSE.
       
    20  */
       
    21 
       
    22 /**
       
    23  * DoctrineConverter.
       
    24  *
       
    25  * @author     Fabien Potencier <fabien@symfony.com>
       
    26  */
       
    27 class DoctrineParamConverter implements ParamConverterInterface
       
    28 {
       
    29     protected $registry;
       
    30 
       
    31     public function __construct(Registry $registry = null)
       
    32     {
       
    33         if (is_null($registry)) {
       
    34             return;
       
    35         }
       
    36 
       
    37         $this->registry = $registry;
       
    38     }
       
    39 
       
    40     public function apply(Request $request, ConfigurationInterface $configuration)
       
    41     {
       
    42         $class = $configuration->getClass();
       
    43         $options = $this->getOptions($configuration);
       
    44 
       
    45         // find by identifier?
       
    46         if (false === $object = $this->find($class, $request, $options)) {
       
    47             // find by criteria
       
    48             if (false === $object = $this->findOneBy($class, $request, $options)) {
       
    49                 throw new \LogicException('Unable to guess how to get a Doctrine instance from the request information.');
       
    50             }
       
    51         }
       
    52 
       
    53         if (null === $object && false === $configuration->isOptional()) {
       
    54             throw new NotFoundHttpException(sprintf('%s object not found.', $class));
       
    55         }
       
    56 
       
    57         $request->attributes->set($configuration->getName(), $object);
       
    58     }
       
    59 
       
    60     protected function find($class, Request $request, $options)
       
    61     {
       
    62         if (!$request->attributes->has('id')) {
       
    63             return false;
       
    64         }
       
    65 
       
    66         return $this->registry->getRepository($class, $options['entity_manager'])->find($request->attributes->get('id'));
       
    67     }
       
    68 
       
    69     protected function findOneBy($class, Request $request, $options)
       
    70     {
       
    71         $criteria = array();
       
    72         $metadata = $this->registry->getEntityManager($options['entity_manager'])->getClassMetadata($class);
       
    73         foreach ($request->attributes->all() as $key => $value) {
       
    74             if ($metadata->hasField($key)) {
       
    75                 $criteria[$key] = $value;
       
    76             }
       
    77         }
       
    78 
       
    79         if (!$criteria) {
       
    80             return false;
       
    81         }
       
    82 
       
    83         return $this->registry->getRepository($class, $options['entity_manager'])->findOneBy($criteria);
       
    84     }
       
    85 
       
    86     public function supports(ConfigurationInterface $configuration)
       
    87     {
       
    88         if (null === $this->registry) {
       
    89             return false;
       
    90         }
       
    91 
       
    92         if (null === $configuration->getClass()) {
       
    93             return false;
       
    94         }
       
    95 
       
    96         $options = $this->getOptions($configuration);
       
    97 
       
    98         // Doctrine Entity?
       
    99         try {
       
   100             $this->registry->getEntityManager($options['entity_manager'])->getClassMetadata($configuration->getClass());
       
   101 
       
   102             return true;
       
   103         } catch (MappingException $e) {
       
   104             return false;
       
   105         }
       
   106     }
       
   107 
       
   108     protected function getOptions(ConfigurationInterface $configuration)
       
   109     {
       
   110         return array_replace(array(
       
   111             'entity_manager' => 'default',
       
   112         ), $configuration->getOptions());
       
   113     }
       
   114 }