Beispiel #1
0
 /**
  * @param array $methods
  *
  * @throws Exception
  * @see HttpRequestMethod
  */
 public function setMethods(array $methods)
 {
     foreach ($methods as $method) {
         if (!HttpRequestMethod::isValid($method)) {
             throw new Exception(s('Invalid request method %s. Valid methods are %s.', $method, implode(', ', HttpRequestMethod::getMethods())));
         }
     }
     $this->methods = $methods;
 }
Beispiel #2
0
 public function test_is_valid()
 {
     foreach (HttpRequestMethod::getMethods() as $method) {
         $this->assertTrue(HttpRequestMethod::isValid($method));
     }
     foreach (['foo', 'bar', 'baz'] as $method) {
         $this->assertFalse(HttpRequestMethod::isValid($method));
     }
 }
Beispiel #3
0
 /**
  * @param array $server
  *
  * @return mixed
  */
 public function parseMethod(array $server)
 {
     $method = array_get($server, 'REQUEST_METHOD');
     if (array_has($server, '_method')) {
         $_method = strtoupper(array_get($server, '_method'));
         if (array_contains(HttpRequestMethod::getMethods(), $_method)) {
             return $_method;
         }
     }
     return $method;
 }
 /**
  * @param array $definition
  *
  * @return array
  */
 protected function gatherRouteFacts(array $definition)
 {
     $route = array_get($definition, 'route');
     $method = null;
     $path = null;
     $action = null;
     // routes is expected to be in format like:
     // GET POST /some/path someAction
     if (is_string($route)) {
         $parts = preg_split('/\\s+/', $route);
         $parts = array_map('trim', $parts);
         foreach ($parts as $part) {
             if (HttpRequestMethod::isValid($part) || $part === 'ANY') {
                 if ($method === null) {
                     $method = $part;
                 } else {
                     if (!is_array($method)) {
                         $method = [$method, $part];
                     } else {
                         $method[] = $part;
                     }
                 }
             } else {
                 if ($path === null) {
                     $path = $part;
                 } else {
                     if ($action === null) {
                         $action = $part;
                     }
                 }
             }
         }
     }
     if ($method === null) {
         $method = array_get($definition, 'method');
     }
     if ($method === 'ANY' || is_array($method) && array_contains($method, 'ANY')) {
         $method = HttpRequestMethod::getMethods();
     }
     if ($path === null) {
         $path = array_get($definition, 'path');
     }
     if ($action === null) {
         $action = array_get($definition, 'action');
     }
     return [$method, $path, $action];
 }
 public function test_it_detect_any_route_inside_regular_definition()
 {
     $definition = ['method' => ['ANY'], 'path' => '/foo/bar', 'action' => 'yolo'];
     $configurator = $this->createConfigurator();
     list($method, $path, $action) = $configurator->gatherRouteFacts($definition);
     $this->assertEquals(HttpRequestMethod::getMethods(), $method);
     $this->assertEquals('/foo/bar', $path);
     $this->assertEquals('yolo', $action);
 }