Updated Symfony HttpFoundation to 2.1.6.

This commit is contained in:
Taylor Otwell
2013-01-06 13:58:32 -06:00
parent f754e1fa55
commit cb567c5e4d
55 changed files with 1758 additions and 805 deletions

View File

@@ -14,14 +14,14 @@ namespace Symfony\Component\HttpFoundation\File\Exception;
/**
* Thrown when the access on a file was denied.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class AccessDeniedException extends FileException
{
/**
* Constructor.
*
* @param string $path The path to the accessed file
* @param string $path The path to the accessed file
*/
public function __construct($path)
{

View File

@@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\File\Exception;
/**
* Thrown when an error occurred in the component File
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FileException extends \RuntimeException
{

View File

@@ -14,14 +14,14 @@ namespace Symfony\Component\HttpFoundation\File\Exception;
/**
* Thrown when a file was not found
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FileNotFoundException extends FileException
{
/**
* Constructor.
*
* @param string $path The path to the file that was not found
* @param string $path The path to the file that was not found
*/
public function __construct($path)
{

View File

@@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\File\Exception;
/**
* Thrown when an error occurred during file upload
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UploadException extends FileException
{

View File

@@ -19,7 +19,7 @@ use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
/**
* A file in the file system.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
@@ -106,6 +106,20 @@ class File extends \SplFileInfo
* @api
*/
public function move($directory, $name = null)
{
$target = $this->getTargetFile($directory, $name);
if (!@rename($this->getPathname(), $target)) {
$error = error_get_last();
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
}
@chmod($target, 0666 & ~umask());
return $target;
}
protected function getTargetFile($directory, $name = null)
{
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true)) {
@@ -115,15 +129,24 @@ class File extends \SplFileInfo
throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
}
$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : basename($name));
$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
if (!@rename($this->getPathname(), $target)) {
$error = error_get_last();
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
}
return new File($target, false);
}
chmod($target, 0666);
/**
* Returns locale independent base name of the given path.
*
* @param string $name The new file name
*
* @return string containing
*/
protected function getName($name)
{
$originalName = str_replace('\\', '/', $name);
$pos = strrpos($originalName, '/');
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
return new File($target);
return $originalName;
}
}

View File

@@ -30,12 +30,14 @@ class ExtensionGuesser implements ExtensionGuesserInterface
{
/**
* The singleton instance
*
* @var ExtensionGuesser
*/
static private $instance = null;
private static $instance = null;
/**
* All registered ExtensionGuesserInterface instances
*
* @var array
*/
protected $guessers = array();
@@ -45,7 +47,7 @@ class ExtensionGuesser implements ExtensionGuesserInterface
*
* @return ExtensionGuesser
*/
static public function getInstance()
public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
@@ -82,8 +84,8 @@ class ExtensionGuesser implements ExtensionGuesserInterface
* returns a value that is not NULL, this method terminates and returns the
* value.
*
* @param string $mimeType The mime type
* @return string The guessed extension or NULL, if none could be guessed
* @param string $mimeType The mime type
* @return string The guessed extension or NULL, if none could be guessed
*/
public function guess($mimeType)
{

View File

@@ -19,8 +19,8 @@ interface ExtensionGuesserInterface
/**
* Makes a best guess for a file extension, given a mime type
*
* @param string $mimeType The mime type
* @return string The guessed extension or NULL, if none could be guessed
* @param string $mimeType The mime type
* @return string The guessed extension or NULL, if none could be guessed
*/
function guess($mimeType);
public function guess($mimeType);
}

View File

@@ -17,7 +17,7 @@ use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
/**
* Guesses the mime type with the binary "file" (only available on *nix)
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
{
@@ -43,15 +43,13 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
*
* @return Boolean
*/
static public function isSupported()
public static function isSupported()
{
return !defined('PHP_WINDOWS_VERSION_BUILD');
}
/**
* Guesses the mime type of the file with the given path
*
* @see MimeTypeGuesserInterface::guess()
* {@inheritdoc}
*/
public function guess($path)
{

View File

@@ -17,7 +17,7 @@ use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
/**
* Guesses the mime type using the PECL extension FileInfo
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
{
@@ -26,15 +26,13 @@ class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
*
* @return Boolean
*/
static public function isSupported()
public static function isSupported()
{
return function_exists('finfo_open');
}
/**
* Guesses the mime type of the file with the given path
*
* @see MimeTypeGuesserInterface::guess()
* {@inheritdoc}
*/
public function guess($path)
{

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\File\Mimetype;
namespace Symfony\Component\HttpFoundation\File\MimeType;
/**
* Provides a best-guess mapping of mime type to file extension.
@@ -542,6 +542,7 @@ class MimeTypeExtensionGuesser implements ExtensionGuesserInterface
'application/x-pkcs7-certificates' => 'p7b',
'application/x-pkcs7-certreqresp' => 'p7r',
'application/x-rar-compressed' => 'rar',
'application/x-rar' => 'rar',
'application/x-sh' => 'sh',
'application/x-shar' => 'shar',
'application/x-shockwave-flash' => 'swf',
@@ -730,11 +731,7 @@ class MimeTypeExtensionGuesser implements ExtensionGuesserInterface
);
/**
* Returns the extension based on the mime type.
*
* If the mime type is unknown, returns null.
*
* @return string|null The guessed extension or null if it cannot be guessed
* {@inheritdoc}
*/
public function guess($mimeType)
{

View File

@@ -11,6 +11,7 @@
namespace Symfony\Component\HttpFoundation\File\MimeType;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
@@ -28,18 +29,20 @@ use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
*
* The last registered guesser is preferred over previously registered ones.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class MimeTypeGuesser implements MimeTypeGuesserInterface
{
/**
* The singleton instance
*
* @var MimeTypeGuesser
*/
static private $instance = null;
private static $instance = null;
/**
* All registered MimeTypeGuesserInterface instances
*
* @var array
*/
protected $guessers = array();
@@ -49,7 +52,7 @@ class MimeTypeGuesser implements MimeTypeGuesserInterface
*
* @return MimeTypeGuesser
*/
static public function getInstance()
public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
@@ -92,7 +95,7 @@ class MimeTypeGuesser implements MimeTypeGuesserInterface
* returns a value that is not NULL, this method terminates and returns the
* value.
*
* @param string $path The path to the file
* @param string $path The path to the file
*
* @return string The mime type or NULL, if none could be guessed
*

View File

@@ -11,22 +11,25 @@
namespace Symfony\Component\HttpFoundation\File\MimeType;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
/**
* Guesses the mime type of a file
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface MimeTypeGuesserInterface
{
/**
* Guesses the mime type of the file with the given path.
*
* @param string $path The path to the file
* @param string $path The path to the file
*
* @return string The mime type or NULL, if none could be guessed
*
* @throws FileNotFoundException If the file does not exist
* @throws AccessDeniedException If the file could not be read
*/
function guess($path);
public function guess($path);
}

View File

@@ -17,7 +17,7 @@ use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
/**
* A file uploaded through a form.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org>
* @author Fabien Potencier <fabien@symfony.com>
*
@@ -94,7 +94,7 @@ class UploadedFile extends File
throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
}
$this->originalName = basename($originalName);
$this->originalName = $this->getName($originalName);
$this->mimeType = $mimeType ?: 'application/octet-stream';
$this->size = $size;
$this->error = $error ?: UPLOAD_ERR_OK;
@@ -166,7 +166,7 @@ class UploadedFile extends File
/**
* Returns whether the file was uploaded successfully.
*
* @return Boolean True if no error occurred during uploading
* @return Boolean True if no error occurred during uploading
*
* @api
*/
@@ -189,8 +189,21 @@ class UploadedFile extends File
*/
public function move($directory, $name = null)
{
if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) {
return parent::move($directory, $name);
if ($this->isValid()) {
if ($this->test) {
return parent::move($directory, $name);
} elseif (is_uploaded_file($this->getPathname())) {
$target = $this->getTargetFile($directory, $name);
if (!@move_uploaded_file($this->getPathname(), $target)) {
$error = error_get_last();
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
}
@chmod($target, 0666 & ~umask());
return $target;
}
}
throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
@@ -199,9 +212,9 @@ class UploadedFile extends File
/**
* Returns the maximum size of an uploaded file as configured in php.ini
*
* @return type The maximum size of an uploaded file in bytes
* @return int The maximum size of an uploaded file in bytes
*/
static public function getMaxFilesize()
public static function getMaxFilesize()
{
$max = trim(ini_get('upload_max_filesize'));