Completed
Pull Request — develop (#184)
by Tom
04:42
created

ConfigFile::setBuffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
        if (!is_readable($path)) {
41
            throw new InvalidArgumentException(sprintf("Config-file is not readable: '%s'", $path));
42
        }
43
44
        $configFile = new static();
45
        $configFile->loadFile($path);
46
47
        return $configFile;
48
    }
49
50
    /**
51
     * @param $path
52
     */
53
    public function loadFile($path)
54
    {
55
        $this->path = $path;
56
        $buffer = file_get_contents($path);
57
        if (!is_string($buffer)) {
58
            throw new InvalidArgumentException(sprintf("Invalid path for config file: '%s'", $path));
59
        }
60
61
        $this->setBuffer($buffer);
62
    }
63
64
    public function setBuffer($buffer)
65
    {
66
        $this->buffer = $buffer;
67
    }
68
69
    /**
70
     * @param string      $magentoRootFolder
71
     * @param SplFileInfo $file [optional]
0 ignored issues
show
Documentation introduced by
Should the type for parameter $file not be null|SplFileInfo?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
72
     *
73
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function applyVariables($magentoRootFolder, SplFileInfo $file = null)
76
    {
77
        $replace = array(
78
            '%module%' => $file ? $file->getPath() : '',
79
            '%root%'   => $magentoRootFolder,
80
        );
81
82
        $this->buffer = strtr($this->buffer, $replace);
83
    }
84
85
    /**
86
     * @throws RuntimeException
87
     */
88
    public function toArray()
89
    {
90
        $result = Yaml::parse($this->buffer);
91
92
        if (!is_array($result)) {
93
            throw new RuntimeException(sprintf("Failed to parse config-file '%s'", $this->path));
94
        }
95
96
        return $result;
97
    }
98
99
    public function mergeArray(array $array)
100
    {
101
        $result = $this->toArray();
102
103
        return ArrayFunctions::mergeArrays($array, $result);
104
    }
105
}
106