Completed
Push — master ( 9792a6...aaaf86 )
by recca
10:22
created

AbstractPanel::findSource()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9.2363

Importance

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