1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Configuration\Loader; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
8
|
|
|
use Doctrine\Migrations\Configuration\Exception\FileNotFound; |
9
|
|
|
use Doctrine\Migrations\Configuration\Exception\YamlNotAvailable; |
10
|
|
|
use Doctrine\Migrations\Configuration\Exception\YamlNotValid; |
11
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
use function assert; |
14
|
|
|
use function class_exists; |
15
|
|
|
use function file_exists; |
16
|
|
|
use function file_get_contents; |
17
|
|
|
use function is_array; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @internal |
21
|
|
|
*/ |
22
|
|
|
final class YamlFileLoader extends AbstractFileLoader |
23
|
|
|
{ |
24
|
|
|
/** @var ArrayLoader */ |
25
|
|
|
private $arrayLoader; |
26
|
|
|
|
27
|
12 |
|
public function __construct() |
28
|
|
|
{ |
29
|
12 |
|
$this->arrayLoader = new ArrayLoader(); |
30
|
12 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $file |
34
|
|
|
*/ |
35
|
10 |
|
public function load($file) : Configuration |
36
|
|
|
{ |
37
|
10 |
|
if (! class_exists(Yaml::class)) { |
38
|
|
|
throw YamlNotAvailable::new(); |
39
|
|
|
} |
40
|
|
|
|
41
|
10 |
|
if (! file_exists($file)) { |
42
|
1 |
|
throw FileNotFound::new(); |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
$content = file_get_contents($file); |
46
|
|
|
|
47
|
9 |
|
assert($content !== false); |
48
|
|
|
|
49
|
|
|
try { |
50
|
9 |
|
$config = Yaml::parse($content); |
51
|
1 |
|
} catch (ParseException $e) { |
52
|
1 |
|
throw YamlNotValid::malformed(); |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
if (! is_array($config)) { |
56
|
|
|
throw YamlNotValid::invalid(); |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
if (isset($config['migrations_paths'])) { |
60
|
3 |
|
$config['migrations_paths'] = $this->getDirectoryRelativeToFile( |
61
|
3 |
|
$file, |
62
|
3 |
|
$config['migrations_paths'] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
return $this->arrayLoader->load($config); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|