add propel paramConverter, to be used with Sensio Framework Extra

This commit is contained in:
jaugustin 2011-09-22 16:11:14 +02:00
parent a5c87b3c87
commit 537799b3c2
3 changed files with 105 additions and 0 deletions

View file

@ -65,6 +65,7 @@ class PropelExtension extends Extension
if (!$container->hasDefinition('propel')) {
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('propel.xml');
$loader->load('converters.xml');
}
if (0 === strncasecmp(PHP_SAPI, 'cli', 3)) {

View file

@ -0,0 +1,88 @@
<?php
namespace Propel\PropelBundle\Request\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
/**
* PropelConverter.
*
* @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com>
*/
class PropelParamConverter implements ParamConverterInterface
{
public function apply(Request $request, ConfigurationInterface $configuration)
{
$classQuery = $configuration->getClass() . 'Query';
$options = $configuration->getOptions();
if (!class_exists($classQuery)) {
throw new \Exception(sprintf('The %s Query class does not exist', $classQuery));
}
// find by Pk
if (false === $object = $this->findPk($classQuery, $request)) {
throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
// find by criteria
if (false === $object = $this->findOneBy($classQuery, $request, $options)) {
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);
}
protected function findPk($classQuery, Request $request)
{
if (!$request->attributes->has('id')) {
return false;
}
return $classQuery::create()->findPk($request->attributes->get('id'));
}
protected function findOneBy($classQuery, Request $request, $options)
{
$query = $classQuery::create();
$hasCriteria = false;
foreach ($request->attributes->all() as $key => $value) {
try {
$query->{'filterBy' . ucfirst($key)}($value);
$hasCriteria = true;
} catch (\PropelException $e) { }
}
if (!$hasCriteria) {
return false;
}
return $query->findOne();
}
public function supports(ConfigurationInterface $configuration)
{
if (null === ($classname = $configuration->getClass())) {
return false;
}
$options = $configuration->getOptions();
if (!class_exists($classname)) {
return false;
}
// Propel Class?
$class = new \ReflectionClass($configuration->getClass());
if ($class->isSubclassOf('BaseObject')) {
return true;
}
return false;
}
}

View file

@ -0,0 +1,16 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="propel.converter.propel.class">Propel\PropelBundle\Request\ParamConverter\PropelParamConverter</parameter>
</parameters>
<services>
<service id="propel.converter.propel.orm" class="%propel.converter.propel.class%">
<tag name="request.param_converter" priority="10" />
</service>
</services>
</container>