1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of file-fixture. |
5
|
|
|
* |
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace holyshared\fixture\factory; |
13
|
|
|
|
14
|
|
|
use Yosymfony\Toml\Toml; |
15
|
|
|
use Eloquent\Pathogen\Factory\PathFactory; |
16
|
|
|
use Eloquent\Pathogen\RelativePath; |
17
|
|
|
use holyshared\fixture\container\FixtureContainer; |
18
|
|
|
use holyshared\fixture\ContainerFactory; |
19
|
|
|
use holyshared\fixture\ArrayFlattener; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class FixtureContainerFactory implements ContainerFactory |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
private $flattener; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->flattener = new ArrayFlattener(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function createFromFile($configFile) |
33
|
|
|
{ |
34
|
|
|
$configPath = PathFactory::instance()->create($configFile); |
35
|
|
|
$loadConfigFile = $configPath->normalize(); |
36
|
|
|
|
37
|
|
|
if (file_exists($loadConfigFile) === false) { |
38
|
|
|
throw new ConfigFileNotFoundException($loadConfigFile); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$configDirectory = $configPath->parent(); |
42
|
|
|
|
43
|
|
|
$configValues = Toml::parse( $loadConfigFile ); |
44
|
|
|
$result = $this->flattener->flatten($configValues); |
45
|
|
|
|
46
|
|
|
foreach ($result as $key => $fixturePath) { |
47
|
|
|
$fixtureRelativePath = RelativePath::fromString($fixturePath); |
48
|
|
|
$fixturePath = $configDirectory->join($fixtureRelativePath); |
49
|
|
|
$result[$key] = (string) $fixturePath->normalize(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new FixtureContainer($result); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function createFromArray(array $values = []) |
56
|
|
|
{ |
57
|
|
|
$result = $this->flattener->flatten($values); |
58
|
|
|
return new FixtureContainer($result); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|