Exemple #1
0
 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;
 }
Exemple #2
0
 public function bindConfigItem($name, $item, $source)
 {
     static $PATTERN = '/\\{\\s*?(.*?)\\s*?\\}/';
     $key = Key::ofConstant(Named::name($name));
     if (is_array($item)) {
         $elements = $this->bindConfigCollection($item, "{$name}.", $source);
         $binding = new ConfigCollectionBinding($name, $source, $elements);
     } else {
         if (preg_match_all($PATTERN, $item, $matches)) {
             $dependencies = [];
             foreach (preg_split($PATTERN, $item) as $constant) {
                 if ($constant) {
                     $dependencies[] = new ConstantValueBinding($key, $constant);
                 }
                 $dependency = array_shift($matches[1]);
                 if ($dependency) {
                     $dependencies[] = defined($dependency) ? new ConstantNameBinding($key, $dependency) : new LateBinding(Key::ofConstant(Named::name($dependency)));
                 }
             }
             $binding = new ConfigItemBinding($name, $source, $dependencies);
         } else {
             $binding = new ConstantValueBinding($key, $item);
         }
     }
     $this->bindings->put($binding);
     return $binding;
 }
 public function bindParameter(Parameter $parameter)
 {
     $key = Key::ofParameter($parameter);
     if ($parameter->isAnnotatedWith("Spot\\Inject\\Lazy")) {
         return new LazyBinding($key);
     }
     $binding = $this->bindings->get($key);
     if (empty($binding)) {
         $class = $parameter->getClass();
         if ($class && !$parameter->isDefaultValueAvailable()) {
             try {
                 $this->bind($class);
             } catch (ConfigurationException $e) {
                 throw new ConfigurationException($e->getMessage() . ", required by parameter \${$parameter->name} in " . $parameter->getDeclaringClass()->name . "::" . $parameter->getDeclaringFunction()->name);
             }
             $binding = $this->bindings->get($key);
         }
     }
     if (empty($binding) && $parameter->isDefaultValueAvailable()) {
         $binding = $parameter->isDefaultValueConstant() ? new ConstantNameBinding($key, $parameter->getDefaultValueConstantName()) : new ConstantValueBinding($key, $parameter->getDefaultValue());
     }
     if (empty($binding)) {
         throw new ConfigurationException("Missing dependency {$key}" . ", required by parameter \${$parameter->name} in " . $parameter->getMethod()->getType()->name . "::" . $parameter->getMethod()->name . "()");
     }
     return $binding;
 }
Exemple #4
0
 public function bindParameter(Parameter $parameter)
 {
     $key = Key::ofParameter($parameter);
     if ($parameter->isAnnotatedWith("Spot\\Inject\\Lazy")) {
         return new LazyBinding($key);
     }
     $binding = new LateBinding($key);
     if ($parameter->isDefaultValueAvailable()) {
         $binding = new OptionalBinding($key, $parameter->isDefaultValueConstant() ? new ConstantNameBinding($key, $parameter->getDefaultValueConstantName()) : new ConstantValueBinding($key, $parameter->getDefaultValue()));
     }
     return $binding;
 }
Exemple #5
0
 public static function ofParameter(Parameter $parameter)
 {
     $qualifier = $parameter->getAnnotation("Spot\\Inject\\Qualifier");
     if ($class = $parameter->getClass()) {
         return Key::ofType($class->name, $qualifier);
     }
     if (!$qualifier) {
         throw new ConfigurationException("Parameter \${$parameter->name} in " . $parameter->getDeclaringClass()->name . "::" . $parameter->getDeclaringFunction()->name . " is unbindable " . "because it's not type-hinted nor annotated with Qualifier annotation");
     }
     if ($parameter->isArray()) {
         return Key::ofCollection($qualifier);
     }
     return Key::ofConstant($qualifier);
 }
 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;
 }
 public function __construct()
 {
     parent::__construct(Key::ofType("Spot\\Inject\\Injector"));
 }
Exemple #8
0
 public function __construct(Type $type, array $dependencies)
 {
     parent::__construct(Key::ofType($type->name));
     $this->type = $type;
     $this->dependencies = $dependencies;
 }
Exemple #9
0
 public function __construct($name, $source)
 {
     parent::__construct(Key::ofConstant(Named::name($name)));
     $this->name = $name;
     $this->source = $source;
 }
Exemple #10
0
 function getInstance($type)
 {
     return $this->get(Key::ofType($type));
 }
Exemple #11
0
 /**
  * @param Key $key
  * @return Binding
  */
 public function get(Key $key)
 {
     if (isset($this->bindings[$key->hash()])) {
         return $this->bindings[$key->hash()];
     }
 }