57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Serverctl\Registry;
|
|
|
|
use RecursiveDirectoryIterator;
|
|
use RecursiveIteratorIterator;
|
|
use SplFileInfo;
|
|
|
|
class ServiceRegistry
|
|
{
|
|
|
|
private $services = [];
|
|
|
|
public function __construct(array $paths)
|
|
{
|
|
foreach ($paths as $path) {
|
|
if (is_dir($path)) {
|
|
$this->readPath($path);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function readPath(string $path)
|
|
{
|
|
$iter = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator(
|
|
$path
|
|
)
|
|
);
|
|
|
|
/** @var SplFileInfo $iteminfo */
|
|
foreach ($iter as $itempath=>$iteminfo) {
|
|
if (!fnmatch("*.json", $itempath)) continue;
|
|
$json = file_get_contents($itempath);
|
|
$parsed = json_decode($json, true);
|
|
$this->services[$itempath] = $parsed;
|
|
}
|
|
|
|
usort($this->services, function ($a,$b) {
|
|
return $a['name'] <=> $b['name'];
|
|
});
|
|
}
|
|
|
|
public function findServiceByName(string $name): ?array
|
|
{
|
|
foreach ($this->services as $service) {
|
|
if ($service['name'] === $name) return $service;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function findAllServices(): array
|
|
{
|
|
return $this->services;
|
|
}
|
|
|
|
} |