public function get(Key $key) { $lazyClass = "Lazy__" . $key->hash(); if ($this->codeStorage->load($lazyClass)) { return $lazyClass; } $type = $this->reflection->get($key->getType()); $writer = new CodeWriter(); $writer->write("class ", $lazyClass); $type->isInterface() ? $writer->write(" implements ") : $writer->write(" extends "); $writer->write($type->name, " {"); $this->lazyGen->generate($type, $writer); $writer->write("}"); $this->codeStorage->store($lazyClass, $writer); return $lazyClass; }
public function getFactory(Key $key) { $factory = "InjectFactory__" . $this->modules->hash() . "__" . $key->hash(); if ($this->storage->load($factory)) { return $factory; } $this->builder->build(); $binding = $this->locator->get($key); if (empty($binding)) { throw new \RuntimeException("Unable to resolve dependency of {$key}, maybe you forgot to configure it ?"); } $writer = CodeWriter::create(); $writer->writeln("use Spot\\Inject\\Key;"); $writer->nl(); $writer->writeln("/**"); $writer->writeln(" * Provides {$key}"); $writer->writeln(" * "); $writer->writeln(" * Configured with: "); array_map(function ($module) use($writer) { $writer->write(" * "); $writer->writeln(is_object($module) ? get_class($module) : $module); }, iterator_to_array($this->modules)); $writer->writeln(" */"); $writer->write("class {$factory} {"); $writer->indent(); $writer->write('static function get($s, $m, $i, $a) {'); $writer->indent(); $writer->write("return "); $binding->accept(new FactoryCompilerVisitor($writer, $this->locator, $this->aspect)); $writer->write(";"); $writer->outdent(); $writer->write("}"); $writer->outdent(); $writer->writeln("}"); $this->storage->store($factory, $writer); return $factory; }
/** * @param Key $key * @return Binding */ public function get(Key $key) { if (isset($this->bindings[$key->hash()])) { return $this->bindings[$key->hash()]; } }