浏览代码

[HttFoundation] Add File::getExtension() as \SplFileInfo::getExtension() was introduced in PHP 5.3.6

Victor Berchet 14 年之前
父节点
当前提交
136b80ae63

+ 12 - 0
src/Symfony/Component/HttpFoundation/File/File.php

@@ -486,6 +486,18 @@ class File extends \SplFileInfo
         return $guesser->guess($this->getPathname());
     }
 
+    /**
+     * Returns the extension of the file.
+     *
+     * \SplFileInfo::getExtension() is not available before PHP 5.3.6
+     *
+     * @return string The extension
+     */
+    public function getExtension()
+    {
+        return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
+    }
+
     /**
      * Moves the file to a new location.
      *

+ 6 - 0
tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php

@@ -115,6 +115,12 @@ class FileTest extends \PHPUnit_Framework_TestCase
         @rmdir($targetDir);
     }
 
+    public function testGetExtension()
+    {
+        $file = new File(__DIR__.'/Fixtures/test.gif');
+        $this->assertEquals('gif', $file->getExtension());
+    }
+
     protected function createMockGuesser($path, $mimeType)
     {
         $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');