1
+ <?php
2
+
3
+ namespace STS \Phpinfo \Collections ;
4
+
5
+ use Illuminate \Support \Collection ;
6
+
7
+ /**
8
+ * Used by our TextParser to help walk through the CLI text version
9
+ */
10
+ class Lines extends Collection
11
+ {
12
+ protected int $ index = 0 ;
13
+
14
+ /**
15
+ * Moves one line forward, regardless of what it is
16
+ */
17
+ public function step (): string |null
18
+ {
19
+ $ this ->index ++;
20
+
21
+ return $ this ->current ();
22
+ }
23
+
24
+ /**
25
+ * Advances to the next usable line and returns the new line
26
+ */
27
+ public function advance (): string |null
28
+ {
29
+ do {
30
+ $ this ->index ++;
31
+ } while ($ this ->shouldIgnore ());
32
+
33
+ return $ this ->current ();
34
+ }
35
+
36
+ /**
37
+ * Similar to the above advance() method, except this returns
38
+ * the CURRENT line before advancing
39
+ */
40
+ public function consume (): string |null
41
+ {
42
+ $ current = $ this ->current ();
43
+
44
+ $ this ->advance ();
45
+
46
+ return $ current ;
47
+ }
48
+
49
+ public function consumeUntil (callable $ callback ): Collection
50
+ {
51
+ $ lines = new static ;
52
+
53
+ do {
54
+ $ current = $ this ->current ();
55
+ $ lines ->push ($ current );
56
+ $ this ->step ();
57
+ } while (!$ callback ($ current ));
58
+
59
+ return $ lines ;
60
+ }
61
+
62
+ public function shouldIgnore (): bool
63
+ {
64
+ return $ this ->currentIsBlank ()
65
+ // || str_contains($this->current(), '_______________________________________________________________________')
66
+ || in_array ($ this ->current (), ['Configuration ' ]);
67
+ }
68
+
69
+ public function currentIsBlank (): bool
70
+ {
71
+ return $ this ->current () === '' ;
72
+ }
73
+
74
+ public function previousIsBlank (): bool
75
+ {
76
+ return $ this ->previous () === '' ;
77
+ }
78
+
79
+ public function nextIsBlank (): bool
80
+ {
81
+ return $ this ->next () === '' ;
82
+ }
83
+
84
+ public function current (): string |null
85
+ {
86
+ return $ this ->get ($ this ->index );
87
+ }
88
+
89
+ public function previous (): string |null
90
+ {
91
+ return $ this ->get ($ this ->index - 1 );
92
+ }
93
+
94
+ public function next (): string |null
95
+ {
96
+ return $ this ->get ($ this ->index + 1 );
97
+ }
98
+
99
+ public function isDivider (): bool
100
+ {
101
+ return str_contains ($ this ->current (), '_______________________________________________________________________ ' );
102
+ }
103
+
104
+ public function isModuleName (): bool
105
+ {
106
+ return !$ this ->hasItems ()
107
+ && $ this ->nextIsBlank ()
108
+ && strlen ($ this ->current ()) < 50 ;
109
+ }
110
+
111
+ public function isGroupTitle (): bool
112
+ {
113
+ if (str_contains ($ this ->current (), " " )) {
114
+ return true ;
115
+ }
116
+
117
+ return !$ this ->hasItems ()
118
+ && !$ this ->nextIsBlank ()
119
+ && strlen ($ this ->current ()) < 50 ;
120
+ }
121
+
122
+ public function isTableHeading (): bool
123
+ {
124
+ return in_array ($ this ->items ()->first (), ['Directive ' , 'Variable ' , 'Contribution ' , 'Module ' ]);
125
+ }
126
+
127
+ public function isNote (): bool
128
+ {
129
+ return !$ this ->hasItems ()
130
+ && !$ this ->isDivider ()
131
+ && !$ this ->isGroupTitle ()
132
+ && strlen ($ this ->current ()) > 50 ;
133
+ }
134
+
135
+ public function items (): Items
136
+ {
137
+ $ items = Items::make (explode (" => " , $ this ->current ()));
138
+
139
+ // A few weird cases we need to fix
140
+ if ($ items ->first () == "Features " && $ items ->count () == 1 ) {
141
+ $ items ->put (1 , null );
142
+ }
143
+
144
+ return $ items ;
145
+ }
146
+
147
+ public function hasItems (): bool
148
+ {
149
+ return $ this ->items ()->count () > 1 ;
150
+ }
151
+
152
+ public function consumeItems (): Items
153
+ {
154
+ $ items = $ this ->items ();
155
+
156
+ $ this ->advance ();
157
+
158
+ return $ items ;
159
+ }
160
+ }
0 commit comments