annotate vendor/symfony/http-foundation/File/File.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpFoundation\File;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
Chris@0 15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
Chris@17 16 use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
Chris@0 17 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * A file in the file system.
Chris@0 21 *
Chris@0 22 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 23 */
Chris@0 24 class File extends \SplFileInfo
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * Constructs a new file from the given path.
Chris@0 28 *
Chris@0 29 * @param string $path The path to the file
Chris@0 30 * @param bool $checkPath Whether to check the path or not
Chris@0 31 *
Chris@0 32 * @throws FileNotFoundException If the given path is not a file
Chris@0 33 */
Chris@0 34 public function __construct($path, $checkPath = true)
Chris@0 35 {
Chris@0 36 if ($checkPath && !is_file($path)) {
Chris@0 37 throw new FileNotFoundException($path);
Chris@0 38 }
Chris@0 39
Chris@0 40 parent::__construct($path);
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Returns the extension based on the mime type.
Chris@0 45 *
Chris@0 46 * If the mime type is unknown, returns null.
Chris@0 47 *
Chris@0 48 * This method uses the mime type as guessed by getMimeType()
Chris@0 49 * to guess the file extension.
Chris@0 50 *
Chris@0 51 * @return string|null The guessed extension or null if it cannot be guessed
Chris@0 52 *
Chris@0 53 * @see ExtensionGuesser
Chris@0 54 * @see getMimeType()
Chris@0 55 */
Chris@0 56 public function guessExtension()
Chris@0 57 {
Chris@0 58 $type = $this->getMimeType();
Chris@0 59 $guesser = ExtensionGuesser::getInstance();
Chris@0 60
Chris@0 61 return $guesser->guess($type);
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Returns the mime type of the file.
Chris@0 66 *
Chris@0 67 * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(),
Chris@0 68 * mime_content_type() and the system binary "file" (in this order), depending on
Chris@0 69 * which of those are available.
Chris@0 70 *
Chris@0 71 * @return string|null The guessed mime type (e.g. "application/pdf")
Chris@0 72 *
Chris@0 73 * @see MimeTypeGuesser
Chris@0 74 */
Chris@0 75 public function getMimeType()
Chris@0 76 {
Chris@0 77 $guesser = MimeTypeGuesser::getInstance();
Chris@0 78
Chris@0 79 return $guesser->guess($this->getPathname());
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Moves the file to a new location.
Chris@0 84 *
Chris@0 85 * @param string $directory The destination folder
Chris@0 86 * @param string $name The new file name
Chris@0 87 *
Chris@0 88 * @return self A File object representing the new file
Chris@0 89 *
Chris@0 90 * @throws FileException if the target file could not be created
Chris@0 91 */
Chris@0 92 public function move($directory, $name = null)
Chris@0 93 {
Chris@0 94 $target = $this->getTargetFile($directory, $name);
Chris@0 95
Chris@16 96 set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
Chris@16 97 $renamed = rename($this->getPathname(), $target);
Chris@16 98 restore_error_handler();
Chris@16 99 if (!$renamed) {
Chris@16 100 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
Chris@0 101 }
Chris@0 102
Chris@0 103 @chmod($target, 0666 & ~umask());
Chris@0 104
Chris@0 105 return $target;
Chris@0 106 }
Chris@0 107
Chris@0 108 protected function getTargetFile($directory, $name = null)
Chris@0 109 {
Chris@0 110 if (!is_dir($directory)) {
Chris@0 111 if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
Chris@0 112 throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
Chris@0 113 }
Chris@0 114 } elseif (!is_writable($directory)) {
Chris@0 115 throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
Chris@0 116 }
Chris@0 117
Chris@17 118 $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
Chris@0 119
Chris@0 120 return new self($target, false);
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Returns locale independent base name of the given path.
Chris@0 125 *
Chris@0 126 * @param string $name The new file name
Chris@0 127 *
Chris@0 128 * @return string containing
Chris@0 129 */
Chris@0 130 protected function getName($name)
Chris@0 131 {
Chris@0 132 $originalName = str_replace('\\', '/', $name);
Chris@0 133 $pos = strrpos($originalName, '/');
Chris@0 134 $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
Chris@0 135
Chris@0 136 return $originalName;
Chris@0 137 }
Chris@0 138 }