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
|
|
|
if ($baseName !== null && $hasPharPrefix && $flags & static::RESOLVE_ALIAS) { |
51
|
|
|
$this->learnAlias($baseName); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $baseName; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $path |
59
|
|
|
* @return null|string |
60
|
|
|
*/ |
61
|
|
|
private function resolveBaseNameFromAliasMap(string $path) |
62
|
|
|
{ |
63
|
|
|
$normalizedBaseName = Helper::normalizePath($path); |
64
|
|
|
$possibleAlias = strstr($normalizedBaseName, '/', true); |
65
|
|
|
return $this->getAlias($possibleAlias ?: ''); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $basePath |
71
|
|
|
*/ |
72
|
|
|
private function learnAlias(string $basePath) |
73
|
|
|
{ |
74
|
|
|
$alias = (new Reader($basePath))->resolveContainer()->getAlias(); |
75
|
|
|
if ($alias !== '' && $alias !== $basePath) { |
76
|
|
|
$this->setAlias($alias, $basePath); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $alias |
82
|
|
|
* @return null|string |
83
|
|
|
*/ |
84
|
|
|
private function getAlias(string $alias) |
85
|
|
|
{ |
86
|
|
|
return $this->aliasMap[$alias] ?? null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Updates alias map for current Phar archive. |
91
|
|
|
* |
92
|
|
|
* Since Phar aliases are intended to be used only inside Phar archives, |
93
|
|
|
* there is no further check for duplicates in our alias map. |
94
|
|
|
* |
95
|
|
|
* @param string $alias |
96
|
|
|
* @param string $basePath |
97
|
|
|
* @see https://secure.php.net/manual/en/phar.setalias.php |
98
|
|
|
* @see https://secure.php.net/manual/en/phar.mapphar.php |
99
|
|
|
*/ |
100
|
|
|
private function setAlias(string $alias, string $basePath) |
101
|
|
|
{ |
102
|
|
|
$this->aliasMap[$alias] = $basePath; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|