[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 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\Model;
  11  
  12  use Joomla\CMS\Extension\LegacyComponent;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Filesystem\Path;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Log\Log;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface;
  18  use Joomla\CMS\Table\Table;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Trait which contains the legacy getInstance functionality
  26   *
  27   * @since       4.0.0
  28   * @deprecated  5.0 Will be removed without replacement
  29   */
  30  trait LegacyModelLoaderTrait
  31  {
  32      /**
  33       * Create the filename for a resource
  34       *
  35       * @param   string  $type   The resource type to create the filename for.
  36       * @param   array   $parts  An associative array of filename information.
  37       *
  38       * @return  string  The filename
  39       *
  40       * @since       3.0
  41       * @deprecated  5.0 See getInstance
  42       */
  43      protected static function _createFileName($type, $parts = array())
  44      {
  45          return $type === 'model' ? strtolower($parts['name']) . '.php' : '';
  46      }
  47  
  48      /**
  49       * Returns a Model object, always creating it
  50       *
  51       * @param   string  $type    The model type to instantiate
  52       * @param   string  $prefix  Prefix for the model class name. Optional.
  53       * @param   array   $config  Configuration array for model. Optional.
  54       *
  55       * @return  self|boolean   A \JModelLegacy instance or false on failure
  56       *
  57       * @since       3.0
  58       * @deprecated  5.0 Get the model through the MVCFactory instead
  59       */
  60      public static function getInstance($type, $prefix = '', $config = array())
  61      {
  62          @trigger_error(
  63              sprintf(
  64                  '%1$s::getInstance() is deprecated. Load it through the MVC factory.',
  65                  self::class
  66              ),
  67              E_USER_DEPRECATED
  68          );
  69  
  70          $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
  71  
  72          if ($model = self::createModelFromComponent($type, $prefix, $config)) {
  73              return $model;
  74          }
  75  
  76          $modelClass = $prefix . ucfirst($type);
  77  
  78          if (!class_exists($modelClass)) {
  79              $path = Path::find(self::addIncludePath(null, $prefix), self::_createFileName('model', array('name' => $type)));
  80  
  81              if (!$path) {
  82                  $path = Path::find(self::addIncludePath(null, ''), self::_createFileName('model', array('name' => $type)));
  83              }
  84  
  85              if (!$path) {
  86                  return false;
  87              }
  88  
  89              require_once $path;
  90  
  91              if (!class_exists($modelClass)) {
  92                  Log::add(Text::sprintf('JLIB_APPLICATION_ERROR_MODELCLASS_NOT_FOUND', $modelClass), Log::WARNING, 'jerror');
  93  
  94                  return false;
  95              }
  96          }
  97  
  98          return new $modelClass($config);
  99      }
 100  
 101      /**
 102       * Adds to the stack of model table paths in LIFO order.
 103       *
 104       * @param   mixed  $path  The directory as a string or directories as an array to add.
 105       *
 106       * @return  void
 107       *
 108       * @since       3.0
 109       * @deprecated  5.0 See getInstance
 110       */
 111      public static function addTablePath($path)
 112      {
 113          Table::addIncludePath($path);
 114      }
 115  
 116      /**
 117       * Returns a Model object by loading the component from the prefix.
 118       *
 119       * @param   string  $type    The model type to instantiate
 120       * @param   string  $prefix  Prefix for the model class name. Optional.
 121       * @param   array   $config  Configuration array for model. Optional.
 122       *
 123       * @return  ModelInterface|null   A ModelInterface instance or null on failure
 124       *
 125       * @since       4.0.0
 126       * @deprecated  5.0 See getInstance
 127       */
 128      private static function createModelFromComponent($type, $prefix = '', $config = []): ?ModelInterface
 129      {
 130          // Do nothing when prefix is not given
 131          if (!$prefix) {
 132              return null;
 133          }
 134  
 135          // Boot the component
 136          $componentName = 'com_' . str_replace('model', '', strtolower($prefix));
 137          $component     = Factory::getApplication()->bootComponent($componentName);
 138  
 139          // When it is a legacy component or not a MVCFactoryService then ignore
 140          if ($component instanceof LegacyComponent || !$component instanceof MVCFactoryServiceInterface) {
 141              return null;
 142          }
 143  
 144          // Setup the client
 145          $client = Factory::getApplication()->getName();
 146  
 147          // Detect the client based on the include paths
 148          $adminPath = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $componentName);
 149          $sitePath  = Path::clean(JPATH_SITE . '/components/' . $componentName);
 150  
 151          foreach (self::addIncludePath() as $path) {
 152              if (strpos($path, $adminPath) !== false) {
 153                  $client = 'Administrator';
 154                  break;
 155              }
 156  
 157              if (strpos($path, $sitePath) !== false) {
 158                  $client = 'Site';
 159                  break;
 160              }
 161          }
 162  
 163          // Create the model
 164          $model = $component->getMVCFactory()->createModel($type, $client, $config);
 165  
 166          // When the model can't be loaded, then return null
 167          if (!$model) {
 168              return null;
 169          }
 170  
 171          // Return the model instance
 172          return $model;
 173      }
 174  }


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