1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Exception; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
23
|
|
|
|
24
|
|
|
class Art |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param Exception $exception |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public static function forException(Exception $exception) |
31
|
|
|
{ |
32
|
|
|
return self::calculateArt(get_class($exception), $exception->getMessage()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FlattenException $exception |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function forFlattenException(FlattenException $exception) |
40
|
|
|
{ |
41
|
|
|
return self::calculateArt($exception->getClass(), $exception->getMessage()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $className |
46
|
|
|
* @param string $message |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
private static function calculateArt($className, $message) |
50
|
|
|
{ |
51
|
|
|
$message = self::stripVariableArgumentsFromMessage($message); |
52
|
|
|
|
53
|
|
|
return substr(abs(crc32(md5($className . $message))), 0, 5); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Strip variable arguments from exception messages. |
58
|
|
|
* |
59
|
|
|
* Some exception messages are formatted using sprintf, and result in a |
60
|
|
|
* unique art-code for each distinct message. In order for the art-code to |
61
|
|
|
* be useful it must be the same for each distinct error situation without |
62
|
|
|
* taking into account variable parts of the message. |
63
|
|
|
* |
64
|
|
|
* This method strips all strings inside quotes. This is not perfect |
65
|
|
|
* because it relies on sprintf arguments to always be quoted inside the |
66
|
|
|
* message. |
67
|
|
|
* |
68
|
|
|
* @param $message |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
private static function stripVariableArgumentsFromMessage($message) |
72
|
|
|
{ |
73
|
|
|
return preg_replace('#".*"|\'.*\'#', '', $message); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|