Overview

Namespaces

  • Mapbender
    • Component
      • HTTP
    • CoreBundle
      • Command
      • Component
        • Exception
      • Controller
      • DataFixtures
        • ORM
      • DependencyInjection
      • Element
        • Type
      • Entity
      • EventListener
      • Extension
      • Form
        • DataTransformer
        • EventListener
        • Type
      • Security
      • Template
    • KmlBundle
      • Element
    • ManagerBundle
      • Controller
      • Form
        • DataTransformer
        • Type
    • MonitoringBundle
      • Command
      • Component
      • Controller
      • DependencyInjection
      • Entity
      • EventListener
      • Form
    • PrintBundle
      • Component
      • Controller
    • WmcBundle
      • Component
        • Exception
      • Element
        • Type
      • Entity
      • Form
        • Type
    • WmsBundle
      • Component
        • Exception
      • Controller
      • DependencyInjection
      • Element
        • Type
      • Entity
      • Event
      • Form
        • EventListener
        • Type
    • WmtsBundle
      • Component
        • Exception
      • Controller
      • Entity
      • Form
        • Type
  • None
  • PHP

Classes

  • CustomDoctrineParamConverter
  • Ldap
  • UrlHelper
  • UrlSessionFormEntryPoint
  • UrlSessionLoginHandler
  • UrlSessionRedirectListener
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  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:  * @Author: Karim Malhas <karim@malhas.de>
 15:  * @package: bkg
 16:  * This Class overides the default Doctrine ParamConverter to allow routes like this:
 17:  * @Route(/{/{nameaId}/foo/{namebid})
 18:  * @ParamConverter("namea",class="FOOBundle:A")
 19:  * public function (A $namea)
 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:         // TODO: needs a security context
 36: 
 37:         $this->configuration = $configuration;
 38:         $class = $configuration->getClass();
 39:         $options = $this->getOptions($configuration);
 40: 
 41: 
 42:         // find by identifier?
 43:         if ($request->attributes->has('id') || $request->attributes->has($configuration->getName()."Id" )) {
 44:             $object = $this->find($class, $request, $options);
 45:         }else{
 46:             // $object always becomes an array here
 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:             // if limit or offset are set we assume we the caller is trying for pagination
 62:             // FIXME: is this clever? what if someone just wants to mess with us?
 63: 
 64:             $offset = $request->get('offset') ? $request->get('offset') : 0;
 65:             $limit = $request->get('limit') ? $request->get('limit') : 10;
 66:             // allow 1000 results per page at most
 67:             $limit = $limit < 1000 ? $limit : 1000;
 68: 
 69:             $request->attributes->set("usedOffset", $offset);
 70:             $request->attributes->set("usedLimit", $limit);
 71: 
 72:             // this generally allows the user to search in any field
 73:             // something else might be wanted in any case
 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:         // try to load by Id
 92:         // Supports only single parameter - this is how the "normal" DoctrineParameterConverter works
 93:         // keeping it just in case
 94:         if ($request->attributes->has('id')) {
 95:             return $this->registry->getRepository($class, $options['entity_manager'])->find($request->attributes->get('id'));
 96:         }
 97: 
 98:         // try to load by classname + id
 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:         // Doctrine Entity?
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: 
Mapbender3 API documenation API documentation generated by ApiGen 2.8.0