/vendor/psy/psysh/src/Psy/Command/ListCommand/Enumerator.php

https://gitlab.com/dzakiafif/cokelatklasik · PHP · 146 lines · 72 code · 19 blank · 55 comment · 8 complexity · 0d3054a4f8551d3cb9c8a1b32b363f40 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Psy Shell
  4. *
  5. * (c) 2012-2014 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Command\ListCommand;
  11. use Psy\Formatter\SignatureFormatter;
  12. use Psy\Presenter\PresenterManager;
  13. use Psy\Util\Mirror;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. /**
  16. * Abstract Enumerator class.
  17. */
  18. abstract class Enumerator
  19. {
  20. // Output styles
  21. const IS_PUBLIC = 'public';
  22. const IS_PROTECTED = 'protected';
  23. const IS_PRIVATE = 'private';
  24. const IS_GLOBAL = 'global';
  25. const IS_CONSTANT = 'const';
  26. const IS_CLASS = 'class';
  27. const IS_FUNCTION = 'function';
  28. private $presenterManager;
  29. private $filter = false;
  30. private $invertFilter = false;
  31. private $pattern;
  32. /**
  33. * Enumerator constructor.
  34. *
  35. * @param PresenterManager $presenterManager
  36. */
  37. public function __construct(PresenterManager $presenterManager)
  38. {
  39. $this->presenterManager = $presenterManager;
  40. }
  41. /**
  42. * Return a list of categorized things with the given input options and target.
  43. *
  44. * @param InputInterface $input
  45. * @param Reflector $reflector
  46. * @param mixed $target
  47. *
  48. * @return array
  49. */
  50. public function enumerate(InputInterface $input, \Reflector $reflector = null, $target = null)
  51. {
  52. $this->setFilter($input);
  53. return $this->listItems($input, $reflector, $target);
  54. }
  55. /**
  56. * Enumerate specific items with the given input options and target.
  57. *
  58. * Implementing classes should return an array of arrays:
  59. *
  60. * [
  61. * 'Constants' => [
  62. * 'FOO' => [
  63. * 'name' => 'FOO',
  64. * 'style' => 'public',
  65. * 'value' => '123',
  66. * ],
  67. * ],
  68. * ]
  69. *
  70. * @param InputInterface $input
  71. * @param Reflector $reflector
  72. * @param mixed $target
  73. *
  74. * @return array
  75. */
  76. abstract protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null);
  77. protected function presentRef($value)
  78. {
  79. return $this->presenterManager->presentRef($value);
  80. }
  81. protected function showItem($name)
  82. {
  83. return $this->filter === false || (preg_match($this->pattern, $name) xor $this->invertFilter);
  84. }
  85. private function setFilter(InputInterface $input)
  86. {
  87. if ($pattern = $input->getOption('grep')) {
  88. if (substr($pattern, 0, 1) !== '/' || substr($pattern, -1) !== '/' || strlen($pattern) < 3) {
  89. $pattern = '/' . preg_quote($pattern, '/') . '/';
  90. }
  91. if ($input->getOption('insensitive')) {
  92. $pattern .= 'i';
  93. }
  94. $this->validateRegex($pattern);
  95. $this->filter = true;
  96. $this->pattern = $pattern;
  97. $this->invertFilter = $input->getOption('invert');
  98. } else {
  99. $this->filter = false;
  100. }
  101. }
  102. /**
  103. * Validate that $pattern is a valid regular expression.
  104. *
  105. * @param string $pattern
  106. *
  107. * @return boolean
  108. */
  109. private function validateRegex($pattern)
  110. {
  111. set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
  112. try {
  113. preg_match($pattern, '');
  114. } catch (ErrorException $e) {
  115. throw new RuntimeException(str_replace('preg_match(): ', 'Invalid regular expression: ', $e->getRawMessage()));
  116. }
  117. restore_error_handler();
  118. }
  119. protected function presentSignature($target)
  120. {
  121. // This might get weird if the signature is actually for a reflector. Hrm.
  122. if (!$target instanceof \Reflector) {
  123. $target = Mirror::get($target);
  124. }
  125. return SignatureFormatter::format($target);
  126. }
  127. }