Esempio n. 1
0
 public function testInstance()
 {
     $rule = new RedirectRule([1, Redirect::TYPE_EXACT, Redirect::TARGET_TYPE_PATH_URL, '/from/url', '/to/url', null, null, 301, null, Carbon::today(), Carbon::tomorrow()]);
     self::assertEquals(1, $rule->getId());
     self::assertEquals(Redirect::TYPE_EXACT, $rule->getMatchType());
     self::assertEquals(Redirect::TARGET_TYPE_PATH_URL, $rule->getTargetType());
     self::assertEquals('/from/url', $rule->getFromUrl());
     self::assertEquals('/to/url', $rule->getToUrl());
     self::assertEquals(301, $rule->getStatusCode());
     self::assertEquals(Carbon::today(), $rule->getFromDate());
     self::assertEquals(Carbon::tomorrow(), $rule->getToDate());
 }
Esempio n. 2
0
 /**
  * Perform a placeholder URL match
  *
  * @param RedirectRule $rule
  * @param string $url
  * @return RedirectRule|bool
  */
 private function matchPlaceholders(RedirectRule $rule, $url)
 {
     $route = new Route($rule->getFromUrl());
     foreach ($rule->getRequirements() as $requirement) {
         try {
             $route->setRequirement(str_replace(['{', '}'], '', $requirement['placeholder']), $requirement['requirement']);
         } catch (\InvalidArgumentException $e) {
             // Catch empty requirement / placeholder
         }
     }
     $routeCollection = new RouteCollection();
     $routeCollection->add($rule->getId(), $route);
     try {
         $matcher = new UrlMatcher($routeCollection, new RequestContext('/'));
         $match = $matcher->match($url);
         $items = array_except($match, '_route');
         foreach ($items as $key => $value) {
             $placeholder = '{' . $key . '}';
             $replacement = $this->findReplacementForPlaceholder($rule, $placeholder);
             $items[$placeholder] = $replacement === null ? $value : $replacement;
             unset($items[$key]);
         }
         $rule->setPlaceholderMatches($items);
     } catch (\Exception $e) {
         return false;
     }
     return $rule;
 }