PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / http-foundation / File / File.php
matomo / app / vendor / prefixed / symfony / http-foundation / File Last commit date
Exception 2 years ago File.php 1 year ago Stream.php 1 year ago UploadedFile.php 1 year ago
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