db backup prior to drupal security update
[yaffs-website] / vendor / symfony / http-kernel / DataCollector / LoggerDataCollector.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\DataCollector;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
17
18 /**
19  * LogDataCollector.
20  *
21  * @author Fabien Potencier <fabien@symfony.com>
22  */
23 class LoggerDataCollector extends DataCollector implements LateDataCollectorInterface
24 {
25     private $errorNames = array(
26         E_DEPRECATED => 'E_DEPRECATED',
27         E_USER_DEPRECATED => 'E_USER_DEPRECATED',
28         E_NOTICE => 'E_NOTICE',
29         E_USER_NOTICE => 'E_USER_NOTICE',
30         E_STRICT => 'E_STRICT',
31         E_WARNING => 'E_WARNING',
32         E_USER_WARNING => 'E_USER_WARNING',
33         E_COMPILE_WARNING => 'E_COMPILE_WARNING',
34         E_CORE_WARNING => 'E_CORE_WARNING',
35         E_USER_ERROR => 'E_USER_ERROR',
36         E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
37         E_COMPILE_ERROR => 'E_COMPILE_ERROR',
38         E_PARSE => 'E_PARSE',
39         E_ERROR => 'E_ERROR',
40         E_CORE_ERROR => 'E_CORE_ERROR',
41     );
42
43     private $logger;
44
45     public function __construct($logger = null)
46     {
47         if (null !== $logger && $logger instanceof DebugLoggerInterface) {
48             $this->logger = $logger;
49         }
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function collect(Request $request, Response $response, \Exception $exception = null)
56     {
57         // everything is done as late as possible
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function lateCollect()
64     {
65         if (null !== $this->logger) {
66             $this->data = $this->computeErrorsCount();
67             $this->data['logs'] = $this->sanitizeLogs($this->logger->getLogs());
68         }
69     }
70
71     /**
72      * Gets the logs.
73      *
74      * @return array An array of logs
75      */
76     public function getLogs()
77     {
78         return isset($this->data['logs']) ? $this->data['logs'] : array();
79     }
80
81     public function getPriorities()
82     {
83         return isset($this->data['priorities']) ? $this->data['priorities'] : array();
84     }
85
86     public function countErrors()
87     {
88         return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
89     }
90
91     public function countDeprecations()
92     {
93         return isset($this->data['deprecation_count']) ? $this->data['deprecation_count'] : 0;
94     }
95
96     public function countScreams()
97     {
98         return isset($this->data['scream_count']) ? $this->data['scream_count'] : 0;
99     }
100
101     /**
102      * {@inheritdoc}
103      */
104     public function getName()
105     {
106         return 'logger';
107     }
108
109     private function sanitizeLogs($logs)
110     {
111         $errorContextById = array();
112         $sanitizedLogs = array();
113
114         foreach ($logs as $log) {
115             $context = $this->sanitizeContext($log['context']);
116
117             if (isset($context['type'], $context['file'], $context['line'], $context['level'])) {
118                 $errorId = md5("{$context['type']}/{$context['line']}/{$context['file']}\x00{$log['message']}", true);
119                 $silenced = !($context['type'] & $context['level']);
120                 if (isset($this->errorNames[$context['type']])) {
121                     $context = array_merge(array('name' => $this->errorNames[$context['type']]), $context);
122                 }
123
124                 if (isset($errorContextById[$errorId])) {
125                     if (isset($errorContextById[$errorId]['errorCount'])) {
126                         ++$errorContextById[$errorId]['errorCount'];
127                     } else {
128                         $errorContextById[$errorId]['errorCount'] = 2;
129                     }
130
131                     if (!$silenced && isset($errorContextById[$errorId]['scream'])) {
132                         unset($errorContextById[$errorId]['scream']);
133                         $errorContextById[$errorId]['level'] = $context['level'];
134                     }
135
136                     continue;
137                 }
138
139                 $errorContextById[$errorId] = &$context;
140                 if ($silenced) {
141                     $context['scream'] = true;
142                 }
143
144                 $log['context'] = &$context;
145                 unset($context);
146             } else {
147                 $log['context'] = $context;
148             }
149
150             $sanitizedLogs[] = $log;
151         }
152
153         return $sanitizedLogs;
154     }
155
156     private function sanitizeContext($context)
157     {
158         if (is_array($context)) {
159             foreach ($context as $key => $value) {
160                 $context[$key] = $this->sanitizeContext($value);
161             }
162
163             return $context;
164         }
165
166         if (is_resource($context)) {
167             return sprintf('Resource(%s)', get_resource_type($context));
168         }
169
170         if (is_object($context)) {
171             if ($context instanceof \Exception) {
172                 return sprintf('Exception(%s): %s', get_class($context), $context->getMessage());
173             }
174
175             return sprintf('Object(%s)', get_class($context));
176         }
177
178         return $context;
179     }
180
181     private function computeErrorsCount()
182     {
183         $count = array(
184             'error_count' => $this->logger->countErrors(),
185             'deprecation_count' => 0,
186             'scream_count' => 0,
187             'priorities' => array(),
188         );
189
190         foreach ($this->logger->getLogs() as $log) {
191             if (isset($count['priorities'][$log['priority']])) {
192                 ++$count['priorities'][$log['priority']]['count'];
193             } else {
194                 $count['priorities'][$log['priority']] = array(
195                     'count' => 1,
196                     'name' => $log['priorityName'],
197                 );
198             }
199
200             if (isset($log['context']['type'], $log['context']['level'])) {
201                 if (E_DEPRECATED === $log['context']['type'] || E_USER_DEPRECATED === $log['context']['type']) {
202                     ++$count['deprecation_count'];
203                 } elseif (!($log['context']['type'] & $log['context']['level'])) {
204                     ++$count['scream_count'];
205                 }
206             }
207         }
208
209         ksort($count['priorities']);
210
211         return $count;
212     }
213 }