|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace hanneskod\readmetester\InputLanguage; |
|
6
|
|
|
|
|
7
|
|
|
use hanneskod\readmetester\Attribute\NamespaceName; |
|
8
|
|
|
use hanneskod\readmetester\Example\ExampleStoreInterface; |
|
9
|
|
|
use hanneskod\readmetester\Utils\Loader; |
|
10
|
|
|
|
|
11
|
|
|
class ReflectiveExampleStoreTemplate |
|
12
|
|
|
{ |
|
13
|
|
|
private const CLASS_TEMPLATE = <<<CLASS_TEMPLATE |
|
14
|
|
|
use hanneskod\\readmetester\\Attribute as ReadmeTester; |
|
15
|
|
|
return new class() extends \\hanneskod\\readmetester\\InputLanguage\\ReflectiveExampleStore |
|
16
|
|
|
{ |
|
17
|
|
|
%s |
|
18
|
|
|
function __construct() {} |
|
19
|
|
|
|
|
20
|
|
|
%s |
|
21
|
|
|
}; |
|
22
|
|
|
CLASS_TEMPLATE; |
|
23
|
|
|
|
|
24
|
|
|
private const METHOD_TEMPLATE = <<<METHOD_TEMPLATE |
|
25
|
|
|
%s |
|
26
|
|
|
function %s(): string |
|
27
|
|
|
{ |
|
28
|
|
|
return <<<'___example_code_delimiter___' |
|
29
|
|
|
%s |
|
30
|
|
|
___example_code_delimiter___; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
METHOD_TEMPLATE; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array<string> $globalAttributes |
|
37
|
|
|
* @param array<Definition> $definitions |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
private array $globalAttributes = [], |
|
41
|
|
|
private array $definitions = [], |
|
42
|
|
|
) {} |
|
43
|
|
|
|
|
44
|
|
|
public function setDefaultNamespace(string $namespace): void |
|
45
|
|
|
{ |
|
46
|
|
|
array_unshift( |
|
47
|
|
|
$this->globalAttributes, |
|
48
|
|
|
NamespaceName::createAttribute($namespace) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function render(): ExampleStoreInterface |
|
53
|
|
|
{ |
|
54
|
|
|
return Loader::load($this->getCode()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function getCode(): string |
|
58
|
|
|
{ |
|
59
|
|
|
return sprintf( |
|
60
|
|
|
self::CLASS_TEMPLATE, |
|
61
|
|
|
implode("\n", $this->globalAttributes), |
|
62
|
|
|
array_reduce( |
|
63
|
|
|
$this->definitions, |
|
64
|
|
|
fn($carry, $definition) => $carry .= sprintf( |
|
65
|
|
|
self::METHOD_TEMPLATE, |
|
66
|
|
|
implode("\n ", $definition->attributes), |
|
67
|
|
|
uniqid(ReflectiveExampleStore::EXAMPLE_METHOD_PREFIX), |
|
68
|
|
|
$definition->code |
|
69
|
|
|
) |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|