|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManager\Inflector; |
|
6
|
|
|
|
|
7
|
|
|
use ProxyManager\Inflector\Util\ParameterHasher; |
|
8
|
|
|
use function ltrim; |
|
9
|
|
|
use function strlen; |
|
10
|
|
|
use function strrpos; |
|
11
|
|
|
use function substr; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritDoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ClassNameInflector implements ClassNameInflectorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected string $proxyNamespace; |
|
|
|
|
|
|
19
|
|
|
/** @var int @TODO annotation still needed for phpstan to understand this */ |
|
20
|
|
|
private int $proxyMarkerLength; |
|
21
|
|
|
private string $proxyMarker; |
|
22
|
|
|
private ParameterHasher $parameterHasher; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $proxyNamespace) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->proxyNamespace = $proxyNamespace; |
|
27
|
|
|
$this->proxyMarker = '\\' . self::PROXY_MARKER . '\\'; |
|
28
|
|
|
$this->proxyMarkerLength = strlen($this->proxyMarker); |
|
29
|
|
|
$this->parameterHasher = new ParameterHasher(); |
|
30
|
|
|
} |
|
31
|
13 |
|
|
|
32
|
|
|
/** |
|
33
|
13 |
|
* {@inheritDoc} |
|
34
|
13 |
|
*/ |
|
35
|
13 |
|
public function getUserClassName(string $className) : string |
|
36
|
13 |
|
{ |
|
37
|
13 |
|
$className = ltrim($className, '\\'); |
|
38
|
|
|
$position = strrpos($className, $this->proxyMarker); |
|
39
|
|
|
|
|
40
|
|
|
if ($position === false) { |
|
41
|
|
|
return $className; |
|
42
|
13 |
|
} |
|
43
|
|
|
|
|
44
|
13 |
|
return substr( |
|
45
|
13 |
|
$className, |
|
46
|
|
|
$this->proxyMarkerLength + $position, |
|
47
|
13 |
|
strrpos($className, '\\') - ($position + $this->proxyMarkerLength) |
|
48
|
13 |
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
/** |
|
52
|
2 |
|
* {@inheritDoc} |
|
53
|
2 |
|
*/ |
|
54
|
2 |
|
public function getProxyClassName(string $className, array $options = []) : string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->proxyNamespace |
|
57
|
|
|
. $this->proxyMarker |
|
58
|
|
|
. $this->getUserClassName($className) |
|
59
|
|
|
. '\\Generated' . $this->parameterHasher->hashParameters($options); |
|
60
|
|
|
} |
|
61
|
13 |
|
|
|
62
|
|
|
/** |
|
63
|
13 |
|
* {@inheritDoc} |
|
64
|
13 |
|
*/ |
|
65
|
13 |
|
public function isProxyClassName(string $className) : bool |
|
66
|
13 |
|
{ |
|
67
|
|
|
return strrpos($className, $this->proxyMarker) !== false; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|