[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/Factory/ -> LegacyFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\MVC\Factory;
  11  
  12  use Joomla\CMS\Application\CMSApplicationInterface;
  13  use Joomla\CMS\Filesystem\Path;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\BaseController;
  16  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  17  use Joomla\CMS\MVC\Model\ModelInterface;
  18  use Joomla\CMS\Table\Table;
  19  use Joomla\Input\Input;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('JPATH_PLATFORM') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Factory to create MVC objects in legacy mode.
  27   * Uses the static getInstance function on the classes itself. Behavior of the old none
  28   * namespaced extension set up.
  29   *
  30   * @since  3.10.0
  31   */
  32  class LegacyFactory implements MVCFactoryInterface
  33  {
  34      /**
  35       * Method to load and return a controller object.
  36       *
  37       * @param   string                   $name    The name of the controller
  38       * @param   string                   $prefix  The controller prefix
  39       * @param   array                    $config  The configuration array for the controller
  40       * @param   CMSApplicationInterface  $app     The app
  41       * @param   Input                    $input   The input
  42       *
  43       * @return  \Joomla\CMS\MVC\Controller\ControllerInterface
  44       *
  45       * @since   4.0.0
  46       * @throws  \Exception
  47       */
  48      public function createController($name, $prefix, array $config, CMSApplicationInterface $app, Input $input)
  49      {
  50          throw new \BadFunctionCallException('Legacy controller creation not supported.');
  51      }
  52  
  53      /**
  54       * Method to load and return a model object.
  55       *
  56       * @param   string  $name    The name of the model.
  57       * @param   string  $prefix  Optional model prefix.
  58       * @param   array   $config  Optional configuration array for the model.
  59       *
  60       * @return  ModelInterface  The model object
  61       *
  62       * @since   3.10.0
  63       * @throws  \Exception
  64       */
  65      public function createModel($name, $prefix = '', array $config = [])
  66      {
  67          // Clean the model name
  68          $modelName   = preg_replace('/[^A-Z0-9_]/i', '', $name);
  69          $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
  70  
  71          return BaseDatabaseModel::getInstance($modelName, $classPrefix, $config);
  72      }
  73  
  74      /**
  75       * Method to load and return a view object.
  76       *
  77       * @param   string  $name    The name of the view.
  78       * @param   string  $prefix  Optional view prefix.
  79       * @param   string  $type    Optional type of view.
  80       * @param   array   $config  Optional configuration array for the view.
  81       *
  82       * @return  \Joomla\CMS\MVC\View\ViewInterface  The view object
  83       *
  84       * @since   3.10.0
  85       * @throws  \Exception
  86       */
  87      public function createView($name, $prefix = '', $type = '', array $config = [])
  88      {
  89          // Clean the view name
  90          $viewName    = preg_replace('/[^A-Z0-9_]/i', '', $name);
  91          $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
  92          $viewType    = preg_replace('/[^A-Z0-9_]/i', '', $type);
  93  
  94          // Build the view class name
  95          $viewClass = $classPrefix . $viewName;
  96  
  97          if (!class_exists($viewClass)) {
  98              $path = Path::find($config['paths'], BaseController::createFileName('view', array('name' => $viewName, 'type' => $viewType)));
  99  
 100              if (!$path) {
 101                  return null;
 102              }
 103  
 104              \JLoader::register($viewClass, $path);
 105  
 106              if (!class_exists($viewClass)) {
 107                  throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $viewClass, $path), 500);
 108              }
 109          }
 110  
 111          return new $viewClass($config);
 112      }
 113  
 114      /**
 115       * Method to load and return a table object.
 116       *
 117       * @param   string  $name    The name of the table.
 118       * @param   string  $prefix  Optional table prefix.
 119       * @param   array   $config  Optional configuration array for the table.
 120       *
 121       * @return  \Joomla\CMS\Table\Table  The table object
 122       *
 123       * @since   3.10.0
 124       * @throws  \Exception
 125       */
 126      public function createTable($name, $prefix = 'Table', array $config = [])
 127      {
 128          // Clean the model name
 129          $name   = preg_replace('/[^A-Z0-9_]/i', '', $name);
 130          $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
 131  
 132          return Table::getInstance($name, $prefix, $config);
 133      }
 134  }


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