[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/Model/ -> BaseModel.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2006 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\MVC\Model;
  11  
  12  use Joomla\CMS\Filesystem\Path;
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\Object\CMSObject;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Base class for a Joomla Model
  22   *
  23   * @since  4.0.0
  24   */
  25  abstract class BaseModel extends CMSObject implements ModelInterface, StatefulModelInterface
  26  {
  27      use StateBehaviorTrait;
  28      use LegacyModelLoaderTrait;
  29  
  30      /**
  31       * The model (base) name
  32       *
  33       * @var    string
  34       * @since  4.0.0
  35       */
  36      protected $name;
  37  
  38      /**
  39       * The include paths
  40       *
  41       * @var   array
  42       * @since  4.0.0
  43       */
  44      protected static $paths;
  45  
  46      /**
  47       * Constructor
  48       *
  49       * @param   array  $config  An array of configuration options (name, state, ignore_request).
  50       *
  51       * @since   4.0.0
  52       * @throws  \Exception
  53       */
  54      public function __construct($config = array())
  55      {
  56          // Set the view name
  57          if (empty($this->name)) {
  58              if (\array_key_exists('name', $config)) {
  59                  $this->name = $config['name'];
  60              } else {
  61                  $this->name = $this->getName();
  62              }
  63          }
  64  
  65          // Set the model state
  66          if (\array_key_exists('state', $config)) {
  67              $this->state = $config['state'];
  68          }
  69  
  70          // Set the internal state marker - used to ignore setting state from the request
  71          if (!empty($config['ignore_request'])) {
  72              $this->__state_set = true;
  73          }
  74      }
  75  
  76      /**
  77       * Add a directory where \JModelLegacy should search for models. You may
  78       * either pass a string or an array of directories.
  79       *
  80       * @param   mixed   $path    A path or array[sting] of paths to search.
  81       * @param   string  $prefix  A prefix for models.
  82       *
  83       * @return  array  An array with directory elements. If prefix is equal to '', all directories are returned.
  84       *
  85       * @since       3.0
  86       * @deprecated  5.0 See LegacyModelLoaderTrait\getInstance
  87       */
  88      public static function addIncludePath($path = '', $prefix = '')
  89      {
  90          if (!isset(self::$paths)) {
  91              self::$paths = array();
  92          }
  93  
  94          if (!isset(self::$paths[$prefix])) {
  95              self::$paths[$prefix] = array();
  96          }
  97  
  98          if (!isset(self::$paths[''])) {
  99              self::$paths[''] = array();
 100          }
 101  
 102          if (!empty($path)) {
 103              foreach ((array) $path as $includePath) {
 104                  if (!\in_array($includePath, self::$paths[$prefix])) {
 105                      array_unshift(self::$paths[$prefix], Path::clean($includePath));
 106                  }
 107  
 108                  if (!\in_array($includePath, self::$paths[''])) {
 109                      array_unshift(self::$paths[''], Path::clean($includePath));
 110                  }
 111              }
 112          }
 113  
 114          return self::$paths[$prefix];
 115      }
 116  
 117      /**
 118       * Method to get the model name
 119       *
 120       * The model name. By default parsed using the classname or it can be set
 121       * by passing a $config['name'] in the class constructor
 122       *
 123       * @return  string  The name of the model
 124       *
 125       * @since   4.0.0
 126       * @throws  \Exception
 127       */
 128      public function getName()
 129      {
 130          if (empty($this->name)) {
 131              $r = null;
 132  
 133              if (!preg_match('/Model(.*)/i', \get_class($this), $r)) {
 134                  throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500);
 135              }
 136  
 137              $this->name = str_replace(['\\', 'model'], '', strtolower($r[1]));
 138          }
 139  
 140          return $this->name;
 141      }
 142  }


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