Ejemplo n.º 1
0
 public function generateBindedParameter($name, CodeWriter $writer)
 {
     if (empty($name)) {
         $writer->write('iterator_to_array($rq)');
     } else {
         $writer->write('$rq[');
         $writer->literal($name);
         $writer->write(']');
     }
 }
Ejemplo n.º 2
0
 public function generateParameter(Parameter $parameter, CodeWriter $writer)
 {
     $class = $parameter->getClass();
     if ($class) {
         $writer->write("\\", $class->name, " ");
     } else {
         if ($parameter->isArray()) {
             $writer->write("array ");
         } else {
             if ($parameter->isCallable()) {
                 $writer->write("callable ");
             }
         }
     }
     $writer->write("\$", $parameter->name);
 }
Ejemplo n.º 3
0
 public function get(array $action)
 {
     $adapter = "ActionAdapter__" . md5("{$action[0]}::{$action[1]}");
     if ($this->storage->load($adapter)) {
         return $adapter;
     }
     $method = $this->reflection->get($action[0])->getMethod($action[1]);
     $writer = new CodeWriter();
     $writer->write("class ", $adapter, " {");
     $writer->indent();
     $method->isStatic() ? $this->static->generate($method, $writer) : $this->instance->generate($method, $writer);
     $writer->outdent();
     $writer->writeln("}");
     $this->storage->store($adapter, $writer);
     return $adapter;
 }
Ejemplo n.º 4
0
 public function get()
 {
     $router = "InvertedIndex__{$this->hash}";
     if ($this->storage->load($router)) {
         return new $router();
     }
     $writer = new CodeWriter();
     $writer->writeln("use Spot\\App\\Web\\Impl\\Router\\InvertedIndexRouter;");
     $writer->writeln("use Spot\\App\\Web\\Impl\\Router\\InvertedIndex\\MethodIndex;");
     $writer->writeln("use Spot\\App\\Web\\Impl\\Router\\InvertedIndex\\AjaxIndex;");
     $writer->writeln("use Spot\\App\\Web\\Impl\\Router\\InvertedIndex\\StaticPathIndex;");
     $writer->writeln("use Spot\\App\\Web\\Impl\\Router\\InvertedIndex\\PrefixPathIndex;");
     $writer->writeln("use Spot\\App\\Web\\Impl\\Router\\InvertedIndex\\RegexPathIndex;");
     $writer->write("class ", $router, " extends InvertedIndexRouter {");
     $writer->indent();
     $this->gen->generate($writer);
     $writer->outdent();
     $writer->writeln("}");
     $this->storage->store($router, $writer);
     return new $router();
 }
Ejemplo n.º 5
0
 public function get($className, $delegate, Injector $injector)
 {
     $proxy = "AspectProxy__" . $this->modules->hash() . "__" . md5($className);
     if (!$this->storage->load($proxy)) {
         $writer = CodeWriter::create();
         $writer->writeln("use Spot\\Inject\\Impl\\Aspect\\DelegateInvocation;");
         $writer->writeln("use Spot\\Inject\\Impl\\Aspect\\TerminalInvocation;");
         $writer->write("class ", $proxy, " extends ", $className, " {");
         $writer->indent();
         $this->proxyGen->generate($this->reflection->get($className), $writer);
         $writer->outdent();
         $writer->write("}");
         $this->storage->store($proxy, $writer);
     }
     return new $proxy($this->singletons, $this->modules, $injector, $this->reflection, $delegate);
 }
Ejemplo n.º 6
0
 public function get(Modules $modules)
 {
     $adapter = "ModulesAdapter__" . $modules->hash();
     if ($this->storage->load($adapter)) {
         return $adapter;
     }
     $this->builder->build();
     $writer = new CodeWriter();
     $writer->write("class ", $adapter, " {");
     $writer->indent();
     $writer->write("const SINGLETONS_SIZE = ", $this->singletons->getSize(), ";");
     $writer->outdent();
     $writer->write("}");
     $this->storage->store($adapter, $writer);
     return $adapter;
 }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
0
 public function getBinder($domainName)
 {
     $binder = "DomainBinder__" . md5($domainName);
     if ($this->storage->load($binder)) {
         return $binder;
     }
     $writer = new CodeWriter();
     $writer->write("class ", $binder, "{");
     $writer->indent();
     $writer->write('static function newInstance($d, $n, $b) {');
     $writer->indent();
     $this->gen->generateNewInstance($domainName, $writer);
     $writer->outdent();
     $writer->writeln("}");
     $writer->write('static function bind($d, $i, $b) {');
     $writer->indent();
     $this->gen->generateBind($domainName, $writer);
     $writer->outdent();
     $writer->writeln("}");
     $writer->outdent();
     $writer->write("}");
     $this->storage->store($binder, $writer);
     return $binder;
 }
Ejemplo n.º 10
0
 public function generate(CodeWriter $writer)
 {
     $actions = $names = $methods = $staticPaths = $regexPaths = $prefixPaths = [];
     $ajax = [true => [], false => []];
     foreach ($this->scanner->scan() as $i => $mapping) {
         $route = $mapping->route;
         $action = $mapping->action;
         if ($this->compiler->checkSymbol($route->value)) {
             $pattern = $this->compiler->compile($route->value);
             $regexPaths[$i] = $pattern;
             $prefix = strtok($route->value, "*{(");
             $prefixPaths[$prefix][$i] = 1;
         } else {
             $staticPaths[$route->value][$i] = 1;
         }
         $actions[$i] = $action;
         foreach ((array) $route->method as $method) {
             $methods[$method][$i] = 1;
         }
         if ($route->ajax === null) {
             $ajax[true][$i] = $ajax[false][$i] = 1;
         } else {
             $ajax[$route->ajax][$i] = 1;
         }
         if ($route->name) {
             $name = ["params" => array_values($this->compiler->parse($route->value)), "uri" => preg_replace('/\\{\\$(.*?)\\}/', '$1', $route->value)];
             $names[$route->name] = $name;
         }
     }
     $writer->write("function __construct() {");
     $writer->indent();
     $writer->write("parent::__construct(");
     $writer->indent();
     $writer->writeln(var_export($names, true), ", ");
     $writer->writeln(var_export($actions, true), ", ");
     $writer->writeln("new MethodIndex(", var_export($methods, true), "), ");
     $writer->writeln("new AjaxIndex(", var_export($ajax, true), "), ");
     $writer->writeln("new StaticPathIndex(", var_export($staticPaths, true), "), ");
     $writer->writeln("new PrefixPathIndex(", var_export($prefixPaths, true), "), ");
     $writer->write("new RegexPathIndex(", var_export($regexPaths, true), ")");
     $writer->outdent();
     $writer->write(");");
     $writer->outdent();
     $writer->writeln("}");
 }
Ejemplo n.º 11
0
 public function generateMethodCall(Method $method, CodeWriter $writer)
 {
     $writer->write('$i');
     if ($this->isTypeBinded($method)) {
         $writer->write('->fork([new self($rq)])');
     }
     $writer->write('->getInstance(');
     $writer->literal($method->getType()->name);
     $writer->write(')->', $method->name);
 }
Ejemplo n.º 12
0
 public function writeTypedBinding(Type $class, $name, CodeWriter $writer)
 {
     $writer->write('is_array($b[');
     $writer->literal($name);
     $writer->write(']) ? $d->newInstance(');
     $writer->literal($class->name);
     $writer->write(', $b[');
     $writer->literal($name);
     $writer->write(']) : $d->find(');
     $writer->literal($class->name);
     $writer->write(', $b[');
     $writer->literal($name);
     $writer->write('])');
 }
Ejemplo n.º 13
0
 public function generateParameter(Parameter $parameter, CodeWriter $writer)
 {
     if ($parameter->getClass()) {
         $writer->write($parameter->getClass()->name, " ");
     } else {
         if ($parameter->isArray()) {
             $writer->write("array ");
         }
     }
     $writer->write('$', $parameter->name);
     if ($parameter->isDefaultValueAvailable()) {
         $writer->write(" = ");
         $writer->literal($parameter->getDefaultValue());
     }
 }
Ejemplo n.º 14
0
 public function generateMethodCall(Method $method, CodeWriter $writer)
 {
     $writer->write($method->getType()->name, "::", $method->name);
 }