فهرست منبع

merged branch vicb/uploaded-file/max-size (PR #1574)

Commits
-------

6786e81 [HttpFoundation] code factorization in UploadedFile

Discussion
----------

[HttpFoundation] code factorization in UploadedFile

As both #1542 and #1544 have been merged.
Fabien Potencier 14 سال پیش
والد
کامیت
9b3c2ca3d3

+ 25 - 0
src/Symfony/Component/HttpFoundation/File/UploadedFile.php

@@ -181,4 +181,29 @@ class UploadedFile extends File
 
         throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
     }
+
+    /**
+     * Returns the maximum size of an uploaded file as configured in php.ini
+     *
+     * @return type The maximum size of an uploaded file in bytes
+     */
+    public static function getMaxFilesize()
+    {
+        $max = trim(ini_get('upload_max_filesize'));
+
+        if ('' === $max) {
+            return PHP_INT_MAX;
+        }
+
+        switch (strtolower(substr($max, -1))) {
+            case 'g':
+                $max *= 1024;
+            case 'm':
+                $max *= 1024;
+            case 'k':
+                $max *= 1024;
+        }
+
+        return (integer) $max;
+    }
 }

+ 1 - 26
src/Symfony/Component/HttpKernel/Client.php

@@ -125,7 +125,7 @@ EOF;
             if (is_array($value)) {
                 $filtered[$key] = $this->filterFiles($value);
             } elseif ($value instanceof UploadedFile) {
-                if ($value->isValid() && $value->getSize() > static::getMaxUploadFilesize()) {
+                if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
                     $filtered[$key] = new UploadedFile(
                         '',
                         $value->getClientOriginalName(),
@@ -172,29 +172,4 @@ EOF;
 
         return new DomResponse($response->getContent(), $response->getStatusCode(), $headers);
     }
-
-    /**
-     * Returns the maximum size of an uploaded file
-     *
-     * @return type The maximum size of an uploaded file in bytes
-     */
-    static protected function getMaxUploadFilesize()
-    {
-        $max = trim(ini_get('upload_max_filesize'));
-
-        if ('' === $max) {
-            return PHP_INT_MAX;
-        }
-
-        switch (strtolower(substr($max, -1))) {
-            case 'g':
-                $max *= 1024;
-            case 'm':
-                $max *= 1024;
-            case 'k':
-                $max *= 1024;
-        }
-
-        return (integer) $max;
-    }
 }

+ 1 - 9
src/Symfony/Component/Validator/Constraints/FileValidator.php

@@ -29,15 +29,7 @@ class FileValidator extends ConstraintValidator
         if ($value instanceof UploadedFile && !$value->isValid()) {
             switch ($value->getError()) {
                 case UPLOAD_ERR_INI_SIZE:
-                    $maxSize = trim(ini_get('upload_max_filesize'));
-                    switch (strtolower(substr($maxSize, -1))) {
-                        case 'g':
-                            $maxSize *= 1024;
-                        case 'm':
-                            $maxSize *= 1024;
-                        case 'k':
-                            $maxSize *= 1024;
-                    }
+                    $maxSize = UploadedFile::getMaxFilesize();
                     $maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
                     $this->setMessage($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' bytes'));
 

+ 9 - 11
tests/Symfony/Tests/Component/HttpKernel/ClientTest.php

@@ -110,24 +110,22 @@ class ClientTest extends \PHPUnit_Framework_TestCase
     {
         $source = tempnam(sys_get_temp_dir(), 'source');
 
-        file_put_contents($source, 'foo');
-
         $kernel = new TestHttpKernel();
+        $client = new Client($kernel);
 
-        $client = $this
-            ->getMockBuilder('Symfony\Component\HttpKernel\Client')
-            ->setConstructorArgs(array($kernel))
-            ->setMethods(array('getMaxUploadFilesize'))
+        $file = $this
+            ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
+            ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK))
+            ->setMethods(array('getSize'))
             ->getMock()
         ;
 
-        $client
-            ->staticExpects($this->once())
-            ->method('getMaxUploadFilesize')
-            ->will($this->returnValue(1))
+        $file->expects($this->once())
+            ->method('getSize')
+            ->will($this->returnValue(INF))
         ;
 
-        $client->request('POST', '/', array(), array(new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK)));
+        $client->request('POST', '/', array(), array($file));
 
         $files = $kernel->request->files->all();