|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Doppelgaenger\Exceptions\ExceptionFactory |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Bernhard Wick <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
18
|
|
|
* @link http://www.appserver.io/ |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Doppelgaenger\Exceptions; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Factory to get the right exception object (or class name) for the right occasion. |
|
25
|
|
|
* This was implemented to enable custom exception mapping |
|
26
|
|
|
* |
|
27
|
|
|
* @author Bernhard Wick <[email protected]> |
|
28
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
29
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
30
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
31
|
|
|
* @link http://www.appserver.io/ |
|
32
|
|
|
*/ |
|
33
|
|
|
class ExceptionFactory |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Will return the name of the exception class for the needed error type |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $type The type of exception we need |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getClassName($type) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getName($type); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Will return an instance of the exception fitting the error type we specified |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $type The type of exception we need |
|
51
|
|
|
* @param array $params Parameter array we will pass to the exception's constructor |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getInstance($type, $params = array()) |
|
56
|
|
|
{ |
|
57
|
|
|
$name = $this->getName($type); |
|
58
|
|
|
|
|
59
|
|
|
return call_user_func_array(array($name, '__construct'), $params); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Will return the name of the Exception class as it is mapped to a certain error type |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $type The type of exception we need |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getName($type) |
|
70
|
|
|
{ |
|
71
|
|
|
// What kind of exception do we need? |
|
72
|
|
|
switch ($type) { |
|
73
|
|
|
case 'precondition': |
|
74
|
|
|
$name = 'AppserverIo\Doppelgaenger\Exceptions\BrokenPreconditionException'; |
|
75
|
|
|
break; |
|
76
|
|
|
|
|
77
|
|
|
case 'postcondition': |
|
78
|
|
|
$name = 'AppserverIo\Doppelgaenger\Exceptions\BrokenPostconditionException'; |
|
79
|
|
|
break; |
|
80
|
|
|
|
|
81
|
|
|
case 'invariant': |
|
82
|
|
|
$name = 'AppserverIo\Doppelgaenger\Exceptions\BrokenInvariantException'; |
|
83
|
|
|
break; |
|
84
|
|
|
|
|
85
|
|
|
default: |
|
86
|
|
|
$name = $type; |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// If we got an exception from this namespace, return it's full name |
|
91
|
|
|
if (class_exists(__NAMESPACE__ . '\\' . $name)) { |
|
92
|
|
|
return __NAMESPACE__ . '\\' . $name; |
|
93
|
|
|
|
|
|
|
|
|
|
94
|
|
|
} elseif (class_exists('\\' . $name)) { |
|
95
|
|
|
// If we got an exception class from another namespace we will return this one |
|
96
|
|
|
|
|
97
|
|
|
return $name; |
|
98
|
|
|
|
|
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
// Otherwise we will return the most basic thing |
|
101
|
|
|
|
|
102
|
|
|
return 'Exception'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|