| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | * |
| 47 | 47 | * This method uses the mime type as guessed by getMimeType() |
| 48 | 48 | * to guess the file extension. |
| 49 | 49 | * |
| 50 | - * @return string|null The guessed extension or null if it cannot be guessed | |
| 50 | + * @return string|null | |
| 51 | 51 | * |
| 52 | 52 | * @see MimeTypes |
| 53 | 53 | * @see getMimeType() |
| 54 | 54 | */ |
| @@ -53,8 +53,12 @@ | ||
| 53 | 53 | * @see getMimeType() |
| 54 | 54 | */ |
| 55 | 55 | public function guessExtension() |
| 56 | 56 | { |
| 57 | + if (!class_exists(MimeTypes::class)) { | |
| 58 | + throw new \LogicException('You cannot guess the extension as the Mime component is not installed. Try running "composer require symfony/mime".'); | |
| 59 | + } | |
| 60 | + | |
| 57 | 61 | return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null; |
| 58 | 62 | } |
| 59 | 63 | |
| 60 | 64 | /** |
| @@ -63,14 +67,18 @@ | ||
| 63 | 67 | * The mime type is guessed using a MimeTypeGuesserInterface instance, |
| 64 | 68 | * which uses finfo_file() then the "file" system binary, |
| 65 | 69 | * depending on which of those are available. |
| 66 | 70 | * |
| 67 | - * @return string|null The guessed mime type (e.g. "application/pdf") | |
| 71 | + * @return string|null | |
| 68 | 72 | * |
| 69 | 73 | * @see MimeTypes |
| 70 | 74 | */ |
| 71 | 75 | public function getMimeType() |
| 72 | 76 | { |
| 77 | + if (!class_exists(MimeTypes::class)) { | |
| 78 | + throw new \LogicException('You cannot guess the mime type as the Mime component is not installed. Try running "composer require symfony/mime".'); | |
| 79 | + } | |
| 80 | + | |
| 73 | 81 | return MimeTypes::getDefault()->guessMimeType($this->getPathname()); |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | 84 | /** |
| @@ -75,22 +83,22 @@ | ||
| 75 | 83 | |
| 76 | 84 | /** |
| 77 | 85 | * Moves the file to a new location. |
| 78 | 86 | * |
| 79 | - * @param string $directory The destination folder | |
| 80 | - * @param string $name The new file name | |
| 87 | + * @return self | |
| 81 | 88 | * |
| 82 | - * @return self A File object representing the new file | |
| 83 | - * | |
| 84 | 89 | * @throws FileException if the target file could not be created |
| 85 | 90 | */ |
| 86 | - public function move($directory, $name = null) | |
| 91 | + public function move(string $directory, ?string $name = null) | |
| 87 | 92 | { |
| 88 | 93 | $target = $this->getTargetFile($directory, $name); |
| 89 | 94 | |
| 90 | 95 | set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; }); |
| 91 | - $renamed = rename($this->getPathname(), $target); | |
| 92 | - restore_error_handler(); | |
| 96 | + try { | |
| 97 | + $renamed = rename($this->getPathname(), $target); | |
| 98 | + } finally { | |
| 99 | + restore_error_handler(); | |
| 100 | + } | |
| 93 | 101 | if (!$renamed) { |
| 94 | 102 | throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error))); |
| 95 | 103 | } |
| 96 | 104 | |
| @@ -98,12 +106,23 @@ | ||
| 98 | 106 | |
| 99 | 107 | return $target; |
| 100 | 108 | } |
| 101 | 109 | |
| 110 | + public function getContent(): string | |
| 111 | + { | |
| 112 | + $content = file_get_contents($this->getPathname()); | |
| 113 | + | |
| 114 | + if (false === $content) { | |
| 115 | + throw new FileException(sprintf('Could not get the content of the file "%s".', $this->getPathname())); | |
| 116 | + } | |
| 117 | + | |
| 118 | + return $content; | |
| 119 | + } | |
| 120 | + | |
| 102 | 121 | /** |
| 103 | 122 | * @return self |
| 104 | 123 | */ |
| 105 | - protected function getTargetFile($directory, $name = null) | |
| 124 | + protected function getTargetFile(string $directory, ?string $name = null) | |
| 106 | 125 | { |
| 107 | 126 | if (!is_dir($directory)) { |
| 108 | 127 | if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) { |
| 109 | 128 | throw new FileException(sprintf('Unable to create the "%s" directory.', $directory)); |
| @@ -119,13 +138,11 @@ | ||
| 119 | 138 | |
| 120 | 139 | /** |
| 121 | 140 | * Returns locale independent base name of the given path. |
| 122 | 141 | * |
| 123 | - * @param string $name The new file name | |
| 124 | - * | |
| 125 | 142 | * @return string |
| 126 | 143 | */ |
| 127 | - protected function getName($name) | |
| 144 | + protected function getName(string $name) | |
| 128 | 145 | { |
| 129 | 146 | $originalName = str_replace('\\', '/', $name); |
| 130 | 147 | $pos = strrpos($originalName, '/'); |
| 131 | 148 | $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |