-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeneratedCoreExtension.php
More file actions
51 lines (39 loc) · 1.81 KB
/
GeneratedCoreExtension.php
File metadata and controls
51 lines (39 loc) · 1.81 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Extension\Generated;
use Patchlevel\Hydrator\Extension;
use Patchlevel\Hydrator\Guesser\BuiltInGuesser;
use Patchlevel\Hydrator\HydratorBuilder;
final class GeneratedCoreExtension implements Extension
{
public function __construct(
private readonly string $cachePath,
private readonly array $classes,
)
{
}
//@todo most probably this is not the best idea to add this as an extension, as we have here some bidirectional dependencies
public function configure(HydratorBuilder $builder): void
{
$builder->addGuesser(new BuiltInGuesser(), -64); // @todo this should be somehow considered in generator
$metadataFactory = $builder->getMetadataFactory();
$generator = new MiddlewareGenerator($metadataFactory);
$middlewareClassName = 'GeneratedTransformMiddleware';
$fullMiddlewareClassName = 'Patchlevel\\Hydrator\\Generated\\' . $middlewareClassName;
$middlewareCode = $generator->dump($this->classes, $fullMiddlewareClassName);
//if (class_exists($fullMiddlewareClassName)) {
// throw new \RuntimeException(sprintf('Middleware class %s already exists', $fullMiddlewareClassName));
//}
$filename = sprintf('%s/%s.php', $this->cachePath, $middlewareClassName);
//if (file_exists($filename)) {
// throw new \RuntimeException(sprintf('Middleware file %s already exists', $filename));
//}
//
//if (!is_dir(dirname($filename))) {
// mkdir(dirname($filename), 0777, true);
//}
file_put_contents($filename, $middlewareCode);
require_once $filename; // should not be needed if autoload config is valid?
$builder->addMiddleware(new $fullMiddlewareClassName($metadataFactory), -64);
}
}