Quellcode durchsuchen

[HttpFoundation] added support for attributes in RequestMatcher

Fabien Potencier vor 14 Jahren
Ursprung
Commit
caa9d82746
1 geänderte Dateien mit 18 neuen und 0 gelöschten Zeilen
  1. 18 0
      src/Symfony/Component/HttpFoundation/RequestMatcher.php

+ 18 - 0
src/Symfony/Component/HttpFoundation/RequestMatcher.php

@@ -22,6 +22,7 @@ class RequestMatcher implements RequestMatcherInterface
     protected $host;
     protected $methods;
     protected $ip;
+    protected $attributes = array();
 
     /**
      * Adds a check for the URL host name.
@@ -63,6 +64,17 @@ class RequestMatcher implements RequestMatcherInterface
         $this->methods = array_map(function ($m) { return strtolower($m); }, is_array($method) ? $method : array($method));
     }
 
+    /**
+     * Adds a check for request attribute.
+     *
+     * @param string $key    The request attribute name
+     * @param string $regexp A Regexp
+     */
+    public function matchAttribute($key, $regexp)
+    {
+        $this->attributes[$key] = $regexp;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -72,6 +84,12 @@ class RequestMatcher implements RequestMatcherInterface
             return false;
         }
 
+        foreach ($this->attributes as $key => $pattern) {
+            if (!preg_match($pattern, $request->attributes->get($key))) {
+                return false;
+            }
+        }
+
         if (null !== $this->path && !preg_match($this->path, $request->getPathInfo())) {
             return false;
         }