From 136b80ae63a16198071030c2147f22d3b6fffe76 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 14 Jun 2011 18:18:43 +0200 Subject: [PATCH] [HttFoundation] Add File::getExtension() as \SplFileInfo::getExtension() was introduced in PHP 5.3.6 --- src/Symfony/Component/HttpFoundation/File/File.php | 12 ++++++++++++ .../Tests/Component/HttpFoundation/File/FileTest.php | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/File/File.php b/src/Symfony/Component/HttpFoundation/File/File.php index bb6fda2a50..a6fa665331 100644 --- a/src/Symfony/Component/HttpFoundation/File/File.php +++ b/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. * diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php index 0a5fe85548..8411020528 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php +++ b/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');