ImageMacro::macroImage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rostenkowski\Resize\Macro;
4
5
6
use Latte\Compiler;
7
use Latte\MacroNode;
8
use Latte\Macros\MacroSet;
9
use Latte\PhpWriter;
10
11
/**
12
 * Latte template engine image macros
13
 */
14
class ImageMacro extends MacroSet
15
{
16
17
18
	/**
19
	 * Installs the macro set into the template compiler.
20
	 *
21
	 * @param Compiler $compiler
22
	 * @return void|static
23
	 */
24
	public static function install(Compiler $compiler)
25
	{
26
		$macroSet = new static($compiler);
27
		$macroSet->setup();
28
	}
29
30
31
	/**
32
	 * Sets the template macros up.
33
	 */
34
	public function setup()
35
	{
36
		$this->addMacro('src', NULL, NULL, [$this, 'macroSrc']);
37
		$this->addMacro('bg', NULL, NULL, [$this, 'macroBg']);
38
		$this->addMacro('image', [$this, 'macroImageBegin'], NULL, [$this, 'macroImage']);
39
		$this->addMacro('crop', [$this, 'macroCropBegin'], NULL, [$this, 'macroCrop']);
40
	}
41
42
43
	/**
44
	 * Renders the `n:src="$image"` macro.
45
	 *
46
	 * @param MacroNode $node
47
	 * @param PhpWriter $writer
48
	 * @return string
49
	 */
50
	public function macroBg(MacroNode $node, PhpWriter $writer)
51
	{
52
		return $writer->write(' ?> style="background-image: url(\'<?php $imageRequest = Rostenkowski\Resize\Requests\ImageRequest::crop(%node.word, %node.array?); echo %escape($__imagestore->link($imageRequest)); ?>\');"<?php ');
53
	}
54
55
56
	/**
57
	 * Renders the `n:crop="$image"` macro.
58
	 *
59
	 * @param MacroNode $node
60
	 * @param PhpWriter $writer
61
	 * @return string
62
	 */
63
	public function macroCrop(MacroNode $node, PhpWriter $writer)
64
	{
65
		return $writer->write(' ?> src="<?php $imageRequest = Rostenkowski\Resize\Requests\ImageRequest::crop(%node.word, %node.array?); echo %escape($__imagestore->link($imageRequest)); ?>"<?php ');
66
	}
67
68
69
	/**
70
	 * Renders the `{crop $image ...}` macro.
71
	 *
72
	 * @param MacroNode $node
73
	 * @param PhpWriter $writer
74
	 * @return string
75
	 */
76
	public function macroCropBegin(MacroNode $node, PhpWriter $writer)
77
	{
78
		return $writer->write('$imageRequest = Rostenkowski\Resize\Requests\ImageRequest::crop(%node.word, %node.array?); echo %escape($__imagestore->link($imageRequest));');
79
	}
80
81
82
	/**
83
	 * Renders the `n:image="$image"` macro.
84
	 *
85
	 * @param MacroNode $node
86
	 * @param PhpWriter $writer
87
	 * @return string
88
	 */
89
	public function macroImage(MacroNode $node, PhpWriter $writer)
90
	{
91
		return $writer->write(' ?> href="<?php $imageRequest = Rostenkowski\Resize\Requests\ImageRequest::fromMacro(%node.word, %node.array?); echo %escape($__imagestore->link($imageRequest)); ?>"<?php ');
92
	}
93
94
95
	/**
96
	 * Renders the `{image $image ...}` macro.
97
	 *
98
	 * @param MacroNode $node
99
	 * @param PhpWriter $writer
100
	 * @return string
101
	 */
102
	public function macroImageBegin(MacroNode $node, PhpWriter $writer)
103
	{
104
		return $writer->write('$imageRequest = Rostenkowski\Resize\Requests\ImageRequest::fromMacro(%node.word, %node.array?); echo %escape($__imagestore->link($imageRequest));');
105
	}
106
107
108
	/**
109
	 * Renders the `n:src="$image"` macro.
110
	 *
111
	 * @param MacroNode $node
112
	 * @param PhpWriter $writer
113
	 * @return string
114
	 */
115
	public function macroSrc(MacroNode $node, PhpWriter $writer)
116
	{
117
		return $writer->write('$imageRequest = Rostenkowski\Resize\Requests\ImageRequest::fromMacro(%node.word, %node.array?); ?> src="<?php echo %escape($__imagestore->link($imageRequest)); ?>"<?php ');
118
	}
119
120
}
121