1: <?php
2:
3: namespace Mapbender\Component;
4: use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter;
5: use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
6: use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
7: use Symfony\Component\HttpFoundation\Request;
8: use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9: use Doctrine\ORM\Mapping\MappingException;
10:
11:
12:
13: 14: 15: 16: 17: 18: 19: 20:
21: class CustomDoctrineParamConverter implements ParamConverterInterface {
22:
23: protected $configuration = null;
24:
25: public function __construct(\Doctrine\Bundle\DoctrineBundle\Registry $registry = null)
26: {
27: if (is_null($registry)) {
28: return;
29: }
30:
31: $this->registry = $registry;
32: }
33:
34: public function apply(Request $request, ConfigurationInterface $configuration) {
35:
36:
37: $this->configuration = $configuration;
38: $class = $configuration->getClass();
39: $options = $this->getOptions($configuration);
40:
41:
42:
43: if ($request->attributes->has('id') || $request->attributes->has($configuration->getName()."Id" )) {
44: $object = $this->find($class, $request, $options);
45: }else{
46:
47: if (false === $object = $this->findList($class, $request, $options)) {
48: throw new \LogicException('Unable to guess how to get a Doctrine instance from the request information.');
49: }
50: }
51:
52: if (null === $object && false === $configuration->isOptional()) {
53: throw new NotFoundHttpException(sprintf('%s object not found.', $class));
54: }
55:
56: $request->attributes->set($configuration->getName(), $object);
57:
58: }
59:
60: protected function findList($class,Request $request, $options){
61:
62:
63:
64: $offset = $request->get('offset') ? $request->get('offset') : 0;
65: $limit = $request->get('limit') ? $request->get('limit') : 10;
66:
67: $limit = $limit < 1000 ? $limit : 1000;
68:
69: $request->attributes->set("usedOffset", $offset);
70: $request->attributes->set("usedLimit", $limit);
71:
72:
73:
74: $criteria = array();
75: if ($request->attributes->get('criteria'))
76: parse_str($request->attributes->get('criteria'), $criteria);
77:
78: $result = $this->registry->getRepository($class, $options['entity_manager'])
79: ->findBy($criteria,array('id' => 'ASC'),$limit + 1,$offset);
80: if(count($result) === ($limit + 1)) {
81: $result = array_splice($result, 0, $limit);
82: $request->attributes->set('lastPage', false);
83: } else {
84: $request->attributes->set('lastPage', true);
85: }
86: return $result;
87: }
88:
89: protected function find($class, Request $request, $options) {
90:
91:
92:
93:
94: if ($request->attributes->has('id')) {
95: return $this->registry->getRepository($class, $options['entity_manager'])->find($request->attributes->get('id'));
96: }
97:
98:
99: $name = $this->configuration->getName();
100: if ($request->attributes->has($name."Id")) {
101: return $this->registry->getRepository($class, $options['entity_manager'])->find($request->attributes->get($name."Id"));
102: }
103:
104: }
105:
106: public function supports(ConfigurationInterface $configuration) {
107: if (null === $this->registry) {
108: return false;
109: }
110:
111: if (null === $configuration->getClass()) {
112: return false;
113: }
114:
115: $options = $this->getOptions($configuration);
116:
117:
118: try {
119: $this->registry->getEntityManager($options['entity_manager'])->getClassMetadata($configuration->getClass());
120:
121: return true;
122: } catch (MappingException $e) {
123: return false;
124: }
125: }
126: protected function getOptions(ConfigurationInterface $configuration) {
127: return array_replace(array(
128: 'entity_manager' => 'default',
129: ), $configuration->getOptions());
130: }
131: }
132: