Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * User: Alex Grand <Kiral.Group@gmail.com>
- * Date: 10/8/14
- * Time: 12:52 AM
- */
- namespace Page\Mvc\Route;
- use Arilas\ORM\EntityManager;
- use Zend\I18n\Translator\TranslatorInterface as Translator;
- use Zend\Mvc\Router\Exception\InvalidArgumentException;
- use Zend\Mvc\Router\Exception\RuntimeException;
- use Zend\Mvc\Router\Http\RouteMatch;
- use Zend\Mvc\Router\Http\Segment;
- use Zend\Mvc\Router\RoutePluginManager;
- use Zend\ServiceManager\ServiceLocatorAwareInterface;
- use Zend\ServiceManager\ServiceLocatorInterface;
- use Zend\ServiceManager\ServiceManager;
- use Zend\Stdlib\RequestInterface as Request;
- class PageRoute extends Segment implements ServiceLocatorAwareInterface
- {
- protected $entityClassName;
- protected $property = 'id';
- public function __construct($route, array $constraints = [], array $defaults = [])
- {
- $this->defaults = $defaults;
- $this->parts = $this->parseRouteDefinition($route);
- $this->regex = $this->buildRegex($this->parts, $constraints);
- if (isset($this->defaults['entity']) && class_exists($this->defaults['entity'])) {
- $this->entityClassName = $this->defaults['entity'];
- } else {
- throw new InvalidArgumentException(
- 'Route must have an "entity" field inside defaults'
- );
- }
- if (isset($this->defaults['property'])) {
- $this->property = $this->defaults['property'];
- }
- }
- /** @var RoutePluginManager */
- protected $serviceManager;
- /**
- * Match a given request.
- *
- * @param \Zend\Http\PhpEnvironment\Request|Request $request
- * @param null $pathOffset
- * @param array $options
- * @throws RuntimeException
- * @return RouteMatch|null
- */
- public function match(Request $request, $pathOffset = null, array $options = [])
- {
- if (!method_exists($request, 'getUri')) {
- return null;
- }
- $uri = $request->getUri();
- $path = $uri->getPath();
- $regex = $this->regex;
- if ($this->translationKeys) {
- if (!isset($options['translator']) || !$options['translator'] instanceof Translator) {
- throw new RuntimeException('No translator provided');
- }
- $translator = $options['translator'];
- $textDomain = (isset($options['text_domain']) ? $options['text_domain'] : 'default');
- $locale = (isset($options['locale']) ? $options['locale'] : null);
- foreach ($this->translationKeys as $key) {
- $regex = str_replace('#' . $key . '#', $translator->translate($key, $textDomain, $locale), $regex);
- }
- }
- if ($pathOffset !== null) {
- $result = preg_match('(\G' . $regex . ')', $path, $matches, null, $pathOffset);
- } else {
- $result = preg_match('(^' . $regex . '$)', $path, $matches);
- }
- if (!$result) {
- return null;
- }
- $matchedLength = strlen($matches[0]);
- $params = array();
- foreach ($this->paramMap as $index => $name) {
- if (isset($matches[$index]) && $matches[$index] !== '') {
- $params[$name] = $this->decode($matches[$index]);
- }
- }
- if (!isset($params[$this->property])) {
- return null;
- }
- $value = $params[$this->property];
- /** @var EntityManager $entityManager */
- $entityManager = $this->getServiceLocator()->get('arilas.orm.entity_manager');
- $entity = $entityManager->getRepository($this->entityClassName)->findOneBy(
- [
- $this->property => $value,
- ]
- );
- if (!$entity) {
- return null;
- } else {
- $params['entity'] = $entity;
- }
- return new RouteMatch(array_merge($this->defaults, $params), $matchedLength);
- }
- /**
- * Set service locator
- *
- * @param ServiceLocatorInterface $serviceLocator
- */
- public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
- {
- $this->serviceManager = $serviceLocator;
- }
- /**
- * Get service locator
- *
- * @return ServiceManager
- */
- public function getServiceLocator()
- {
- return $this->serviceManager->getServiceLocator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement