1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\AppMeta; |
6
|
|
|
|
7
|
|
|
use BEAR\Resource\ResourceObject; |
8
|
|
|
use Generator; |
9
|
|
|
use Koriym\Psr4List\Psr4List; |
10
|
|
|
|
11
|
|
|
use function array_slice; |
12
|
|
|
use function array_walk; |
13
|
|
|
use function explode; |
14
|
|
|
use function implode; |
15
|
|
|
use function is_a; |
16
|
|
|
use function ltrim; |
17
|
|
|
use function preg_replace; |
18
|
|
|
use function sprintf; |
19
|
|
|
use function strtolower; |
20
|
|
|
|
21
|
|
|
use const DIRECTORY_SEPARATOR; |
22
|
|
|
|
23
|
|
|
abstract class AbstractAppMeta |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Application name "{Vendor}\{Project}" |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $name; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
public $appDir; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
public $tmpDir; |
37
|
|
|
|
38
|
1 |
|
/** @var string */ |
39
|
|
|
public $logDir; |
40
|
1 |
|
|
41
|
1 |
|
/** @return Generator<array{0: class-string<ResourceObject>, 1: string}> */ |
42
|
|
|
public function getResourceListGenerator(): Generator |
43
|
1 |
|
{ |
44
|
|
|
$list = new Psr4List(); |
45
|
|
|
|
46
|
|
|
foreach ($list($this->name . '\Resource', $this->appDir . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Resource') as [$class, $file]) { |
47
|
|
|
if (! is_a($class, ResourceObject::class, true)) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
yield [$class, $file]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $scheme 'app' | 'page' | '*' |
57
|
|
|
* |
58
|
|
|
* @return Generator<ResMeta> |
59
|
|
|
*/ |
60
|
|
|
public function getGenerator(string $scheme = '*'): Generator |
61
|
|
|
{ |
62
|
|
|
foreach ($this->getResourceListGenerator() as [$class, $file]) { |
63
|
|
|
$paths = explode('\\', $class); |
64
|
|
|
/** @var array<string> $paths */ |
65
|
|
|
$path = array_slice($paths, 3); |
66
|
|
|
array_walk($path, [$this, 'camel2kebab']); |
67
|
|
|
if ($scheme === '*') { |
68
|
|
|
/** @var array<string> $slice */ |
69
|
|
|
$slice = array_slice($path, 1); |
70
|
|
|
$uri = sprintf('%s://self/%s', (string) $path[0], implode('/', $slice)); |
71
|
|
|
|
72
|
|
|
yield new ResMeta($uri, $class, $file); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($scheme === $path[0]) { |
76
|
|
|
/** @var array<string> $sliceSchema */ |
77
|
|
|
$sliceSchema = array_slice($path, 1); |
78
|
|
|
$uri = sprintf('/%s', implode('/', $sliceSchema)); |
79
|
|
|
|
80
|
|
|
yield new ResMeta($uri, $class, $file); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* False positive: used in array_walk |
87
|
|
|
*/ |
88
|
|
|
private function camel2kebab(string &$str): void // phpcs:ignore |
89
|
|
|
{ |
90
|
|
|
$str = ltrim(strtolower((string) preg_replace('/[A-Z]/', '-\0', $str)), '-'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|