1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the phpflo\phpflo-fbp package. |
4
|
|
|
* |
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PhpFlo\Loader; |
11
|
|
|
|
12
|
|
|
use PhpFlo\Common\DefinitionInterface; |
13
|
|
|
use PhpFlo\Common\LoaderInterface; |
14
|
|
|
use PhpFlo\Exception\LoaderException; |
15
|
|
|
use PhpFlo\Fbp\FbpDefinition; |
16
|
|
|
use PhpFlo\Fbp\FbpParser; |
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Loader |
21
|
|
|
* |
22
|
|
|
* @package PhpFlo\Loader |
23
|
|
|
* @author Marc Aschmann <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class Loader implements LoaderInterface |
26
|
|
|
{ |
27
|
|
|
private static $types = [ |
28
|
|
|
'yml' => 'yaml', |
29
|
|
|
'yaml' => 'yaml', |
30
|
|
|
'json' => 'json', |
31
|
|
|
'fbp' => 'fbp', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $file name/path of file to load |
36
|
|
|
* @return DefinitionInterface|null |
37
|
|
|
* @throws LoaderException |
38
|
|
|
*/ |
39
|
7 |
|
public static function load($file) |
40
|
|
|
{ |
41
|
7 |
|
$type = self::$types[self::checkType($file)]; |
42
|
6 |
|
$content = self::loadFile($file); |
43
|
|
|
|
44
|
4 |
|
if (empty($content)) { |
45
|
1 |
|
throw new LoaderException("Loader::load(): no data found in file!"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
switch ($type) { |
49
|
3 |
|
case 'fbp': |
50
|
1 |
|
$loader = new FbpParser($content); |
51
|
1 |
|
$definition = $loader->run(); |
52
|
1 |
|
break; |
53
|
2 |
|
case 'yaml': |
54
|
1 |
|
$definition = new FbpDefinition( |
55
|
1 |
|
Yaml::parse($content) |
56
|
1 |
|
); |
57
|
1 |
|
break; |
58
|
1 |
|
case 'json': |
59
|
1 |
|
$definition = new FbpDefinition( |
60
|
1 |
|
json_decode($content, true) |
61
|
1 |
|
); |
62
|
1 |
|
break; |
63
|
|
|
default: |
64
|
|
|
throw new LoaderException("Loader::load(): Something unexpected happened."); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
return $definition; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check file if extension matches a loader. |
72
|
|
|
* |
73
|
|
|
* @param string $file |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
7 |
|
private static function checkType($file) |
77
|
|
|
{ |
78
|
7 |
|
$parts = explode('.', $file); |
79
|
7 |
|
$type = array_pop($parts); |
80
|
|
|
|
81
|
7 |
|
if (!in_array($type, array_keys(self::$types))) { |
82
|
1 |
|
throw new LoaderException("Loader::checkType(): Could not find parser for {$file}!"); |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return $type; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $file |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
6 |
|
private static function loadFile($file) |
93
|
|
|
{ |
94
|
6 |
|
if (file_exists($file) && is_readable($file)) { |
95
|
4 |
|
$content = file_get_contents($file); |
96
|
4 |
|
} else { |
97
|
2 |
|
throw new LoaderException( |
98
|
2 |
|
"Loader::loadFile(): {$file} does not exist!" |
99
|
2 |
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
return $content; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|