Base for a static organization website

ConsoleInput.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * ConsoleInput file.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Console
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. /**
  19. * Object wrapper for interacting with stdin
  20. *
  21. * @package Cake.Console
  22. */
  23. class ConsoleInput {
  24. /**
  25. * Input value.
  26. *
  27. * @var resource
  28. */
  29. protected $_input;
  30. /**
  31. * Can this instance use readline?
  32. * Two conditions must be met:
  33. * 1. Readline support must be enabled.
  34. * 2. Handle we are attached to must be stdin.
  35. * Allows rich editing with arrow keys and history when inputting a string.
  36. *
  37. * @var bool
  38. */
  39. protected $_canReadline;
  40. /**
  41. * Constructor
  42. *
  43. * @param string $handle The location of the stream to use as input.
  44. */
  45. public function __construct($handle = 'php://stdin') {
  46. $this->_canReadline = extension_loaded('readline') && $handle === 'php://stdin' ? true : false;
  47. $this->_input = fopen($handle, 'r');
  48. }
  49. /**
  50. * Read a value from the stream
  51. *
  52. * @return mixed The value of the stream
  53. */
  54. public function read() {
  55. if ($this->_canReadline) {
  56. $line = readline('');
  57. if (!empty($line)) {
  58. readline_add_history($line);
  59. }
  60. return $line;
  61. }
  62. return fgets($this->_input);
  63. }
  64. /**
  65. * Checks if data is available on the stream
  66. *
  67. * @param int $timeout An optional time to wait for data
  68. * @return bool True for data available, false otherwise
  69. */
  70. public function dataAvailable($timeout = 0) {
  71. $readFds = array($this->_input);
  72. $readyFds = stream_select($readFds, $writeFds, $errorFds, $timeout);
  73. return ($readyFds > 0);
  74. }
  75. }