Bläddra i källkod

[HttpFoundation] added static Request::fromGlobals()

The Request constructor no longer uses values from PHP's super globals. If you want a Request populated with these values you must use the new static method Request::fromGlobals().

Your front controllers (i.e. web/app.php, web/app_dev.php ...) will need to be updated:

    // old
    $kernel->handle(new Request())->send();

    // new
    $kernel->handle(Request::fromGlobals())->send();
Kris Wallsmith 14 år sedan
förälder
incheckning
224e66f77b

+ 1 - 1
src/Symfony/Bundle/DoctrineBundle/Tests/Request/ParamConverter/DoctrineConverterTest.php

@@ -156,7 +156,7 @@ class DoctrineConverterTest extends TestCase
 
     protected function buildRequestWithAttributes(array $attributes)
     {
-        return new Request(null, null, $attributes);
+        return new Request(array(), array(), $attributes);
     }
 }
 

+ 18 - 8
src/Symfony/Component/HttpFoundation/Request.php

@@ -70,6 +70,16 @@ class Request
 
     static protected $formats;
 
+    /**
+     * Creates a new request with values from PHP's super globals.
+     *
+     * @return Request A new request
+     */
+    static public function fromGlobals()
+    {
+        return new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
+    }
+
     /**
      * Constructor.
      *
@@ -80,7 +90,7 @@ class Request
      * @param array $files      The FILES parameters
      * @param array $server     The SERVER parameters
      */
-    public function __construct(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
+    public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
     {
         $this->initialize($query, $request, $attributes, $cookies, $files, $server);
     }
@@ -97,14 +107,14 @@ class Request
      * @param array $files      The FILES parameters
      * @param array $server     The SERVER parameters
      */
-    public function initialize(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
+    public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array())
     {
-        $this->request = new ParameterBag(null !== $request ? $request : $_POST);
-        $this->query = new ParameterBag(null !== $query ? $query : $_GET);
-        $this->attributes = new ParameterBag(null !== $attributes ? $attributes : array());
-        $this->cookies = new ParameterBag(null !== $cookies ? $cookies : $_COOKIE);
-        $this->files = new FileBag(null !== $files ? $files : $_FILES);
-        $this->server = new ServerBag(null !== $server ? $server : $_SERVER);
+        $this->request = new ParameterBag($request);
+        $this->query = new ParameterBag($query);
+        $this->attributes = new ParameterBag($attributes);
+        $this->cookies = new ParameterBag($cookies);
+        $this->files = new FileBag($files);
+        $this->server = new ServerBag($server);
         $this->headers = new HeaderBag($this->server->getHeaders());
 
         $this->content = null;

+ 39 - 26
tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php

@@ -34,13 +34,13 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $request->initialize(array('foo' => 'bar'));
         $this->assertEquals('bar', $request->query->get('foo'), '->initialize() takes an array of query parameters as its first argument');
 
-        $request->initialize(null, array('foo' => 'bar'));
+        $request->initialize(array(), array('foo' => 'bar'));
         $this->assertEquals('bar', $request->request->get('foo'), '->initialize() takes an array of request parameters as its second argument');
 
-        $request->initialize(null, null, array('foo' => 'bar'));
+        $request->initialize(array(), array(), array('foo' => 'bar'));
         $this->assertEquals('bar', $request->attributes->get('foo'), '->initialize() takes an array of attributes as its thrid argument');
 
-        $request->initialize(null, null, null, null, null, array('HTTP_FOO' => 'bar'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_FOO' => 'bar'));
         $this->assertEquals('bar', $request->headers->get('FOO'), '->initialize() takes an array of HTTP headers as its fourth argument');
     }
 
@@ -134,7 +134,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
 
         $request = new Request();
 
-        $request->initialize(null, null, null, null, null,$server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname:8080/index.php/path/info?query=string', $request->getUri(), '->getUri() with non default port');
 
@@ -143,7 +143,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port');
 
@@ -152,7 +152,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
 
@@ -174,7 +174,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['PHP_SELF'] = '/index.php';
         $server['SCRIPT_FILENAME'] = '/some/where/index.php';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
         $this->assertEquals('http://hostname:8080/path/info?query=string', $request->getUri(), '->getUri() with rewrite');
 
         // Use std port number
@@ -183,7 +183,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite and default port');
 
@@ -192,7 +192,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
    }
@@ -233,7 +233,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
 
         $request = new Request();
 
-        $request->initialize(null, null, null, null, null,$server);
+        $request->initialize(array(), array(), array(), array(), array(),$server);
 
         $this->assertEquals('http://hostname:8080/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with non default port');
 
@@ -242,7 +242,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port');
 
@@ -251,7 +251,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
 
@@ -273,7 +273,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['PHP_SELF'] = '/index.php';
         $server['SCRIPT_FILENAME'] = '/some/where/index.php';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
         $this->assertEquals('http://hostname:8080/some/path', $request->getUriForPath('/some/path'), '->getUri() with rewrite');
 
         // Use std port number
@@ -282,7 +282,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite and default port');
 
@@ -291,7 +291,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $server['SERVER_NAME'] = 'hostname';
         $server['SERVER_PORT'] = '80';
 
-        $request->initialize(null, null, null, null, null, $server);
+        $request->initialize(array(), array(), array(), array(), array(), $server);
 
         $this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
     }
@@ -335,36 +335,36 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $request->initialize(array('foo' => 'bar'));
         $this->assertEquals('', $request->getHost(), '->getHost() return empty string if not initialized');
 
-        $request->initialize(null, null, null, null, null, array('HTTP_HOST' => 'www.exemple.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com'));
         $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header');
 
         // Host header with port number.
-        $request->initialize(null, null, null, null, null, array('HTTP_HOST' => 'www.exemple.com:8080'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com:8080'));
         $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header with port number');
 
         // Server values.
-        $request->initialize(null, null, null, null, null, array('SERVER_NAME' => 'www.exemple.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com'));
         $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name');
 
         // X_FORWARDED_HOST.
-        $request->initialize(null, null, null, null, null, array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com'));
         $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from X_FORWARDED_HOST');
 
         // X_FORWARDED_HOST
-        $request->initialize(null, null, null, null, null, array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com'));
         $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST use last value');
 
         // X_FORWARDED_HOST with port number
-        $request->initialize(null, null, null, null, null, array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com:8080'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com:8080'));
         $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST with port number');
 
-        $request->initialize(null, null, null, null, null, array('HTTP_HOST' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
         $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over Host');
 
-        $request->initialize(null, null, null, null, null, array('SERVER_NAME' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
         $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over SERVER_NAME ');
 
-        $request->initialize(null, null, null, null, null, array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com'));
+        $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com'));
         $this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME ');
     }
 
@@ -428,8 +428,21 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    protected function createTempFile()
+    public function testFromGlobals()
     {
-        return tempnam(sys_get_temp_dir(), 'FormTest');
+        $_GET['foo1']    = 'bar1';
+        $_POST['foo2']   = 'bar2';
+        $_COOKIE['foo3'] = 'bar3';
+        $_FILES['foo4']  = array('bar4');
+        $_SERVER['foo5'] = 'bar5';
+
+        $request = Request::fromGlobals();
+        $this->assertEquals('bar1', $request->query->get('foo1'), '::fromGlobals() uses values from $_GET');
+        $this->assertEquals('bar2', $request->request->get('foo2'), '::fromGlobals() uses values from $_POST');
+        $this->assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE');
+        $this->assertEquals(array('bar4'), $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES');
+        $this->assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER');
+
+        unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
     }
 }