예제 #1
0
파일: Method.php 프로젝트: riskatlas/micka
 /** @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}");
 }
예제 #2
0
파일: Engine.php 프로젝트: nette/latte
 /**
  * Compiles template to PHP code.
  * @return string
  */
 public function compile($name)
 {
     foreach ($this->onCompile ?: [] as $cb) {
         call_user_func(Helpers::checkCallback($cb), $this);
     }
     $this->onCompile = [];
     $source = $this->getLoader()->getContent($name);
     try {
         $tokens = $this->getParser()->setContentType($this->contentType)->parse($source);
         $code = $this->getCompiler()->setContentType($this->contentType)->compile($tokens, $this->getTemplateClass($name));
     } catch (\Exception $e) {
         if (!$e instanceof CompileException) {
             $e = new CompileException("Thrown exception '{$e->getMessage()}'", NULL, $e);
         }
         $line = isset($tokens) ? $this->getCompiler()->getLine() : $this->getParser()->getLine();
         throw $e->setSource($source, $line, $name);
     }
     if (!preg_match('#\\n|\\?#', $name)) {
         $code = "<?php\n// source: {$name}\n?>" . $code;
     }
     $code = PhpHelpers::reformatCode($code);
     return $code;
 }
예제 #3
0
파일: Compiler.php 프로젝트: nette/latte
 /**
  * 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";
 }
예제 #4
0
    /**
     * Formats PHP statement.
     * @return string
     */
    public function formatPhp($statement, $args, $self = NULL)
    {
        $that = $this;
        array_walk_recursive($args, create_function('&$val', 'extract(NCFix::$vars[' . NCFix::uses(array('self' => $self, 'that' => $that)) . '], EXTR_REFS);
			list($val) = $that->normalizeEntity(array($val));

			if ($val instanceof DIStatement) {
				$val = new PhpLiteral($that->formatStatement($val, $self));

			} elseif ($val === \'@\' . DIContainerBuilder::THIS_CONTAINER) {
				$val = new PhpLiteral(\'$this\');

			} elseif ($service = $that->getServiceName($val, $self)) {
				$val = $service === $self ? \'$service\' : $that->formatStatement(new DIStatement($val));
				$val = new PhpLiteral($val);
			}
		'));
        return PhpHelpers::formatArgs($statement, $args);
    }
예제 #5
0
 /**
  * Generates configuration in PHP format.
  * @param  array
  * @return string
  */
 public function dump(array $data)
 {
     return "<?php // generated by Nette \nreturn " . PhpHelpers::dump($data) . ';';
 }
예제 #6
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";
 }