diff --git a/DependencyInjection/PropelExtension.php b/DependencyInjection/PropelExtension.php index 80f588c..62a6999 100644 --- a/DependencyInjection/PropelExtension.php +++ b/DependencyInjection/PropelExtension.php @@ -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)) { diff --git a/Request/ParamConverter/PropelParamConverter.php b/Request/ParamConverter/PropelParamConverter.php new file mode 100644 index 0000000..a273313 --- /dev/null +++ b/Request/ParamConverter/PropelParamConverter.php @@ -0,0 +1,88 @@ + + */ +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; + } +} diff --git a/Resources/config/converters.xml b/Resources/config/converters.xml new file mode 100644 index 0000000..0da4402 --- /dev/null +++ b/Resources/config/converters.xml @@ -0,0 +1,16 @@ + + + + + + Propel\PropelBundle\Request\ParamConverter\PropelParamConverter + + + + + + + +