PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Aura/Cli/AbstractCommand.php

https://bitbucket.org/harikt/aura.cli
PHP | 194 lines | 52 code | 15 blank | 127 comment | 0 complexity | 90ad4b287ccaea108414ae0a1d703e80 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. * This file is part of the Aura project for PHP.
  5. *
  6. * @package Aura.Cli
  7. *
  8. * @license http://opensource.org/licenses/bsd-license.php BSD
  9. *
  10. */
  11. namespace Aura\Cli;
  12. /**
  13. *
  14. * The CLI equivalent of a page-controller to perform a single action.
  15. *
  16. * @package Aura.Cli
  17. *
  18. */
  19. abstract class AbstractCommand
  20. {
  21. /**
  22. *
  23. * A Getopt object for the Command; retains the short and long options
  24. * passed at the command line.
  25. *
  26. * @var Aura\Cli\Getopt
  27. *
  28. */
  29. protected $getopt;
  30. /**
  31. *
  32. * The option definitions for the Getopt object.
  33. *
  34. * @var array
  35. *
  36. */
  37. protected $options = [];
  38. /**
  39. *
  40. * Should Getopt be strict about how options are processed? In strict
  41. * mode, passing an undefined option will throw an exception; in
  42. * non-strict, it will not.
  43. *
  44. * @var bool
  45. *
  46. */
  47. protected $options_strict = Getopt::STRICT;
  48. /**
  49. *
  50. * The positional (numeric) arguments passed at the command line.
  51. *
  52. * @var array
  53. *
  54. */
  55. protected $params = [];
  56. /**
  57. *
  58. * Constructor.
  59. *
  60. * @param \Aura\Cli\Context $context The command-line context.
  61. *
  62. * @param \Aura\Cli\Stdio $stdio Standard input/output streams.
  63. *
  64. * @param \Aura\Cli\Getopt $getopt An options processor and reader.
  65. *
  66. */
  67. public function __construct(
  68. Context $context,
  69. Stdio $stdio,
  70. Getopt $getopt
  71. ) {
  72. $this->context = $context;
  73. $this->stdio = $stdio;
  74. $this->getopt = $getopt;
  75. $this->initGetopt();
  76. $this->initParams();
  77. }
  78. /**
  79. *
  80. * Passes the Context arguments to `$getopt`.
  81. *
  82. * @return void
  83. *
  84. */
  85. protected function initGetopt()
  86. {
  87. $this->getopt->init($this->options, $this->options_strict);
  88. $this->getopt->load($this->context->getArgv());
  89. }
  90. /**
  91. *
  92. * Loads `$params` from `$getopt`.
  93. *
  94. * @return void
  95. *
  96. */
  97. protected function initParams()
  98. {
  99. $this->params = $this->getopt->getParams();
  100. }
  101. /**
  102. *
  103. * Executes the Command. In order, it does these things:
  104. *
  105. * - calls `preExec()`
  106. *
  107. * - calls `preAction()`
  108. *
  109. * - calles `action()`
  110. *
  111. * - calls `postAction()`
  112. *
  113. * - calls `postExec()`
  114. *
  115. * @see action()
  116. *
  117. * @return void
  118. *
  119. */
  120. public function exec()
  121. {
  122. $this->preExec();
  123. $this->preAction();
  124. $this->action();
  125. $this->postAction();
  126. $this->postExec();
  127. // return terminal output to normal colors
  128. $this->stdio->out("%n");
  129. $this->stdio->err("%n");
  130. }
  131. /**
  132. *
  133. * Runs at the beginning of `exec()` before `preAction()`.
  134. *
  135. * @return void
  136. *
  137. */
  138. public function preExec()
  139. {
  140. }
  141. /**
  142. *
  143. * Runs before `action()` but after `preExec()`.
  144. *
  145. * @return mixed
  146. *
  147. */
  148. public function preAction()
  149. {
  150. }
  151. /**
  152. *
  153. * The main logic for the Command.
  154. *
  155. * @return void
  156. *
  157. */
  158. abstract protected function action();
  159. /**
  160. *
  161. * Runs after `action()` but before `postExec()`.
  162. *
  163. * @return mixed
  164. *
  165. */
  166. public function postAction()
  167. {
  168. }
  169. /**
  170. *
  171. * Runs at the end of `exec()` after `postAction()`.
  172. *
  173. * @return mixed
  174. *
  175. */
  176. public function postExec()
  177. {
  178. }
  179. }