Resolve.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Protocol\Bin;
  36. use Hoa\Console;
  37. use Hoa\Protocol;
  38. /**
  39. * Class \Hoa\Protocol\Bin\Resolve.
  40. *
  41. * This command resolves some `hoa://` paths.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Resolve extends Console\Dispatcher\Kit
  47. {
  48. /**
  49. * Options description.
  50. *
  51. * @var array
  52. */
  53. protected $options = [
  54. ['exists', Console\GetOption::NO_ARGUMENT, 'E'],
  55. ['unfold', Console\GetOption::NO_ARGUMENT, 'u'],
  56. ['tree', Console\GetOption::NO_ARGUMENT, 't'],
  57. ['no-verbose', Console\GetOption::NO_ARGUMENT, 'V'],
  58. ['help', Console\GetOption::NO_ARGUMENT, 'h'],
  59. ['help', Console\GetOption::NO_ARGUMENT, '?']
  60. ];
  61. /**
  62. * The entry method.
  63. *
  64. * @return int
  65. */
  66. public function main()
  67. {
  68. $exists = true;
  69. $unfold = false;
  70. $tree = false;
  71. $verbose = Console::isDirect(STDOUT);
  72. while (false !== $c = $this->getOption($v)) {
  73. switch ($c) {
  74. case 'E':
  75. $exists = false;
  76. break;
  77. case 'u':
  78. $unfold = true;
  79. break;
  80. case 't':
  81. $tree = true;
  82. break;
  83. case 'V':
  84. $verbose = false;
  85. break;
  86. case 'h':
  87. case '?':
  88. return $this->usage();
  89. case '__ambiguous':
  90. $this->resolveOptionAmbiguity($v);
  91. break;
  92. }
  93. }
  94. $this->parser->listInputs($path);
  95. if (null === $path) {
  96. return $this->usage();
  97. }
  98. if (true === $tree) {
  99. $protocol = Protocol::getInstance();
  100. $foo = substr($path, 0, 6);
  101. if ('hoa://' !== $foo) {
  102. return;
  103. }
  104. $path = substr($path, 6);
  105. $current = $protocol;
  106. foreach (explode('/', $path) as $component) {
  107. if (!isset($current[$component])) {
  108. break;
  109. }
  110. $current = $current[$component];
  111. }
  112. echo $current;
  113. return;
  114. }
  115. if (true === $verbose) {
  116. echo
  117. Console\Cursor::colorize('foreground(yellow)'),
  118. $path,
  119. Console\Cursor::colorize('normal'),
  120. ' is equivalent to:', "\n";
  121. }
  122. $resolved = resolve($path, $exists, $unfold);
  123. foreach ((array) $resolved as $r) {
  124. echo $r, "\n";
  125. }
  126. return;
  127. }
  128. /**
  129. * The command usage.
  130. *
  131. * @return int
  132. */
  133. public function usage()
  134. {
  135. echo
  136. 'Usage : protocol:resolve <options> path', "\n",
  137. 'Options :', "\n",
  138. $this->makeUsageOptionsList([
  139. 'E' => 'Do not check if the resolution result exists.',
  140. 'u' => 'Unfold all possible results.',
  141. 't' => 'Print the tree from the path.',
  142. 'V' => 'No-verbose, i.e. be as quiet as possible, just print ' .
  143. 'essential information.',
  144. 'help' => 'This help.'
  145. ]), "\n";
  146. return;
  147. }
  148. }
  149. __halt_compiler();
  150. Resolve `hoa://` paths.