ExitCode.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console;
  8. /**
  9. * This class provides constants for defining console command exit codes.
  10. *
  11. * The exit codes follow the codes defined in the [FreeBSD sysexits(3)](http://man.openbsd.org/sysexits) manual page.
  12. *
  13. * These constants can be used in console controllers for example like this:
  14. *
  15. * ```php
  16. * public function actionIndex()
  17. * {
  18. * if (!$this->isAllowedToPerformAction()) {
  19. * $this->stderr('Error: ' . ExitCode::getReason(ExitCode::NOPERM));
  20. * return ExitCode::NOPERM;
  21. * }
  22. *
  23. * // do something
  24. *
  25. * return ExitCode::OK;
  26. * }
  27. * ```
  28. *
  29. * @author Tom Worster <fsb@thefsb.org>
  30. * @author Alexander Makarov <sam@rmcreative.ru>
  31. * @see http://man.openbsd.org/sysexits
  32. * @since 2.0.13
  33. */
  34. class ExitCode
  35. {
  36. /**
  37. * The command completed successfully.
  38. */
  39. const OK = 0;
  40. /**
  41. * The command exited with an error code that says nothing about the error.
  42. */
  43. const UNSPECIFIED_ERROR = 1;
  44. /**
  45. * The command was used incorrectly, e.g., with the wrong number of
  46. * arguments, a bad flag, a bad syntax in a parameter, or whatever.
  47. */
  48. const USAGE = 64;
  49. /**
  50. * The input data was incorrect in some way. This should only be used for
  51. * user's data and not system files.
  52. */
  53. const DATAERR = 65;
  54. /**
  55. * An input file (not a system file) did not exist or was not readable.
  56. * This could also include errors like ``No message'' to a mailer (if it
  57. * cared to catch it).
  58. */
  59. const NOINPUT = 66;
  60. /**
  61. * The user specified did not exist. This might be used for mail addresses
  62. * or remote logins.
  63. */
  64. const NOUSER = 67;
  65. /**
  66. * The host specified did not exist. This is used in mail addresses or
  67. * network requests.
  68. */
  69. const NOHOST = 68;
  70. /**
  71. * A service is unavailable. This can occur if a support program or file
  72. * does not exist. This can also be used as a catchall message when
  73. * something you wanted to do does not work, but you do not know why.
  74. */
  75. const UNAVAILABLE = 69;
  76. /**
  77. * An internal software error has been detected. This should be limited to
  78. * non-operating system related errors as possible.
  79. */
  80. const SOFTWARE = 70;
  81. /**
  82. * An operating system error has been detected. This is intended to be
  83. * used for such things as ``cannot fork'', ``cannot create pipe'', or the
  84. * like. It includes things like getuid returning a user that does not
  85. * exist in the passwd file.
  86. */
  87. const OSERR = 71;
  88. /**
  89. * Some system file (e.g., /etc/passwd, /var/run/utx.active, etc.) does not
  90. * exist, cannot be opened, or has some sort of error (e.g., syntax error).
  91. */
  92. const OSFILE = 72;
  93. /**
  94. * A (user specified) output file cannot be created.
  95. */
  96. const CANTCREAT = 73;
  97. /**
  98. * An error occurred while doing I/O on some file.
  99. */
  100. const IOERR = 74;
  101. /**
  102. * Temporary failure, indicating something that is not really an error. In
  103. * sendmail, this means that a mailer (e.g.) could not create a connection,
  104. * and the request should be reattempted later.
  105. */
  106. const TEMPFAIL = 75;
  107. /**
  108. * The remote system returned something that was ``not possible'' during a
  109. * protocol exchange.
  110. */
  111. const PROTOCOL = 76;
  112. /**
  113. * You did not have sufficient permission to perform the operation. This
  114. * is not intended for file system problems, which should use NOINPUT or
  115. * CANTCREAT, but rather for higher level permissions.
  116. */
  117. const NOPERM = 77;
  118. /**
  119. * Something was found in an unconfigured or misconfigured state.
  120. */
  121. const CONFIG = 78;
  122. /**
  123. * @var array a map of reason descriptions for exit codes.
  124. */
  125. public static $reasons = [
  126. self::OK => 'Success',
  127. self::UNSPECIFIED_ERROR => 'Unspecified error',
  128. self::USAGE => 'Incorrect usage, argument or option error',
  129. self::DATAERR => 'Error in input data',
  130. self::NOINPUT => 'Input file not found or unreadable',
  131. self::NOUSER => 'User not found',
  132. self::NOHOST => 'Host not found',
  133. self::UNAVAILABLE => 'A required service is unavailable',
  134. self::SOFTWARE => 'Internal error',
  135. self::OSERR => 'Error making system call or using OS service',
  136. self::OSFILE => 'Error accessing system file',
  137. self::CANTCREAT => 'Cannot create output file',
  138. self::IOERR => 'I/O error',
  139. self::TEMPFAIL => 'Temporary failure',
  140. self::PROTOCOL => 'Unexpected remote service behavior',
  141. self::NOPERM => 'Insufficient permissions',
  142. self::CONFIG => 'Configuration error',
  143. ];
  144. /**
  145. * Returns a short reason text for the given exit code.
  146. *
  147. * This method uses [[$reasons]] to determine the reason for an exit code.
  148. * @param int $exitCode one of the constants defined in this class.
  149. * @return string the reason text, or `"Unknown exit code"` if the code is not listed in [[$reasons]].
  150. */
  151. public static function getReason($exitCode)
  152. {
  153. return isset(static::$reasons[$exitCode]) ? static::$reasons[$exitCode] : 'Unknown exit code';
  154. }
  155. }