Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support to using adapters to parse different config files formats. #10

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 22 additions & 156 deletions src/Utils/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,175 +3,41 @@

namespace App\Utils;

use BenTools\CartesianProduct\CartesianProduct;
use App\Utils\Config\AdapterInterface;

use InvalidArgumentException;
use \ReflectionClass;
use \ReflectionMethod;

final class Config {
private $basePath;
private $loadedContent = [];
private $adapter;

private function loadJson(string $fileName): void {
if (isset($this->loadedContent[$fileName]) === true) {
return;
}

$filePath = $this->basePath . DIRECTORY_SEPARATOR . $fileName;
if (! is_file($filePath)) {
throw new InvalidArgumentException('$fileName must be a valid file');
}

$raw = file_get_contents($filePath);
if ($raw === false) {
throw new InvalidArgumentException('Could not read from file');
}

$json = json_decode($raw, true);
if ($json === false) {
throw new InvalidArgumentException('Could not decode json data');
}

$this->loadedContent[$fileName] = $json;
public function __construct(AdapterInterface $adapter) {
$this->adapter = $adapter;
$this->methods = $this->getAdapterPublicMethods();
}

public function __construct(string $basePath) {
$basePath = rtrim($basePath, DIRECTORY_SEPARATOR);
if (! is_dir($basePath)) {
throw new InvalidArgumentException('$basePath must be a valid directory');
}

$this->basePath = $basePath;
}
public function __call(string $method, array $arguments): array {
$argument = $this->getArgument($arguments);

public function getExtensionSpecs(string $extName = null): array {
$this->loadJson('extensions.json');
if ($extName !== null) {
return $this->loadedContent['extensions.json'][$extName] ?? [];
if (in_array($method, $this->methods)) {
return $this->adapter->$method($argument);
}

$extensions = array_filter(
$this->loadedContent['extensions.json'],
function (array $specs): bool {
return (isset($specs['disabled']) === false || $specs['disabled'] === false);
},
ARRAY_FILTER_USE_BOTH
);
ksort($extensions);

return $extensions;
}

public function getExtensionList(): array {
return array_keys($this->getExtensionSpecs());
}

private function getAdapterPublicMethods(): array {
$interface = new ReflectionClass('App\Utils\Config\AdapterInterface');
$reflection = $interface->getMethods(ReflectionMethod::IS_PUBLIC);

public function getExtensionUrls(): array {
return array_map(
function (array $items): string {
return $items['build']['url'] ?? '';
},
$this->getExtensionSpecs()
);
return array_map(function ($item) {
return $item->name;
}, $reflection);
}

public function getOsSpecs(string $osName = null): array {
$this->loadJson('operating-systems.json');
if ($osName !== null) {
return $this->loadedContent['operating-systems.json'][$osName] ?? [];
private function getArgument($arguments): ?string {
if (empty($arguments)) {
return null;
}

$os = array_filter(
$this->loadedContent['operating-systems.json'],
function (array $specs): bool {
return (isset($specs['disabled']) === false || $specs['disabled'] === false);
},
ARRAY_FILTER_USE_BOTH
);
ksort($os);

return $os;
}

public function getOsList(): array {
return array_keys($this->getOSSpecs());
}

public function getPhpList(): array {
$this->loadJson('php-versions.json');
$php = array_map(
function (array $items): string {
return implode('-', array_filter($items));
},
iterator_to_array(
new CartesianProduct(
[
$this->loadedContent['php-versions.json'],
['', 'zts']
]
)
)
);
sort($php);

return $php;
}

public function getVersionList(): array {
return ['pecl', 'dev'];
}

public function getExtensionMatrix(): array {
$ext = array_map(
function (array $items): string {
return implode(':', $items);
},
iterator_to_array(
new CartesianProduct(
[
$this->getExtensionList(),
$this->getVersionList()
]
)
)
);
sort($ext);

return $ext;
}

public function getPhpMatrix(): array {
$php = array_map(
function (array $items): string {
return implode('-', $items);
},
iterator_to_array(
new CartesianProduct(
[
$this->getPhpList(),
$this->getOSList()
]
)
)
);

sort($php);

return $php;
}

public function getBuildMatrix(): array {
return array_map(
function (array $items): string {
return implode('@', $items);
},
iterator_to_array(
new CartesianProduct(
[
$this->getExtensionMatrix(),
$this->getPhpMatrix()
]
)
)
);
return array_shift($arguments);
}
}
17 changes: 17 additions & 0 deletions src/Utils/Config/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types = 1);

namespace App\Utils\Config;

interface AdapterInterface {
public function getExtensionSpecs(string $extName = null): array;
public function getExtensionList(): array;
public function getExtensionUrls(): array;
public function getOsSpecs(string $osName = null): array;
public function getOsList(): array;
public function getPhpList(): array;
public function getVersionList(): array;
public function getExtensionMatrix(): array;
public function getPhpMatrix(): array;
public function getBuildMatrix(): array;
}
178 changes: 178 additions & 0 deletions src/Utils/Config/Adapters/JsonAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
declare(strict_types = 1);

namespace App\Utils\Config\Adapters;

use App\Utils\Config\AdapterInterface;
use BenTools\CartesianProduct\CartesianProduct;

use InvalidArgumentException;

final class JsonAdapter implements AdapterInterface {
private $basePath;
private $loadedContent = [];

private function loadJson(string $fileName): void {
if (isset($this->loadedContent[$fileName]) === true) {
return;
}

$filePath = $this->basePath . DIRECTORY_SEPARATOR . $fileName;
if (! is_file($filePath)) {
throw new InvalidArgumentException('$fileName must be a valid file');
}

$raw = file_get_contents($filePath);
if ($raw === false) {
throw new InvalidArgumentException('Could not read from file');
}

$json = json_decode($raw, true);
if ($json === false) {
throw new InvalidArgumentException('Could not decode json data');
}

$this->loadedContent[$fileName] = $json;
}

public function __construct(string $basePath) {
$basePath = rtrim($basePath, DIRECTORY_SEPARATOR);
if (! is_dir($basePath)) {
throw new InvalidArgumentException('$basePath must be a valid directory');
}

$this->basePath = $basePath;
}

public function getExtensionSpecs(string $extName = null): array {
$this->loadJson('extensions.json');
if ($extName !== null) {
return $this->loadedContent['extensions.json'][$extName] ?? [];
}

$extensions = array_filter(
$this->loadedContent['extensions.json'],
function (array $specs): bool {
return (isset($specs['disabled']) === false || $specs['disabled'] === false);
},
ARRAY_FILTER_USE_BOTH
);
ksort($extensions);

return $extensions;
}

public function getExtensionList(): array {
return array_keys($this->getExtensionSpecs());
}

public function getExtensionUrls(): array {
return array_map(
function (array $items): string {
return $items['build']['url'] ?? '';
},
$this->getExtensionSpecs()
);
}

public function getOSSpecs(string $osName = null): array {
$this->loadJson('operating-systems.json');
if ($osName !== null) {
return $this->loadedContent['operating-systems.json'][$osName] ?? [];
}

$os = array_filter(
$this->loadedContent['operating-systems.json'],
function (array $specs): bool {
return (isset($specs['disabled']) === false || $specs['disabled'] === false);
},
ARRAY_FILTER_USE_BOTH
);
ksort($os);

return $os;
}

public function getOSList(): array {
return array_keys($this->getOSSpecs());
}

public function getPHPList(): array {
$this->loadJson('php-versions.json');
$php = array_map(
function (array $items): string {
return implode('-', array_filter($items));
},
iterator_to_array(
new CartesianProduct(
[
$this->loadedContent['php-versions.json'],
['', 'zts']
]
)
)
);
sort($php);

return $php;
}

public function getVersionList(): array {
return ['pecl', 'dev'];
}

public function getExtensionMatrix(): array {
$ext = array_map(
function (array $items): string {
return implode(':', $items);
},
iterator_to_array(
new CartesianProduct(
[
$this->getExtensionList(),
$this->getVersionList()
]
)
)
);
sort($ext);

return $ext;
}

public function getPHPMatrix(): array {
$php = array_map(
function (array $items): string {
return implode('-', $items);
},
iterator_to_array(
new CartesianProduct(
[
$this->getPHPList(),
$this->getOSList()
]
)
)
);

sort($php);

return $php;
}

public function getBuildMatrix(): array {
return array_map(
function (array $items): string {
return implode('@', $items);
},
iterator_to_array(
new CartesianProduct(
[
$this->getExtensionMatrix(),
$this->getPHPMatrix()
]
)
)
);
}
}
Loading