Exemple #1
0
 /** @return string  PHP code */
 public function __toString()
 {
     $parameters = array();
     foreach ($this->parameters as $param) {
         $parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '') . ($param->reference ? '&' : '') . '$' . $param->name . ($param->optional ? ' = ' . PhpHelpers::dump($param->defaultValue) : '');
     }
     $uses = array();
     foreach ($this->uses as $param) {
         $uses[] = ($param->reference ? '&' : '') . '$' . $param->name;
     }
     return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . ($this->visibility ? $this->visibility . ' ' : '') . ($this->static ? 'static ' : '') . 'function' . ($this->returnReference ? ' &' : '') . ($this->name ? ' ' . $this->name : '') . '(' . implode(', ', $parameters) . ')' . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '') . ($this->abstract || $this->body === FALSE ? ';' : ($this->name ? "\n" : ' ') . "{\n" . Strings::indent(trim($this->body), 1) . "\n}");
 }
Exemple #2
0
 /**
  * Compiles tokens to PHP code.
  * @param  Token[]
  * @return string
  */
 public function compile(array $tokens, $className)
 {
     $this->tokens = $tokens;
     $output = '';
     $this->output =& $output;
     $this->inHead = TRUE;
     $this->htmlNode = $this->macroNode = $this->context = NULL;
     $this->placeholders = $this->properties = [];
     $this->methods = ['main' => NULL, 'prepare' => NULL];
     $macroHandlers = new \SplObjectStorage();
     array_map([$macroHandlers, 'attach'], call_user_func_array('array_merge', $this->macros));
     foreach ($macroHandlers as $handler) {
         $handler->initialize($this);
     }
     foreach ($tokens as $this->position => $token) {
         if ($this->inHead && !($token->type === $token::COMMENT || $token->type === $token::MACRO_TAG && isset($this->flags[$token->name]) && $this->flags[$token->name] & IMacro::ALLOWED_IN_HEAD || $token->type === $token::TEXT && trim($token->text) === '')) {
             $this->inHead = FALSE;
         }
         $this->{"process{$token->type}"}($token);
     }
     while ($this->htmlNode) {
         if (!empty($this->htmlNode->macroAttrs)) {
             throw new CompileException('Missing ' . self::printEndTag($this->htmlNode));
         }
         $this->htmlNode = $this->htmlNode->parentNode;
     }
     while ($this->macroNode) {
         if (~$this->flags[$this->macroNode->name] & IMacro::AUTO_CLOSE) {
             throw new CompileException('Missing ' . self::printEndTag($this->macroNode));
         }
         $this->closeMacro($this->macroNode->name);
     }
     $prepare = $epilogs = '';
     foreach ($macroHandlers as $handler) {
         $res = $handler->finalize();
         $prepare .= empty($res[0]) ? '' : "<?php {$res['0']} ?>";
         $epilogs = (empty($res[1]) ? '' : "<?php {$res['1']} ?>") . $epilogs;
     }
     $this->addMethod('main', $this->expandTokens("extract(\$this->params);?>\n{$output}{$epilogs}<?php return get_defined_vars();"));
     if ($prepare) {
         $this->addMethod('prepare', "extract(\$this->params);?>{$prepare}<?php");
     }
     if ($this->contentType !== self::CONTENT_HTML) {
         $this->addProperty('contentType', $this->contentType);
     }
     foreach ($this->properties as $name => $value) {
         $members[] = "\tpublic \${$name} = " . PhpHelpers::dump($value) . ';';
     }
     foreach (array_filter($this->methods) as $name => $method) {
         $members[] = "\n\tfunction {$name}({$method['arguments']})\n\t{\n" . ($method['body'] ? "\t\t{$method['body']}\n" : '') . "\t}";
     }
     return "<?php\n" . "use Latte\\Runtime as LR;\n\n" . "class {$className} extends Latte\\Runtime\\Template\n{\n" . implode("\n\n", $members) . "\n\n}\n";
 }
Exemple #3
0
 /**
  * Generates configuration in PHP format.
  * @param  array
  * @return string
  */
 public function dump(array $data)
 {
     return "<?php // generated by Nette \nreturn " . PhpHelpers::dump($data) . ';';
 }
Exemple #4
0
 /**
  * Formats PHP code for class instantiating, function calling or property setting in PHP.
  * @return string
  * @internal
  */
 public function formatStatement(DIStatement $statement, $self = NULL)
 {
     $entity = $this->normalizeEntity($statement->entity);
     $arguments = $statement->arguments;
     if (is_string($entity) && Strings::contains($entity, '?')) {
         // PHP literal
         return $this->formatPhp($entity, $arguments, $self);
     } elseif ($service = $this->getServiceName($entity)) {
         // factory calling or service retrieving
         if ($this->definitions[$service]->shared) {
             if ($arguments) {
                 throw new ServiceCreationException("Unable to call service '{$entity}'.");
             }
             return $this->formatPhp('$this->getService(?)', array($service));
         }
         $params = array();
         foreach ($this->definitions[$service]->parameters as $k => $v) {
             $params[] = preg_replace('#\\w+$#', '\\$$0', is_int($k) ? $v : $k) . (is_int($k) ? '' : ' = ' . PhpHelpers::dump($v));
         }
         $rm = new FunctionReflection(create_function(implode(', ', $params), ''));
         $arguments = DIHelpers::autowireArguments($rm, $arguments, $this);
         return $this->formatPhp('$this->?(?*)', array(DIContainer::getMethodName($service, FALSE), $arguments), $self);
     } elseif ($entity === 'not') {
         // operator
         return $this->formatPhp('!?', array($arguments[0]));
     } elseif (is_string($entity)) {
         // class name
         if ($constructor = ClassReflection::from($entity)->getConstructor()) {
             $this->addDependency($constructor->getFileName());
             $arguments = DIHelpers::autowireArguments($constructor, $arguments, $this);
         } elseif ($arguments) {
             throw new ServiceCreationException("Unable to pass arguments, class {$entity} has no constructor.");
         }
         return $this->formatPhp("new {$entity}" . ($arguments ? '(?*)' : ''), array($arguments), $self);
     } elseif (!Validators::isList($entity) || count($entity) !== 2) {
         throw new InvalidStateException("Expected class, method or property, " . PhpHelpers::dump($entity) . " given.");
     } elseif ($entity[0] === '') {
         // globalFunc
         return $this->formatPhp("{$entity['1']}(?*)", array($arguments), $self);
     } elseif (Strings::contains($entity[1], '$')) {
         // property setter
         Validators::assert($arguments, 'list:1', "setup arguments for '" . Callback::create($entity) . "'");
         if ($this->getServiceName($entity[0], $self)) {
             return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), $arguments[0]), $self);
         } else {
             return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), $arguments[0]), $self);
         }
     } elseif ($service = $this->getServiceName($entity[0], $self)) {
         // service method
         if ($this->definitions[$service]->class) {
             $arguments = $this->autowireArguments($this->definitions[$service]->class, $entity[1], $arguments);
         }
         return $this->formatPhp('?->?(?*)', array($entity[0], $entity[1], $arguments), $self);
     } else {
         // static method
         $arguments = $this->autowireArguments($entity[0], $entity[1], $arguments);
         return $this->formatPhp("{$entity['0']}::{$entity['1']}(?*)", array($arguments), $self);
     }
 }
Exemple #5
0
 /** @return string  PHP code */
 public function __toString()
 {
     $consts = array();
     foreach ($this->consts as $name => $value) {
         $consts[] = "const {$name} = " . PhpHelpers::dump($value) . ";\n";
     }
     $properties = array();
     foreach ($this->properties as $property) {
         $properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '') . $property->visibility . ($property->static ? ' static' : '') . ' $' . $property->name . ($property->value === NULL ? '' : ' = ' . PhpHelpers::dump($property->value)) . ";\n";
     }
     return Strings::normalize(($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . $this->type . ' ' . $this->name . ' ' . ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '') . ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '') . "\n{\n\n" . Strings::indent(($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '') . ($this->consts ? implode('', $consts) . "\n\n" : '') . ($this->properties ? implode("\n", $properties) . "\n\n" : '') . implode("\n\n\n", $this->methods), 1) . "\n\n}") . "\n";
 }