Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/File/File.php

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