[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/installation/src/Application/ -> InstallationApplication.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Installation
   5   * @subpackage  Application
   6   *
   7   * @copyright   (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\CMS\Installation\Application;
  12  
  13  use Joomla\Application\Web\WebClient;
  14  use Joomla\CMS\Application\CMSApplication;
  15  use Joomla\CMS\Date\Date;
  16  use Joomla\CMS\Document\Document;
  17  use Joomla\CMS\Document\FactoryInterface;
  18  use Joomla\CMS\Document\HtmlDocument;
  19  use Joomla\CMS\Exception\ExceptionHandler;
  20  use Joomla\CMS\Factory;
  21  use Joomla\CMS\Filesystem\Folder;
  22  use Joomla\CMS\Input\Input;
  23  use Joomla\CMS\Language\LanguageHelper;
  24  use Joomla\CMS\Language\Text;
  25  use Joomla\CMS\MVC\Factory\MVCFactory;
  26  use Joomla\CMS\Uri\Uri;
  27  use Joomla\Database\DatabaseInterface;
  28  use Joomla\DI\Container;
  29  use Joomla\Registry\Registry;
  30  use Joomla\Session\SessionEvent;
  31  
  32  // phpcs:disable PSR1.Files.SideEffects
  33  \defined('_JEXEC') or die;
  34  // phpcs:enable PSR1.Files.SideEffects
  35  
  36  /**
  37   * Joomla! Installation Application class.
  38   *
  39   * @since  3.1
  40   */
  41  final class InstallationApplication extends CMSApplication
  42  {
  43      use \Joomla\CMS\Application\ExtensionNamespaceMapper;
  44  
  45      /**
  46       * Class constructor.
  47       *
  48       * @param   Input|null      $input      An optional argument to provide dependency injection for the application's input
  49       *                                      object.  If the argument is a JInput object that object will become the
  50       *                                      application's input object, otherwise a default input object is created.
  51       * @param   Registry|null   $config     An optional argument to provide dependency injection for the application's
  52       *                                      config object.  If the argument is a Registry object that object will become
  53       *                                      the application's config object, otherwise a default config object is created.
  54       * @param   WebClient|null  $client     An optional argument to provide dependency injection for the application's
  55       *                                      client object.  If the argument is a WebClient object that object will become the
  56       *                                      application's client object, otherwise a default client object is created.
  57       * @param   Container|null  $container  Dependency injection container.
  58       *
  59       * @since   3.1
  60       */
  61      public function __construct(Input $input = null, Registry $config = null, WebClient $client = null, Container $container = null)
  62      {
  63          // Register the application name.
  64          $this->name = 'installation';
  65  
  66          // Register the client ID.
  67          $this->clientId = 2;
  68  
  69          // Run the parent constructor.
  70          parent::__construct($input, $config, $client, $container);
  71  
  72          // Store the debug value to config based on the JDEBUG flag.
  73          $this->config->set('debug', JDEBUG);
  74  
  75          // Register the config to Factory.
  76          Factory::$config = $this->config;
  77  
  78          // Set the root in the URI one level up.
  79          $parts = explode('/', Uri::base(true));
  80          array_pop($parts);
  81          Uri::root(null, implode('/', $parts));
  82      }
  83  
  84      /**
  85       * After the session has been started we need to populate it with some default values.
  86       *
  87       * @param   SessionEvent  $event  Session event being triggered
  88       *
  89       * @return  void
  90       *
  91       * @since   4.0.0
  92       */
  93      public function afterSessionStart(SessionEvent $event)
  94      {
  95          $session = $event->getSession();
  96  
  97          if ($session->isNew()) {
  98              $session->set('registry', new Registry('session'));
  99          }
 100      }
 101  
 102      /**
 103       * Method to display errors in language parsing.
 104       *
 105       * @return  string  Language debug output.
 106       *
 107       * @since   3.1
 108       */
 109      public function debugLanguage()
 110      {
 111          if ($this->getDocument()->getType() != 'html') {
 112              return '';
 113          }
 114  
 115          $lang   = Factory::getLanguage();
 116          $output = '<h4>' . Text::_('JDEBUG_LANGUAGE_FILES_IN_ERROR') . '</h4>';
 117  
 118          $errorfiles = $lang->getErrorFiles();
 119  
 120          if (count($errorfiles)) {
 121              $output .= '<ul>';
 122  
 123              foreach ($errorfiles as $error) {
 124                  $output .= "<li>$error</li>";
 125              }
 126  
 127              $output .= '</ul>';
 128          } else {
 129              $output .= '<pre>' . Text::_('JNONE') . '</pre>';
 130          }
 131  
 132          $output .= '<h4>' . Text::_('JDEBUG_LANGUAGE_UNTRANSLATED_STRING') . '</h4>';
 133          $output .= '<pre>';
 134          $orphans = $lang->getOrphans();
 135  
 136          if (count($orphans)) {
 137              ksort($orphans, SORT_STRING);
 138  
 139              $guesses = array();
 140  
 141              foreach ($orphans as $key => $occurrence) {
 142                  $guess = str_replace('_', ' ', $key);
 143  
 144                  $parts = explode(' ', $guess);
 145  
 146                  if (count($parts) > 1) {
 147                      array_shift($parts);
 148                      $guess = implode(' ', $parts);
 149                  }
 150  
 151                  $guess = trim($guess);
 152  
 153                  $key = strtoupper(trim($key));
 154                  $key = preg_replace('#\s+#', '_', $key);
 155                  $key = preg_replace('#\W#', '', $key);
 156  
 157                  // Prepare the text.
 158                  $guesses[] = $key . '="' . $guess . '"';
 159              }
 160  
 161              $output .= implode("\n", $guesses);
 162          } else {
 163              $output .= '<pre>' . Text::_('JNONE') . '</pre>';
 164          }
 165  
 166          $output .= '</pre>';
 167  
 168          return $output;
 169      }
 170  
 171      /**
 172       * Dispatch the application.
 173       *
 174       * @return  void
 175       *
 176       * @since   3.1
 177       */
 178      public function dispatch()
 179      {
 180          // Load the document to the API.
 181          $this->loadDocument();
 182  
 183          // Set up the params
 184          $document = $this->getDocument();
 185  
 186          // Register the document object with Factory.
 187          Factory::$document = $document;
 188  
 189          // Define component path.
 190          \define('JPATH_COMPONENT', JPATH_BASE);
 191          \define('JPATH_COMPONENT_SITE', JPATH_SITE);
 192          \define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR);
 193  
 194          // Execute the task.
 195          ob_start();
 196          $this->executeController();
 197          $contents = ob_get_clean();
 198  
 199          // If debug language is set, append its output to the contents.
 200          if ($this->config->get('debug_lang')) {
 201              $contents .= $this->debugLanguage();
 202          }
 203  
 204          // Set the content on the document
 205          $this->getDocument()->setBuffer($contents, 'component');
 206  
 207          // Set the document title
 208          $document->setTitle(Text::_('INSTL_PAGE_TITLE'));
 209      }
 210  
 211      /**
 212       * Method to run the Web application routines.
 213       *
 214       * @return  void
 215       *
 216       * @since   3.1
 217       */
 218      protected function doExecute()
 219      {
 220          // Ensure we load the namespace loader
 221          $this->createExtensionNamespaceMap();
 222  
 223          // Initialise the application.
 224          $this->initialiseApp();
 225  
 226          // Dispatch the application.
 227          $this->dispatch();
 228      }
 229  
 230      /**
 231       * Execute the application.
 232       *
 233       * @return  void
 234       *
 235       * @since   4.0.0
 236       */
 237      public function execute()
 238      {
 239          try {
 240              // Perform application routines.
 241              $this->doExecute();
 242  
 243              // If we have an application document object, render it.
 244              if ($this->document instanceof Document) {
 245                  // Render the application output.
 246                  $this->render();
 247              }
 248  
 249              // If gzip compression is enabled in configuration and the server is compliant, compress the output.
 250              if ($this->get('gzip') && !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler')) {
 251                  $this->compress();
 252              }
 253          } catch (\Throwable $throwable) {
 254              ExceptionHandler::render($throwable);
 255          }
 256  
 257          // Send the application response.
 258          $this->respond();
 259      }
 260  
 261      /**
 262       * Method to load a PHP configuration class file based on convention and return the instantiated data object.  You
 263       * will extend this method in child classes to provide configuration data from whatever data source is relevant
 264       * for your specific application.
 265       *
 266       * @param   string  $file   The path and filename of the configuration file. If not provided, configuration.php
 267       *                          in JPATH_BASE will be used.
 268       * @param   string  $class  The class name to instantiate.
 269       *
 270       * @return  mixed   Either an array or object to be loaded into the configuration object.
 271       *
 272       * @since   1.7.3
 273       * @throws  \RuntimeException
 274       */
 275      protected function fetchConfigurationData($file = '', $class = 'JConfig')
 276      {
 277          return array();
 278      }
 279  
 280      /**
 281       * Executed a controller from the input task.
 282       *
 283       * @return  void
 284       *
 285       * @since   4.0.0
 286       */
 287      private function executeController()
 288      {
 289          $task = $this->input->getCmd('task', '');
 290  
 291          // The name of the controller
 292          $controllerName = 'display';
 293  
 294          // Parse task in format controller.task
 295          if ($task !== '') {
 296              list($controllerName, $task) = explode('.', $task, 2);
 297          }
 298  
 299          $factory = new MVCFactory('Joomla\\CMS');
 300          $factory->setDatabase($this->getContainer()->get(DatabaseInterface::class));
 301  
 302          // Create the instance
 303          $controller = $factory->createController($controllerName, 'Installation', [], $this, $this->input);
 304  
 305          // Execute the task
 306          $controller->execute($task);
 307      }
 308  
 309      /**
 310       * Returns the language code and help URL set in the localise.xml file.
 311       * Used for forcing a particular language in localised releases.
 312       *
 313       * @return  mixed  False on failure, array on success.
 314       *
 315       * @since   3.1
 316       */
 317      public function getLocalise()
 318      {
 319          $xml = simplexml_load_file(JPATH_INSTALLATION . '/localise.xml');
 320  
 321          if (!$xml) {
 322              return false;
 323          }
 324  
 325          // Check that it's a localise file.
 326          if ($xml->getName() !== 'localise') {
 327              return false;
 328          }
 329  
 330          $ret = array();
 331  
 332          $ret['language']   = (string) $xml->forceLang;
 333          $ret['debug']      = (string) $xml->debug;
 334          $ret['sampledata'] = (string) $xml->sampledata;
 335  
 336          return $ret;
 337      }
 338  
 339      /**
 340       * Returns the installed language files in the administrative and frontend area.
 341       *
 342       * @param   DatabaseInterface|null  $db  Database driver.
 343       *
 344       * @return  array  Array with installed language packs in admin and site area.
 345       *
 346       * @since   3.1
 347       */
 348      public function getLocaliseAdmin(DatabaseInterface $db = null)
 349      {
 350          $langfiles = array();
 351  
 352          // If db connection, fetch them from the database.
 353          if ($db) {
 354              foreach (LanguageHelper::getInstalledLanguages() as $clientId => $language) {
 355                  $clientName = $clientId === 0 ? 'site' : 'admin';
 356  
 357                  foreach ($language as $languageCode => $lang) {
 358                      $langfiles[$clientName][] = $lang->element;
 359                  }
 360              }
 361          } else {
 362              // Read the folder names in the site and admin area.
 363              $langfiles['site']  = Folder::folders(LanguageHelper::getLanguagePath(JPATH_SITE));
 364              $langfiles['admin'] = Folder::folders(LanguageHelper::getLanguagePath(JPATH_ADMINISTRATOR));
 365          }
 366  
 367          return $langfiles;
 368      }
 369  
 370      /**
 371       * Gets the name of the current template.
 372       *
 373       * @param   boolean  $params  True to return the template parameters
 374       *
 375       * @return  string|\stdClass  The name of the template.
 376       *
 377       * @since   3.1
 378       */
 379      public function getTemplate($params = false)
 380      {
 381          if ($params) {
 382              $template = new \stdClass();
 383              $template->template = 'template';
 384              $template->params = new Registry();
 385              $template->inheritable = 0;
 386              $template->parent = '';
 387  
 388              return $template;
 389          }
 390  
 391          return 'template';
 392      }
 393  
 394      /**
 395       * Initialise the application.
 396       *
 397       * @param   array  $options  An optional associative array of configuration settings.
 398       *
 399       * @return  void
 400       *
 401       * @since   3.1
 402       */
 403      protected function initialiseApp($options = array())
 404      {
 405          // Get the localisation information provided in the localise.xml file.
 406          $forced = $this->getLocalise();
 407  
 408          // Check the request data for the language.
 409          if (empty($options['language'])) {
 410              $requestLang = $this->input->getCmd('lang', null);
 411  
 412              if ($requestLang !== null) {
 413                  $options['language'] = $requestLang;
 414              }
 415          }
 416  
 417          // Check the session for the language.
 418          if (empty($options['language'])) {
 419              $sessionOptions = $this->getSession()->get('setup.options');
 420  
 421              if (isset($sessionOptions['language'])) {
 422                  $options['language'] = $sessionOptions['language'];
 423              }
 424          }
 425  
 426          // This could be a first-time visit - try to determine what the client accepts.
 427          if (empty($options['language'])) {
 428              if (!empty($forced['language'])) {
 429                  $options['language'] = $forced['language'];
 430              } else {
 431                  $options['language'] = LanguageHelper::detectLanguage();
 432  
 433                  if (empty($options['language'])) {
 434                      $options['language'] = 'en-GB';
 435                  }
 436              }
 437          }
 438  
 439          // Give the user English.
 440          if (empty($options['language'])) {
 441              $options['language'] = 'en-GB';
 442          }
 443  
 444          // Set the official helpurl.
 445          $options['helpurl'] = 'https://help.joomla.org/proxy?keyref=Help{major}{minor}:{keyref}&lang={langcode}';
 446  
 447          // Store helpurl in the session.
 448          $this->getSession()->set('setup.helpurl', $options['helpurl']);
 449  
 450          // Set the language in the class.
 451          $this->config->set('language', $options['language']);
 452          $this->config->set('debug_lang', $forced['debug']);
 453          $this->config->set('sampledata', $forced['sampledata']);
 454          $this->config->set('helpurl', $options['helpurl']);
 455      }
 456  
 457      /**
 458       * Allows the application to load a custom or default document.
 459       *
 460       * The logic and options for creating this object are adequately generic for default cases
 461       * but for many applications it will make sense to override this method and create a document,
 462       * if required, based on more specific needs.
 463       *
 464       * @param   Document|null  $document  An optional document object. If omitted, the factory document is created.
 465       *
 466       * @return  InstallationApplication This method is chainable.
 467       *
 468       * @since   3.2
 469       */
 470      public function loadDocument(Document $document = null)
 471      {
 472          if ($document === null) {
 473              $lang = Factory::getLanguage();
 474              $type = $this->input->get('format', 'html', 'word');
 475              $date = new Date('now');
 476  
 477              $attributes = array(
 478                  'charset'      => 'utf-8',
 479                  'lineend'      => 'unix',
 480                  'tab'          => "\t",
 481                  'language'     => $lang->getTag(),
 482                  'direction'    => $lang->isRtl() ? 'rtl' : 'ltr',
 483                  'mediaversion' => md5($date->format('YmdHi')),
 484              );
 485  
 486              $document = $this->getContainer()->get(FactoryInterface::class)->createDocument($type, $attributes);
 487  
 488              // Register the instance to Factory.
 489              Factory::$document = $document;
 490          }
 491  
 492          $this->document = $document;
 493  
 494          return $this;
 495      }
 496  
 497      /**
 498       * Rendering is the process of pushing the document buffers into the template
 499       * placeholders, retrieving data from the document and pushing it into
 500       * the application response buffer.
 501       *
 502       * @return  void
 503       *
 504       * @since   3.1
 505       */
 506      public function render()
 507      {
 508          $options = [];
 509  
 510          if ($this->document instanceof HtmlDocument) {
 511              $file = $this->input->getCmd('tmpl', 'index');
 512  
 513              $options = [
 514                  'template'         => 'template',
 515                  'file'             => $file . '.php',
 516                  'directory'        => JPATH_THEMES,
 517                  'params'           => '{}',
 518                  "templateInherits" => ''
 519              ];
 520          }
 521  
 522          // Parse the document.
 523          $this->document->parse($options);
 524  
 525          // Render the document.
 526          $data = $this->document->render($this->get('cache_enabled'), $options);
 527  
 528          // Set the application output data.
 529          $this->setBody($data);
 530      }
 531  
 532      /**
 533       * Set configuration values.
 534       *
 535       * @param   array   $vars       Array of configuration values
 536       * @param   string  $namespace  The namespace
 537       *
 538       * @return  void
 539       *
 540       * @since   3.1
 541       */
 542      public function setCfg(array $vars = array(), $namespace = 'config')
 543      {
 544          $this->config->loadArray($vars, $namespace);
 545      }
 546  
 547      /**
 548       * Returns the application \JMenu object.
 549       *
 550       * @param   string|null  $name     The name of the application/client.
 551       * @param   array        $options  An optional associative array of configuration settings.
 552       *
 553       * @return  null
 554       *
 555       * @since   3.2
 556       */
 557      public function getMenu($name = null, $options = array())
 558      {
 559          return null;
 560      }
 561  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer