22
33namespace STS \Phpinfo \Parsers ;
44
5- use Illuminate \Support \Collection ;
65use STS \Phpinfo \Collections \Lines ;
76use STS \Phpinfo \Models \Config ;
87use STS \Phpinfo \Models \Group ;
@@ -16,7 +15,7 @@ class TextParser extends Result
1615 public static function canParse (string $ contents ): bool
1716 {
1817 return str_contains (str_replace ("\r\n" , "\n" , $ contents ), "phpinfo() \nPHP Version " )
19- && count (explode ("_______________________________________________________________________ " , $ contents )) === 3 ;
18+ && count (explode ("_______________________________________________________________________ " , $ contents )) >= 2 ;
2019 }
2120
2221 protected function parse (): void
@@ -27,7 +26,7 @@ protected function parse(): void
2726 // Our first line is just phpinfo()
2827 $ this ->lines ->advance ();
2928
30- $ this ->version = explode ( " => " , $ this ->lines ->consume ())[ 1 ] ;
29+ $ this ->version = $ this ->lines ->consumeItems ()-> last () ;
3130
3231 // Ok now we start with General info
3332 $ this ->modules = collect ([$ this ->processModule ('General ' )]);
@@ -39,8 +38,9 @@ protected function parse(): void
3938 $ this ->processModules ();
4039 $ this ->lines ->advance ();
4140
42- $ this ->modules ->push ($ this ->processCredits ());
43- $ this ->modules ->push ($ this ->processLicense ());
41+ // These will find and jump to the right spot, should it exist
42+ $ this ->processCredits ();
43+ $ this ->processLicense ();
4444 }
4545
4646 protected function processModules ()
@@ -127,13 +127,20 @@ protected function processConfig(): Config|false
127127 $ items ->appendLocalValue (
128128 "\n" . $ this ->lines ->consumeUntil (fn ($ line ) => $ line == ") " )->implode ("\n" )
129129 );
130+
131+ $ this ->lines ->advance ();
130132 }
131133
132134 return Config::fromValues ($ items );
133135 }
134136
135- protected function processCredits (): Module
137+ protected function processCredits (): void
136138 {
139+ // First make sure we have credits
140+ if (!$ this ->lines ->startAt ("PHP Credits " )) {
141+ return ;
142+ }
143+
137144 // Our credit groups are a bit odd. Some are simple with just a list of names, which can look
138145 // like a "note" to this parser. We're going to walk through each of these manually.
139146
@@ -161,20 +168,25 @@ protected function processCredits(): Module
161168 // Websites and Infrastructure team
162169 $ groups ->push ($ this ->processGroup ());
163170
164- return new Module ($ moduleName , $ groups );
171+ $ this -> modules -> push ( new Module ($ moduleName , $ groups) );
165172 }
166173
167- protected function processLicense (): Module
174+ protected function processLicense (): void
168175 {
169- return new Module (
176+ // First make sure we have a license, and jump to that position
177+ if (!$ this ->lines ->startAt ("PHP License " )) {
178+ return ;
179+ }
180+
181+ $ this ->modules ->push (new Module (
170182 $ this ->lines ->consume (),
171183 collect ([
172184 Group::noteOnly (
173185 $ this ->lines
174186 ->
consumeUntil (
fn (
$ line) =>
str_contains (
$ line,
'[email protected] ' ))
175187 ->implode ("\n" )
176- )]
177- )
178- );
188+ )
189+ ] )
190+ )) ;
179191 }
180192}
0 commit comments