1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace TYPO3\PharStreamWrapper; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under the terms |
9
|
|
|
* of the MIT License (MIT). For the full copyright and license information, |
10
|
|
|
* please read the LICENSE file that was distributed with this source code. |
11
|
|
|
* |
12
|
|
|
* The TYPO3 project - inspiring people to share! |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
16
|
|
|
use TYPO3\PharStreamWrapper\Resolver\PharInvocationResolver; |
17
|
|
|
use TYPO3\PharStreamWrapper\Resolver\PharInvocation; |
18
|
|
|
use TYPO3\PharStreamWrapper\Resolver\PharInvocationCollection; |
19
|
|
|
|
20
|
|
|
class Manager |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var self |
24
|
|
|
*/ |
25
|
|
|
private static $instance; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Behavior |
29
|
|
|
*/ |
30
|
|
|
private $behavior; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Resolvable |
34
|
|
|
*/ |
35
|
|
|
private $resolver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Collectable |
39
|
|
|
*/ |
40
|
|
|
private $collection; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MimeTypeGuesserInterface |
44
|
|
|
*/ |
45
|
|
|
private $mimeTypeGuesser; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Behavior $behaviour |
49
|
|
|
* @param Resolvable $resolver |
50
|
|
|
* @param Collectable $collection |
51
|
|
|
* @param MimeTypeGuesserInterface $mimeTypeGuesser |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
|
|
public static function initialize( |
55
|
|
|
Behavior $behaviour, |
56
|
|
|
Resolvable $resolver = null, |
57
|
|
|
Collectable $collection = null, |
58
|
|
|
MimeTypeGuesserInterface $mimeTypeGuesser = null |
59
|
|
|
): self { |
60
|
|
|
if (self::$instance === null) { |
61
|
|
|
self::$instance = new self($behaviour, $resolver, $collection, $mimeTypeGuesser); |
62
|
|
|
return self::$instance; |
63
|
|
|
} |
64
|
|
|
throw new \LogicException( |
65
|
|
|
'Manager can only be initialized once', |
66
|
|
|
1535189871 |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
|
|
public static function instance(): self |
74
|
|
|
{ |
75
|
|
|
if (self::$instance !== null) { |
76
|
|
|
return self::$instance; |
77
|
|
|
} |
78
|
|
|
throw new \LogicException( |
79
|
|
|
'Manager needs to be initialized first', |
80
|
|
|
1535189872 |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public static function destroy(): bool |
88
|
|
|
{ |
89
|
|
|
if (self::$instance === null) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
self::$instance = null; |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Behavior $behaviour |
98
|
|
|
* @param Resolvable $resolver |
99
|
|
|
* @param Collectable $collection |
100
|
|
|
*/ |
101
|
|
|
private function __construct( |
102
|
|
|
Behavior $behaviour, |
103
|
|
|
Resolvable $resolver = null, |
104
|
|
|
Collectable $collection = null, |
105
|
|
|
MimeTypeGuesserInterface $mimeTypeGuesser = null |
106
|
|
|
) { |
107
|
|
|
$this->collection = $collection ?? new PharInvocationCollection(); |
108
|
|
|
$this->resolver = $resolver ?? new PharInvocationResolver(); |
109
|
|
|
$this->behavior = $behaviour; |
110
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesser; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $path |
115
|
|
|
* @param string $command |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function assert(string $path, string $command): bool |
119
|
|
|
{ |
120
|
|
|
return $this->behavior->assert($path, $command); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $path |
125
|
|
|
* @param null|int $flags |
126
|
|
|
* @return PharInvocation|null |
127
|
|
|
*/ |
128
|
|
|
public function resolve(string $path, int $flags = null) |
129
|
|
|
{ |
130
|
|
|
return $this->resolver->resolve($path, $flags); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Collectable |
135
|
|
|
*/ |
136
|
|
|
public function getCollection(): Collectable |
137
|
|
|
{ |
138
|
|
|
return $this->collection; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $fileName |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function guessMimeType(string $fileName): string |
146
|
|
|
{ |
147
|
|
|
if ($this->mimeTypeGuesser) { |
148
|
|
|
return $this->mimeTypeGuesser->guess($fileName); |
149
|
|
|
} |
150
|
|
|
elseif (class_exists('\finfo')) { |
151
|
|
|
$fileInfo = new \finfo(); |
152
|
|
|
return $fileInfo->file($fileName, FILEINFO_MIME_TYPE); |
153
|
|
|
} |
154
|
|
|
throw new \RuntimeException( |
155
|
|
|
'Unable to guess the mime type of a file. Install PHP\'s fileinfo extension or set mime type guesser.', |
156
|
|
|
1535189873 |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|