Skip to content

Commit

Permalink
feat: basic manifest loading with caching
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfaassen committed Feb 16, 2021
1 parent 61880d4 commit a138fe3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tmp/
*.bak
*.swp
*~.nib
example/cache
data/
local.properties
.settings/
Expand Down
11 changes: 10 additions & 1 deletion example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
$arrayRepository = new ArrayResourceRepository($context);
$context->addRepository($arrayRepository);

$loader = new ResourceLoader();

$yaml = file_get_contents(__DIR__ . '/xillion.manifest.yaml');
$manifestConfig = Yaml::parse($yaml);

$loader->loadManifest($arrayRepository, $manifestConfig, __DIR__ . '/cache');

/*
$filenames = [
__DIR__ . '/../assets/xacml-10.xillion.cloud.yaml',
__DIR__ . '/../assets/core.xillion.cloud.yaml',
__DIR__ . '/example.linkorb.com.yaml',
];
$loader = new ResourceLoader();
foreach ($filenames as $filename) {
if (!file_exists($filename)) {
Expand All @@ -36,6 +42,9 @@
$config = Yaml::parse($yaml);
$loader->load($arrayRepository, $config['resources']);
}
*/



foreach ($context->getResources() as $resource) {
echo "\e[32m" . $resource->getId() . "\e[0m";
Expand Down
8 changes: 8 additions & 0 deletions example/xillion.manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies:
xacml-10.xillion.cloud:
url: https://raw.githubusercontent.com/linkorb/xillion/master/assets/xacml-10.xillion.cloud.yaml
core.xillion.cloud:
url: https://raw.githubusercontent.com/linkorb/xillion/master/assets/core.xillion.cloud.yaml
example.linkorb.com:
url: https://raw.githubusercontent.com/linkorb/xillion/master/example/example.linkorb.com.yaml

39 changes: 39 additions & 0 deletions src/Core/Resource/ResourceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Xillion\Core\Resource;

use Xillion\Core\ResourceRepository\ResourceRepositoryInterface;
use Symfony\Component\Yaml\Yaml;
use RuntimeException;

class ResourceLoader
Expand All @@ -16,4 +17,42 @@ public function load(ResourceRepositoryInterface $repository, array $config): vo
}
}

public function loadManifest(ResourceRepositoryInterface $repository, array $manifest, ?string $cachePath): void
{
foreach (($manifest['dependencies'] ?? []) as $packageName => $packageConfig) {
$yaml = null;
$filename = null;
if ($cachePath) {
if (!file_exists($cachePath)) {
mkdir($cachePath, 0777, true);
}

$filename = $cachePath . '/' . $packageName . '.yaml';
if (file_exists($filename)) {
$yaml = file_get_contents($filename);
}
}

if (!$yaml) {
$url = $packageConfig['url'] ?? null;
if (!$url) {
throw new RuntimeException("Package $packageName does not have a source URL specified");
}

$yaml = file_get_contents($url);
}

if (!$yaml) {
throw new RuntimeException("Could not load yaml definition for package " . $packageName);
}

if ($cachePath) {
file_put_contents($filename, $yaml);
}

$config = Yaml::parse($yaml);
$this->load($repository, $config['resources'] ?? []);
}
}

}

0 comments on commit a138fe3

Please sign in to comment.