Issues (15)

src/VibroCompiler.php (2 issues)

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: georg
5
 * Date: 5/26/2018
6
 * Time: 1:30 PM
7
 */
8
9
namespace Ghaskell\Scaffold;
10
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Str;
13
use Illuminate\View\Compilers\BladeCompiler;
14
use Ghaskell\Scaffold\Facades\Code;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Ghaskell\Scaffold\Code. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
16
17
class VibroCompiler extends BladeCompiler
18
{
19
20
    /**
21
     * Array of opening and closing tags for raw echos.
22
     *
23
     * @var array
24
     */
25
    protected $rawTags = ['\*\|', '\|\*']; //pipes are escaped for regex, actual tags are #|, |#
26
27
28
    /**
29
     * Array of opening and closing tags for regular echos.
30
     *
31
     * @var array
32
     */
33
    protected $contentTags = ['<%', '%>'];
34
35
    /**
36
     * Array of opening and closing tags for escaped echos.
37
     *
38
     * @var array
39
     */
40
    protected $escapedTags = ['<%%%', '%%%>'];
41
42
    /**
43
     * The "regular" / legacy echo string format.
44
     *
45
     * @var string
46
     */
47
    protected $echoFormat = 'e(%s)';
48
49
    /**
50
     * Array of footer lines to be added to template.
51
     *
52
     * @var array
53
     */
54
    protected $footer = [];
55
56
    /**
57
     * Array to temporary store the raw blocks found in the template.
58
     *
59
     * @var array
60
     */
61
    protected $rawBlocks = [];
62
63
64
65
    public function compileFileName($fileName, $data) {
66
        extract($data);
67
        $parsed = $this->compileString($fileName);
68
        ob_start();
69
        eval( '?>' . $parsed );
70
        $output = ob_get_contents();
71
        ob_end_clean();
72
        return $output;
73
    }
74
75
    public function compileFile($filePath, array $data)
76
    {
77
        $result = Code::file($filePath, $data)->with($data)->render();
0 ignored issues
show
The method file() does not exist on Ghaskell\Scaffold\Facades\Code. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        $result = Code::/** @scrutinizer ignore-call */ file($filePath, $data)->with($data)->render();
Loading history...
78
        return str_replace("<?stub", "<?php", $result);
79
80
    }
81
82
    public function compileString($value)
83
    {
84
        if (strpos($value, '@verbatim') !== false) {
85
            $value = $this->storeVerbatimBlocks($value);
86
        }
87
88
        $this->footer = [];
89
90
        if (strpos($value, '@php') !== false) {
91
            $value = $this->storePhpBlocks($value);
92
        }
93
94
        $result = '';
95
96
        // Here we will loop through all of the tokens returned by the Zend lexer and
97
        // parse each one into the corresponding valid PHP. We will then have this
98
        // template as the correctly rendered PHP that can be rendered natively.
99
        foreach (token_get_all($value) as $token) {
100
            $result .= is_array($token) ? $this->parseToken($token) : $token;
101
        }
102
103
        if (! empty($this->rawBlocks)) {
104
            $result = $this->restoreRawContent($result);
105
        }
106
107
        // If there are any footer lines that need to get added to a template we will
108
        // add them here at the end of the template. This gets used mainly for the
109
        // template inheritance via the extends keyword that should be appended.
110
        if (count($this->footer) > 0) {
111
            $result = $this->addFooters($result);
112
        }
113
114
        return $result;
115
    }
116
117
    /**
118
     * Compile Blade statements that start with "@%".
119
     *
120
     * @param  string  $value
121
     * @return string
122
     */
123
    protected function compileStatements($value)
124
    {
125
        return preg_replace_callback(
126
            '/\B@%(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) {
127
            return $this->compileStatement($match);
128
        }, $value
129
        );
130
    }
131
132
    /**
133
     * Compile a single Blade @% statement.
134
     *
135
     * @param  array  $match
136
     * @return string
137
     */
138
    protected function compileStatement($match)
139
    {
140
        if (Str::contains($match[1], '@%')) {
141
            $match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1];
142
        }  elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) {
143
            $match[0] = $this->$method(Arr::get($match, 3));
144
        }
145
146
        return isset($match[3]) ? $match[0] : $match[0].$match[2];
147
    }
148
149
}