Completed
Pull Request — develop (#1155)
by Tom
02:14
created

ConfigFile   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromFile() 0 7 1
A loadFile() 0 18 4
A setBuffer() 0 4 1
A applyVariables() 0 9 2
A toArray() 0 10 2
A mergeArray() 0 6 1
A getPath() 0 4 1
1
<?php
2
/*
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Magento\Application;
9
10
use InvalidArgumentException;
11
use N98\Util\ArrayFunctions;
12
use RuntimeException;
13
use SplFileInfo;
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * Class ConfigFileParser
18
 *
19
 * @package N98\Magento\Application
20
 */
21
class ConfigFile
22
{
23
    /**
24
     * @var string
25
     */
26
    private $buffer;
27
28
    /**
29
     * @var string
30
     */
31
    private $path;
32
33
    /**
34
     * @param string $path
35
     * @return ConfigFile
36
     * @throws InvalidArgumentException if $path is invalid (can't be read for whatever reason)
37
     */
38
    public static function createFromFile($path)
39
    {
40
        $configFile = new static();
41
        $configFile->loadFile($path);
42
43
        return $configFile;
44
    }
45
46
    /**
47
     * @param string $path
48
     */
49
    public function loadFile($path)
50
    {
51
        $this->path = $path;
52
53
        if (
54
            'data://' !== substr($path, 0, 7)
55
            && !is_readable($path)
56
        ) {
57
            throw new InvalidArgumentException(sprintf("Config-file is not readable: '%s'", $path));
58
        }
59
60
        $buffer = file_get_contents($path);
61
        if (!is_string($buffer)) {
62
            throw new InvalidArgumentException(sprintf("Fail while reading config-file: '%s'", $path));
63
        }
64
65
        $this->setBuffer($buffer);
66
    }
67
68
    /**
69
     * @param string $buffer
70
     */
71
    public function setBuffer($buffer)
72
    {
73
        $this->buffer = $buffer;
74
    }
75
76
    /**
77
     * @param string $magentoRootFolder
78
     * @param null|SplFileInfo $file [optional]
79
     *
80
     * @return void
81
     */
82
    public function applyVariables($magentoRootFolder, SplFileInfo $file = null)
83
    {
84
        $replace = array(
85
            '%module%' => $file ? $file->getPath() : '',
86
            '%root%'   => $magentoRootFolder,
87
        );
88
89
        $this->buffer = strtr($this->buffer, $replace);
90
    }
91
92
    /**
93
     * @throws RuntimeException
94
     */
95
    public function toArray()
96
    {
97
        $result = Yaml::parse($this->buffer);
98
99
        if (!is_array($result)) {
100
            throw new RuntimeException(sprintf("Failed to parse config-file '%s'", $this->path));
101
        }
102
103
        return $result;
104
    }
105
106
    public function mergeArray(array $array)
107
    {
108
        $result = $this->toArray();
109
110
        return ArrayFunctions::mergeArrays($array, $result);
111
    }
112
113
    /**
114
     * @return string path to config-file
115
     */
116
    public function getPath(): string
117
    {
118
        return $this->path;
119
    }
120
}
121