Htsl::setFileGetter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Htsl;
4
5
use Htsl\ReadingBuffer\Contracts\ABuffer;
6
use Htsl\ReadingBuffer\StringBuffer;
7
use Htsl\ReadingBuffer\FileBuffer;
8
use Htsl\Parser\Document;
9
use Htsl\Helper\DefaultConfigs;
10
use Htsl\Helper\IConfigProvider;
11
12
////////////////////////////////////////////////////////////////
13
14
/**
15
 * @link http://htsl.fenzland.com/ Document of Htsl.php
16
 *
17
 * @license https://opensource.org/licenses/MIT MIT
18
 *
19
 * @author Fenz <[email protected]>
20
 */
21
class Htsl implements IConfigProvider
22
{
23
	/**
24
	 * Getter of file content.
25
	 *
26
	 * @var callable
27
	 *
28
	 * @access protected
29
	 */
30
	protected $fileGetter;
31
32
	/**
33
	 * The base path.
34
	 *
35
	 * @var string
36
	 *
37
	 * @access protected
38
	 */
39
	protected $basePath;
40
41
	/**
42
	 * Configurations.
43
	 *
44
	 * @var array
45
	 *
46
	 * @access protected
47
	 */
48
	protected $config;
49
50
	/**
51
	 * Constructor of HTSL
52
	 *
53
	 * @api
54
	 *
55
	 * @access public
56
	 *
57
	 * @param array $config
58
	 */
59
	public function __construct( array$config=[] )
60
	{
61
		$this->config= array_replace_recursive($this->getDefaultConfigs(),$config);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
62
	}
63
64
	/**
65
	 * Parsing a HTSL code string into HTML or PHP code string.
66
	 *
67
	 * @api
68
	 *
69
	 * @access public
70
	 *
71
	 * @param  string $content
72
	 *
73
	 * @return string
74
	 */
75
	public function parse( string$content ):string
76
	{
77
		return $this->execute(new StringBuffer($this,$content));
78
	}
79
80
	/**
81
	 * Compiling a HTSL file into a HTML or PHP file.
82
	 *
83
	 * @api
84
	 *
85
	 * @access public
86
	 *
87
	 * @param  string $fromFile
88
	 * @param  string $toFile
89
	 *
90
	 * @return int|string
91
	 */
92
	public function compile( string$fromFile, string$toFile='' )
93
	{
94
		$fromFile= $this->getFilePath($fromFile);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $fromFile. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
95
96
		$result= $this->execute(new FileBuffer($this,$fromFile));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
97
98
		if( $toFile ){
99
			return file_put_contents($toFile,$result);
100
		}else{
101
			return $result;
102
		}
103
	}
104
105
	/**
106
	 * Setting the file getter.
107
	 *
108
	 * @api
109
	 *
110
	 * @access public
111
	 *
112
	 * @param callable $fileGetter
113
	 */
114
	public function setFileGetter( callable$fileGetter ):self
115
	{
116
		$this->fileGetter=$fileGetter;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
117
118
		return $this;
119
	}
120
121
	/**
122
	 * Setting the base path of the HTSL project to parse.
123
	 *
124
	 * @api
125
	 *
126
	 * @access public
127
	 *
128
	 * @param string $basePath
129
	 *
130
	 * @return self
131
	 */
132
	public function setBasePath( string$basePath ):self
133
	{
134
		$this->basePath= '/'===substr($basePath,-1) ? substr($basePath,0,-1) : $basePath;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
135
136
		return $this;
137
	}
138
139
	/**
140
	 * Returning whether the debug model is on or off.
141
	 *
142
	 * @api
143
	 *
144
	 * @access public
145
	 *
146
	 * @return boolean
147
	 */
148
	public function isDebug():bool
149
	{
150
		return !!$this->getConfig('debug');
151
	}
152
153
	/**
154
	 * Executing the parsing.
155
	 *
156
	 * @internal
157
	 *
158
	 * @access protected
159
	 *
160
	 * @param  \Htsl\ReadingBuffer\Contracts\ABuffer $buffer
161
	 *
162
	 * @return string
163
	 */
164
	protected function execute( ABuffer$buffer ):string
165
	{
166
		return (new Document($this,$buffer))->content;
167
	}
168
169
	/**
170
	 * Getting the config of Htsl.
171
	 *
172
	 * @internal
173
	 *
174
	 * @access public
175
	 *
176
	 * @param  string $keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $keys not be string[]?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
177
	 *
178
	 * @return mixed
179
	 */
180
	public function getConfig( string...$keys )
181
	{
182
		$result= $this->config;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
183
184
		foreach( $keys as $key ){
185
			if( !isset($result[$key]) )
186
				{ return null; }
187
188
			$result= $result[$key];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
189
		}
190
191
		return $result;
192
	}
193
194
	/**
195
	 * Getting the real file path of the HTSL file by relative path.
196
	 *
197
	 * @internal
198
	 *
199
	 * @access public
200
	 *
201
	 * @param  string      $filePath
202
	 * @param  string|null $path
203
	 *
204
	 * @return string
205
	 */
206
	public function getFilePath( string$filePath, string$path=null ):string
207
	{
208
		if( !isset($this->basePath) )
209
			{ throw new \Exception('BasePath musbe set.'); }
210
211
		if( !strlen($filePath) )
212
			{ throw new \Exception('FilePath cannot be empty.'); }
213
214
		if( '/'===$filePath{0} ){
215
			if( is_null($path) )
216
				{ return $filePath; }
217
			else
218
				{ return $this->basePath.$filePath; }
219
		}else{
220
			if( !strlen($path) )
221
				{ return $this->basePath.'/'.$filePath; }
222
			elseif( '/'===substr($path,-1) )
223
				{ return $path.$filePath; }
224
			else
225
				{ return $path.'/'.$filePath; }
226
		}
227
228
	}
229
230
	/**
231
	 * Getting the content of file.
232
	 *
233
	 * @internal
234
	 *
235
	 * @access public
236
	 *
237
	 * @param  string $filePath
238
	 *
239
	 * @return string
240
	 */
241
	public function getFileContent( string$filePath ):string
242
	{
243
		return isset($this->fileGetter) ? $this->fileGetter($filePath) : file_get_contents($filePath);
244
	}
245
246
	/**
247
	 * Getting default config.
248
	 *
249
	 * @internal
250
	 *
251
	 * @access private
252
	 *
253
	 * @return array
254
	 */
255
	private function getDefaultConfigs():array
256
	{
257
		return DefaultConfigs::get();
258
	}
259
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
260