Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Server/Reflection/Node.php';
25 
29 #require_once 'Zend/Server/Reflection/Parameter.php';
30 
34 #require_once 'Zend/Server/Reflection/Prototype.php';
35 
54 {
58  protected $_reflection;
59 
64  protected $_argv = array();
65 
73  protected $_config = array();
74 
79  protected $_class;
80 
85  protected $_description = '';
86 
91  protected $_namespace;
92 
97  protected $_prototypes = array();
98 
99  private $_return;
100  private $_returnDesc;
101  private $_paramDesc;
102  private $_sigParams;
103  private $_sigParamsDepth;
104 
110  public function __construct(Reflector $r, $namespace = null, $argv = array())
111  {
112  // In PHP 5.1.x, ReflectionMethod extends ReflectionFunction. In 5.2.x,
113  // both extend ReflectionFunctionAbstract. So, we can't do normal type
114  // hinting in the prototype, but instead need to do some explicit
115  // testing here.
116  if ((!$r instanceof ReflectionFunction)
117  && (!$r instanceof ReflectionMethod)) {
118  #require_once 'Zend/Server/Reflection/Exception.php';
119  throw new Zend_Server_Reflection_Exception('Invalid reflection class');
120  }
121  $this->_reflection = $r;
122 
123  // Determine namespace
124  if (null !== $namespace){
125  $this->setNamespace($namespace);
126  }
127 
128  // Determine arguments
129  if (is_array($argv)) {
130  $this->_argv = $argv;
131  }
132 
133  // If method call, need to store some info on the class
134  if ($r instanceof ReflectionMethod) {
135  $this->_class = $r->getDeclaringClass()->getName();
136  }
137 
138  // Perform some introspection
139  $this->_reflect();
140  }
141 
153  protected function _addTree(Zend_Server_Reflection_Node $parent, $level = 0)
154  {
155  if ($level >= $this->_sigParamsDepth) {
156  return;
157  }
158 
159  foreach ($this->_sigParams[$level] as $value) {
160  $node = new Zend_Server_Reflection_Node($value, $parent);
161  if ((null !== $value) && ($this->_sigParamsDepth > $level + 1)) {
162  $this->_addTree($node, $level + 1);
163  }
164  }
165  }
166 
176  protected function _buildTree()
177  {
178  $returnTree = array();
179  foreach ((array) $this->_return as $value) {
180  $node = new Zend_Server_Reflection_Node($value);
181  $this->_addTree($node);
182  $returnTree[] = $node;
183  }
184 
185  return $returnTree;
186  }
187 
200  protected function _buildSignatures($return, $returnDesc, $paramTypes, $paramDesc)
201  {
202  $this->_return = $return;
203  $this->_returnDesc = $returnDesc;
204  $this->_paramDesc = $paramDesc;
205  $this->_sigParams = $paramTypes;
206  $this->_sigParamsDepth = count($paramTypes);
207  $signatureTrees = $this->_buildTree();
208  $signatures = array();
209 
210  $endPoints = array();
211  foreach ($signatureTrees as $root) {
212  $tmp = $root->getEndPoints();
213  if (empty($tmp)) {
214  $endPoints = array_merge($endPoints, array($root));
215  } else {
216  $endPoints = array_merge($endPoints, $tmp);
217  }
218  }
219 
220  foreach ($endPoints as $node) {
221  if (!$node instanceof Zend_Server_Reflection_Node) {
222  continue;
223  }
224 
225  $signature = array();
226  do {
227  array_unshift($signature, $node->getValue());
228  $node = $node->getParent();
229  } while ($node instanceof Zend_Server_Reflection_Node);
230 
231  $signatures[] = $signature;
232  }
233 
234  // Build prototypes
235  $params = $this->_reflection->getParameters();
236  foreach ($signatures as $signature) {
237  $return = new Zend_Server_Reflection_ReturnValue(array_shift($signature), $this->_returnDesc);
238  $tmp = array();
239  foreach ($signature as $key => $type) {
240  $param = new Zend_Server_Reflection_Parameter($params[$key], $type, (isset($this->_paramDesc[$key]) ? $this->_paramDesc[$key] : null));
241  $param->setPosition($key);
242  $tmp[] = $param;
243  }
244 
245  $this->_prototypes[] = new Zend_Server_Reflection_Prototype($return, $tmp);
246  }
247  }
248 
259  protected function _reflect()
260  {
261  $function = $this->_reflection;
262  $helpText = '';
263  $signatures = array();
264  $returnDesc = '';
265  $paramCount = $function->getNumberOfParameters();
266  $paramCountRequired = $function->getNumberOfRequiredParameters();
267  $parameters = $function->getParameters();
268  $docBlock = $function->getDocComment();
269 
270  if (!empty($docBlock)) {
271  // Get help text
272  if (preg_match(':/\*\*\s*\r?\n\s*\*\s(.*?)\r?\n\s*\*(\s@|/):s', $docBlock, $matches))
273  {
274  $helpText = $matches[1];
275  $helpText = preg_replace('/(^\s*\*\s)/m', '', $helpText);
276  $helpText = preg_replace('/\r?\n\s*\*\s*(\r?\n)*/s', "\n", $helpText);
277  $helpText = trim($helpText);
278  }
279 
280  // Get return type(s) and description
281  $return = 'void';
282  if (preg_match('/@return\s+(\S+)/', $docBlock, $matches)) {
283  $return = explode('|', $matches[1]);
284  if (preg_match('/@return\s+\S+\s+(.*?)(@|\*\/)/s', $docBlock, $matches))
285  {
286  $value = $matches[1];
287  $value = preg_replace('/\s?\*\s/m', '', $value);
288  $value = preg_replace('/\s{2,}/', ' ', $value);
289  $returnDesc = trim($value);
290  }
291  }
292 
293  // Get param types and description
294  if (preg_match_all('/@param\s+([^\s]+)/m', $docBlock, $matches)) {
295  $paramTypesTmp = $matches[1];
296  if (preg_match_all('/@param\s+\S+\s+(\$\S+)\s+(.*?)(?=@|\*\/)/s', $docBlock, $matches))
297  {
298  $paramDesc = $matches[2];
299  foreach ($paramDesc as $key => $value) {
300  $value = preg_replace('/\s?\*\s/m', '', $value);
301  $value = preg_replace('/\s{2,}/', ' ', $value);
302  $paramDesc[$key] = trim($value);
303  }
304  }
305  }
306  } else {
307  $helpText = $function->getName();
308  $return = 'void';
309 
310  // Try and auto-determine type, based on reflection
311  $paramTypesTmp = array();
312  foreach ($parameters as $i => $param) {
313  $paramType = 'mixed';
314  if ($param->isArray()) {
315  $paramType = 'array';
316  }
317  $paramTypesTmp[$i] = $paramType;
318  }
319  }
320 
321  // Set method description
322  $this->setDescription($helpText);
323 
324  // Get all param types as arrays
325  if (!isset($paramTypesTmp) && (0 < $paramCount)) {
326  $paramTypesTmp = array_fill(0, $paramCount, 'mixed');
327  } elseif (!isset($paramTypesTmp)) {
328  $paramTypesTmp = array();
329  } elseif (count($paramTypesTmp) < $paramCount) {
330  $start = $paramCount - count($paramTypesTmp);
331  for ($i = $start; $i < $paramCount; ++$i) {
332  $paramTypesTmp[$i] = 'mixed';
333  }
334  }
335 
336  // Get all param descriptions as arrays
337  if (!isset($paramDesc) && (0 < $paramCount)) {
338  $paramDesc = array_fill(0, $paramCount, '');
339  } elseif (!isset($paramDesc)) {
340  $paramDesc = array();
341  } elseif (count($paramDesc) < $paramCount) {
342  $start = $paramCount - count($paramDesc);
343  for ($i = $start; $i < $paramCount; ++$i) {
344  $paramDesc[$i] = '';
345  }
346  }
347 
348  if (count($paramTypesTmp) != $paramCount) {
349  #require_once 'Zend/Server/Reflection/Exception.php';
351  'Variable number of arguments is not supported for services (except optional parameters). '
352  . 'Number of function arguments in ' . $function->getDeclaringClass()->getName() . '::'
353  . $function->getName() . '() must correspond to actual number of arguments described in the '
354  . 'docblock.');
355  }
356 
357  $paramTypes = array();
358  foreach ($paramTypesTmp as $i => $param) {
359  $tmp = explode('|', $param);
360  if ($parameters[$i]->isOptional()) {
361  array_unshift($tmp, null);
362  }
363  $paramTypes[] = $tmp;
364  }
365 
366  $this->_buildSignatures($return, $returnDesc, $paramTypes, $paramDesc);
367  }
368 
369 
377  public function __call($method, $args)
378  {
379  if (method_exists($this->_reflection, $method)) {
380  return call_user_func_array(array($this->_reflection, $method), $args);
381  }
382 
383  #require_once 'Zend/Server/Reflection/Exception.php';
384  throw new Zend_Server_Reflection_Exception('Invalid reflection method ("' .$method. '")');
385  }
386 
396  public function __get($key)
397  {
398  if (isset($this->_config[$key])) {
399  return $this->_config[$key];
400  }
401 
402  return null;
403  }
404 
414  public function __set($key, $value)
415  {
416  $this->_config[$key] = $value;
417  }
418 
425  public function setNamespace($namespace)
426  {
427  if (empty($namespace)) {
428  $this->_namespace = '';
429  return;
430  }
431 
432  if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) {
433  #require_once 'Zend/Server/Reflection/Exception.php';
434  throw new Zend_Server_Reflection_Exception('Invalid namespace');
435  }
436 
437  $this->_namespace = $namespace;
438  }
439 
445  public function getNamespace()
446  {
447  return $this->_namespace;
448  }
449 
456  public function setDescription($string)
457  {
458  if (!is_string($string)) {
459  #require_once 'Zend/Server/Reflection/Exception.php';
460  throw new Zend_Server_Reflection_Exception('Invalid description');
461  }
462 
463  $this->_description = $string;
464  }
465 
471  public function getDescription()
472  {
473  return $this->_description;
474  }
475 
482  public function getPrototypes()
483  {
484  return $this->_prototypes;
485  }
486 
492  public function getInvokeArguments()
493  {
494  return $this->_argv;
495  }
496 
505  public function __wakeup()
506  {
507  if ($this->_reflection instanceof ReflectionMethod) {
508  $class = new ReflectionClass($this->_class);
509  $this->_reflection = new ReflectionMethod($class->newInstance(), $this->getName());
510  } else {
511  $this->_reflection = new ReflectionFunction($this->getName());
512  }
513  }
514 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_addTree(Zend_Server_Reflection_Node $parent, $level=0)
Definition: Abstract.php:153
__construct(Reflector $r, $namespace=null, $argv=array())
Definition: Abstract.php:110
$start
Definition: listing.phtml:18
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
_buildSignatures($return, $returnDesc, $paramTypes, $paramDesc)
Definition: Abstract.php:200
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31