|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Doppelgaenger\ContractContext |
|
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; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This class will keep track if there is any contract evaluation going on currently. |
|
25
|
|
|
* This is used to prevent endless loops of contracts using userland functions which are contracted themselves |
|
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 ContractContext |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var int $contractDepth At which depth are we in the middle of an ongoing contract evaluation? |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $contractDepth = 0; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @const int MAX_NESTING_DEPTH The maximum depth we allow. |
|
43
|
|
|
*/ |
|
44
|
|
|
const MAX_NESTING_DEPTH = 20; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Will open a contract context for any current ongoing verification. |
|
48
|
|
|
* Will return true if successful (you are the only ongoing contract) and |
|
49
|
|
|
* false if there already is something going on. |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function open() |
|
54
|
|
|
{ |
|
55
|
|
|
if (self::$contractDepth < self::MAX_NESTING_DEPTH) { |
|
56
|
|
|
// increment the contract depth |
|
57
|
|
|
self::$contractDepth++; |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
|
|
|
|
|
|
|
61
|
|
|
} else { |
|
62
|
|
|
// we reached max nesting level, tell them we were not able to open the contract |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Is there an ongoing contract beyond the maximal depth? |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function isOngoing() |
|
73
|
|
|
{ |
|
74
|
|
|
return !(self::$contractDepth <= self::MAX_NESTING_DEPTH); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Will close an open contract context for the ongoing verification. |
|
79
|
|
|
* Will return true if contract was successfully closed and |
|
80
|
|
|
* false if there was no contract at all. |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \Exception |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function close() |
|
87
|
|
|
{ |
|
88
|
|
|
if (self::$contractDepth <= self::MAX_NESTING_DEPTH && self::$contractDepth > 0) { |
|
89
|
|
|
// Decrement the used depth |
|
90
|
|
|
self::$contractDepth--; |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
|
|
|
|
|
|
|
94
|
|
|
} else { |
|
95
|
|
|
// Did we reach a place where the sun does not shine (metaphorically speaking ;-) |
|
96
|
|
|
if (self::$contractDepth < 0) { |
|
97
|
|
|
// Reset the used up contract depth and fail |
|
98
|
|
|
self::$contractDepth = 0; |
|
99
|
|
|
throw new \Exception('Contract depth surveillance ran out of bounds!'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|