1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Application
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Application\AbstractApplication;
13 use Joomla\Registry\Registry;
14
15 /**
16 * Joomla Platform Base Application Class
17 *
18 * @property-read JInput $input The application input object
19 *
20 * @since 12.1
21 */
22 abstract class JApplicationBase extends AbstractApplication
23 {
24 /**
25 * The application dispatcher object.
26 *
27 * @var JEventDispatcher
28 * @since 12.1
29 */
30 protected $dispatcher;
31
32 /**
33 * The application identity object.
34 *
35 * @var JUser
36 * @since 12.1
37 */
38 protected $identity;
39
40 /**
41 * Class constructor.
42 *
43 * @param JInput $input An optional argument to provide dependency injection for the application's
44 * input object. If the argument is a JInput object that object will become
45 * the application's input object, otherwise a default input object is created.
46 * @param Registry $config An optional argument to provide dependency injection for the application's
47 * config object. If the argument is a Registry object that object will become
48 * the application's config object, otherwise a default config object is created.
49 *
50 * @since 12.1
51 */
52 public function __construct(JInput $input = null, Registry $config = null)
53 {
54 $this->input = $input instanceof JInput ? $input : new JInput;
55 $this->config = $config instanceof Registry ? $config : new Registry;
56
57 $this->initialise();
58 }
59
60 /**
61 * Get the application identity.
62 *
63 * @return mixed A JUser object or null.
64 *
65 * @since 12.1
66 */
67 public function getIdentity()
68 {
69 return $this->identity;
70 }
71
72 /**
73 * Registers a handler to a particular event group.
74 *
75 * @param string $event The event name.
76 * @param callable $handler The handler, a function or an instance of an event object.
77 *
78 * @return JApplicationBase The application to allow chaining.
79 *
80 * @since 12.1
81 */
82 public function registerEvent($event, $handler)
83 {
84 if ($this->dispatcher instanceof JEventDispatcher)
85 {
86 $this->dispatcher->register($event, $handler);
87 }
88
89 return $this;
90 }
91
92 /**
93 * Calls all handlers associated with an event group.
94 *
95 * @param string $event The event name.
96 * @param array $args An array of arguments (optional).
97 *
98 * @return array An array of results from each function call, or null if no dispatcher is defined.
99 *
100 * @since 12.1
101 */
102 public function triggerEvent($event, array $args = null)
103 {
104 if ($this->dispatcher instanceof JEventDispatcher)
105 {
106 return $this->dispatcher->trigger($event, $args);
107 }
108
109 return;
110 }
111
112 /**
113 * Allows the application to load a custom or default dispatcher.
114 *
115 * The logic and options for creating this object are adequately generic for default cases
116 * but for many applications it will make sense to override this method and create event
117 * dispatchers, if required, based on more specific needs.
118 *
119 * @param JEventDispatcher $dispatcher An optional dispatcher object. If omitted, the factory dispatcher is created.
120 *
121 * @return JApplicationBase This method is chainable.
122 *
123 * @since 12.1
124 */
125 public function loadDispatcher(JEventDispatcher $dispatcher = null)
126 {
127 $this->dispatcher = ($dispatcher === null) ? JEventDispatcher::getInstance() : $dispatcher;
128
129 return $this;
130 }
131
132 /**
133 * Allows the application to load a custom or default identity.
134 *
135 * The logic and options for creating this object are adequately generic for default cases
136 * but for many applications it will make sense to override this method and create an identity,
137 * if required, based on more specific needs.
138 *
139 * @param JUser $identity An optional identity object. If omitted, the factory user is created.
140 *
141 * @return JApplicationBase This method is chainable.
142 *
143 * @since 12.1
144 */
145 public function loadIdentity(JUser $identity = null)
146 {
147 $this->identity = ($identity === null) ? JFactory::getUser() : $identity;
148
149 return $this;
150 }
151
152 /**
153 * Method to run the application routines. Most likely you will want to instantiate a controller
154 * and execute it, or perform some sort of task directly.
155 *
156 * @return void
157 *
158 * @since 3.4 (CMS)
159 * @deprecated 4.0 The default concrete implementation of doExecute() will be removed, subclasses will need to provide their own implementation.
160 */
161 protected function doExecute()
162 {
163 return;
164 }
165 }
166