- md5
- 6e2ef1ba4931e7cbc24b6744157663b1
- sha1
- f6b478540262898d3564a6bca31a30b4af23f090
- sha256
- b67916cf1c3e1da767893996a53fd3d294bad6bab4b1464c3e70b8d4973ed924
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\MVC\Model;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Object\LegacyErrorHandlingTrait;
use Joomla\CMS\Object\LegacyPropertyManagementTrait;
use Joomla\Filesystem\Path;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Base class for a Joomla Model
*
* @since 4.0.0
*/
#[\AllowDynamicProperties]
abstract class BaseModel implements ModelInterface, StatefulModelInterface
{
use StateBehaviorTrait;
use LegacyModelLoaderTrait;
use LegacyErrorHandlingTrait;
use LegacyPropertyManagementTrait;
/**
* The model (base) name
*
* @var string
* @since 4.0.0
*/
protected $name;
/**
* The include paths
*
* @var array
* @since 4.0.0
*/
protected static $paths;
/**
* Constructor
*
* @param array $config An array of configuration options (name, state, ignore_request).
*
* @since 4.0.0
* @throws \Exception
*/
public function __construct($config = [])
{
// Set the view name
if (empty($this->name)) {
if (\array_key_exists('name', $config)) {
$this->name = $config['name'];
} else {
$this->name = $this->getName();
}
}
// Set the model state
if (\array_key_exists('state', $config)) {
$this->state = $config['state'];
}
// Set the internal state marker - used to ignore setting state from the request
if (!empty($config['ignore_request'])) {
$this->__state_set = true;
}
}
/**
* Add a directory where \JModelLegacy should search for models. You may
* either pass a string or an array of directories.
*
* @param mixed $path A path or array[sting] of paths to search.
* @param string $prefix A prefix for models.
*
* @return array An array with directory elements. If prefix is equal to '', all directories are returned.
*
* @since 3.0
*
* @deprecated 4.3 will be removed in 7.0
* Will be removed without replacement. Get the model through the MVCFactory + namespace instead
*
* @see LegacyModelLoaderTrait::getInstance()
*/
public static function addIncludePath($path = '', $prefix = '')
{
if (!isset(self::$paths)) {
self::$paths = [];
}
if (!isset(self::$paths[$prefix])) {
self::$paths[$prefix] = [];
}
if (!isset(self::$paths[''])) {
self::$paths[''] = [];
}
if (!empty($path)) {
foreach ((array) $path as $includePath) {
if (!\in_array($includePath, self::$paths[$prefix])) {
array_unshift(self::$paths[$prefix], Path::clean($includePath));
}
if (!\in_array($includePath, self::$paths[''])) {
array_unshift(self::$paths[''], Path::clean($includePath));
}
}
}
return self::$paths[$prefix];
}
/**
* Method to get the model name
*
* The model name. By default parsed using the classname or it can be set
* by passing a $config['name'] in the class constructor
*
* @return string The name of the model
*
* @since 4.0.0
* @throws \Exception
*/
public function getName()
{
if (empty($this->name)) {
$r = null;
if (!preg_match('/Model(.*)/i', \get_class($this), $r)) {
throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500);
}
$this->name = str_replace(['\\', 'model'], '', strtolower($r[1]));
}
return $this->name;
}
}
The same content is in 38 releases, Joomla 6.0.1-rc1 to 6.2.0. See them all, or every version of this file.