|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Htsl\ReadingBuffer; |
|
4
|
|
|
|
|
5
|
|
|
use Htsl\Htsl; |
|
6
|
|
|
|
|
7
|
|
|
//////////////////////////////////////////////////////////////// |
|
8
|
|
|
|
|
9
|
|
|
class FileBuffer extends Contracts\ABuffer |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* File handle. |
|
13
|
|
|
* |
|
14
|
|
|
* @access private |
|
15
|
|
|
* |
|
16
|
|
|
* @var resource |
|
17
|
|
|
*/ |
|
18
|
|
|
private $handle; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructing a file buffer reading HTSL content from file system. |
|
22
|
|
|
* |
|
23
|
|
|
* @access public |
|
24
|
|
|
* |
|
25
|
|
|
* @param Htsl $htsl Main Htsl object |
|
26
|
|
|
* @param string $filePath |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct( Htsl$htsl, string$filePath ) |
|
29
|
|
|
{ |
|
30
|
|
|
substr($filePath,-5)==='.htsl' or $filePath.= '.htsl'; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
if( !file_exists($filePath) || !is_file($filePath) ){ |
|
33
|
|
|
throw new \Exception("File $filePath not exists.", 1); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->filePath= $filePath; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$this->handle= fopen($filePath,'r'); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
parent::__construct($htsl); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Destructor |
|
45
|
|
|
* |
|
46
|
|
|
* @access public |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __destruct() |
|
49
|
|
|
{ |
|
50
|
|
|
fclose($this->handle); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Getting first line or next line. |
|
55
|
|
|
* |
|
56
|
|
|
* @access public |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Htsl\ReadingBuffer\Line |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getLine():Line |
|
61
|
|
|
{ |
|
62
|
|
|
while( "\n"===$content= fgets($this->handle) ); |
|
63
|
|
|
|
|
64
|
|
|
return new Line($content); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Getting another file reference file of this buffer. |
|
69
|
|
|
* |
|
70
|
|
|
* @access public |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $fileName |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
|
|
public function goSide( $fileName ):parent |
|
77
|
|
|
{ |
|
78
|
|
|
$filePath= $this->htsl->getFilePath($fileName,dirname($this->filePath)); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return new static($this->htsl,$filePath); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
|
|
|
|
|
83
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.