1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JK\SamBundle\Configuration\Loader; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use JK\SamBundle\DependencyInjection\Configuration; |
7
|
|
|
use SplFileInfo; |
8
|
|
|
use Symfony\Component\Config\Definition\Processor; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Use to load the configuration from a yaml file or from the container. |
16
|
|
|
*/ |
17
|
|
|
class ConfigurationLoader |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Load the configuration using the service container. |
21
|
|
|
* |
22
|
|
|
* @param ContainerInterface $container |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
1 |
|
public function loadFromContainer(ContainerInterface $container) |
27
|
|
|
{ |
28
|
1 |
|
return $container->getParameter('jk.assets'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Load the configuration from a yaml file in the file system. |
33
|
|
|
* |
34
|
|
|
* @param string $path |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
* |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function loadFromFile($path) |
41
|
|
|
{ |
42
|
|
|
// the file should exists |
43
|
|
|
$fileSystem = new Filesystem(); |
44
|
|
|
|
45
|
|
|
if (!$fileSystem->exists($path)) { |
46
|
|
|
throw new FileNotFoundException(); |
47
|
|
|
} |
48
|
|
|
// the file should be a yml file |
49
|
|
|
$configurationFile = new SplFileInfo($path); |
50
|
|
|
|
51
|
|
|
if ($configurationFile->getExtension() !== 'yml') { |
52
|
|
|
throw new Exception('Only yml are allowed for assets configuration loading'); |
53
|
|
|
} |
54
|
|
|
// parse configuration using Symfony processor |
55
|
|
|
$configuration = Yaml::parse(file_get_contents($path)); |
56
|
|
|
$configurationDI = new Configuration(); |
57
|
|
|
$processor = new Processor(); |
58
|
|
|
|
59
|
|
|
return $processor->processConfiguration($configurationDI, $configuration); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|