DeprecationsPanel.php
4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Panel;
use Cake\Event\Event;
use Cake\Utility\Hash;
use Composer\Json\JsonFile;
use DebugKit\DebugInclude;
use DebugKit\DebugPanel;
/**
* Provides a list of deprecated methods for the current request
*
*/
class DeprecationsPanel extends DebugPanel
{
/**
* The list of depreated errors.
*
* @var array
*/
protected static $deprecatedErrors = [];
/**
* instance of DebugInclude
*
* @var \DebugKit\DebugInclude
*/
protected $_debug;
/**
* construct
*/
public function __construct()
{
$this->_debug = new DebugInclude();
}
/**
* Get a list of files that were deprecated and split them out into the various parts of the app
*
* @return array
*/
protected function _prepare()
{
$errors = static::$deprecatedErrors;
$return = ['cake' => [], 'app' => [], 'plugins' => [], 'vendor' => [], 'other' => []];
foreach ($errors as $error) {
$file = $error['file'];
$line = $error['line'];
if (isset($error['context']['frame'])) {
$file = $error['context']['frame']['file'];
$line = $error['context']['frame']['line'];
}
$errorData = [
'file' => $file,
'line' => $line,
'message' => $error['message'],
];
$pluginName = $this->_debug->getPluginName($file);
if ($pluginName) {
$errorData['niceFile'] = $this->_debug->niceFileName($file, 'plugin', $pluginName);
$return['plugins'][$pluginName][] = $errorData;
} elseif ($this->_debug->isAppFile($file)) {
$errorData['niceFile'] = $this->_debug->niceFileName($file, 'app');
$return['app'][] = $errorData;
} elseif ($this->_debug->isCakeFile($file)) {
$errorData['niceFile'] = $this->_debug->niceFileName($file, 'cake');
$return['cake'][] = $errorData;
} else {
$vendorName = $this->_debug->getComposerPackageName($file);
if ($vendorName) {
$errorData['niceFile'] = $this->_debug->niceFileName($file, 'vendor', $vendorName);
$return['vendor'][$vendorName][] = $errorData;
} else {
$errorData['niceFile'] = $this->_debug->niceFileName($file, 'root');
$return['other'][] = $errorData;
}
}
}
ksort($return['app']);
ksort($return['cake']);
ksort($return['plugins']);
ksort($return['vendor']);
foreach ($return['plugins'] as &$plugin) {
ksort($plugin);
}
return $return;
}
/**
* Add a error
*
* @param array $error The deprecated error
* @return void
*/
public static function addDeprecatedError($error)
{
static::$deprecatedErrors[] = $error;
}
/**
* Reset the tracked errors.
*
* @return void
*/
public static function clearDeprecatedErrors()
{
static::$deprecatedErrors = [];
}
/**
* Get the number of files deprecated in this request.
*
* @return string
*/
public function summary()
{
$data = $this->_data;
if (empty($data)) {
$data = $this->_prepare();
}
return array_reduce($data, function ($carry, $item) {
if (empty($item)) {
return $carry;
}
// app, cake, or other groups
if (Hash::dimensions($item) == 2) {
return $carry + count($item);
}
// plugin and vendor groups
foreach ($item as $group) {
$carry += count($group);
}
return $carry;
}, 0);
}
/**
* Shutdown callback
*
* @param \Cake\Event\Event $event Event
* @return void
*/
public function shutdown(Event $event)
{
$this->_data = $this->_prepare();
}
}