/libraries/vendor/joomla/application/src/AbstractCliApplication.php

https://gitlab.com/vitaliylukin91/idea-rating · PHP · 108 lines · 34 code · 13 blank · 61 comment · 3 complexity · e8b51ad5e149d1a049eaf60971757353 MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Joomla Framework Application Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Application;
  9. use Joomla\Registry\Registry;
  10. use Joomla\Input;
  11. use Joomla\Application\Cli\CliOutput;
  12. /**
  13. * Base class for a Joomla! command line application.
  14. *
  15. * @since 1.0
  16. */
  17. abstract class AbstractCliApplication extends AbstractApplication
  18. {
  19. /**
  20. * @var CliOutput Output object
  21. * @since 1.0
  22. */
  23. protected $output;
  24. /**
  25. * Class constructor.
  26. *
  27. * @param Input\Cli $input An optional argument to provide dependency injection for the application's
  28. * input object. If the argument is a InputCli object that object will become
  29. * the application's input object, otherwise a default input object is created.
  30. * @param Registry $config An optional argument to provide dependency injection for the application's
  31. * config object. If the argument is a Registry object that object will become
  32. * the application's config object, otherwise a default config object is created.
  33. *
  34. * @param CliOutput $output The output handler.
  35. *
  36. * @since 1.0
  37. */
  38. public function __construct(Input\Cli $input = null, Registry $config = null, CliOutput $output = null)
  39. {
  40. // Close the application if we are not executed from the command line.
  41. // @codeCoverageIgnoreStart
  42. if (!defined('STDOUT') || !defined('STDIN') || !isset($_SERVER['argv']))
  43. {
  44. $this->close();
  45. }
  46. // @codeCoverageIgnoreEnd
  47. $this->output = ($output instanceof CliOutput) ? $output : new Cli\Output\Stdout;
  48. // Call the constructor as late as possible (it runs `initialise`).
  49. parent::__construct($input instanceof Input\Input ? $input : new Input\Cli, $config);
  50. // Set the execution datetime and timestamp;
  51. $this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
  52. $this->set('execution.timestamp', time());
  53. // Set the current directory.
  54. $this->set('cwd', getcwd());
  55. }
  56. /**
  57. * Get an output object.
  58. *
  59. * @return CliOutput
  60. *
  61. * @since 1.0
  62. */
  63. public function getOutput()
  64. {
  65. return $this->output;
  66. }
  67. /**
  68. * Write a string to standard output.
  69. *
  70. * @param string $text The text to display.
  71. * @param boolean $nl True (default) to append a new line at the end of the output string.
  72. *
  73. * @return AbstractCliApplication Instance of $this to allow chaining.
  74. *
  75. * @codeCoverageIgnore
  76. * @since 1.0
  77. */
  78. public function out($text = '', $nl = true)
  79. {
  80. $this->output->out($text, $nl);
  81. return $this;
  82. }
  83. /**
  84. * Get a value from standard input.
  85. *
  86. * @return string The input string from standard input.
  87. *
  88. * @codeCoverageIgnore
  89. * @since 1.0
  90. */
  91. public function in()
  92. {
  93. return rtrim(fread(STDIN, 8192), "\n\r");
  94. }
  95. }