Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineBundle] added a request param converter for Doctrine
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/Symfony/Bundle/DoctrineBundle/Request/ParamConverter/DoctrineConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\DoctrineBundle\Request\ParamConverter; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterInterface; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Doctrine\ORM\NoResultException; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Mapping\MappingException; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* DoctrineConverter. | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class DoctrineConverter implements ConverterInterface | ||
{ | ||
protected $manager; | ||
|
||
public function __construct(EntityManager $manager) | ||
{ | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* Convert the \ReflectionParameter to something else. | ||
* | ||
* @param Request $request | ||
* @param \ReflectionParameter $property | ||
*/ | ||
public function apply(Request $request, \ReflectionParameter $parameter) | ||
{ | ||
$class = $parameter->getClass()->getName(); | ||
|
||
// find by identifier? | ||
if (false === $object = $this->find($class, $request)) { | ||
// find by criteria | ||
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) { | ||
throw new NotFoundHttpException(sprintf('%s object not found.', $class)); | ||
} | ||
|
||
$request->attributes->set($parameter->getName(), $object); | ||
} | ||
|
||
protected function find($class, Request $request) | ||
{ | ||
return $this->manager->getRepository($class)->find($request->attributes->get('id')); | ||
} | ||
|
||
protected function findOneBy($class, Request $request) | ||
{ | ||
$criteria = array(); | ||
$metadata = $this->manager->getClassMetadata($class); | ||
foreach ($request->attributes->all() as $key => $value) { | ||
if ($metadata->hasField($key)) { | ||
$criteria[$key] = $value; | ||
} | ||
} | ||
|
||
if (!$criteria) { | ||
return false; | ||
} | ||
|
||
return $this->manager->getRepository($class)->findOneBy($criteria); | ||
} | ||
|
||
/** | ||
* Returns boolean true if the ReflectionClass is supported, false otherwise | ||
* | ||
* @param \ReflectionParameter $parameter | ||
* | ||
* @return boolean | ||
*/ | ||
public function supports(\ReflectionClass $class) | ||
{ | ||
// Doctrine Entity? | ||
try { | ||
$this->manager->getClassMetadata($class->getName()); | ||
|
||
return true; | ||
} catch (MappingException $e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters