public function getActionForCurrentController(string $action) : string
 {
     $currentPath = $this->getRouterRequestContext()->getPathInfo();
     $currentRoute = $this->router->match($currentPath);
     list($mode, $controller) = explode('.', $currentRoute['_route'], 3);
     $route = sprintf('%s.%s.%s', $mode, $controller, $action);
     return $route;
 }
Beispiel #2
0
 private function getRefererParams(Request $request)
 {
     $referer = $request->headers->get('referer');
     $matchBasePath = $this->router->match('');
     $baseUrl = $this->router->generate($matchBasePath['_route'], [], true);
     $lastPath = substr($referer, strpos($referer, $baseUrl) + strlen($baseUrl));
     return $this->router->match('/' . $lastPath);
 }
 /**
  * @param $key
  *
  * @return array An array of parameters
  */
 public function getPathInfoFromMetaTagKey($key)
 {
     $info = $this->keyGenerator->generatePathInfoFromMetaTagKey($key);
     $this->router->getContext()->setMethod('GET');
     if ($this->keyGenerator->isAddQueryString()) {
         $info = substr($info, 0, strpos($info, '?'));
         $match = $this->router->match($info);
         return $match;
     } else {
         return $this->router->match($info);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getItemFromIri($iri, $fetchData = false)
 {
     try {
         $parameters = $this->router->match($iri);
     } catch (ResourceNotFoundException $e) {
         return;
     }
     if (!isset($parameters['_resource']) || !isset($parameters['id']) || !($resource = $this->resourceCollection->getResourceForShortName($parameters['_resource']))) {
         throw new \InvalidArgumentException(sprintf('No resource associated with the IRI "%s".', $iri));
     }
     return $this->dataProvider->getItem($resource, $parameters['id'], $fetchData);
 }
Beispiel #5
0
 public function match($pathInfo)
 {
     $baseContext = $this->router->getContext();
     $request = Request::create($pathInfo);
     $context = (new RequestContext())->fromRequest($request);
     $context->setPathInfo($pathInfo);
     try {
         $this->router->setContext($context);
         return $this->router->match($request->getPathInfo());
     } finally {
         $this->router->setContext($baseContext);
     }
 }
 /**
  * @param string $value
  *
  * @return NodeTranslation|null
  */
 public function reverseTransform($value)
 {
     if ("" === $value || null === $value) {
         return null;
     }
     try {
         $route = $this->router->match($value);
         if (false === isset($route['_nodeTranslation']) || false === $route['_nodeTranslation'] instanceof NodeTranslation) {
             throw new TransformationFailedException('Matched route has no nodeTranslation');
         }
         return $route['_nodeTranslation'];
     } catch (RouteNotFoundException $e) {
         throw new TransformationFailedException('Cannot match URL to a nodeTranslation', 0, $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getItemFromIri($iri, $fetchData = false)
 {
     try {
         $parameters = $this->router->match($iri);
     } catch (ExceptionInterface $e) {
         throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
     }
     if (!isset($parameters['_resource']) || !isset($parameters['id']) || !($resource = $this->resourceCollection->getResourceForShortName($parameters['_resource']))) {
         throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
     }
     if ($item = $this->dataProvider->getItem($resource, $parameters['id'], $fetchData)) {
         return $item;
     }
     throw new InvalidArgumentException(sprintf('Item not found for "%s".', $iri));
 }
 /**
  * @param $path
  * @return array|null
  */
 private function findResourceInformationFromRouter($path)
 {
     // store and reset the request method
     $requestMethod = $this->getRequestMethod();
     $this->setRequestMethod(Request::METHOD_GET);
     try {
         $parameters = $this->router->match($path);
         $this->setRequestMethod($requestMethod);
     } catch (ResourceNotFoundException $e) {
         $this->setRequestMethod($requestMethod);
         return null;
     } catch (MethodNotAllowedException $e) {
         $this->setRequestMethod($requestMethod);
         return null;
     }
     if (!isset($parameters['_route'])) {
         return null;
     }
     $route = $this->router->getRouteCollection()->get($parameters['_route']);
     if (null === $route) {
         return null;
     }
     if (!$route->hasOption(self::RESOURCE_ENTITY_CLASS_OPTION) || !$route->hasOption(self::RESOURCE_ENTITY_ID_OPTION)) {
         return null;
     }
     if (!isset($parameters[$route->getOption(self::RESOURCE_ENTITY_ID_OPTION)])) {
         return null;
     }
     $converter = $this->defaultConverter;
     if ($route->hasOption(self::RESOURCE_CONVERTER_OPTION)) {
         $converter = $route->getOption(self::RESOURCE_CONVERTER_OPTION);
     }
     return [$route->getOption(self::RESOURCE_ENTITY_CLASS_OPTION), $parameters[$route->getOption(self::RESOURCE_ENTITY_ID_OPTION)], $converter];
 }
 public function canonical($pathinfo, $uri = null)
 {
     if ($uri) {
         if (!$this->keyGenerator->isAddQueryString()) {
             //remove query string
             if ($pos = strpos($uri, '?')) {
                 $uri = substr($uri, 0, $pos);
             }
         }
         //remove if rewrite not used
         $uri = str_replace('app_dev.php/', '', $uri);
         $uri = str_replace('app.php/', '', $uri);
         return $uri;
     }
     try {
         $infos = $this->router->match($pathinfo);
     } catch (ResourceNotFoundException $e) {
         $infos = false;
     } catch (MethodNotAllowedException $e) {
         $infos = false;
     }
     if ($infos === false) {
         return;
     }
     if (strpos($infos['_route'], RouteLoader::ROUTE_BEGIN) === 0) {
         $infos = RouteLoader::getPathinfo($infos['_route']);
     }
     $route = $infos['_route'];
     unset($infos['_route']);
     unset($infos['_controller']);
     return $this->router->generate($route, $infos, true);
 }
 public function action(RouterInterface $router, RequestContext $context)
 {
     $request = Request::createFromGlobals();
     $bPath = $context->getPathInfo();
     try {
         $parameters = $router->match($bPath);
         var_dump($parameters);
         $_controller = $parameters["_controller"];
         $_controller = explode(":", $_controller);
         $class = $_controller[0];
         $action = strtolower($_controller[1]) . "Action";
         $class = new $class();
         ob_start();
         if (method_exists($class, $action)) {
             $class->{$action}($request, new JsonResponse());
             $response = new Response(ob_get_clean());
         } else {
             $response = new Response('Not Found', 404);
         }
     } catch (ResourceNotFoundException $e) {
         $response = new Response('Not Found', 404);
     } catch (Exception $e) {
         $response = new Response('An error occurred', 500);
     }
     $response->send();
 }
 public function addRootRelations(Root $root, ClassMetadataInterface $classMetadata)
 {
     $prefix = $root->getPrefix();
     $relations = array();
     $self_args = $this->router->match($root->getPrefix());
     $relations[] = new Hateoas\Relation('self', new Hateoas\Route($self_args['_route'], array(), false));
     foreach ($root->getEntityNames() as $rel => $entityName) {
         /** @var ClassMetadata $metadata */
         $metadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityName);
         $routeName = $this->routeResolver->resolveRouteName($metadata->getName(), 'cgetAction');
         if ($routeName) {
             $arguments = array();
             foreach (self::$COLLECTION_ARGUMENTS as $argument) {
                 $arguments[$argument] = '{' . $argument . '}';
             }
             $relations[] = new Hateoas\Relation(Inflector::pluralize($rel), new Hateoas\Route($routeName, $arguments, false));
         }
         $routeName = $this->routeResolver->resolveRouteName($metadata->getName(), 'getAction');
         if ($routeName) {
             $relations[] = new Hateoas\Relation($rel, new Hateoas\Route($routeName, array('id' => "{id}"), false));
         }
         $routeName = $self_args['_route'] . '_schema';
         if ($this->router->getRouteCollection()->get($routeName)) {
             $relations[] = new Hateoas\Relation('schema:' . $rel, new Hateoas\Route($routeName, array('rel' => $rel), false));
         }
     }
     $user = $root->getCurrentUser();
     if ($user instanceof User) {
         $routeName = $this->routeResolver->resolveRouteName(get_class($user), 'getAction');
         if ($routeName) {
             $relations[] = new Hateoas\Relation('currentUser', new Hateoas\Route($routeName, array('id' => $user->getId()), false));
         }
     }
     return $relations;
 }
Beispiel #12
0
 public function getCurrentRoute()
 {
     try {
         return $this->router->match($this->router->getPathInfo());
     } catch (\Exception $e) {
         return array('_route' => null, '_locale' => $this->getLocale());
     }
 }
Beispiel #13
0
 public function validate($value, Constraint $constraint)
 {
     if (!preg_match('#^/[/a-z0-9-_%]*#', $value->getStaticPrefix())) {
         $this->context->buildViolation($constraint->urlValidation)->setParameter('%string%', $value->getStaticPrefix())->addViolation();
     }
     $isUnique = true;
     try {
         $route = $this->router->match($value->getStaticPrefix());
         if ($value->getName() && $value->getName() != $route['_route']) {
             $isUnique = false;
         }
     } catch (ResourceNotFoundException $e) {
     }
     if (!$isUnique) {
         $this->context->buildViolation($constraint->uniqueUrlMessage)->setParameter('%string%', $value->getStaticPrefix())->addViolation();
     }
 }
Beispiel #14
0
 /**
  * @param string|bool $check
  * @param string      $css
  *
  * @return null|string
  */
 public function getCssIfMatchRoute($check, $css = 'active')
 {
     if (is_string($check)) {
         $matcher = $this->router->match($this->request->getCurrentRequest()->getPathInfo());
         $check = $matcher['_route'] === $check;
     }
     return $check ? $css : null;
 }
 /**
  * Get route info by uri
  *
  * @param  string      $uri
  * @return null|string
  */
 protected function getRouteInfoByUri($uri)
 {
     try {
         $routeInfo = $this->router->match($uri);
         return $routeInfo[self::ROUTE_CONTROLLER_KEY];
     } catch (ResourceNotFoundException $e) {
     }
     return null;
 }
 /**
  * @inheritDoc
  */
 public function match($pathinfo)
 {
     if ($this->context->hasParameter('page_actions') || "/_" === substr($pathinfo, 0, 2)) {
         // some other router before this one should match something:
         throw new ResourceNotFoundException();
     }
     $candidates = $this->repository->getCandidatesForUrl($pathinfo);
     foreach ($candidates as $route) {
         $this->getContext()->setParameter('page_actions', $route['actions']);
         try {
             $res = $this->router->match(PageRouteLoader::PATH_PREFIX . substr($pathinfo, strlen($route['url'])));
             if ($res) {
                 $res['_nodeTranslation'] = $route['id'];
                 return $res;
             }
         } catch (ResourceNotFoundException $e) {
             // noop. try another candidate
         }
     }
     throw new ResourceNotFoundException();
 }
 /**
  * {@inheritDoc}
  */
 public function match($pathinfo)
 {
     $match = $this->router->match($pathinfo);
     // if a _locale parameter isset remove the .locale suffix that is appended to each route in I18nRoute
     if (!empty($match['_locale']) && preg_match('#^(.+)\\.' . preg_quote($match['_locale'], '#') . '+$#', $match['_route'], $route)) {
         $match['_route'] = $route[1];
         // now also check if we want to translate parameters:
         if (null !== $this->translator && isset($match['_translate'])) {
             foreach ((array) $match['_translate'] as $attribute) {
                 $match[$attribute] = $this->translator->translate($match['_route'], $match['_locale'], $attribute, $match[$attribute]);
             }
         }
     }
     return $match;
 }
Beispiel #18
0
 /**
  * set version of api doc
  */
 protected function setVersionApiDoc()
 {
     try {
         $request = $this->container->get('request');
         $paramsRoute = $this->router->match($request->getPathInfo());
         $this->versionApi = isset($paramsRoute['version']) ? $paramsRoute['version'] : null;
         if (isset($paramsRoute['view']) && is_null($this->versionApi)) {
             $this->versionApi = $paramsRoute['view'];
         }
         if (is_null($this->versionApi)) {
             $this->versionApi = 'v2';
         }
     } catch (\Exception $ex) {
         $this->versionApi = 'v2';
     }
 }
 /**
  * @param string $entityClass
  *
  * @return bool
  */
 protected function isResourceAccessible($entityClass)
 {
     $result = false;
     $uri = $this->getEntityResourceUri($entityClass);
     if ($uri) {
         $matchingContext = $this->router->getContext();
         $prevMethod = $matchingContext->getMethod();
         $matchingContext->setMethod('GET');
         try {
             $match = $this->router->match($uri);
             $matchingContext->setMethod($prevMethod);
             if ($this->isAcceptableMatch($match)) {
                 $result = true;
             }
         } catch (RoutingException $e) {
             // any exception from UrlMatcher means that the requested resource is not accessible
             $matchingContext->setMethod($prevMethod);
         }
     }
     return $result;
 }
 public function generateMetaTagKeyFromRelativePath($pathInfo, RouterInterface $router, $locale)
 {
     try {
         $info = $router->match($pathInfo);
     } catch (ResourceNotFoundException $e) {
         $info = false;
     } catch (MethodNotAllowedException $e) {
         $info = false;
     }
     if ($info !== false && strpos($info['_route'], RouteLoader::ROUTE_BEGIN) === 0) {
         // allready alias, get the base pathinfo
         $oldInfo = RouteLoader::getPathinfo($info['_route']);
         $oldRoute = $oldInfo['_route'];
         unset($oldInfo['_route']);
         $oldInfo[UrlGenerator::GENERATE_NORMAL_ROUTE] = true;
         $pathInfo = $router->generate($oldRoute, $oldInfo);
         $pathInfo = str_replace('/app_dev.php', '', $pathInfo);
         $pathInfo = preg_replace('!([^?]*)(\\?_locale=[^&]*)!', '$1', $pathInfo);
     }
     return $this->generateMetaTagKeyFromPathInfo($pathInfo, $locale);
 }
 public function getRoute($name)
 {
     $route = $this->router->match($name);
     $compiler = new RouteCompiler();
     return $compiler->compile($route);
 }
 /**
  * {@inheritdoc}
  */
 public function match($pathinfo)
 {
     return $this->router->match($pathinfo);
 }