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\Input;
12 use Joomla\Registry\Registry;
13 use Psr\Log\LoggerAwareInterface;
14 use Psr\Log\LoggerInterface;
15 use Psr\Log\NullLogger;
16
17 /**
18 * Joomla Framework Base Application Class
19 *
20 * @since 1.0
21 */
22 abstract class AbstractApplication implements LoggerAwareInterface
23 {
24 /**
25 * The application configuration object.
26 *
27 * @var Registry
28 * @since 1.0
29 */
30 protected $config;
31
32 /**
33 * The application input object.
34 *
35 * @var Input
36 * @since 1.0
37 */
38 public $input = null;
39
40 /**
41 * A logger.
42 *
43 * @var LoggerInterface
44 * @since 1.0
45 */
46 private $logger;
47
48 /**
49 * Class constructor.
50 *
51 * @param Input $input An optional argument to provide dependency injection for the application's input object. If the argument is an
52 * Input object that object will become the application's input object, otherwise a default input object is created.
53 * @param Registry $config An optional argument to provide dependency injection for the application's config object. If the argument
54 * is a Registry object that object will become the application's config object, otherwise a default config
55 * object is created.
56 *
57 * @since 1.0
58 */
59 public function __construct(Input $input = null, Registry $config = null)
60 {
61 $this->input = $input instanceof Input ? $input : new Input;
62 $this->config = $config instanceof Registry ? $config : new Registry;
63
64 // Set the execution datetime and timestamp;
65 $this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
66 $this->set('execution.timestamp', time());
67 $this->set('execution.microtimestamp', microtime(true));
68
69 $this->initialise();
70 }
71
72 /**
73 * Method to close the application.
74 *
75 * @param integer $code The exit code (optional; default is 0).
76 *
77 * @return void
78 *
79 * @codeCoverageIgnore
80 * @since 1.0
81 */
82 public function close($code = 0)
83 {
84 exit($code);
85 }
86
87 /**
88 * Method to run the application routines. Most likely you will want to instantiate a controller
89 * and execute it, or perform some sort of task directly.
90 *
91 * @return void
92 *
93 * @since 1.0
94 */
95 abstract protected function doExecute();
96
97 /**
98 * Execute the application.
99 *
100 * @return void
101 *
102 * @since 1.0
103 */
104 public function execute()
105 {
106 // @event onBeforeExecute
107
108 // Perform application routines.
109 $this->doExecute();
110
111 // @event onAfterExecute
112 }
113
114 /**
115 * Returns a property of the object or the default value if the property is not set.
116 *
117 * @param string $key The name of the property.
118 * @param mixed $default The default value (optional) if none is set.
119 *
120 * @return mixed The value of the configuration.
121 *
122 * @since 1.0
123 */
124 public function get($key, $default = null)
125 {
126 return $this->config->get($key, $default);
127 }
128
129 /**
130 * Get the logger.
131 *
132 * @return LoggerInterface
133 *
134 * @since 1.0
135 */
136 public function getLogger()
137 {
138 // If a logger hasn't been set, use NullLogger
139 if (! ($this->logger instanceof LoggerInterface))
140 {
141 $this->logger = new NullLogger;
142 }
143
144 return $this->logger;
145 }
146
147 /**
148 * Custom initialisation method.
149 *
150 * Called at the end of the AbstractApplication::__construct method.
151 * This is for developers to inject initialisation code for their application classes.
152 *
153 * @return void
154 *
155 * @codeCoverageIgnore
156 * @since 1.0
157 */
158 protected function initialise()
159 {
160 }
161
162 /**
163 * Modifies a property of the object, creating it if it does not already exist.
164 *
165 * @param string $key The name of the property.
166 * @param mixed $value The value of the property to set (optional).
167 *
168 * @return mixed Previous value of the property
169 *
170 * @since 1.0
171 */
172 public function set($key, $value = null)
173 {
174 $previous = $this->config->get($key);
175 $this->config->set($key, $value);
176
177 return $previous;
178 }
179
180 /**
181 * Sets the configuration for the application.
182 *
183 * @param Registry $config A registry object holding the configuration.
184 *
185 * @return AbstractApplication Returns itself to support chaining.
186 *
187 * @since 1.0
188 */
189 public function setConfiguration(Registry $config)
190 {
191 $this->config = $config;
192
193 return $this;
194 }
195
196 /**
197 * Set the logger.
198 *
199 * @param LoggerInterface $logger The logger.
200 *
201 * @return AbstractApplication Returns itself to support chaining.
202 *
203 * @since 1.0
204 */
205 public function setLogger(LoggerInterface $logger)
206 {
207 $this->logger = $logger;
208
209 return $this;
210 }
211 }
212