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