Completed
Push — master ( 598945...316866 )
by Kirill
09:09
created

Compiler::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Compiler;
11
12
use Railt\Compiler\Grammar\Reader;
13
use Railt\Io\Readable;
14
use Railt\Parser\Driver\Proxy;
15
use Railt\Parser\ParserInterface;
16
use Zend\Code\Generator\ValueGenerator as Value;
17
18
/**
19
 * Class Compiler
20
 */
21
class Compiler extends Proxy
22
{
23
    /**
24
     * @var string|null
25
     */
26
    private $namespace;
27
28
    /**
29
     * @var string
30
     */
31
    private $class = 'Parser';
32
33
    /**
34
     * @param Readable $grammar
35
     * @return Compiler
36
     * @throws \Railt\Io\Exception\ExternalFileException
37
     * @throws \Railt\Io\Exception\NotReadableException
38
     */
39
    public static function load(Readable $grammar): Compiler
40
    {
41
        $reader = new Reader($grammar);
42
43
        return new static($reader->getParser());
44
    }
45
46
    /**
47
     * @param ParserInterface $parser
48
     * @return Compiler
49
     */
50
    public static function fromParser(ParserInterface $parser): Compiler
51
    {
52
        return new static($parser);
53
    }
54
55
    /**
56
     * @param string $namespace
57
     * @return Compiler
58
     */
59
    public function setNamespace(string $namespace): Compiler
60
    {
61
        $this->namespace = $namespace;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $name
68
     * @return Compiler
69
     */
70
    public function setClassName(string $name): Compiler
71
    {
72
        $this->class = $name;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $path
79
     * @throws \Throwable
80
     */
81
    public function saveTo(string $path): void
82
    {
83
        $pathName = $path . '/' . $this->class . '.php';
84
85
        \file_put_contents($pathName, $this->build());
86
    }
87
88
    /**
89
     * @return string
90
     * @throws \Throwable
91
     */
92
    public function build(): string
93
    {
94
        \ob_start();
95
96
        try {
97
            require __DIR__ . '/../resources/templates/parser.tpl.php';
98
            return \ob_get_contents();
99
        } catch (\Throwable $e) {
100
            throw $e;
101
        } finally {
102
            \ob_end_clean();
103
        }
104
    }
105
106
    /**
107
     * @param mixed $value
108
     * @return string
109
     * @throws \Zend\Code\Exception\InvalidArgumentException
110
     * @throws \Zend\Code\Generator\Exception\RuntimeException
111
     */
112
    protected function render($value): string
113
    {
114
        $generator = new Value($value, Value::TYPE_AUTO, Value::OUTPUT_SINGLE_LINE);
115
116
        return $generator->generate();
117
    }
118
}
119