Example #1
0
 /**
  * Compiles the given container builder and takes care of scoped proxy generation.
  * 
  * @param ContainerBuilder $builder
  * @param string $proxyCachePath
  * @param array<string> $additionalProxies
  * @return string
  */
 protected function compileBuilder(ContainerBuilder $builder, $proxyCachePath = NULL, array $additionalProxies = [])
 {
     $imports = [DelegateBinding::class, ContextLookupException::class, InjectionPoint::class, InjectionPointInterface::class];
     $code = '<?php' . "\n\n";
     $code .= 'namespace ' . $this->namespace . " {\n";
     foreach ($imports as $typeName) {
         $code .= "\nuse " . ltrim($typeName, '\\') . ";";
     }
     if (!empty($imports)) {
         $code .= "\n";
     }
     $code .= "\nfinal class {$this->typeName} extends \\KoolKode\\Context\\CompiledContainer {\n\n";
     $bindings = $builder->getBindings();
     $marked = [];
     // Write bound properties:
     $bound = [];
     foreach ($bindings as $binding) {
         $bound[$binding->getTypeName()] = true;
     }
     $code .= "\tprotected \$bound = " . var_export($bound, true) . ";\n";
     foreach ($bindings as $binding) {
         foreach ($binding->getMarkers() as $marker) {
             $marked[get_class($marker)][(string) $binding] = $binding;
         }
         $code .= $this->compileBinding($binding);
     }
     $code .= $this->compileMarkerLookup($marked);
     foreach ($this->methods as $method) {
         $code .= "\n\n" . $method . "\n";
     }
     if ($proxyCachePath !== NULL) {
         $code .= $this->compileScopedProxies($builder, $proxyCachePath, $additionalProxies);
     }
     $code .= "}\n\n";
     $code .= "}\n\n";
     return $code;
 }