[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Helper/ -> LibraryHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 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\Helper;
  11  
  12  use Joomla\CMS\Cache\CacheControllerFactoryInterface;
  13  use Joomla\CMS\Cache\Controller\CallbackController;
  14  use Joomla\CMS\Cache\Exception\CacheExceptionInterface;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Log\Log;
  18  use Joomla\Registry\Registry;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Library helper class
  26   *
  27   * @since  3.2
  28   */
  29  class LibraryHelper
  30  {
  31      /**
  32       * The component list cache
  33       *
  34       * @var    array
  35       * @since  3.2
  36       */
  37      protected static $libraries = array();
  38  
  39      /**
  40       * Get the library information.
  41       *
  42       * @param   string   $element  Element of the library in the extensions table.
  43       * @param   boolean  $strict   If set and the library does not exist, the enabled attribute will be set to false.
  44       *
  45       * @return  \stdClass   An object with the library's information.
  46       *
  47       * @since   3.2
  48       */
  49      public static function getLibrary($element, $strict = false)
  50      {
  51          // Is already cached?
  52          if (isset(static::$libraries[$element]) || static::loadLibrary($element)) {
  53              $result = static::$libraries[$element];
  54  
  55              // Convert the params to an object.
  56              if (\is_string($result->params)) {
  57                  $result->params = new Registry($result->params);
  58              }
  59          } else {
  60              $result = new \stdClass();
  61              $result->enabled = $strict ? false : true;
  62              $result->params = new Registry();
  63          }
  64  
  65          return $result;
  66      }
  67  
  68      /**
  69       * Checks if a library is enabled
  70       *
  71       * @param   string  $element  Element of the library in the extensions table.
  72       *
  73       * @return  boolean
  74       *
  75       * @since   3.2
  76       */
  77      public static function isEnabled($element)
  78      {
  79          return static::getLibrary($element, true)->enabled;
  80      }
  81  
  82      /**
  83       * Gets the parameter object for the library
  84       *
  85       * @param   string   $element  Element of the library in the extensions table.
  86       * @param   boolean  $strict   If set and the library does not exist, false will be returned
  87       *
  88       * @return  Registry  A Registry object.
  89       *
  90       * @see     Registry
  91       * @since   3.2
  92       */
  93      public static function getParams($element, $strict = false)
  94      {
  95          return static::getLibrary($element, $strict)->params;
  96      }
  97  
  98      /**
  99       * Save the parameters object for the library
 100       *
 101       * @param   string    $element  Element of the library in the extensions table.
 102       * @param   Registry  $params   Params to save
 103       *
 104       * @return  Registry|boolean  A Registry object.
 105       *
 106       * @see     Registry
 107       * @since   3.2
 108       */
 109      public static function saveParams($element, $params)
 110      {
 111          if (static::isEnabled($element)) {
 112              // Save params in DB
 113              $db           = Factory::getDbo();
 114              $paramsString = $params->toString();
 115              $query        = $db->getQuery(true)
 116                  ->update($db->quoteName('#__extensions'))
 117                  ->set($db->quoteName('params') . ' = :params')
 118                  ->where($db->quoteName('type') . ' = ' . $db->quote('library'))
 119                  ->where($db->quoteName('element') . ' = :element')
 120                  ->bind(':params', $paramsString)
 121                  ->bind(':element', $element);
 122              $db->setQuery($query);
 123  
 124              $result = $db->execute();
 125  
 126              // Update params in libraries cache
 127              if ($result && isset(static::$libraries[$element])) {
 128                  static::$libraries[$element]->params = $params;
 129              }
 130  
 131              return $result;
 132          }
 133  
 134          return false;
 135      }
 136  
 137      /**
 138       * Load the installed library into the libraries property.
 139       *
 140       * @param   string  $element  The element value for the extension
 141       *
 142       * @return  boolean  True on success
 143       *
 144       * @since   3.7.0
 145       */
 146      protected static function loadLibrary($element)
 147      {
 148          $loader = function ($element) {
 149              $db = Factory::getDbo();
 150              $query = $db->getQuery(true)
 151                  ->select($db->quoteName(['extension_id', 'element', 'params', 'enabled'], ['id', 'option', null, null]))
 152                  ->from($db->quoteName('#__extensions'))
 153                  ->where($db->quoteName('type') . ' = ' . $db->quote('library'))
 154                  ->where($db->quoteName('element') . ' = :element')
 155                  ->bind(':element', $element);
 156              $db->setQuery($query);
 157  
 158              return $db->loadObject();
 159          };
 160  
 161          /** @var CallbackController $cache */
 162          $cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController('callback', ['defaultgroup' => '_system']);
 163  
 164          try {
 165              static::$libraries[$element] = $cache->get($loader, array($element), __METHOD__ . $element);
 166          } catch (CacheExceptionInterface $e) {
 167              static::$libraries[$element] = $loader($element);
 168          }
 169  
 170          if (empty(static::$libraries[$element])) {
 171              // Fatal error.
 172              $error = Text::_('JLIB_APPLICATION_ERROR_LIBRARY_NOT_FOUND');
 173              Log::add(Text::sprintf('JLIB_APPLICATION_ERROR_LIBRARY_NOT_LOADING', $element, $error), Log::WARNING, 'jerror');
 174  
 175              return false;
 176          }
 177  
 178          return true;
 179      }
 180  }


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