Completed
Push — master ( 474e5f...92cc73 )
by CodexShaper
02:52
created

View::blade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace CodexShaper\Blade;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Container\Container as ContainerContract;
7
use Illuminate\Events\Dispatcher;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Facades\Facade;
10
use Illuminate\View\ViewServiceProvider;
11
12
class View
13
{
14
    /**
15
     * @var
16
     */
17
    protected $app;
18
19
    /**
20
     * @var
21
     */
22
    private $view;
23
24
    /**
25
     * @var
26
     */
27
    private $viewPaths;
28
29
    /**
30
     * @var
31
     */
32
    private $cachePath;
33
34
    /**
35
     * Create view factory instance if not exists.
36
     *
37
     * @param array                                          $views
38
     * @param string                                         $cache
39
     * @param \Illuminate\Contracts\Container\Container|null $container
40
     *
41
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
42
     */
43
    public function __construct($views = [], $cache = '', ContainerContract $container = null)
44
    {
45
        $this->app = $container;
46
47
        if (is_null($this->app)) {
48
            if (!is_array($views)) {
49
                $views = (array) $views;
50
            }
51
52
            $this->viewPaths = $views;
53
            $this->cachePath = $cache;
54
55
            $this->app = $container ?? new Container();
56
            Facade::setFacadeApplication($this->app);
0 ignored issues
show
Compatibility introduced by
$this->app of type object<Illuminate\Contracts\Container\Container> is not a sub-type of object<Illuminate\Contra...Foundation\Application>. It seems like you assume a child interface of the interface Illuminate\Contracts\Container\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
57
58
            // Check this files, events and config is binding with shared or not. If not then bind
59
            $this->registerFileSystem();
60
            $this->registerEvents();
61
            $this->registerConfig();
62
63
            // Make sure files, events and config are register before call register
64
            with(new ViewServiceProvider($this->app))->register();
0 ignored issues
show
Compatibility introduced by
$this->app of type object<Illuminate\Contracts\Container\Container> is not a sub-type of object<Illuminate\Contra...Foundation\Application>. It seems like you assume a child interface of the interface Illuminate\Contracts\Container\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
65
        }
66
67
        $this->view = $this->app['view'];
68
    }
69
70
    /**
71
     * Bind files calss if not exists.
72
     *
73
     * @return void
74
     */
75
    protected function registerFileSystem()
76
    {
77
        $this->app->bindIf('files', function () {
78
            return new Filesystem();
79
        }, true);
80
    }
81
82
    /**
83
     * Bind events calss if not exists.
84
     *
85
     * @return void
86
     */
87
    protected function registerEvents()
88
    {
89
        $this->app->bindIf('events', function () {
90
            return new Dispatcher();
91
        }, true);
92
    }
93
94
    /**
95
     * Bind config calss if not exists.
96
     *
97
     * @return void
98
     */
99
    protected function registerConfig()
100
    {
101
        if (!is_array($this->viewPaths)) {
102
            throw new \Exception('Views path must be array');
103
        }
104
105
        $self = $this;
106
107
        $this->app->bindIf('config', function () use ($self) {
108
            return [
109
                'view.paths'    => $self->viewPaths,
110
                'view.compiled' => $self->cachePath,
111
            ];
112
        }, true);
113
    }
114
115
    /**
116
     * Get blade compiler.
117
     *
118
     * @return \Illuminate\View\Compilers\BladeCompiler
119
     */
120
    public function blade()
121
    {
122
        return $this->app['blade.compiler'];
123
    }
124
125
    /**
126
     * Call view factory methods dynamically.
127
     *
128
     * @param string $method
129
     * @param array  $parameters
130
     *
131
     * @return \Illuminate\View\View|\BadMethodCallException
132
     */
133
    public function __call($method, $parameters)
134
    {
135
        if (! method_exists(new self, $method) && method_exists($this->view, $method)) {
136
            return call_user_func_array([$this->view, $method], $parameters);
137
        }
138
139
        return $this->$method(...$parameters);
140
    }
141
142
    /**
143
     * Call view factory methods statically.
144
     *
145
     * @param string $method
146
     * @param array  $parameters
147
     *
148
     * @return \Illuminate\View\View|\BadMethodCallException
149
     */
150
    public static function __callStatic($method, $parameters)
151
    {
152
        if (! method_exists(new static(), $method) && method_exists($this->view, $method)) {
153
            return forward_static_call_array([$this->view, $method], $parameters);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
154
        }
155
156
        return (new static() )->$method(...$parameters);
157
    }
158
}
159