|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
|
6
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OCA\TwoFactorGateway\Provider; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @template T of object |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AFactory { |
|
15
|
|
|
/** @var array<string, T> */ |
|
16
|
|
|
protected array $instances = []; |
|
17
|
|
|
/** @var array<string> */ |
|
18
|
|
|
protected array $fqcn = []; |
|
19
|
|
|
|
|
20
|
|
|
abstract protected function getPrefix(): string; |
|
21
|
|
|
|
|
22
|
|
|
abstract protected function getSuffix(): string; |
|
23
|
|
|
|
|
24
|
|
|
/** @return class-string<T> */ |
|
|
|
|
|
|
25
|
|
|
abstract protected function getBaseClass(): string; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param class-string<T> $name |
|
|
|
|
|
|
29
|
|
|
* @return T |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function get(string $name): object { |
|
32
|
6 |
|
$needle = match(str_contains($name, '\\')) { |
|
33
|
5 |
|
true => $this->typeFrom($name), |
|
34
|
1 |
|
false => strtolower($name), |
|
35
|
6 |
|
}; |
|
36
|
6 |
|
if (isset($this->instances[$needle])) { |
|
37
|
2 |
|
return $this->instances[$needle]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
foreach ($this->getFqcnList() as $fqcn) { |
|
41
|
5 |
|
$type = $this->typeFrom($fqcn); |
|
42
|
5 |
|
if ($type !== $needle) { |
|
43
|
4 |
|
continue; |
|
44
|
|
|
} |
|
45
|
4 |
|
$instance = \OCP\Server::get($fqcn); |
|
46
|
4 |
|
$this->instances[$type] = $instance; |
|
47
|
4 |
|
return $instance; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
throw new \InvalidArgumentException("Invalid type <$name>"); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @return array<string> */ |
|
54
|
7 |
|
public function getFqcnList(): array { |
|
55
|
7 |
|
if (!empty($this->fqcn)) { |
|
56
|
5 |
|
return $this->fqcn; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
6 |
|
$loader = require __DIR__ . '/../../vendor/autoload.php'; |
|
60
|
6 |
|
foreach ($loader->getClassMap() as $fqcn => $_) { |
|
61
|
6 |
|
$type = $this->typeFrom($fqcn); |
|
62
|
6 |
|
if ($type === null) { |
|
63
|
6 |
|
continue; |
|
64
|
|
|
} |
|
65
|
6 |
|
if (!is_subclass_of($fqcn, $this->getBaseClass(), true)) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
6 |
|
$this->fqcn[] = $fqcn; |
|
69
|
|
|
} |
|
70
|
6 |
|
return $this->fqcn; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
7 |
|
protected function typeFrom(string $fqcn): ?string { |
|
74
|
7 |
|
$prefix = $this->getPrefix(); |
|
75
|
7 |
|
if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) { |
|
76
|
6 |
|
return null; |
|
77
|
|
|
} |
|
78
|
7 |
|
$suffix = $this->getSuffix(); |
|
79
|
7 |
|
if (!str_ends_with($fqcn, $suffix)) { |
|
80
|
5 |
|
return null; |
|
81
|
|
|
} |
|
82
|
7 |
|
$type = substr($fqcn, strlen($prefix)); |
|
83
|
7 |
|
$type = substr($type, 0, -strlen('\\' . $suffix)); |
|
84
|
7 |
|
if (strpos($type, '\\') !== false || $type === '') { |
|
85
|
1 |
|
return null; |
|
86
|
|
|
} |
|
87
|
7 |
|
return strtolower($type); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|