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