Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2015
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.40 KB | None | 0 0
  1. <?php
  2. /**
  3.  * User: Alex Grand <Kiral.Group@gmail.com>
  4.  * Date: 10/8/14
  5.  * Time: 12:52 AM
  6.  */
  7.  
  8. namespace Page\Mvc\Route;
  9.  
  10.  
  11. use Arilas\ORM\EntityManager;
  12. use Zend\I18n\Translator\TranslatorInterface as Translator;
  13. use Zend\Mvc\Router\Exception\InvalidArgumentException;
  14. use Zend\Mvc\Router\Exception\RuntimeException;
  15. use Zend\Mvc\Router\Http\RouteMatch;
  16. use Zend\Mvc\Router\Http\Segment;
  17. use Zend\Mvc\Router\RoutePluginManager;
  18. use Zend\ServiceManager\ServiceLocatorAwareInterface;
  19. use Zend\ServiceManager\ServiceLocatorInterface;
  20. use Zend\ServiceManager\ServiceManager;
  21. use Zend\Stdlib\RequestInterface as Request;
  22.  
  23. class PageRoute extends Segment implements ServiceLocatorAwareInterface
  24. {
  25.     protected $entityClassName;
  26.  
  27.     protected $property = 'id';
  28.  
  29.     public function __construct($route, array $constraints = [], array $defaults = [])
  30.     {
  31.         $this->defaults = $defaults;
  32.         $this->parts    = $this->parseRouteDefinition($route);
  33.         $this->regex    = $this->buildRegex($this->parts, $constraints);
  34.  
  35.         if (isset($this->defaults['entity']) && class_exists($this->defaults['entity'])) {
  36.             $this->entityClassName = $this->defaults['entity'];
  37.         } else {
  38.             throw new InvalidArgumentException(
  39.                 'Route must have an "entity" field inside defaults'
  40.             );
  41.         }
  42.  
  43.         if (isset($this->defaults['property'])) {
  44.             $this->property = $this->defaults['property'];
  45.         }
  46.     }
  47.  
  48.     /** @var  RoutePluginManager */
  49.     protected $serviceManager;
  50.  
  51.     /**
  52.      * Match a given request.
  53.      *
  54.      * @param  \Zend\Http\PhpEnvironment\Request|Request $request
  55.      * @param null $pathOffset
  56.      * @param array $options
  57.      * @throws RuntimeException
  58.      * @return RouteMatch|null
  59.      */
  60.     public function match(Request $request, $pathOffset = null, array $options = [])
  61.     {
  62.         if (!method_exists($request, 'getUri')) {
  63.             return null;
  64.         }
  65.  
  66.         $uri  = $request->getUri();
  67.         $path = $uri->getPath();
  68.  
  69.         $regex = $this->regex;
  70.  
  71.         if ($this->translationKeys) {
  72.             if (!isset($options['translator']) || !$options['translator'] instanceof Translator) {
  73.                 throw new RuntimeException('No translator provided');
  74.             }
  75.  
  76.             $translator = $options['translator'];
  77.             $textDomain = (isset($options['text_domain']) ? $options['text_domain'] : 'default');
  78.             $locale     = (isset($options['locale']) ? $options['locale'] : null);
  79.  
  80.             foreach ($this->translationKeys as $key) {
  81.                 $regex = str_replace('#' . $key . '#', $translator->translate($key, $textDomain, $locale), $regex);
  82.             }
  83.         }
  84.  
  85.         if ($pathOffset !== null) {
  86.             $result = preg_match('(\G' . $regex . ')', $path, $matches, null, $pathOffset);
  87.         } else {
  88.             $result = preg_match('(^' . $regex . '$)', $path, $matches);
  89.         }
  90.  
  91.         if (!$result) {
  92.             return null;
  93.         }
  94.  
  95.         $matchedLength = strlen($matches[0]);
  96.         $params        = array();
  97.  
  98.         foreach ($this->paramMap as $index => $name) {
  99.             if (isset($matches[$index]) && $matches[$index] !== '') {
  100.                 $params[$name] = $this->decode($matches[$index]);
  101.             }
  102.         }
  103.  
  104.         if (!isset($params[$this->property])) {
  105.             return null;
  106.         }
  107.  
  108.         $value = $params[$this->property];
  109.         /** @var EntityManager $entityManager */
  110.         $entityManager = $this->getServiceLocator()->get('arilas.orm.entity_manager');
  111.         $entity = $entityManager->getRepository($this->entityClassName)->findOneBy(
  112.             [
  113.                 $this->property => $value,
  114.             ]
  115.         );
  116.  
  117.         if (!$entity) {
  118.             return null;
  119.         } else {
  120.             $params['entity'] = $entity;
  121.         }
  122.  
  123.         return new RouteMatch(array_merge($this->defaults, $params), $matchedLength);
  124.     }
  125.  
  126.     /**
  127.      * Set service locator
  128.      *
  129.      * @param ServiceLocatorInterface $serviceLocator
  130.      */
  131.     public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
  132.     {
  133.         $this->serviceManager = $serviceLocator;
  134.     }
  135.  
  136.     /**
  137.      * Get service locator
  138.      *
  139.      * @return ServiceManager
  140.      */
  141.     public function getServiceLocator()
  142.     {
  143.         return $this->serviceManager->getServiceLocator();
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement