Completed
Push — master ( 18cad2...606ef9 )
by Steevan
01:50
created

Parser::registerFileFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace steevanb\PhpYaml;
4
5
use steevanb\PhpYaml\Exception\FunctionIdNotFoundException;
6
use steevanb\PhpYaml\Exception\FunctionNotFoundException;
7
use Symfony\Component\Yaml\Parser as SymfonyYamlParser;
8
9
class Parser extends SymfonyYamlParser
10
{
11
    /** @var array */
12
    protected static $functions = [];
13
14
    /**
15
     * @param string $id
16
     * @param callable $callable
17
     */
18
    public static function registerFunction($id, $callable)
19
    {
20
        static::$functions[$id] = $callable;
21
    }
22
23
    public static function registerFileFunction()
24
    {
25
        static::registerFunction('file', function($fileName) {
26
            return file_get_contents($fileName);
27
        });
28
    }
29
30
    public static function registerDateFunction()
31
    {
32
        static::registerFunction('date', function($date = null) {
33
            return new \DateTime($date);
34
        });
35
    }
36
37
    /**
38
     * @param string $value
39
     * @param int $flags
40
     * @return array|null
41
     */
42
    public function parse($value, $flags = 0)
43
    {
44
        $return = parent::parse($value, $flags);
45
46
        if (is_array($return)) {
47
            $this->parseValues($return);
48
        }
49
50
        return $return;
51
    }
52
53
    /**
54
     * @param array $values
55
     * @return $this
56
     * @throws \Exception
57
     */
58
    protected function parseValues(array &$values)
59
    {
60
        foreach ($values as &$value) {
61
            if (is_array($value)) {
62
                $this->parseValues($value);
63
            } elseif (is_string($value) && substr($value, 0, 1) === '<' && substr($value, -1) === '>') {
64
                $functionId = null;
65
                $parameters = [];
66
                foreach (token_get_all('<?php ' . $value) as $token) {
67
                    if (is_array($token)) {
68
                        if ($token[0] === T_STRING && $functionId === null) {
69
                            $functionId = $token[1];
70
                        } elseif ($token[0] === T_STRING) {
71
                            $parameters[] = $token[1];
72
                        } elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
73
                            $parameters[] = substr($token[1], 1, -1);
74
                        } elseif ($token[0] === T_LNUMBER || $token[0] === T_DNUMBER) {
75
                            $parameters[] = $token[1];
76
                        }
77
                    }
78
                }
79
80
                if ($functionId === null) {
81
                    throw new FunctionIdNotFoundException('Function name cannont be found in ' . $value . '.');
82
                } elseif (isset(static::$functions[$functionId]) === false) {
83
                    throw new FunctionNotFoundException('Function "' . $functionId . '" not found.');
84
                }
85
86
                $value = call_user_func_array(static::$functions[$functionId], $parameters);
87
            }
88
        }
89
90
        return $this;
91
    }
92
}
93