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 |
37
|
|
|
* @throws LoaderException |
38
|
|
|
*/ |
39
|
4 |
|
public static function load($file) |
40
|
|
|
{ |
41
|
4 |
|
$type = self::$types[self::checkType($file)]; |
42
|
4 |
|
$content = self::loadFile($file); |
43
|
|
|
|
44
|
3 |
|
if (empty($content)) { |
45
|
|
|
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
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
return $definition; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check file if extension matches a loader. |
70
|
|
|
* |
71
|
|
|
* @param string $file |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
4 |
|
private static function checkType($file) |
75
|
|
|
{ |
76
|
4 |
|
$parts = explode('.', $file); |
77
|
4 |
|
$type = array_pop($parts); |
78
|
|
|
|
79
|
4 |
|
if (!in_array($type, array_keys(self::$types))) { |
80
|
|
|
throw new LoaderException("Loader::checkType(): Could not find parser for {$file}!"); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return $type; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $file |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
4 |
|
private static function loadFile($file) |
91
|
|
|
{ |
92
|
4 |
|
if (file_exists($file) && is_readable($file)) { |
93
|
3 |
|
$content = file_get_contents($file); |
94
|
3 |
|
} else { |
95
|
1 |
|
throw new LoaderException( |
96
|
1 |
|
"Loader::loadFile(): {$file} does not exist!" |
97
|
1 |
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
return $content; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: