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\FunctionDefinitionList; |
25
|
|
|
use AppserverIo\Doppelgaenger\Entities\Lists\TypedListList; |
26
|
|
|
use AppserverIo\Doppelgaenger\Exceptions\GeneratorException; |
27
|
|
|
use AppserverIo\Doppelgaenger\Dictionaries\Placeholders; |
28
|
|
|
use AppserverIo\Doppelgaenger\Dictionaries\ReservedKeywords; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This filter will buffer the input stream and add all postcondition related information at prepared locations |
32
|
|
|
* (see $dependencies) |
33
|
|
|
* |
34
|
|
|
* @author Bernhard Wick <[email protected]> |
35
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
36
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
37
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
38
|
|
|
* @link http://www.appserver.io/ |
39
|
|
|
*/ |
40
|
|
|
class ProcessingFilter extends AbstractFilter |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @const integer FILTER_ORDER Order number if filters are used as a stack, higher means below others |
44
|
|
|
*/ |
45
|
|
|
const FILTER_ORDER = 00; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array $dependencies Other filters on which we depend |
49
|
|
|
*/ |
50
|
|
|
protected $dependencies = array('SkeletonFilter'); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Filter a chunk of data by adding processing |
54
|
|
|
* |
55
|
|
|
* @param string $chunk The data chunk to be filtered |
56
|
|
|
* @param FunctionDefinitionList $functionDefinitions Definition of the structure the chunk belongs to |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function filterChunk($chunk, FunctionDefinitionList $functionDefinitions) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
// Get the tokens |
63
|
|
|
$tokens = token_get_all($chunk); |
64
|
|
|
|
65
|
|
|
// Go through the tokens and check what we found |
66
|
|
|
$tokensCount = count($tokens); |
67
|
|
|
for ($i = 0; $i < $tokensCount; $i++) { |
68
|
|
|
// Did we find a function? If so check if we know that thing and insert the code of its preconditions |
69
|
|
|
if (is_array($tokens[$i]) && $tokens[$i][0] === T_FUNCTION && is_array($tokens[$i + 2])) { |
70
|
|
|
// Get the name of the function |
71
|
|
|
$functionName = $tokens[$i + 2][1]; |
72
|
|
|
|
73
|
|
|
// Check if we got the function in our list, if not continue |
74
|
|
|
$functionDefinition = $functionDefinitions->get($functionName); |
75
|
|
|
|
76
|
|
|
if (!$functionDefinition instanceof FunctionDefinition) { |
77
|
|
|
continue; |
78
|
|
|
|
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
// Get the code for the needed call |
81
|
|
|
$code = $this->generateCode($functionDefinition); |
82
|
|
|
|
83
|
|
|
// Insert the code |
84
|
|
|
$chunk = str_replace( |
85
|
|
|
Placeholders::AROUND_JOINPOINT . $functionDefinition->getName() . |
86
|
|
|
Placeholders::PLACEHOLDER_CLOSE, |
87
|
|
|
$code, |
88
|
|
|
$chunk |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
// "Destroy" code and function definition |
92
|
|
|
$code = null; |
|
|
|
|
93
|
|
|
$functionDefinition = null; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $chunk; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Will generate the code to call the original method logic |
103
|
|
|
* |
104
|
|
|
* @param \AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition $functionDefinition The function |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function generateCode(FunctionDefinition $functionDefinition) |
109
|
|
|
{ |
110
|
|
|
// Build up the call to the original function |
111
|
|
|
return ReservedKeywords::RESULT . ' = ' . $functionDefinition->getHeader('call', ReservedKeywords::ORIGINAL_FUNCTION_SUFFIX, false, true) . ';'; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.