Skip to content

Commit 9942a39

Browse files
committed
support for php -i output, without credits included
1 parent 632ec00 commit 9942a39

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/Collections/Lines.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,26 @@ public function isModuleName(): bool
105105
{
106106
return !$this->hasItems()
107107
&& $this->nextIsBlank()
108+
&& !$this->isGroupTitle()
108109
&& strlen($this->current()) < 50;
109110
}
110111

111112
public function isGroupTitle(): bool
112113
{
113-
if(str_contains($this->current(), " ")) {
114+
// Some group titles have obvious signals
115+
if(
116+
str_contains($this->current(), " ")
117+
|| in_array($this->current(), ["Module Name"])
118+
) {
114119
return true;
115120
}
116121

122+
// Some look like group titles but aren't
123+
if(in_array($this->current(), ["PHP License"])) {
124+
return false;
125+
}
126+
127+
// Otherwise we have a pattern
117128
return !$this->hasItems()
118129
&& !$this->nextIsBlank()
119130
&& strlen($this->current()) < 50;
@@ -157,4 +168,14 @@ public function consumeItems(): Items
157168

158169
return $items;
159170
}
171+
172+
public function startAt($contents): string|null
173+
{
174+
if($index = $this->search($contents)) {
175+
$this->index = $index;
176+
return $this->current();
177+
}
178+
179+
return null;
180+
}
160181
}

src/Parsers/TextParser.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace STS\Phpinfo\Parsers;
44

5-
use Illuminate\Support\Collection;
65
use STS\Phpinfo\Collections\Lines;
76
use STS\Phpinfo\Models\Config;
87
use 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

Comments
 (0)