1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DummyGenerator; |
6
|
|
|
|
7
|
|
|
use DummyGenerator\Container\DefinitionContainerBuilder; |
8
|
|
|
use DummyGenerator\Container\DefinitionContainerInterface; |
9
|
|
|
use DummyGenerator\Definitions\DefinitionInterface; |
10
|
|
|
use DummyGenerator\Definitions\Exception\DefinitionNotFound; |
11
|
|
|
use DummyGenerator\Definitions\Extension\AddressExtensionInterface; |
12
|
|
|
use DummyGenerator\Definitions\Extension\Awareness\GeneratorAwareExtensionInterface; |
13
|
|
|
use DummyGenerator\Definitions\Extension\BarcodeExtensionInterface; |
14
|
|
|
use DummyGenerator\Definitions\Extension\BiasedExtensionInterface; |
15
|
|
|
use DummyGenerator\Definitions\Extension\BloodExtensionInterface; |
16
|
|
|
use DummyGenerator\Definitions\Extension\ColorExtensionInterface; |
17
|
|
|
use DummyGenerator\Definitions\Extension\CompanyExtensionInterface; |
18
|
|
|
use DummyGenerator\Definitions\Extension\CoordinatesExtensionInterface; |
19
|
|
|
use DummyGenerator\Definitions\Extension\CountryExtensionInterface; |
20
|
|
|
use DummyGenerator\Definitions\Extension\DateTimeExtensionInterface; |
21
|
|
|
use DummyGenerator\Definitions\Extension\FileExtensionInterface; |
22
|
|
|
use DummyGenerator\Definitions\Extension\HashExtensionInterface; |
23
|
|
|
use DummyGenerator\Definitions\Extension\InternetExtensionInterface; |
24
|
|
|
use DummyGenerator\Definitions\Extension\LanguageExtensionInterface; |
25
|
|
|
use DummyGenerator\Definitions\Extension\LoremExtensionInterface; |
26
|
|
|
use DummyGenerator\Definitions\Extension\NumberExtensionInterface; |
27
|
|
|
use DummyGenerator\Definitions\Extension\PaymentExtensionInterface; |
28
|
|
|
use DummyGenerator\Definitions\Extension\PersonExtensionInterface; |
|
|
|
|
29
|
|
|
use DummyGenerator\Definitions\Extension\PhoneNumberExtensionInterface; |
30
|
|
|
use DummyGenerator\Definitions\Extension\TextExtensionInterface; |
31
|
|
|
use DummyGenerator\Definitions\Extension\UserAgentExtensionInterface; |
32
|
|
|
use DummyGenerator\Definitions\Extension\VersionExtensionInterface; |
33
|
|
|
use DummyGenerator\Strategy\SimpleStrategy; |
34
|
|
|
use DummyGenerator\Strategy\StrategyInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @mixin AddressExtensionInterface |
38
|
|
|
* @mixin BarcodeExtensionInterface |
39
|
|
|
* @mixin BiasedExtensionInterface |
40
|
|
|
* @mixin BloodExtensionInterface |
41
|
|
|
* @mixin ColorExtensionInterface |
42
|
|
|
* @mixin CompanyExtensionInterface |
43
|
|
|
* @mixin CoordinatesExtensionInterface |
44
|
|
|
* @mixin CountryExtensionInterface |
45
|
|
|
* @mixin DateTimeExtensionInterface |
46
|
|
|
* @mixin FileExtensionInterface |
47
|
|
|
* @mixin HashExtensionInterface |
48
|
|
|
* @mixin InternetExtensionInterface |
49
|
|
|
* @mixin LanguageExtensionInterface |
50
|
|
|
* @mixin LoremExtensionInterface |
51
|
|
|
* @mixin NumberExtensionInterface |
52
|
|
|
* @mixin PaymentExtensionInterface |
53
|
|
|
* @mixin PersonExtensionInterface |
54
|
|
|
* @mixin PhoneNumberExtensionInterface |
55
|
|
|
* @mixin TextExtensionInterface |
56
|
|
|
* @mixin UserAgentExtensionInterface |
57
|
|
|
* @mixin VersionExtensionInterface |
58
|
|
|
*/ |
59
|
|
|
class DummyGenerator |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* @var array<string, DefinitionInterface> |
63
|
|
|
*/ |
64
|
|
|
protected array $extensions = []; |
65
|
|
|
private DefinitionContainerInterface $container; |
66
|
|
|
private StrategyInterface $strategy; |
67
|
|
|
|
68
|
152 |
|
public function __construct(DefinitionContainerInterface $container = null, StrategyInterface $strategy = null) |
69
|
|
|
{ |
70
|
152 |
|
$this->container = $container ?: DefinitionContainerBuilder::base(); |
71
|
152 |
|
$this->strategy = $strategy ?: new SimpleStrategy(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns new Generator with given strategy and same extensions |
76
|
|
|
*/ |
77
|
1 |
|
public function withStrategy(StrategyInterface $strategy): self |
78
|
|
|
{ |
79
|
1 |
|
return new self($this->container, $strategy); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function usedStrategy(string $strategy): bool |
83
|
|
|
{ |
84
|
1 |
|
return $this->strategy instanceof $strategy; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return extension stored in container with given ID |
89
|
|
|
* |
90
|
|
|
* @throws DefinitionNotFound |
91
|
|
|
*/ |
92
|
7 |
|
public function ext(string $id): DefinitionInterface |
93
|
|
|
{ |
94
|
7 |
|
if (!$this->container->has($id)) { |
95
|
1 |
|
throw new DefinitionNotFound(sprintf( |
96
|
1 |
|
'No DummyGenerator definition with id "%s" was loaded.', |
97
|
1 |
|
$id |
98
|
1 |
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
$extension = $this->container->get($id); |
102
|
|
|
|
103
|
6 |
|
return $this->handleAwareness($extension); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add new definition |
108
|
|
|
* |
109
|
|
|
* @param DefinitionInterface|class-string<DefinitionInterface>|callable():DefinitionInterface $value |
|
|
|
|
110
|
|
|
* @param string $name |
111
|
|
|
*/ |
112
|
5 |
|
public function addDefinition(string $name, callable|DefinitionInterface|string $value): void |
113
|
|
|
{ |
114
|
5 |
|
$this->container->add($name, $value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Replaces tokens ('{{ tokenName }}') in given string with the result from the token method call |
119
|
|
|
*/ |
120
|
19 |
|
public function parse(string $string): string |
121
|
|
|
{ |
122
|
19 |
|
$callback = function ($matches) { |
123
|
18 |
|
return $this->process($matches[1]); |
124
|
19 |
|
}; |
125
|
|
|
|
126
|
19 |
|
$replaced = preg_replace_callback('/{{\s?(\w+|[\w\\\]+->\w+?)\s?}}/u', $callback, $string); |
127
|
|
|
|
128
|
19 |
|
return !empty($replaced) ? $replaced : ''; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array<int, mixed> $arguments |
133
|
|
|
* |
134
|
|
|
* Magic method used to load proper extension for given function name (like firstName) and it's parameters |
135
|
|
|
*/ |
136
|
147 |
|
public function __call(string $name, array $arguments): mixed |
137
|
|
|
{ |
138
|
147 |
|
return $this->strategy->generate($name, fn () => $this->process($name, $arguments)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array<int, mixed> $arguments |
143
|
|
|
* |
144
|
|
|
* Get Extension for given method name |
145
|
|
|
*/ |
146
|
148 |
|
protected function process(string $method, array $arguments = []): mixed |
147
|
|
|
{ |
148
|
148 |
|
return $this->findProcessor($method)->$method(...$arguments); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return callable for given method |
153
|
|
|
*/ |
154
|
148 |
|
protected function findProcessor(string $method): DefinitionInterface |
155
|
|
|
{ |
156
|
148 |
|
if (isset($this->extensions[$method])) { |
157
|
12 |
|
return $this->extensions[$method]; |
158
|
|
|
} |
159
|
|
|
|
160
|
148 |
|
$extension = $this->container->findProcessor($method); |
161
|
|
|
|
162
|
148 |
|
if ($extension !== null) { |
163
|
148 |
|
$extension = $this->handleAwareness($extension); |
164
|
|
|
|
165
|
148 |
|
$this->extensions[$method] = $extension; |
166
|
|
|
|
167
|
148 |
|
return $extension; |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
throw new \InvalidArgumentException(sprintf('Unknown method "%s"', $method)); |
171
|
|
|
} |
172
|
|
|
|
173
|
149 |
|
private function handleAwareness(DefinitionInterface $extension): DefinitionInterface |
174
|
|
|
{ |
175
|
149 |
|
if ($extension instanceof GeneratorAwareExtensionInterface) { |
176
|
53 |
|
$extension = $extension->withGenerator($this); |
177
|
|
|
} |
178
|
149 |
|
return $extension; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths