Inheritance: use trait latte\Strict
Example #1
0
 /**
  * {ifCurrent destination [,] [params]}
  */
 public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
 {
     if ($node->modifiers) {
         throw new CompileException('Modifiers are not allowed here.');
     }
     return $writer->write($node->args ? 'if ($_presenter->isLinkCurrent(%node.word, %node.array?)) {' : 'if ($_presenter->getLastCreatedRequestFlag("current")) {');
 }
Example #2
0
 public function macroSecured(Latte\MacroNode $node, Latte\PhpWriter $writer)
 {
     if ($node->prefix === $node::PREFIX_TAG) {
         return $writer->write($node->htmlNode->closing ? 'if(array_pop($_l->secured)){' : 'if($_l->secured[] = $acl->isUserAllowedTo(%node.args)) {');
     }
     return $writer->write('if ($acl->isUserAllowedTo(%node.args)) {');
 }
 /**
  * @param MacroNode $node
  * @param PhpWriter $writer
  * @param string $else
  * @param bool $comma
  * @return string
  */
 private function modify(MacroNode $node, PhpWriter $writer, $else = '', $comma = TRUE)
 {
     if (!$node->modifiers) {
         return $else;
     }
     return $writer->write(($comma ? ', ' : '') . 'function ($s, $type) { $_fi = new LR\\FilterInfo($type); return %modifyContent($s); }');
 }
Example #4
0
 public function macroSrc(PhpWriter $writer)
 {
     $code = self::getCode('%node.array');
     // in macro must go result on output
     $code[] = 'echo %escape(%modify($link));';
     return $writer->write(implode('', $code));
 }
 /**
  * Extracts translation messages from a file to the catalogue.
  *
  * @param string           $file The path to look into
  * @param MessageCatalogue $catalogue The catalogue
  */
 public function extractFile($file, MessageCatalogue $catalogue)
 {
     $buffer = NULL;
     $parser = new Parser();
     $parser->shortNoEscape = TRUE;
     foreach ($tokens = $parser->parse(file_get_contents($file)) as $token) {
         if ($token->type !== $token::MACRO_TAG || !in_array($token->name, array('_', '/_'), TRUE)) {
             if ($buffer !== NULL) {
                 $buffer .= $token->text;
             }
             continue;
         }
         if ($token->name === '/_') {
             $catalogue->set(($this->prefix ? $this->prefix . '.' : '') . $buffer, $buffer);
             $buffer = NULL;
         } elseif ($token->name === '_' && empty($token->value)) {
             $buffer = '';
         } else {
             $args = new MacroTokens($token->value);
             $writer = new PhpWriter($args, $token->modifiers);
             $message = $writer->write('%node.word');
             if (in_array(substr(trim($message), 0, 1), array('"', '\''), TRUE)) {
                 $message = substr(trim($message), 1, -1);
             }
             $catalogue->set(($this->prefix ? $this->prefix . '.' : '') . $message, $message);
         }
     }
 }
Example #6
0
 /**
  * @param MacroNode $node
  * @param PhpWriter $writer
  *
  * @return string
  *
  * @throws Latte\CompileException
  */
 public function macroPhone(MacroNode $node, PhpWriter $writer)
 {
     $arguments = self::prepareMacroArguments($node->args);
     if ($arguments['phone'] === NULL) {
         throw new Latte\CompileException("Please provide phone number.");
     }
     return $writer->write('echo %escape($template->getPhoneNumberService()->format("' . $arguments['phone'] . '", "' . $arguments['country'] . '", ' . $arguments['format'] . '))');
 }
 public function macroSet(MacroNode $node, PhpWriter $writer)
 {
     $parts = Strings::replace($node->args, '~(\\s*(=>|=)\\s*|\\s+)~', '~~~', 1);
     $parts = Strings::split($parts, '/~~~/');
     $variable = $parts[0];
     $rest = $parts[1];
     return $writer->write($variable . ' = %modify(' . $rest . ')');
 }
Example #8
0
    /**
     * {imgLink $img [type]}
     * @param Latte\MacroNode $node
     * @param Latte\PhpWriter $writer
     * @return string
     */
    public function macroImgLink(Latte\MacroNode $node, Latte\PhpWriter $writer)
    {
        list($img, $type) = $this->getImageFromNode($node);
        return '
			$args = [' . ($type ? "'type' => '{$type}', " : NULL) . "'storage' => " . '$imageStorage' . ($node->args ? ', ' . $writer->formatArgs() : NULL) . '];
			echo ' . get_class($this) . '::imgLink(' . $img . ', $args);
		';
    }
Example #9
0
 public function macroRowCol(MacroNode $node, PhpWriter $writer)
 {
     $name = $node->tokenizer->fetchWord();
     if ($name === false) {
         throw new CompileException("Missing Column name in {{$node->name}}.");
     }
     $node->tokenizer->reset();
     return $writer->write("\n            \$rowClassName = 'row';\n            \$args = %node.array;\n            if (array_key_exists('row', \$args)) {\n                \$rowClassName = \$args['row'];\n            }\n            \n            echo '<div class=\"' . \$rowClassName . '\">';\n            echo '<div class=\"' .%node.word. '\">';\n        ");
 }
Example #10
0
 /**
  * @param Latte\MacroNode $node
  * @param Latte\PhpWriter $writer
  * @return string
  */
 public function attrImg(Latte\MacroNode $node, Latte\PhpWriter $writer)
 {
     if ($node->htmlNode->name === 'a') {
         $attr = 'href=';
     } else {
         $attr = 'src=';
     }
     return $writer->write('echo \' ' . $attr . '"\' . $imageStorage->get(%node.args)->getLink() . \'"\'');
 }
 /**
  * Add support of placeholder replacement into the standart translator macro
  *
  * @param MacroNode $node
  * @param PhpWriter $writer
  * @return strings
  */
 public function macroTranslate(MacroNode $node, PhpWriter $writer)
 {
     if ($node->closing) {
         return $writer->write('echo %modify($template->translate(ob_get_clean()))');
     } elseif ($node->isEmpty = $node->args !== '') {
         return $writer->write('echo %modify(Nedryse\\Latte\\Macros\\TemplateMacroSet::template(call_user_func_array(array($template, \'translate\'), array_merge(array(%node.word), $_args = ((is_array(current($_args = %node.array)) === TRUE) ? current($_args) : $_args))), array_merge($template->getParameters(), get_defined_vars(), $_args)))');
     } else {
         return 'ob_start()';
     }
 }
 /**
  * {ifEnabled ...}
  */
 public function macroIfNotEnabled(MacroNode $node, PhpWriter $writer)
 {
     if ($node->data->capture = $node->args === '') {
         return 'ob_start()';
     }
     if ($node->prefix === $node::PREFIX_TAG) {
         return $writer->write($node->htmlNode->closing ? 'if (array_pop($_l->ifs)) {' : 'if ($_l->ifs[] = (%node.args)) {');
     }
     return $writer->write('if (!call_user_func($this->filters->enabled, %node.word, %node.array?)) {');
 }
Example #13
0
 /**
  * {input ...}
  */
 public function macroInput(MacroNode $node, PhpWriter $writer)
 {
     $class = get_class($this);
     $words = $node->tokenizer->fetchWords();
     if (!$words) {
         throw new CompileException("Missing name in {{$node->name}}.");
     }
     $name = array_shift($words);
     return $writer->write(($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $_form[%0.word];' : '$_input = $_form[%0.word];') . 'echo ' . $class . '::input($_input->%1.raw->addAttributes(%node.array), $_input)', $name, $words ? 'getControlPart(' . implode(', ', array_map(array($writer, 'formatWord'), $words)) . ')' : 'getControl()');
 }
Example #14
0
 /**
  * Render button caption
  * @author Pavel Železný <*****@*****.**>
  * @param \Latte\MacroNode $node
  * @param \Latte\PhpWriter $writer
  * @return string
  */
 public function macroCaption(MacroNode $node, PhpWriter $writer)
 {
     if ($node->args !== '') {
         $code = '$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]);';
         $code .= 'echo $_input->getControl()->caption;';
         $code .= 'unset($_input);';
     } else {
         $code = 'echo isset($_buttonCaption) ? $_buttonCaption : NULL;';
     }
     return $writer->write($code);
 }
Example #15
0
 /**
  * @param MacroNode $node
  * @param PhpWriter $writer
  * @throws CompileException
  */
 public function macroEditrouble(MacroNode $node, PhpWriter $writer)
 {
     $name = $node->tokenizer->fetchWord();
     if ($name === false) {
         throw new CompileException("Missing editrouble name in {{$node->name}}.");
     }
     $args = $writer->write('%node.args');
     $attrs = " data-editrouble='\" . json_encode([" . ($args ? $args . ',' : '') . "'name' => \"" . $name . "\"]) . \"'";
     preg_match('#(^.*?>)(.*)(<.*\\z)#s', $node->content, $parts);
     $node->content = '<?php echo \'' . substr($parts[1], 0, -1) . '\' . ' . '($_presenter->editroubleConnector->checkPermission() ? ' . '"' . $attrs . '" : \'\') . \'>\'; ' . $writer->write('echo $_presenter->editroubleConnector->getContent("' . $name . '", [%node.args])') . ' ?>' . $parts[3];
 }
Example #16
0
 public function filter(MacroNode $node, PhpWriter $writer)
 {
     $files = array();
     while ($file = $node->tokenizer->fetchWord()) {
         $files[] = $this->wwwDir . '/' . ($this->pathResolver ? $this->pathResolver->expandResource($file) : $file);
     }
     if (!count($files)) {
         throw new CompileException('Missing file name in {js}');
     }
     eval('$args = ' . $writer->formatArray() . ';');
     return '$_control[\'js\']->render(\'' . join('\', \'', $files) . '\', array(\'config\' => ' . var_export($args, true) . '));';
 }
Example #17
0
    /**
     * overwritten default link macro, added fallback to link generator when neither control or presenter is available
     *
     * @param MacroNode
     * @param PhpWriter
     * @return string
     */
    public function macroLink(MacroNode $node, PhpWriter $writer)
    {
        return $writer->write('
		$_currentLinkBuilder = NULL;
		if(isset(' . ($node->name === 'plink' ? '$_presenter' : '$_control') . ')) {
			$_currentLinkBuilder = ' . ($node->name === 'plink' ? '$_presenter' : '$_control') . ';
		} else {
			$_currentLinkBuilder = $template->global->linkBuilder;
		}
		echo %escape(%modify($_currentLinkBuilder->link(%node.word, %node.array?)));
		');
    }
Example #18
0
 public function ettext(MacroNode $node, PhpWriter $writer)
 {
     $this->setFunction($node->name);
     $args = self::stringToArgs($node->args);
     $argsGettext = $this->getGettextArgs($args);
     $out = $this->function . '(' . implode(', ', $argsGettext) . ')';
     $key = (int) (substr($this->function, 0, 1) == 'd');
     $diff = $this->foundReplace($args[$key]);
     if ($diff) {
         $out = 'sprintf(' . $out . ', ' . implode(', ', array_slice($args, $diff)) . ')';
     }
     $this->function = NULL;
     return $writer->write('echo %modify(' . $out . ')');
 }
Example #19
0
 public function ettext(\Latte\MacroNode $node, \Latte\PhpWriter $writer)
 {
     $this->setFunction($node->name);
     $args = h4kuna\Template\LattePhpTokenizer::toArray($node);
     $argsGettext = $this->getGettextArgs($args);
     $out = $this->function . '(' . implode(', ', $argsGettext) . ')';
     $key = (int) (substr($this->function, 0, 1) == 'd');
     $diff = $this->foundReplace($args[$key]);
     if ($diff) {
         $out = 'sprintf(' . $out . ', ' . implode(', ', array_slice($args, $diff)) . ')';
     }
     $this->function = NULL;
     return $writer->write('echo %modify(' . $out . ')');
 }
Example #20
0
 /**
  * @param MacroNode $node
  * @param PhpWriter $writer
  * @return string
  * @throws CompileException
  */
 public function wizardStart(MacroNode $node, PhpWriter $writer)
 {
     $words = $node->tokenizer->fetchWords();
     if (!$words) {
         throw new CompileException('Missing control name in {wizard}');
     }
     $name = $writer->formatWord($words[0]);
     $method = isset($words[1]) ? ucfirst($words[1]) : '';
     $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}";
     $param = $writer->formatArray();
     if (!Strings::contains($node->args, '=>')) {
         $param = substr($param, $param[0] === '[' ? 1 : 6, -1);
         // removes array() or []
     }
     return ($name[0] === '$' ? "if (is_object({$name})) \$_l->tmp = {$name}; else " : '') . '$_l->tmp = $_control->getComponent(' . $name . '); ' . 'if (!$_l->tmp instanceof WebChemistry\\Forms\\Controls\\IWizard) throw new \\Exception(\'Wizard must be instance of WebChemistry\\Forms\\Controls\\IWizard\');' . '$wizard = new WebChemistry\\Forms\\Controls\\Wizard\\Facade($_l->tmp);';
 }
Example #21
0
 /**
  * New node is found. Returns FALSE to reject.
  *
  * @param MacroNode $node
  * @return bool
  * @throws Latte\CompileException
  */
 public function nodeOpened(MacroNode $node)
 {
     if ($node->prefix) {
         return false;
     }
     if ($node->modifiers) {
         throw new Latte\CompileException("Modifiers are not allowed in {{$node->name}}.");
     }
     $file = $node->tokenizer->fetchWord();
     if ($file === false) {
         throw new Latte\CompileException("Missing file name in {{$node->name}}.");
     } elseif ($node->tokenizer->fetchWord()) {
         throw new Latte\CompileException("Multiple arguments are not supported in {{$node->name}}.");
     }
     $node->isEmpty = true;
     $node->modifiers = '|safeurl|escape';
     // auto-escape
     $writer = Latte\PhpWriter::using($node, $this->compiler);
     if ($this->debugMode) {
         $node->openingCode = $writer->write('<?php echo %modify(%1.word . \'?\' . Nepada\\BustCache\\Helpers::timestamp(%0.var . %1.word)) ?>', $this->wwwDir, $file);
     } elseif (preg_match('#^(["\']?)[^$\'"]*\\1$#', $file)) {
         // Static path
         $file = trim($file, '"\'');
         $url = $file . '?' . Helpers::hash($this->wwwDir . $file);
         $url = Latte\Runtime\Filters::safeUrl($url);
         $node->openingCode = $writer->write('<?php echo %escape(%var) ?>', $url);
     } else {
         $node->openingCode = $writer->write('<?php echo %modify(%1.word . \'?\' . Nepada\\BustCache\\Helpers::hash(%0.var . %1.word)) ?>', $this->wwwDir, $file);
     }
 }
Example #22
0
 /**
  * {ifCurrent destination [,] [params]}
  */
 public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
 {
     if ($node->modifiers) {
         trigger_error("Modifiers are not allowed in {{$node->name}}", E_USER_WARNING);
     }
     return $writer->write($node->args ? 'if ($_presenter->isLinkCurrent(%node.word, %node.array?)) {' : 'if ($_presenter->getLastCreatedRequestFlag("current")) {');
 }
 public function macroLabel(Latte\MacroNode $node, Latte\PhpWriter $writer)
 {
     while ($node->parentNode) {
         if ($node->parentNode->name == 'gpspicker') {
             $cmd = 'if ($_label = $_gpspicker->getPartialLabel(%node.word)) echo $_label->addAttributes(%node.array)';
             if ($node->isEmpty = substr($node->args, -1) === '/') {
                 $node->setArgs(substr($node->args, 0, -1));
                 return $writer->write($cmd);
             } else {
                 return $writer->write($cmd . '->startTag()');
             }
         }
         $node = $node->parentNode;
     }
     throw new CompileException('{gpspicker:label} can be used only within {gpspicker} macro.');
 }
Example #24
0
 /**
  * @param Nette\Latte\MacroNode $node
  * @param Nette\Latte\PhpWriter $writer
  * @return string
  * @throws Nette\Latte\CompileException
  */
 public function macroAttrImg(MacroNode $node, PhpWriter $writer)
 {
     $this->isUsed = TRUE;
     $arguments = Helpers::prepareMacroArguments($node->args);
     if ($arguments["name"] === NULL) {
         throw new Nette\Latte\CompileException("Please provide filename.");
     }
     $namespace = $arguments["namespace"];
     unset($arguments["namespace"]);
     $arguments = array_map(function ($value) use($writer) {
         return $value ? $writer->formatWord($value) : 'NULL';
     }, $arguments);
     $command = '$_imagePipe';
     $command .= $namespace !== NULL ? '->setNamespace(' . $writer->formatWord(trim($namespace)) . ')' : '';
     $command .= '->request(' . implode(", ", $arguments) . ')';
     return $writer->write('?> src="<?php echo %escape(' . $writer->formatWord($command) . ')?>" <?php');
 }
Example #25
0
	/**
	 * New node is found.
	 * @return bool
	 */
	public function nodeOpened(Latte\MacroNode $node)
	{
		$this->used = TRUE;
		$node->isEmpty = FALSE;
		$node->openingCode = Latte\PhpWriter::using($node)
			->write('<?php if (Nette\Bridges\CacheLatte\CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>',
				Nette\Utils\Random::generate()
			);
	}
Example #26
0
 /**
  * New node is found.
  * @return bool
  */
 public function nodeOpened(Latte\MacroNode $node)
 {
     if ($node->modifiers) {
         trigger_error("Modifiers are not allowed in {{$node->name}}", E_USER_WARNING);
     }
     $this->used = TRUE;
     $node->isEmpty = FALSE;
     $node->openingCode = Latte\PhpWriter::using($node)->write('<?php if (Nette\\Bridges\\CacheLatte\\CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>', Nette\Utils\Random::generate());
 }
Example #27
0
 /**
  * New node is found.
  * @return bool
  */
 public function nodeOpened(Latte\MacroNode $node)
 {
     if ($node->modifiers) {
         throw new Latte\CompileException('Modifiers are not allowed in ' . $node->getNotation());
     }
     $this->used = TRUE;
     $node->empty = FALSE;
     $node->openingCode = Latte\PhpWriter::using($node)->write('<?php if (Nette\\Bridges\\CacheLatte\\CacheMacro::createCache($this->global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) { ?>', Nette\Utils\Random::generate());
 }
 private function compile(MacroNode $node, $def)
 {
     $node->tokenizer->reset();
     $writer = PhpWriter::using($node, $this->compiler);
     if (is_string($def)) {
         return $writer->write($def);
     } else {
         return callback($def)->invoke($node, $writer);
     }
 }
 public function macroKeyEnd(MacroNode $node, PhpWriter $writer)
 {
     $tag = $node->htmlNode->name;
     $arg = $node->htmlNode->macroAttrs['key'];
     $start = $writer->write('if (!empty($_components[%word])) { echo $_components[%word]; unset($_components[%word]); }', $arg, $arg, $arg, $arg);
     $node->content = "<?php {$start} else { ?> {$node->content} <?php } ?>";
     if ($tag === 'Void') {
         $node->content = preg_replace("#<{$tag}.*>#U", '', $node->content, 1);
         $node->content = preg_replace("#</{$tag}>#", '', $node->content, 1);
     }
 }
Example #30
0
 /**
  * {js [:renderType] file[, file2]}
  *
  * @param \Latte\MacroNode $node
  * @param \Latte\PhpWriter $writer
  * @return string
  * @throws CompileException
  */
 public function macroJs(\Latte\MacroNode $node, \Latte\PhpWriter $writer)
 {
     $words = $node->tokenizer->fetchWords();
     if (!$words) {
         throw new CompileException("Missing args in {js}");
     }
     $method = isset($words[1]) ? ucfirst($words[1]) : '';
     $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}";
     if (isset($words[0]) && !isset($words[1])) {
         $node->tokenizer->reset();
     }
     $param = $writer->formatArray();
     if (!Strings::contains($node->args, '=>')) {
         $param = substr($param, 6, -1);
         // removes array()
     }
     return 'if (($_ctrl=$_control->getComponent("js")) instanceof Nette\\Application\\UI\\IRenderable) $_ctrl->redrawControl(NULL, FALSE); ' . ($node->modifiers === '' ? "\$_ctrl->{$method}({$param})" : $writer->write("ob_start(); \$_ctrl->{$method}({$param}); echo %modify(ob_get_clean())"));
 }