Completed
Pull Request — master (#111)
by Roman
13:16 queued 10:39
created

TranslateMacros::macroTranslate()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 37
Code Lines 23

Duplication

Lines 18
Ratio 48.65 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 18
loc 37
rs 4.909
cc 9
eloc 23
nc 9
nop 2
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
		if (class_exists('Latte\Runtime\FilterInfo')) { // Nette 2.4
50
			if ($node->closing) {
51
				if (substr($node->modifiers, -7) === '|escape') {
52
					$node->modifiers = substr($node->modifiers, 0, -7);
53
				}
54
				return $writer->write('$_fi = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", $_fi, ob_get_clean()))', $node->context[0]);
55
56 View Code Duplication
			} elseif ($node->empty = ($node->args !== '')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
				if ($this->containsOnlyOneWord($node)) {
58
					return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word))');
59
60
				} else {
61
					return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word, %node.args))');
62
				}
63
64
			} else {
65
				return 'ob_start(function () {})';
66
			}
67
		} else { // <= Nette 2.3
68
			if ($node->closing) {
69
				return $writer->write('echo %modify($template->translate(ob_get_clean()))');
70
71 View Code Duplication
			} elseif ($node->isEmpty = ($node->args !== '')) {
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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
				if ($this->containsOnlyOneWord($node)) {
73
					return $writer->write('echo %modify($template->translate(%node.word))');
74
75
				} else {
76
					return $writer->write('echo %modify($template->translate(%node.word, %node.args))');
77
				}
78
79
			} else {
80
				return 'ob_start()';
81
			}
82
		}
83
	}
84
85
86
87
	/**
88
	 * @param MacroNode $node
89
	 * @param PhpWriter $writer
90
	 */
91
	public function macroDomain(MacroNode $node, PhpWriter $writer)
92
	{
93
		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...
94
			throw new Latte\CompileException("Expected message prefix, none given");
95
		}
96
97
		$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...
98
		return $writer->write('$_translator = \Kdyby\Translation\PrefixedTranslator::register($template, %node.word);');
99
	}
100
101
102
103
	/**
104
	 * @param MacroNode $node
105
	 * @param PhpWriter $writer
106
	 */
107
	public function macroDomainEnd(MacroNode $node, PhpWriter $writer)
108
	{
109
		if ($node->content !== NULL) {
110
			return $writer->write('$_translator->unregister($template);');
111
		}
112
	}
113
114
115
116
	private function containsOnlyOneWord(MacroNode $node)
117
	{
118
		if (method_exists($node->tokenizer, 'fetchUntil')) {
119
			$result = trim($node->tokenizer->fetchUntil(',')) === trim($node->args);
120
121
		} else {
122
			$result = trim($node->tokenizer->joinUntil(',')) === trim($node->args);
123
		}
124
125
		$node->tokenizer->reset();
126
		return $result;
127
	}
128
129
}
130