Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tokenizer.php
Go to the documentation of this file.
1 <?php
7 
11 class Tokenizer
12 {
18  private $_tokens = [];
19 
25  private $_tokensCount;
26 
32  private $_openBrackets;
33 
39  private $_closeBrackets;
40 
47  public function parse($filePath)
48  {
49  $this->_tokens = token_get_all(file_get_contents($filePath));
50  $this->_tokensCount = count($this->_tokens);
51  }
52 
60  public function isMatchingClass($className)
61  {
62  /*
63  * 1 - beginning, class identifier can begin either with identifier or with namespace separator
64  * 2 - after namespace separator, identifier is expected
65  * 3 - after identifier, namespace separator or open brace is expected
66  * 4 - ending, tells that class identifier is valid
67  */
68  $state = 1;
69  $classString = '';
70  while ($token = $this->getNextRealToken()) {
71  if ($token->isNamespaceSeparator() && $state != 2) {
72  $classString .= $token->getValue();
73  $state = 2;
74  } elseif ($token->isIdentifier() && $state != 3) {
75  $classString .= $token->getValue();
76  $state = 3;
77  } elseif ($token->isOpenBrace() && $state == 3) {
78  $state = 4;
79  break;
80  } else {
81  return false;
82  }
83  }
84  if ($state == 4 && $className == substr($classString, -strlen($className))) {
85  return true;
86  }
87  return false;
88  }
89 
95  public function getFunctionArgumentsTokens()
96  {
97  $arguments = [];
98  try {
99  $this->_openBrackets = 1;
100  $this->_closeBrackets = 0;
101  $argumentNumber = 0;
102  while (true) {
103  $token = $this->getNextToken();
104  if ($token->isSemicolon()) {
105  break;
106  }
107  if ($token->isOpenBrace()) {
108  $this->_skipInnerArgumentInvoke();
109  continue;
110  }
111  if ($token->isCloseBrace()) {
112  $this->_closeBrackets++;
113  }
114  $arguments[$argumentNumber][] = $token;
115  if ($token->isComma() && $this->_isInnerArgumentClosed()) {
116  array_pop($arguments[$argumentNumber]);
117  $argumentNumber++;
118  }
119  if ($this->_openBrackets == $this->_closeBrackets) {
120  break;
121  }
122  }
123  } catch (\Exception $e) {
124  return [];
125  }
126  return $arguments;
127  }
128 
134  private function _isInnerArgumentClosed()
135  {
136  return $this->_openBrackets - 1 == $this->_closeBrackets;
137  }
138 
144  private function _skipInnerArgumentInvoke()
145  {
146  $this->_openBrackets++;
147  while (!$this->getNextToken()->isCloseBrace()) {
148  if ($this->getCurrentToken()->isCloseBrace()) {
149  $this->_closeBrackets++;
150  }
151  if ($this->getCurrentToken()->isOpenBrace()) {
152  $this->_openBrackets++;
153  }
154  }
155  $this->_closeBrackets++;
156  }
157 
163  public function getCurrentToken()
164  {
165  return $this->_createToken(current($this->_tokens));
166  }
167 
173  public function getNextToken()
174  {
175  return ($token = next($this->_tokens)) ? $this->_createToken($token) : false;
176  }
177 
183  public function getNextRealToken()
184  {
185  do {
186  $token = $this->getNextToken();
187  } while ($token && $token->isWhitespace());
188  return $token;
189  }
190 
196  public function isEndOfLoop()
197  {
198  return key($this->_tokens) === null;
199  }
200 
207  private function _createToken($tokenData)
208  {
209  if (is_array($tokenData)) {
210  return new Tokenizer\Token($tokenData[0], $tokenData[1], $tokenData[2]);
211  } else {
212  return new Tokenizer\Token($tokenData, $tokenData);
213  }
214  }
215 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31