Initial commit
This commit is contained in:
57
src/Registry/ServiceRegistry.php
Normal file
57
src/Registry/ServiceRegistry.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user