Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
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 <bschussek@gmail.com>
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(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, 0666 & ~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)) {
118
            if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
119
                throw new FileException(sprintf('Unable to create the "%s" directory.', $directory));
120
            }
121
        } elseif (!is_writable($directory)) {
122
            throw new FileException(sprintf('Unable to write in the "%s" directory.', $directory));
123
        }
124
 
125
        $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
126
 
127
        return new self($target, false);
128
    }
129
 
130
    /**
131
     * Returns locale independent base name of the given path.
132
     */
133
    protected function getName(string $name): string
134
    {
135
        $originalName = str_replace('\\', '/', $name);
136
        $pos = strrpos($originalName, '/');
137
        $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
138
 
139
        return $originalName;
140
    }
141
}