[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/View/ -> AbstractView.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 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\View;
  11  
  12  use Joomla\CMS\Document\Document;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  16  use Joomla\CMS\Object\CMSObject;
  17  use Joomla\Event\DispatcherAwareInterface;
  18  use Joomla\Event\DispatcherAwareTrait;
  19  use Joomla\Event\DispatcherInterface;
  20  use Joomla\Event\EventInterface;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('JPATH_PLATFORM') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Base class for a Joomla View
  28   *
  29   * Class holding methods for displaying presentation data.
  30   *
  31   * @since  2.5.5
  32   */
  33  abstract class AbstractView extends CMSObject implements ViewInterface, DispatcherAwareInterface
  34  {
  35      use DispatcherAwareTrait;
  36  
  37      /**
  38       * The active document object
  39       *
  40       * @var    Document
  41       * @since  3.0
  42       */
  43      public $document;
  44  
  45      /**
  46       * The URL option for the component. It is usually passed by controller while it creates the view
  47       *
  48       * @var    string
  49       * @since  3.0
  50       */
  51      protected $option = null;
  52  
  53      /**
  54       * The name of the view
  55       *
  56       * @var    array
  57       * @since  3.0
  58       */
  59      protected $_name = null;
  60  
  61      /**
  62       * Registered models
  63       *
  64       * @var    array
  65       * @since  3.0
  66       */
  67      protected $_models = array();
  68  
  69      /**
  70       * The default model
  71       *
  72       * @var    string
  73       * @since  3.0
  74       */
  75      protected $_defaultModel = null;
  76  
  77      /**
  78       * Constructor
  79       *
  80       * @param   array  $config  A named configuration array for object construction.
  81       *                          name: the name (optional) of the view (defaults to the view class name suffix).
  82       *                          charset: the character set to use for display
  83       *                          escape: the name (optional) of the function to use for escaping strings
  84       *                          base_path: the parent path (optional) of the views directory (defaults to the component folder)
  85       *                          template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name
  86       *                          helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/)
  87       *                          layout: the layout (optional) to use to display the view
  88       *
  89       * @since   3.0
  90       */
  91      public function __construct($config = array())
  92      {
  93          // Set the view name
  94          if (empty($this->_name)) {
  95              if (\array_key_exists('name', $config)) {
  96                  $this->_name = $config['name'];
  97              } else {
  98                  $this->_name = $this->getName();
  99              }
 100          }
 101  
 102          // Set the component name if passed
 103          if (!empty($config['option'])) {
 104              $this->option = $config['option'];
 105          }
 106      }
 107  
 108      /**
 109       * Execute and display a template script.
 110       *
 111       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 112       *
 113       * @return  void
 114       *
 115       * @since   3.0
 116       */
 117      abstract public function display($tpl = null);
 118  
 119      /**
 120       * Method to get data from a registered model or a property of the view
 121       *
 122       * @param   string  $property  The name of the method to call on the model or the property to get
 123       * @param   string  $default   The name of the model to reference or the default value [optional]
 124       *
 125       * @return  mixed  The return value of the method
 126       *
 127       * @since   3.0
 128       */
 129      public function get($property, $default = null)
 130      {
 131          // If $model is null we use the default model
 132          if ($default === null) {
 133              $model = $this->_defaultModel;
 134          } else {
 135              $model = strtolower($default);
 136          }
 137  
 138          // First check to make sure the model requested exists
 139          if (isset($this->_models[$model])) {
 140              // Model exists, let's build the method name
 141              $method = 'get' . ucfirst($property);
 142  
 143              // Does the method exist?
 144              if (method_exists($this->_models[$model], $method)) {
 145                  // The method exists, let's call it and return what we get
 146                  return $this->_models[$model]->$method();
 147              }
 148          }
 149  
 150          // Degrade to CMSObject::get
 151          return parent::get($property, $default);
 152      }
 153  
 154      /**
 155       * Method to get the model object
 156       *
 157       * @param   string  $name  The name of the model (optional)
 158       *
 159       * @return  BaseDatabaseModel  The model object
 160       *
 161       * @since   3.0
 162       */
 163      public function getModel($name = null)
 164      {
 165          if ($name === null) {
 166              $name = $this->_defaultModel;
 167          }
 168  
 169          return $this->_models[strtolower($name)];
 170      }
 171  
 172      /**
 173       * Method to add a model to the view.  We support a multiple model single
 174       * view system by which models are referenced by classname.  A caveat to the
 175       * classname referencing is that any classname prepended by \JModel will be
 176       * referenced by the name without \JModel, eg. \JModelCategory is just
 177       * Category.
 178       *
 179       * @param   BaseDatabaseModel  $model    The model to add to the view.
 180       * @param   boolean            $default  Is this the default model?
 181       *
 182       * @return  BaseDatabaseModel  The added model.
 183       *
 184       * @since   3.0
 185       */
 186      public function setModel($model, $default = false)
 187      {
 188          $name = strtolower($model->getName());
 189          $this->_models[$name] = $model;
 190  
 191          if ($default) {
 192              $this->_defaultModel = $name;
 193          }
 194  
 195          return $model;
 196      }
 197  
 198      /**
 199       * Method to get the view name
 200       *
 201       * The model name by default parsed using the classname, or it can be set
 202       * by passing a $config['name'] in the class constructor
 203       *
 204       * @return  string  The name of the model
 205       *
 206       * @since   3.0
 207       * @throws  \Exception
 208       */
 209      public function getName()
 210      {
 211          if (empty($this->_name)) {
 212              $reflection = new \ReflectionClass($this);
 213  
 214              if ($viewNamespace = $reflection->getNamespaceName()) {
 215                  $pos = strrpos($viewNamespace, '\\');
 216  
 217                  if ($pos !== false) {
 218                      $this->_name = strtolower(substr($viewNamespace, $pos + 1));
 219                  }
 220              } else {
 221                  $className = \get_class($this);
 222                  $viewPos   = strpos($className, 'View');
 223  
 224                  if ($viewPos != false) {
 225                      $this->_name = strtolower(substr($className, $viewPos + 4));
 226                  }
 227              }
 228  
 229              if (empty($this->_name)) {
 230                  throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500);
 231              }
 232          }
 233  
 234          return $this->_name;
 235      }
 236  
 237      /**
 238       * Dispatches the given event on the internal dispatcher, does a fallback to the global one.
 239       *
 240       * @param   EventInterface  $event  The event
 241       *
 242       * @return  void
 243       *
 244       * @since   4.1.0
 245       */
 246      protected function dispatchEvent(EventInterface $event)
 247      {
 248          try {
 249              $this->getDispatcher()->dispatch($event->getName(), $event);
 250          } catch (\UnexpectedValueException $e) {
 251              Factory::getContainer()->get(DispatcherInterface::class)->dispatch($event->getName(), $event);
 252          }
 253      }
 254  }


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