PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Parser / DefinitionTokenizer.php

DefinitionTokenizer.php in ManageWP Worker 4.9.25, at src/MWP/Parser/DefinitionTokenizer.php

120 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * A simple parser able to extract 'define' calls with their first constant string, from a PHP source code.
13 * Helpful for configuration reading.
14 */
15 class MWP_Parser_DefinitionTokenizer
16 {
17 /**
18 * Returns an array of possible 'defined' values.
19 *
20 * @param string $content Content to parse, PHP code is expected.
21 *
22 * @return array All the definitions that could be found.
23 */
24 public function getDefinitions($content)
25 {
26 $tokens = token_get_all($content);
27
28 $definitions = array();
29
30 // The parser has 4 phases:
31 // 1 - found the 'define' keyword
32 // 2 - found an open parentheses '('
33 // 3 - found a constant string
34 // 0 - (closing the circle) found a comma ','
35 $phase = 0;
36 $lastDefinition = '';
37
38 foreach ($tokens as $token) {
39 if (is_array($token) && ($token[0] === T_WHITESPACE || $token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)) {
40 // Skip whitespace and comment tokens.
41 continue;
42 }
43
44 if ($phase === 0) {
45 // Look for a 'define' function call.
46 if (is_array($token) && $token[0] === T_STRING && strtolower($token[1]) === 'define') {
47 // This is a 'define' call, move to next phase.
48 $phase = 1;
49 }
50 } elseif ($phase === 1 && $token === '(') {
51 // Open parentheses found, move to next phase.
52 $phase = 2;
53 } elseif ($phase === 2 && is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
54 // Constant string found, save it for later
55 $lastDefinition = substr($token[1], 1, -1);
56 $phase = 3;
57 } elseif ($phase === 3 && $token === ',') {
58 // Comma found, save the last found constant string, and reset the parser.
59 $definitions[] = $lastDefinition;
60 $phase = 0;
61 } else {
62 // Unsupported token found, reset the parser phase.
63 $phase = 0;
64 }
65 }
66
67 return array_unique($definitions);
68 }
69
70 public function getDefinitionValues($content)
71 {
72 $tokens = token_get_all($content);
73
74 $definitions = array();
75
76 $phase = 0;
77 $lastDefinition = '';
78 $lastValue = '';
79
80 foreach ($tokens as $token) {
81 if (is_array($token) && ($token[0] === T_WHITESPACE || $token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)) {
82 // Skip whitespace and comment tokens.
83 continue;
84 }
85 if ($phase === 0) {
86 // Look for a 'define' function call.
87 if (is_array($token) && $token[0] === T_STRING && strtolower($token[1]) === 'define') {
88 // This is a 'define' call, move to next phase.
89 $phase = 1;
90 }
91 } elseif ($phase === 1 && $token === '(') {
92 // Open parentheses found, move to next phase.
93 $phase = 2;
94 } elseif ($phase === 2 && is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
95 // Constant string found, save it for later
96 $lastDefinition = trim($token[1], '"\'');
97 $phase = 3;
98 } elseif ($phase === 3 && $token === ',') {
99 // Comma found, save the last found constant string.
100 $phase = 4;
101 } elseif ($phase === 4 && is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
102 if (strlen($token[1]) === 2) {
103 $lastValue = '';
104 } else {
105 $lastValue = stripslashes(substr($token[1], 1, -1));
106 }
107 $phase = 5;
108 } elseif ($phase === 5 && $token === ')') {
109 $definitions[$lastDefinition] = $lastValue;
110 $phase = 0;
111 } else {
112 // Unsupported token found, reset the parser phase.
113 $phase = 0;
114 }
115 }
116
117 return $definitions;
118 }
119 }
120