forked from spoon/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Writer.php
104 lines (92 loc) · 1.83 KB
/
Writer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <[email protected]>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
namespace Spoon\Template;
/**
* This class is used to do the actually writing of the compiled templates.
*
* @author Davy Hellemans <[email protected]>
*/
class Writer
{
/**
* Current indentation
*
* @var int
*/
protected $indentation = 0;
/**
* Current line number
*
* @var int
*/
protected $line;
/**
* String containing the source written so far
*
* @var string
*/
protected $source;
/**
* Fetch the written source.
*
* @return string
*/
public function getSource()
{
return $this->source;
}
/**
* Indents one or more levels.
*
* @param int[optional] $level
* @return \Spoon\Template\Writer
*/
public function indent($level = 1)
{
$this->indentation += (int) $level;
return $this;
}
/**
* Outdents one or more levels.
*
* @param int[optional] $level
* @return \Spoon\Template\Writer
*/
public function outdent($level = 1)
{
$this->indentation -= (int) $level;
return $this;
}
/**
* Write to the source, optionally providing a line number.
*
* @param string $value
* @param int[optional] $line
* @return \Spoon\Template\Writer
*/
public function write($value, $line = null)
{
// add current indentation
$prefix = ($this->indentation > 0) ? str_repeat("\t", $this->indentation) : '';
// only add line number once
if($line !== null && $line > $this->line)
{
// add blank line (expect for the first line)
if($line != 1)
{
$this->source .= "\n";
}
$this->line = (int) $line;
$this->source .= $prefix . '// line ' . $this->line . "\n";
}
$this->source .= $prefix . (string) $value;
return $this;
}
}