-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGeneratorHookRunner.php
More file actions
58 lines (49 loc) · 1.62 KB
/
Copy pathGeneratorHookRunner.php
File metadata and controls
58 lines (49 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Helmich\Schema2Class\Generator;
use Helmich\Schema2Class\Codegen\EnumGenerator;
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\FileGenerator;
trait GeneratorHookRunner
{
/**
* @var array<array{hook: object, propagateToSubObjects: bool}>
*/
private array $hooks = [];
public function withHook(object $hook, bool $propagateToSubObjects = true): self
{
$clone = clone $this;
$clone->hooks[] = [
"hook" => $hook,
"propagateToSubObjects" => $propagateToSubObjects,
];
return $clone;
}
private function clearNonPropagatingHooks(): void
{
$this->hooks = array_filter($this->hooks, fn($hook) => $hook["propagateToSubObjects"]);
}
public function onClassCreated(ClassGenerator $class): void
{
foreach ($this->hooks as ['hook' => $hook]) {
if ($hook instanceof Hook\ClassCreatedHook) {
$hook->onClassCreated($class->getName(), $class);
}
}
}
public function onEnumCreated(string $enumName, EnumGenerator $enum): void
{
foreach ($this->hooks as ['hook' => $hook]) {
if ($hook instanceof Hook\EnumCreatedHook) {
$hook->onEnumCreated($enumName, $enum);
}
}
}
public function onFileCreated(string $filename, FileGenerator $fileGenerator): void
{
foreach ($this->hooks as ['hook' => $hook]) {
if ($hook instanceof Hook\FileCreatedHook) {
$hook->onFileCreated($filename, $fileGenerator);
}
}
}
}