1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Micro \Component \DependencyInjection ;
6+
7+ use Micro \Component \DependencyInjection \Proxy \ProxyBuilderInterface ;
8+ use Micro \Component \DependencyInjection \Proxy \ProxyClassNameGeneratorInterface ;
9+ use Psr \Container \ContainerInterface ;
10+
11+ final readonly class ContainerCompiled implements ContainerInterface, ContainerRegistryCompiledInterface
12+ {
13+ public function __construct (
14+ private ContainerInterface $ decorated ,
15+ private ProxyClassNameGeneratorInterface $ classNameGenerator ,
16+ private ProxyBuilderInterface $ proxyBuilder
17+ )
18+ {
19+ }
20+
21+ public function get (string $ id ): mixed
22+ {
23+ $ proxyClass = $ this ->classNameGenerator ->createProxyClassName ($ id );
24+ if (!class_exists ($ proxyClass )) {
25+ return $ this ->decorated ->get ($ id );
26+ }
27+
28+ return new $ proxyClass ($ this );
29+ }
30+
31+ public function has (string $ id ): bool
32+ {
33+ return $ this ->decorated ->has ($ id );
34+ }
35+
36+ public function register (string $ id , callable $ service , bool $ force = false ): void
37+ {
38+ if (interface_exists ($ id )) {
39+ $ this ->proxyBuilder ->add ($ id );
40+ }
41+
42+ $ this ->decorated ->register ($ id , $ service , $ force );
43+ }
44+
45+ public function compile (): void
46+ {
47+ $ this ->proxyBuilder ->build ();
48+ }
49+ }
0 commit comments