This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationCompiler.php
94 lines (81 loc) · 2.51 KB
/
ConfigurationCompiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Configuration\Component\Compiler;
use Ulrack\Vfs\Common\FileSystemDriverInterface;
use Ulrack\Configuration\Common\LocatorInterface;
use Ulrack\Configuration\Common\CompilerInterface;
use Ulrack\Configuration\Common\RegistryInterface;
use Ulrack\Configuration\Component\Registry\Registry;
use Ulrack\Configuration\Component\Configuration\PackageLocator;
class ConfigurationCompiler implements CompilerInterface
{
/**
* Contains the locators.
*
* @var LocatorInterface[]
*/
private $locators = [];
/**
* Contains the driver for traversing over the files.
*
* @var FileSystemDriverInterface
*/
private $driver;
/**
* Constructor
*
* @param FileSystemDriverInterface $driver
*/
public function __construct(FileSystemDriverInterface $driver)
{
$this->driver = $driver;
}
/**
* Adds a locator to the compiler.
*
* @param LocatorInterface $locator
*
* @return void
*/
public function addLocator(LocatorInterface $locator): void
{
$this->locators[] = $locator;
}
/**
* Compiles configuration into an array of data.
*
* @return RegistryInterface
*/
public function compile(): RegistryInterface
{
$packages = PackageLocator::getLocations();
$registry = new Registry();
foreach ($packages as $package) {
$fileSystem = $this->driver->connect($package);
foreach ($this->locators as $locator) {
$directory = rtrim($locator->getLocation(), '/');
foreach ($fileSystem->list($directory) as $file) {
$file = sprintf('%s/%s', $directory, ltrim($file, '/'));
if ($fileSystem->isReadable($file)) {
$configuration = $locator->getDecoder()
->decode(
$fileSystem->get($file)
);
if ($locator->getValidator()
->__invoke($configuration)) {
$registry->register(
$locator->getKey(),
$configuration
);
}
}
}
}
$this->driver->disconnect($fileSystem);
}
return $registry;
}
}