Completed
Push — master ( 80364b...2a2c83 )
by recca
02:30
created

AbstractPanel::hasLaravel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Recca0120\LaravelTracy\Panels;
4
5
use Tracy\Helpers;
6
use Tracy\IBarPanel;
7
use Recca0120\LaravelTracy\Template;
8
use Illuminate\Contracts\Foundation\Application;
9
use Recca0120\LaravelTracy\Contracts\ILaravelPanel;
10
11
abstract class AbstractPanel implements IBarPanel, ILaravelPanel
12
{
13
    /**
14
     * $attributes.
15
     *
16
     * @var mixed
17
     */
18
    protected $attributes;
19
20
    /**
21
     * $viewPath.
22
     *
23
     * @var string
24
     */
25
    protected $viewPath = null;
26
27
    /**
28
     * $template.
29
     *
30
     * @var \Recca0120\LaravelTracy\Template
31
     */
32
    protected $template;
33
34
    /**
35
     * __construct.
36
     *
37
     * @param \Recca0120\LaravelTracy\Template $template
38
     */
39 18
    public function __construct(Template $template = null)
40
    {
41 18
        $this->template = $template ?: new Template;
42 18
    }
43
44
    /**
45
     * $laravel description.
46
     *
47
     * @var \Illuminate\Contracts\Foundation\Application
48
     */
49
    protected $laravel;
50
51
    /**
52
     * setLaravel.
53
     *
54
     * @param \Illuminate\Contracts\Foundation\Application $laravel
55
     * @return $this
56
     */
57 16
    public function setLaravel(Application $laravel = null)
58
    {
59 16
        if (is_null($laravel) === false) {
60 15
            $this->laravel = $laravel;
61 15
        }
62
63 16
        return $this;
64
    }
65
66
    /**
67
     * has laravel.
68
     *
69
     * @return bool
70
     */
71 16
    protected function hasLaravel()
72
    {
73 16
        return is_a($this->laravel, Application::class);
74
    }
75
76
    /**
77
     * Renders HTML code for custom tab.
78
     *
79
     * @return string
80
     */
81 16
    public function getTab()
82
    {
83 16
        return $this->render('tab');
84
    }
85
86
    /**
87
     * Renders HTML code for custom panel.
88
     *
89
     * @return string
90
     */
91 15
    public function getPanel()
92
    {
93 15
        return $this->render('panel');
94
    }
95
96
    /**
97
     * render.
98
     *
99
     * @param string $view
100
     * @return string
101
     */
102 17
    protected function render($view)
103
    {
104 17
        $view = $this->getViewPath().$view.'.php';
105 17
        if (empty($this->attributes) === true) {
106 17
            $this->template->setAttributes(
107 17
                $this->attributes = $this->getAttributes()
108 17
            );
109 17
        }
110
111 17
        return $this->template->render($view);
112
    }
113
114
    /**
115
     * Use a backtrace to search for the origin of the query.
116
     *
117
     * @return string|array
118
     */
119 5
    protected static function findSource()
120
    {
121 5
        $source = '';
122 5
        $trace = debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : false);
123 5
        foreach ($trace as $row) {
124 5
            if (isset($row['file']) === false) {
125 5
                continue;
126
            }
127
128 5
            if (isset($row['function']) === true && strpos($row['function'], 'call_user_func') === 0) {
129
                continue;
130
            }
131
132 5
            if (isset($row['class']) === true && (
133 5
                is_subclass_of($row['class'], '\Tracy\IBarPanel') === true ||
134 5
                strpos(str_replace('/', '\\', $row['file']), 'Illuminate\\') !== false
135 5
            )) {
136 5
                continue;
137
            }
138
139 5
            $source = [$row['file'], (int) $row['line']];
140 5
        }
141
142 5
        return $source;
143
    }
144
145
    /**
146
     * editor link.
147
     *
148
     * @param string|array $source
149
     * @return string
150
     */
151 7
    protected static function editorLink($source)
152
    {
153 7
        if (is_string($source) === true) {
154 2
            $file = $source;
155 2
            $line = null;
156 2
        } else {
157 5
            $file = $source[0];
158 5
            $line = $source[1];
159
        }
160
161 7
        return Helpers::editorLink($file, $line);
162
    }
163
164
    /**
165
     * getViewPath.
166
     *
167
     * @return string
168
     */
169 17
    protected function getViewPath()
170
    {
171 17
        if (is_null($this->viewPath) === false) {
172 16
            return $this->viewPath;
173
        }
174
175 17
        return $this->viewPath = __DIR__.'/../../resources/views/'.ucfirst(class_basename(get_class($this))).'/';
176
    }
177
178
    /**
179
     * getAttributes.
180
     *
181
     * @return array
182
     */
183
    abstract protected function getAttributes();
184
}
185