Пример #1
0
 private function generateCode($class, $extend, $namespace = NULL)
 {
     $ret = "<?php\n\n";
     if ($namespace) {
         $ret .= "namespace {$namespace};\n\n";
     }
     $ret .= "if (!class_exists('{$namespace}\\{$class}')) {\n\n";
     $php = new ClassType($class);
     $php->addExtend('\\' . $extend);
     $ret .= $php->__toString() . "\n\n}\n?>";
     return $ret;
 }
Пример #2
0
 public function generateCode($className, $parentName)
 {
     foreach ($this->extensions as $extension) {
         $extension->beforeCompile();
         $this->container->addDependency(Nette\Reflection\ClassType::from($extension)->getFileName());
     }
     $classes[] = $class = $this->container->generateClass($parentName);
     $class->setName($className)->addMethod('initialize');
     foreach ($this->extensions as $extension) {
         $extension->afterCompile($class);
     }
     $defs = $this->container->getDefinitions();
     ksort($defs);
     $list = array_keys($defs);
     foreach (array_reverse($defs, TRUE) as $name => $def) {
         if ($def->class === 'Nette\\DI\\NestedAccessor' && ($found = preg_grep('#^' . $name . '\\.#i', $list))) {
             $list = array_diff($list, $found);
             $def->class = $className . '_' . preg_replace('#\\W+#', '_', $name);
             $class->documents = preg_replace("#\\S+(?= \\\${$name}\\z)#", $def->class, $class->documents);
             $classes[] = $accessor = new Nette\Utils\PhpGenerator\ClassType($def->class);
             foreach ($found as $item) {
                 if ($defs[$item]->internal) {
                     continue;
                 }
                 $short = substr($item, strlen($name) + 1);
                 $accessor->addDocument($defs[$item]->shared ? "@property {$defs[$item]->class} \${$short}" : "@method {$defs[$item]->class} create" . ucfirst("{$short}()"));
             }
         }
     }
     return implode("\n\n\n", $classes);
 }
Пример #3
0
 /**
  * Generates PHP class.
  * @return Nette\Utils\PhpGenerator\ClassType
  */
 public function generateClass($parentClass = 'Nette\\DI\\Container')
 {
     unset($this->definitions[self::THIS_CONTAINER]);
     $this->addDefinition(self::THIS_CONTAINER)->setClass($parentClass);
     $this->prepareClassList();
     $class = new Nette\Utils\PhpGenerator\ClassType('Container');
     $class->addExtend($parentClass);
     $class->addMethod('__construct')->addBody('parent::__construct(?);', array($this->expand($this->parameters)));
     $classes = $class->addProperty('classes', array());
     foreach ($this->classes as $name => $foo) {
         try {
             $classes->value[$name] = $this->getByType($name);
         } catch (ServiceCreationException $e) {
             $classes->value[$name] = new PhpLiteral('FALSE, //' . strstr($e->getMessage(), ':'));
         }
     }
     $definitions = $this->definitions;
     ksort($definitions);
     $meta = $class->addProperty('meta', array());
     foreach ($definitions as $name => $def) {
         if ($def->shared) {
             foreach ($this->expand($def->tags) as $tag => $value) {
                 $meta->value[$name][Container::TAGS][$tag] = $value;
             }
         }
     }
     foreach ($definitions as $name => $def) {
         try {
             $type = $def->class ?: 'object';
             $methodName = Container::getMethodName($name, $def->shared);
             if (!PhpHelpers::isIdentifier($methodName)) {
                 throw new ServiceCreationException('Name contains invalid characters.');
             }
             if ($def->shared && !$def->internal && PhpHelpers::isIdentifier($name)) {
                 $class->addDocument("@property {$type} \${$name}");
             }
             $method = $class->addMethod($methodName)->addDocument("@return {$type}")->setVisibility($def->shared || $def->internal ? 'protected' : 'public')->setBody($name === self::THIS_CONTAINER ? 'return $this;' : $this->generateService($name));
             foreach ($this->expand($def->parameters) as $k => $v) {
                 $tmp = explode(' ', is_int($k) ? $v : $k);
                 $param = is_int($k) ? $method->addParameter(end($tmp)) : $method->addParameter(end($tmp), $v);
                 if (isset($tmp[1])) {
                     $param->setTypeHint($tmp[0]);
                 }
             }
         } catch (\Exception $e) {
             throw new ServiceCreationException("Service '{$name}': " . $e->getMessage(), NULL, $e);
         }
     }
     return $class;
 }
Пример #4
0
 function generateClass()
 {
     $this->prepareClassList();
     $class = new Nette\Utils\PhpGenerator\ClassType('Container');
     $class->addExtend('Nette\\DI\\Container');
     $class->addProperty('parameters', $this->expand($this->parameters));
     $classes = $class->addProperty('classes', array());
     foreach ($this->classes as $name => $foo) {
         try {
             $classes->value[$name] = $this->findByClass($name);
         } catch (ServiceCreationException $e) {
             $classes->value[$name] = FALSE;
         }
     }
     $meta = $class->addProperty('meta', array());
     foreach ($this->definitions as $name => $def) {
         foreach ($this->expand($def->tags) as $tag => $value) {
             $meta->value[$name][Container::TAGS][$tag] = $value;
         }
     }
     foreach ($this->definitions as $name => $definition) {
         try {
             $type = $definition->class ? $this->expand($definition->class) : 'object';
             if ($definition->shared) {
                 $class->addDocument("@property {$type} \${$name}");
             }
             $method = $class->addMethod(($definition->shared ? 'createService' : 'create') . ucfirst($name))->addDocument("@return {$type}")->setVisibility($definition->shared || $definition->internal ? 'protected' : 'public')->setBody($name === self::THIS_CONTAINER ? 'return $this;' : $this->generateService($name));
             foreach ($definition->parameters as $k => $v) {
                 $tmp = explode(' ', is_int($k) ? $v : $k);
                 $param = is_int($k) ? $method->addParameter(end($tmp)) : $method->addParameter(end($tmp), $v);
                 if (isset($tmp[1])) {
                     $param->setTypeHint($tmp[0]);
                 }
             }
         } catch (\Exception $e) {
             throw new ServiceCreationException("Service '{$name}': " . $e->getMessage(), NULL, $e);
         }
     }
     return $class;
 }