1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Doppelgaenger\StreamFilters\ProcessingFilter |
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\StreamFilters; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition; |
24
|
|
|
use AppserverIo\Doppelgaenger\Entities\Lists\TypedListList; |
25
|
|
|
use AppserverIo\Doppelgaenger\Exceptions\GeneratorException; |
26
|
|
|
use AppserverIo\Doppelgaenger\Dictionaries\Placeholders; |
27
|
|
|
use AppserverIo\Doppelgaenger\Dictionaries\ReservedKeywords; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This filter will buffer the input stream and add all postcondition related information at prepared locations |
31
|
|
|
* (see $dependencies) |
32
|
|
|
* |
33
|
|
|
* @author Bernhard Wick <[email protected]> |
34
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
37
|
|
|
* @link http://www.appserver.io/ |
38
|
|
|
*/ |
39
|
|
|
class ProcessingFilter extends AbstractFilter |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @const integer FILTER_ORDER Order number if filters are used as a stack, higher means below others |
43
|
|
|
*/ |
44
|
|
|
const FILTER_ORDER = 00; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array $dependencies Other filters on which we depend |
48
|
|
|
*/ |
49
|
|
|
protected $dependencies = array('SkeletonFilter'); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The main filter method. |
53
|
|
|
* Implemented according to \php_user_filter class. Will loop over all stream buckets, buffer them and perform |
54
|
|
|
* the needed actions. |
55
|
|
|
* |
56
|
|
|
* @param resource $in Incoming bucket brigade we need to filter |
57
|
|
|
* @param resource $out Outgoing bucket brigade with already filtered content |
58
|
|
|
* @param integer $consumed The count of altered characters as buckets pass the filter |
59
|
|
|
* @param boolean $closing Is the stream about to close? |
60
|
|
|
* |
61
|
|
|
* @throws \AppserverIo\Doppelgaenger\Exceptions\GeneratorException |
62
|
|
|
* |
63
|
|
|
* @return integer |
64
|
|
|
* |
65
|
|
|
* @link http://www.php.net/manual/en/php-user-filter.filter.php |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function filter($in, $out, &$consumed, $closing) |
68
|
|
|
{ |
69
|
|
|
// Get our buckets from the stream |
70
|
|
|
while ($bucket = stream_bucket_make_writeable($in)) { |
71
|
|
|
// Get the tokens |
72
|
|
|
$tokens = token_get_all($bucket->data); |
73
|
|
|
|
74
|
|
|
// Go through the tokens and check what we found |
75
|
|
|
$tokensCount = count($tokens); |
76
|
|
|
for ($i = 0; $i < $tokensCount; $i++) { |
77
|
|
|
// Did we find a function? If so check if we know that thing and insert the code of its preconditions |
78
|
|
|
if (is_array($tokens[$i]) && $tokens[$i][0] === T_FUNCTION && is_array($tokens[$i + 2])) { |
79
|
|
|
// Get the name of the function |
80
|
|
|
$functionName = $tokens[$i + 2][1]; |
81
|
|
|
|
82
|
|
|
// Check if we got the function in our list, if not continue |
83
|
|
|
$functionDefinition = $this->params->get($functionName); |
84
|
|
|
|
85
|
|
|
if (!$functionDefinition instanceof FunctionDefinition) { |
86
|
|
|
continue; |
87
|
|
|
|
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
// Get the code for the needed call |
90
|
|
|
$code = $this->generateCode($functionDefinition); |
91
|
|
|
|
92
|
|
|
// Insert the code |
93
|
|
|
$bucket->data = str_replace( |
94
|
|
|
Placeholders::AROUND_JOINPOINT . $functionDefinition->getName() . |
95
|
|
|
Placeholders::PLACEHOLDER_CLOSE, |
96
|
|
|
$code, |
97
|
|
|
$bucket->data |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
// "Destroy" code and function definition |
101
|
|
|
$code = null; |
|
|
|
|
102
|
|
|
$functionDefinition = null; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Tell them how much we already processed, and stuff it back into the output |
108
|
|
|
$consumed += $bucket->datalen; |
109
|
|
|
stream_bucket_append($out, $bucket); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return PSFS_PASS_ON; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Will generate the code to call the original method logic |
117
|
|
|
* |
118
|
|
|
* @param \AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition $functionDefinition The function |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function generateCode(FunctionDefinition $functionDefinition) |
123
|
|
|
{ |
124
|
|
|
// Build up the call to the original function |
125
|
|
|
return ReservedKeywords::RESULT . ' = ' . $functionDefinition->getHeader('call', ReservedKeywords::ORIGINAL_FUNCTION_SUFFIX, false, true) . ';'; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|