DoctrineConverter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Symfony\Bundle\DoctrineBundle\Request\ParamConverter;
  3. use Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterInterface;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Doctrine\ORM\NoResultException;
  7. use Doctrine\ORM\EntityManager;
  8. use Doctrine\ORM\Mapping\MappingException;
  9. /*
  10. * This file is part of the Symfony framework.
  11. *
  12. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  13. *
  14. * This source file is subject to the MIT license that is bundled
  15. * with this source code in the file LICENSE.
  16. */
  17. /**
  18. * DoctrineConverter.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class DoctrineConverter implements ConverterInterface
  23. {
  24. protected $manager;
  25. public function __construct(EntityManager $manager)
  26. {
  27. $this->manager = $manager;
  28. }
  29. /**
  30. * Convert the \ReflectionParameter to something else.
  31. *
  32. * @param Request $request
  33. * @param \ReflectionParameter $property
  34. */
  35. public function apply(Request $request, \ReflectionParameter $parameter)
  36. {
  37. $class = $parameter->getClass()->getName();
  38. // find by identifier?
  39. if (false === $object = $this->find($class, $request)) {
  40. // find by criteria
  41. if (false === $object = $this->findOneBy($class, $request)) {
  42. throw new \LogicException('Unable to guess how to get a Doctrine instance from the request information.');
  43. }
  44. }
  45. if (null === $object) {
  46. throw new NotFoundHttpException(sprintf('%s object not found.', $class));
  47. }
  48. $request->attributes->set($parameter->getName(), $object);
  49. }
  50. protected function find($class, Request $request)
  51. {
  52. if (!$request->attributes->has('id')) {
  53. return false;
  54. }
  55. return $this->manager->getRepository($class)->find($request->attributes->get('id'));
  56. }
  57. protected function findOneBy($class, Request $request)
  58. {
  59. $criteria = array();
  60. $metadata = $this->manager->getClassMetadata($class);
  61. foreach ($request->attributes->all() as $key => $value) {
  62. if ($metadata->hasField($key)) {
  63. $criteria[$key] = $value;
  64. }
  65. }
  66. if (!$criteria) {
  67. return false;
  68. }
  69. return $this->manager->getRepository($class)->findOneBy($criteria);
  70. }
  71. /**
  72. * Returns boolean true if the ReflectionClass is supported, false otherwise
  73. *
  74. * @param \ReflectionParameter $parameter
  75. *
  76. * @return boolean
  77. */
  78. public function supports(\ReflectionClass $class)
  79. {
  80. // Doctrine Entity?
  81. try {
  82. $this->manager->getClassMetadata($class->getName());
  83. return true;
  84. } catch (MappingException $e) {
  85. return false;
  86. }
  87. }
  88. }