|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace TYPO3\PharStreamWrapper\Resolver; |
|
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 TYPO3\PharStreamWrapper\Helper; |
|
16
|
|
|
use TYPO3\PharStreamWrapper\Phar\Reader; |
|
17
|
|
|
use TYPO3\PharStreamWrapper\Resolvable; |
|
18
|
|
|
|
|
19
|
|
|
class BaseNameResolver implements Resolvable |
|
20
|
|
|
{ |
|
21
|
|
|
const RESOLVE_REALPATH = 1; |
|
22
|
|
|
const RESOLVE_ALIAS = 2; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $aliasMap = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $path |
|
31
|
|
|
* @param int|null $flags |
|
32
|
|
|
* @return null|string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function resolveBaseName(string $path, int $flags = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$hasPharPrefix = Helper::hasPharPrefix($path); |
|
37
|
|
|
$flags = $flags ?? static::RESOLVE_REALPATH | static::RESOLVE_ALIAS; |
|
38
|
|
|
|
|
39
|
|
|
if ($hasPharPrefix && $flags & static::RESOLVE_ALIAS) { |
|
40
|
|
|
$baseNameFromAliasMap = $this->resolveBaseNameFromAliasMap($path); |
|
41
|
|
|
if ($baseNameFromAliasMap !== null) { |
|
42
|
|
|
return $baseNameFromAliasMap; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$baseName = Helper::determineBaseFile($path); |
|
47
|
|
|
if ($baseName !== null && $flags & static::RESOLVE_REALPATH) { |
|
48
|
|
|
$baseName = realpath($baseName); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $baseName; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $path |
|
56
|
|
|
* @return null|string |
|
57
|
|
|
*/ |
|
58
|
|
|
private function resolveBaseNameFromAliasMap(string $path) |
|
59
|
|
|
{ |
|
60
|
|
|
$normalizedBaseName = Helper::normalizePath($path); |
|
61
|
|
|
$possibleAlias = strstr($normalizedBaseName, '/', true); |
|
62
|
|
|
return $this->getAlias($possibleAlias ?: ''); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $path |
|
67
|
|
|
* @param int|null $flags |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function learnAlias(string $path, int $flags = null): bool |
|
71
|
|
|
{ |
|
72
|
|
|
$flags = $flags ?? static::RESOLVE_REALPATH; |
|
73
|
|
|
$baseName = Helper::determineBaseFile($path); |
|
74
|
|
|
if ($baseName === null) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($flags & static::RESOLVE_REALPATH) { |
|
79
|
|
|
$baseName = realpath($baseName); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$alias = (new Reader($baseName))->resolveContainer()->getAlias(); |
|
83
|
|
|
if ($alias === '' || $alias === $baseName) { |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->setAlias($alias, $baseName); |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $alias |
|
93
|
|
|
* @return null|string |
|
94
|
|
|
*/ |
|
95
|
|
|
private function getAlias(string $alias) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->aliasMap[$alias] ?? null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Updates alias map for current Phar archive. |
|
102
|
|
|
* |
|
103
|
|
|
* Since Phar aliases are intended to be used only inside Phar archives, |
|
104
|
|
|
* there is no further check for duplicates in our alias map. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $alias |
|
107
|
|
|
* @param string $basePath |
|
108
|
|
|
* @see https://secure.php.net/manual/en/phar.setalias.php |
|
109
|
|
|
* @see https://secure.php.net/manual/en/phar.mapphar.php |
|
110
|
|
|
*/ |
|
111
|
|
|
private function setAlias(string $alias, string $basePath) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->aliasMap[$alias] = $basePath; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|