Completed
Pull Request — master (#110)
by Roman
11:00
created

TranslateMacros::containsOnlyOneWord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Translation\Latte;
12
13
use Kdyby;
14
use Latte;
15
use Latte\Compiler;
16
use Latte\MacroNode;
17
use Latte\PhpWriter;
18
use Latte\Macros\MacroSet;
19
20
21
22
/**
23
 * @author Filip Procházka <[email protected]>
24
 */
25
class TranslateMacros extends MacroSet
26
{
27
28
	public static function install(Compiler $compiler)
29
	{
30
		$me = new static($compiler);
31
		/** @var TranslateMacros $me */
32
33
		$me->addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']);
34
		$me->addMacro('translator', [$me, 'macroDomain'], [$me, 'macroDomainEnd']);
35
36
		return $me;
37
	}
38
39
40
41
	/**
42
	 * {_$var |modifiers}
43
	 * {_$var, $count |modifiers}
44
	 * {_"Sample message", $count |modifiers}
45
	 * {_some.string.id, $count |modifiers}
46
	 */
47
	public function macroTranslate(MacroNode $node, PhpWriter $writer)
48
	{
49
		$nette24 = (class_exists('Latte\Runtime\FilterInfo')) ? TRUE : FALSE;
50
		if ($node->closing) {
51
			if ($nette24) {
52
				if (substr($node->modifiers, -7) === '|escape') {
53
					$node->modifiers = substr($node->modifiers, 0, -7);
54
				}
55
				return $writer->write('$_fi = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", $_fi, ob_get_clean()))', $node->context[0]);
56
			} else {
57
				return $writer->write('echo %modify($template->translate(ob_get_clean()))');
58
			}
59
60
		} elseif ($node->empty = ($node->args !== '')) {
61
			if ($this->containsOnlyOneWord($node)) {
62
				return $writer->write(($nette24)
63
					? 'echo %modify(call_user_func($this->filters->translate, %node.word))'
64
					: 'echo %modify($template->translate(%node.word))'
65
				);
66
67
			} else {
68
				return $writer->write(($nette24)
69
					? 'echo %modify(call_user_func($this->filters->translate, %node.word, %node.args))'
70
					: 'echo %modify($template->translate(%node.word, %node.args))'
71
				);
72
			}
73
74
		} else {
75
			return ($nette24)
76
				? 'ob_start()'
77
				: 'ob_start(function () {})';
78
		}
79
	}
80
81
82
83
	/**
84
	 * @param MacroNode $node
85
	 * @param PhpWriter $writer
86
	 */
87
	public function macroDomain(MacroNode $node, PhpWriter $writer)
88
	{
89
		if ($node->isEmpty) {
0 ignored issues
show
Deprecated Code introduced by
The property Latte\MacroNode::$isEmpty has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
90
			throw new Latte\CompileException("Expected message prefix, none given");
91
		}
92
93
		$node->isEmpty = $node->isEmpty || (substr($node->args, -1) === '/');
0 ignored issues
show
Deprecated Code introduced by
The property Latte\MacroNode::$isEmpty has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
94
		return $writer->write('$_translator = \Kdyby\Translation\PrefixedTranslator::register($template, %node.word);');
95
	}
96
97
98
99
	/**
100
	 * @param MacroNode $node
101
	 * @param PhpWriter $writer
102
	 */
103
	public function macroDomainEnd(MacroNode $node, PhpWriter $writer)
104
	{
105
		if ($node->content !== NULL) {
106
			return $writer->write('$_translator->unregister($template);');
107
		}
108
	}
109
110
111
112
	private function containsOnlyOneWord(MacroNode $node)
113
	{
114
		if (method_exists($node->tokenizer, 'fetchUntil')) {
115
			$result = trim($node->tokenizer->fetchUntil(',')) === trim($node->args);
116
117
		} else {
118
			$result = trim($node->tokenizer->joinUntil(',')) === trim($node->args);
119
		}
120
121
		$node->tokenizer->reset();
122
		return $result;
123
	}
124
125
}
126