[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2011 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Application; 11 12 use Joomla\Application\AbstractWebApplication; 13 use Joomla\Application\Web\WebClient; 14 use Joomla\CMS\Document\Document; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Input\Input; 17 use Joomla\CMS\Language\Language; 18 use Joomla\CMS\Session\Session; 19 use Joomla\CMS\Uri\Uri; 20 use Joomla\CMS\User\User; 21 use Joomla\CMS\Version; 22 use Joomla\Registry\Registry; 23 use Joomla\Session\SessionEvent; 24 use Psr\Http\Message\ResponseInterface; 25 26 // phpcs:disable PSR1.Files.SideEffects 27 \defined('JPATH_PLATFORM') or die; 28 // phpcs:enable PSR1.Files.SideEffects 29 30 /** 31 * Base class for a Joomla! Web application. 32 * 33 * @since 2.5.0 34 */ 35 abstract class WebApplication extends AbstractWebApplication 36 { 37 use EventAware; 38 use IdentityAware; 39 40 /** 41 * The application document object. 42 * 43 * @var Document 44 * @since 1.7.3 45 */ 46 protected $document; 47 48 /** 49 * The application language object. 50 * 51 * @var Language 52 * @since 1.7.3 53 */ 54 protected $language; 55 56 /** 57 * The application instance. 58 * 59 * @var static 60 * @since 1.7.3 61 */ 62 protected static $instance; 63 64 /** 65 * Class constructor. 66 * 67 * @param Input $input An optional argument to provide dependency injection for the application's 68 * input object. If the argument is a JInput object that object will become 69 * the application's input object, otherwise a default input object is created. 70 * @param Registry $config An optional argument to provide dependency injection for the application's 71 * config object. If the argument is a Registry object that object will become 72 * the application's config object, otherwise a default config object is created. 73 * @param WebClient $client An optional argument to provide dependency injection for the application's 74 * client object. If the argument is a WebClient object that object will become 75 * the application's client object, otherwise a default client object is created. 76 * @param ResponseInterface $response An optional argument to provide dependency injection for the application's 77 * response object. If the argument is a ResponseInterface object that object 78 * will become the application's response object, otherwise a default response 79 * object is created. 80 * 81 * @since 1.7.3 82 */ 83 public function __construct(Input $input = null, Registry $config = null, WebClient $client = null, ResponseInterface $response = null) 84 { 85 // Ensure we have a CMS Input object otherwise the DI for \Joomla\CMS\Session\Storage\JoomlaStorage fails 86 $input = $input ?: new Input(); 87 88 parent::__construct($input, $config, $client, $response); 89 90 // Set the execution datetime and timestamp; 91 $this->set('execution.datetime', gmdate('Y-m-d H:i:s')); 92 $this->set('execution.timestamp', time()); 93 94 // Set the system URIs. 95 $this->loadSystemUris(); 96 } 97 98 /** 99 * Returns a reference to the global WebApplication object, only creating it if it doesn't already exist. 100 * 101 * This method must be invoked as: $web = WebApplication::getInstance(); 102 * 103 * @param string $name The name (optional) of the WebApplication class to instantiate. 104 * 105 * @return WebApplication 106 * 107 * @since 1.7.3 108 * @throws \RuntimeException 109 * @deprecated 5.0 Use \Joomla\CMS\Factory::getContainer()->get($name) instead 110 */ 111 public static function getInstance($name = null) 112 { 113 // Only create the object if it doesn't exist. 114 if (empty(static::$instance)) { 115 if (!is_subclass_of($name, '\\Joomla\\CMS\\Application\\WebApplication')) { 116 throw new \RuntimeException(sprintf('Unable to load application: %s', $name), 500); 117 } 118 119 static::$instance = new $name(); 120 } 121 122 return static::$instance; 123 } 124 125 /** 126 * Execute the application. 127 * 128 * @return void 129 * 130 * @since 1.7.3 131 */ 132 public function execute() 133 { 134 // Trigger the onBeforeExecute event. 135 $this->triggerEvent('onBeforeExecute'); 136 137 // Perform application routines. 138 $this->doExecute(); 139 140 // Trigger the onAfterExecute event. 141 $this->triggerEvent('onAfterExecute'); 142 143 // If we have an application document object, render it. 144 if ($this->document instanceof Document) { 145 // Trigger the onBeforeRender event. 146 $this->triggerEvent('onBeforeRender'); 147 148 // Render the application output. 149 $this->render(); 150 151 // Trigger the onAfterRender event. 152 $this->triggerEvent('onAfterRender'); 153 } 154 155 // If gzip compression is enabled in configuration and the server is compliant, compress the output. 156 if ($this->get('gzip') && !ini_get('zlib.output_compression') && (ini_get('output_handler') !== 'ob_gzhandler')) { 157 $this->compress(); 158 } 159 160 // Trigger the onBeforeRespond event. 161 $this->triggerEvent('onBeforeRespond'); 162 163 // Send the application response. 164 $this->respond(); 165 166 // Trigger the onAfterRespond event. 167 $this->triggerEvent('onAfterRespond'); 168 } 169 170 /** 171 * Rendering is the process of pushing the document buffers into the template 172 * placeholders, retrieving data from the document and pushing it into 173 * the application response buffer. 174 * 175 * @return void 176 * 177 * @since 1.7.3 178 */ 179 protected function render() 180 { 181 // Setup the document options. 182 $options = array( 183 'template' => $this->get('theme'), 184 'file' => $this->get('themeFile', 'index.php'), 185 'params' => $this->get('themeParams'), 186 'templateInherits' => $this->get('themeInherits'), 187 ); 188 189 if ($this->get('themes.base')) { 190 $options['directory'] = $this->get('themes.base'); 191 } else { 192 // Fall back to constants. 193 $options['directory'] = \defined('JPATH_THEMES') ? JPATH_THEMES : (\defined('JPATH_BASE') ? JPATH_BASE : __DIR__) . '/themes'; 194 } 195 196 // Parse the document. 197 $this->document->parse($options); 198 199 // Render the document. 200 $data = $this->document->render($this->get('cache_enabled'), $options); 201 202 // Set the application output data. 203 $this->setBody($data); 204 } 205 206 /** 207 * Method to get the application document object. 208 * 209 * @return Document The document object 210 * 211 * @since 1.7.3 212 */ 213 public function getDocument() 214 { 215 return $this->document; 216 } 217 218 /** 219 * Method to get the application language object. 220 * 221 * @return Language The language object 222 * 223 * @since 1.7.3 224 */ 225 public function getLanguage() 226 { 227 return $this->language; 228 } 229 230 /** 231 * Flush the media version to refresh versionable assets 232 * 233 * @return void 234 * 235 * @since 3.2 236 */ 237 public function flushAssets() 238 { 239 (new Version())->refreshMediaVersion(); 240 } 241 242 /** 243 * Allows the application to load a custom or default document. 244 * 245 * The logic and options for creating this object are adequately generic for default cases 246 * but for many applications it will make sense to override this method and create a document, 247 * if required, based on more specific needs. 248 * 249 * @param Document $document An optional document object. If omitted, the factory document is created. 250 * 251 * @return WebApplication This method is chainable. 252 * 253 * @since 1.7.3 254 */ 255 public function loadDocument(Document $document = null) 256 { 257 $this->document = $document ?? Factory::getDocument(); 258 259 return $this; 260 } 261 262 /** 263 * Allows the application to load a custom or default language. 264 * 265 * The logic and options for creating this object are adequately generic for default cases 266 * but for many applications it will make sense to override this method and create a language, 267 * if required, based on more specific needs. 268 * 269 * @param Language $language An optional language object. If omitted, the factory language is created. 270 * 271 * @return WebApplication This method is chainable. 272 * 273 * @since 1.7.3 274 */ 275 public function loadLanguage(Language $language = null) 276 { 277 $this->language = $language ?? Factory::getLanguage(); 278 279 return $this; 280 } 281 282 /** 283 * Allows the application to load a custom or default session. 284 * 285 * The logic and options for creating this object are adequately generic for default cases 286 * but for many applications it will make sense to override this method and create a session, 287 * if required, based on more specific needs. 288 * 289 * @param Session $session An optional session object. If omitted, the session is created. 290 * 291 * @return WebApplication This method is chainable. 292 * 293 * @since 1.7.3 294 * @deprecated 5.0 The session should be injected as a service. 295 */ 296 public function loadSession(Session $session = null) 297 { 298 $this->getLogger()->warning(__METHOD__ . '() is deprecated. Inject the session as a service instead.', array('category' => 'deprecated')); 299 300 return $this; 301 } 302 303 /** 304 * After the session has been started we need to populate it with some default values. 305 * 306 * @param SessionEvent $event Session event being triggered 307 * 308 * @return void 309 * 310 * @since 3.0.1 311 */ 312 public function afterSessionStart(SessionEvent $event) 313 { 314 $session = $event->getSession(); 315 316 if ($session->isNew()) { 317 $session->set('registry', new Registry()); 318 $session->set('user', new User()); 319 } 320 321 // Ensure the identity is loaded 322 if (!$this->getIdentity()) { 323 $this->loadIdentity($session->get('user')); 324 } 325 } 326 327 /** 328 * Method to load the system URI strings for the application. 329 * 330 * @param string $requestUri An optional request URI to use instead of detecting one from the 331 * server environment variables. 332 * 333 * @return void 334 * 335 * @since 1.7.3 336 */ 337 protected function loadSystemUris($requestUri = null) 338 { 339 // Set the request URI. 340 if (!empty($requestUri)) { 341 $this->set('uri.request', $requestUri); 342 } else { 343 $this->set('uri.request', $this->detectRequestUri()); 344 } 345 346 // Check to see if an explicit base URI has been set. 347 $siteUri = trim($this->get('site_uri', '')); 348 349 if ($siteUri !== '') { 350 $uri = Uri::getInstance($siteUri); 351 $path = $uri->toString(array('path')); 352 } else { 353 // No explicit base URI was set so we need to detect it. 354 // Start with the requested URI. 355 $uri = Uri::getInstance($this->get('uri.request')); 356 357 // If we are working from a CGI SAPI with the 'cgi.fix_pathinfo' directive disabled we use PHP_SELF. 358 if (strpos(PHP_SAPI, 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($_SERVER['REQUEST_URI'])) { 359 // We aren't expecting PATH_INFO within PHP_SELF so this should work. 360 $path = \dirname($_SERVER['PHP_SELF']); 361 } else { 362 // Pretty much everything else should be handled with SCRIPT_NAME. 363 $path = \dirname($_SERVER['SCRIPT_NAME']); 364 } 365 } 366 367 $host = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port')); 368 369 // Check if the path includes "index.php". 370 if (strpos($path, 'index.php') !== false) { 371 // Remove the index.php portion of the path. 372 $path = substr_replace($path, '', strpos($path, 'index.php'), 9); 373 } 374 375 $path = rtrim($path, '/\\'); 376 377 // Set the base URI both as just a path and as the full URI. 378 $this->set('uri.base.full', $host . $path . '/'); 379 $this->set('uri.base.host', $host); 380 $this->set('uri.base.path', $path . '/'); 381 382 // Set the extended (non-base) part of the request URI as the route. 383 if (stripos($this->get('uri.request'), $this->get('uri.base.full')) === 0) { 384 $this->set('uri.route', substr_replace($this->get('uri.request'), '', 0, \strlen($this->get('uri.base.full')))); 385 } 386 387 // Get an explicitly set media URI is present. 388 $mediaURI = trim($this->get('media_uri', '')); 389 390 if ($mediaURI) { 391 if (strpos($mediaURI, '://') !== false) { 392 $this->set('uri.media.full', $mediaURI); 393 $this->set('uri.media.path', $mediaURI); 394 } else { 395 // Normalise slashes. 396 $mediaURI = trim($mediaURI, '/\\'); 397 $mediaURI = !empty($mediaURI) ? '/' . $mediaURI . '/' : '/'; 398 $this->set('uri.media.full', $this->get('uri.base.host') . $mediaURI); 399 $this->set('uri.media.path', $mediaURI); 400 } 401 } else { 402 // No explicit media URI was set, build it dynamically from the base uri. 403 $this->set('uri.media.full', $this->get('uri.base.full') . 'media/'); 404 $this->set('uri.media.path', $this->get('uri.base.path') . 'media/'); 405 } 406 } 407 408 /** 409 * Retrieve the application configuration object. 410 * 411 * @return Registry 412 * 413 * @since 4.0.0 414 */ 415 public function getConfig() 416 { 417 return $this->config; 418 } 419 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |