Skip to content

Commit

Permalink
use native dom/xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
jszobody committed Dec 31, 2022
1 parent c5b0545 commit 001e906
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ foreach($info->modules() AS $module) {
$module->name(); // session

// Now loop over module configs
foreach($module->configurations() AS $config) {
foreach($module->configs() AS $config) {
$config->name(); // session.auto_start
$config->localValue(); // Off
$config->masterValue(); // Off
Expand All @@ -39,7 +39,7 @@ foreach($info->modules() AS $module) {
foreach($info->generals() AS $config) { ... }

// Want a full list of all configs from all modules?
foreach($info->configurations() AS $config) { ... }
foreach($info->configs() AS $config) { ... }
```

### Looking up specific information
Expand Down Expand Up @@ -91,7 +91,7 @@ foreach ($info->modules() AS $module) {
echo '<h2>' . $module->name() . '</h2>';

echo '<ul>';
foreach($module->configuration() AS $config) {
foreach($module->configs() AS $config) {
echo '<li>';
echo $config->name() . ': ' . $config->value();
if($config->hasMasterValue()) {
Expand Down
38 changes: 19 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"name": "stechstudio/phpinfo",
"description": "Easily interact with phpinfo() configuration",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"STS\\Phpinfo\\": "src/"
}
},
"authors": [
{
"name": "Joseph Szobody",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"illuminate/collections": "^7.0|^8.0|^9.0",
"imangazaliev/didom": "^2.0"
"name": "stechstudio/phpinfo",
"description": "Easily interact with phpinfo() configuration",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"STS\\Phpinfo\\": "src/"
}
},
"authors": [
{
"name": "Joseph Szobody",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0",
"ext-dom": "*",
"illuminate/collections": "^7.0|^8.0|^9.0"
}
}
46 changes: 27 additions & 19 deletions src/Parsers/HtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace STS\Phpinfo\Parsers;

use DiDom\Document;
use DiDom\Element;
use DOMDocument;
use DOMElement;
use DOMText;
use DOMXPath;
use Illuminate\Support\Collection;
use STS\Phpinfo\Models\Config;
use STS\Phpinfo\Models\General;
Expand All @@ -14,48 +16,54 @@ class HtmlParser extends Info
{
protected function parse(): void
{
$document = new Document(str_replace("\n", "", $this->contents));
$document = new DOMDocument;
$document->loadHTML(str_replace("\n", "", $this->contents));
$xpath = new DOMXpath($document);

$this->version = str_replace('PHP Version ', '', $document->find('h1')[0]->text());
$this->version = str_replace('PHP Version ', '', $xpath->query('//body//h1')[0]->nodeValue);

$this->general = new General(
collect($document->xpath('//body//table[2]/tr'))
collect($xpath->query('//body//table[2]/tr'))
->map(
fn(Element $row) => new Config(
trim($row->firstChild()->text()), trim($row->lastChild()->text())
fn(DOMElement $row) => new Config(
trim($row->firstChild->nodeValue), trim($row->lastChild->nodeValue)
)
)->keyBy(fn(Config $config) => strtolower($config->name()))
);

$this->modules = collect($document->find('h2'))
->reject(fn(Element $heading) => $heading->text() === 'PHP License')
->mapWithKeys(
fn(Element $heading) => [strtolower($heading->text()) => new Module($heading->text(), $this->findConfigsFor($heading))]
);
$this->modules = collect($xpath->query('//body//h2'))
->reject(fn(DOMElement $heading) => $heading->nodeValue === 'PHP License')
->mapWithKeys(fn(DOMElement $heading) => [
strtolower($heading->nodeValue) => new Module($heading->nodeValue, $this->findConfigsFor($heading))
]);

$this->configs = $this->modules->map->configs()->flatten()->keyBy(fn(Config $config) => strtolower($config->name()));
}

protected function findConfigsFor(Element $heading): Collection
protected function findConfigsFor(DOMElement $heading): Collection
{
$configs = collect();
$current = $heading;

while ($current->nextSibling()?->tagName() === "table") {
$current = $current->nextSibling();
while ($current->nextSibling->nodeName === "table") {
$current = $current->nextSibling;

$configs->push(
collect($current->children())
->map(fn(Element $row) => Config::fromValues($this->rowToValues($row)))
collect($current->childNodes)
->filter(fn($node) => $node instanceof DOMElement && $node->nodeName === "tr")
->map(fn(DOMElement $row) => $this->rowToValues($row))
->map(fn(Collection $values) => Config::fromValues($values))
->reject(fn(Config $config) => in_array($config->name(), ['Directive','Variable']))
);
}

return $configs->flatten()->keyBy(fn(Config $setting) => strtolower($setting->name()));
}

protected function rowToValues(Element $row): Collection
protected function rowToValues(DOMElement $row): Collection
{
return collect($row->children())->map(fn($cell) => trim($cell->text()));
return collect($row->childNodes)
->reject(fn($node) => $node instanceof DOMText)
->map(fn(DOMElement $cell) => trim($cell->nodeValue));
}
}

0 comments on commit 001e906

Please sign in to comment.