@@ 67-113 (lines=47) @@ | ||
64 | * |
|
65 | * @link http://www.php.net/manual/en/php-user-filter.filter.php |
|
66 | */ |
|
67 | 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 assertions |
|
90 | $code = $this->generateCode($functionDefinition->getAllPreconditions(), $functionName); |
|
91 | ||
92 | // Insert the code |
|
93 | $bucket->data = str_replace( |
|
94 | Placeholders::PRECONDITION . $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 needed to enforce made precondition assertions |
@@ 67-113 (lines=47) @@ | ||
64 | * |
|
65 | * @link http://www.php.net/manual/en/php-user-filter.filter.php |
|
66 | */ |
|
67 | 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 |