1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
namespace gossi\codegen\utils; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A writer implementation. |
22
|
|
|
* |
23
|
|
|
* This may be used to simplify writing well-formatted code. |
24
|
|
|
* |
25
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Writer { |
28
|
|
|
|
29
|
|
|
private $content = ''; |
30
|
|
|
private $indentationLevel = 0; |
31
|
|
|
private $indentation; |
32
|
|
|
|
33
|
|
|
private $options = [ |
34
|
|
|
'indentation_character' => "\t", |
35
|
|
|
'indentation_size' => 1 |
36
|
|
|
]; |
37
|
|
|
|
38
|
23 |
|
public function __construct($options = []) { |
39
|
23 |
|
$this->options = array_merge($this->options, $options); |
40
|
|
|
|
41
|
23 |
|
$this->indentation = str_repeat($this->options['indentation_character'], |
42
|
23 |
|
$this->options['indentation_size']); |
43
|
23 |
|
} |
44
|
|
|
|
45
|
22 |
|
public function indent() { |
46
|
22 |
|
$this->indentationLevel += 1; |
47
|
|
|
|
48
|
22 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
21 |
|
public function outdent() { |
52
|
21 |
|
$this->indentationLevel -= 1; |
53
|
|
|
|
54
|
21 |
|
if ($this->indentationLevel < 0) { |
55
|
|
|
throw new \RuntimeException('The identation level cannot be less than zero.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
21 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @param string $content |
64
|
|
|
*/ |
65
|
23 |
|
public function writeln($content = '') { |
66
|
23 |
|
$this->write($content . "\n"); |
67
|
|
|
|
68
|
23 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param string $content |
74
|
|
|
*/ |
75
|
23 |
|
public function write($content) { |
76
|
|
|
|
77
|
23 |
|
if (is_string($content)) { |
78
|
23 |
|
$lines = explode("\n", $content); |
79
|
23 |
|
for ($i = 0, $c = count($lines); $i < $c; $i++) { |
80
|
23 |
|
if ($this->indentationLevel > 0 |
81
|
23 |
|
&& !empty($lines[$i]) |
82
|
23 |
|
&& (empty($this->content) || "\n" === substr($this->content, -1))) { |
83
|
10 |
|
$this->content .= str_repeat($this->indentation, $this->indentationLevel); |
84
|
10 |
|
} |
85
|
|
|
|
86
|
23 |
|
$this->content .= $lines[$i]; |
87
|
|
|
|
88
|
23 |
|
if ($i + 1 < $c) { |
89
|
23 |
|
$this->content .= "\n"; |
90
|
23 |
|
} |
91
|
23 |
|
} |
92
|
23 |
|
} elseif(is_null($content)) { |
93
|
|
|
$this->content .= 'null'; |
94
|
|
|
} |
95
|
|
|
|
96
|
23 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
21 |
|
public function rtrim() { |
100
|
21 |
|
$addNl = "\n" === substr($this->content, -1); |
101
|
21 |
|
$this->content = rtrim($this->content); |
102
|
|
|
|
103
|
21 |
|
if ($addNl) { |
104
|
21 |
|
$this->content .= "\n"; |
105
|
21 |
|
} |
106
|
|
|
|
107
|
21 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
public function endsWith($search) { |
111
|
5 |
|
return substr($this->content, -strlen($search)) === $search; |
112
|
|
|
} |
113
|
|
|
|
114
|
18 |
|
public function reset() { |
115
|
18 |
|
$this->content = ''; |
116
|
18 |
|
$this->indentationLevel = 0; |
117
|
|
|
|
118
|
18 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
23 |
|
public function getContent() { |
122
|
23 |
|
return $this->content; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|