[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
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\Cache\CacheControllerFactoryAwareInterface; 14 use Joomla\CMS\Cache\CacheControllerFactoryAwareTrait; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Form\FormFactoryAwareInterface; 17 use Joomla\CMS\Form\FormFactoryAwareTrait; 18 use Joomla\CMS\MVC\Model\ModelInterface; 19 use Joomla\CMS\Router\SiteRouterAwareInterface; 20 use Joomla\CMS\Router\SiteRouterAwareTrait; 21 use Joomla\Database\DatabaseAwareInterface; 22 use Joomla\Database\DatabaseAwareTrait; 23 use Joomla\Database\DatabaseInterface; 24 use Joomla\Database\Exception\DatabaseNotFoundException; 25 use Joomla\Event\DispatcherAwareInterface; 26 use Joomla\Event\DispatcherAwareTrait; 27 use Joomla\Input\Input; 28 29 // phpcs:disable PSR1.Files.SideEffects 30 \defined('JPATH_PLATFORM') or die; 31 // phpcs:enable PSR1.Files.SideEffects 32 33 /** 34 * Factory to create MVC objects based on a namespace. 35 * 36 * @since 3.10.0 37 */ 38 class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, SiteRouterAwareInterface 39 { 40 use FormFactoryAwareTrait; 41 use DispatcherAwareTrait; 42 use DatabaseAwareTrait; 43 use SiteRouterAwareTrait; 44 use CacheControllerFactoryAwareTrait; 45 46 /** 47 * The namespace to create the objects from. 48 * 49 * @var string 50 * @since 4.0.0 51 */ 52 private $namespace; 53 54 /** 55 * The namespace must be like: 56 * Joomla\Component\Content 57 * 58 * @param string $namespace The namespace 59 * 60 * @since 4.0.0 61 */ 62 public function __construct($namespace) 63 { 64 $this->namespace = $namespace; 65 } 66 67 /** 68 * Method to load and return a controller object. 69 * 70 * @param string $name The name of the controller 71 * @param string $prefix The controller prefix 72 * @param array $config The configuration array for the controller 73 * @param CMSApplicationInterface $app The app 74 * @param Input $input The input 75 * 76 * @return \Joomla\CMS\MVC\Controller\ControllerInterface 77 * 78 * @since 3.10.0 79 * @throws \Exception 80 */ 81 public function createController($name, $prefix, array $config, CMSApplicationInterface $app, Input $input) 82 { 83 // Clean the parameters 84 $name = preg_replace('/[^A-Z0-9_]/i', '', $name); 85 $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); 86 87 $className = $this->getClassName('Controller\\' . ucfirst($name) . 'Controller', $prefix); 88 89 if (!$className) { 90 return null; 91 } 92 93 $controller = new $className($config, $this, $app, $input); 94 $this->setFormFactoryOnObject($controller); 95 $this->setDispatcherOnObject($controller); 96 $this->setRouterOnObject($controller); 97 $this->setCacheControllerOnObject($controller); 98 99 return $controller; 100 } 101 102 /** 103 * Method to load and return a model object. 104 * 105 * @param string $name The name of the model. 106 * @param string $prefix Optional model prefix. 107 * @param array $config Optional configuration array for the model. 108 * 109 * @return ModelInterface The model object 110 * 111 * @since 3.10.0 112 * @throws \Exception 113 */ 114 public function createModel($name, $prefix = '', array $config = []) 115 { 116 // Clean the parameters 117 $name = preg_replace('/[^A-Z0-9_]/i', '', $name); 118 $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); 119 120 if (!$prefix) { 121 @trigger_error( 122 sprintf( 123 'Calling %s() without a prefix is deprecated.', 124 __METHOD__ 125 ), 126 E_USER_DEPRECATED 127 ); 128 129 $prefix = Factory::getApplication()->getName(); 130 } 131 132 $className = $this->getClassName('Model\\' . ucfirst($name) . 'Model', $prefix); 133 134 if (!$className) { 135 return null; 136 } 137 138 $model = new $className($config, $this); 139 $this->setFormFactoryOnObject($model); 140 $this->setDispatcherOnObject($model); 141 $this->setRouterOnObject($model); 142 $this->setCacheControllerOnObject($model); 143 144 if ($model instanceof DatabaseAwareInterface) { 145 try { 146 $model->setDatabase($this->getDatabase()); 147 } catch (DatabaseNotFoundException $e) { 148 @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); 149 $model->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); 150 } 151 } 152 153 return $model; 154 } 155 156 /** 157 * Method to load and return a view object. 158 * 159 * @param string $name The name of the view. 160 * @param string $prefix Optional view prefix. 161 * @param string $type Optional type of view. 162 * @param array $config Optional configuration array for the view. 163 * 164 * @return \Joomla\CMS\MVC\View\ViewInterface The view object 165 * 166 * @since 3.10.0 167 * @throws \Exception 168 */ 169 public function createView($name, $prefix = '', $type = '', array $config = []) 170 { 171 // Clean the parameters 172 $name = preg_replace('/[^A-Z0-9_]/i', '', $name); 173 $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); 174 $type = preg_replace('/[^A-Z0-9_]/i', '', $type); 175 176 if (!$prefix) { 177 @trigger_error( 178 sprintf( 179 'Calling %s() without a prefix is deprecated.', 180 __METHOD__ 181 ), 182 E_USER_DEPRECATED 183 ); 184 185 $prefix = Factory::getApplication()->getName(); 186 } 187 188 $className = $this->getClassName('View\\' . ucfirst($name) . '\\' . ucfirst($type) . 'View', $prefix); 189 190 if (!$className) { 191 return null; 192 } 193 194 $view = new $className($config); 195 $this->setFormFactoryOnObject($view); 196 $this->setDispatcherOnObject($view); 197 $this->setRouterOnObject($view); 198 $this->setCacheControllerOnObject($view); 199 200 return $view; 201 } 202 203 /** 204 * Method to load and return a table object. 205 * 206 * @param string $name The name of the table. 207 * @param string $prefix Optional table prefix. 208 * @param array $config Optional configuration array for the table. 209 * 210 * @return \Joomla\CMS\Table\Table The table object 211 * 212 * @since 3.10.0 213 * @throws \Exception 214 */ 215 public function createTable($name, $prefix = '', array $config = []) 216 { 217 // Clean the parameters 218 $name = preg_replace('/[^A-Z0-9_]/i', '', $name); 219 $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); 220 221 if (!$prefix) { 222 @trigger_error( 223 sprintf( 224 'Calling %s() without a prefix is deprecated.', 225 __METHOD__ 226 ), 227 E_USER_DEPRECATED 228 ); 229 230 $prefix = Factory::getApplication()->getName(); 231 } 232 233 $className = $this->getClassName('Table\\' . ucfirst($name) . 'Table', $prefix) 234 ?: $this->getClassName('Table\\' . ucfirst($name) . 'Table', 'Administrator'); 235 236 if (!$className) { 237 return null; 238 } 239 240 try { 241 $db = \array_key_exists('dbo', $config) ? $config['dbo'] : $this->getDatabase(); 242 } catch (DatabaseNotFoundException $e) { 243 @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); 244 $db = Factory::getContainer()->get(DatabaseInterface::class); 245 } 246 247 return new $className($db); 248 } 249 250 /** 251 * Returns a standard classname, if the class doesn't exist null is returned. 252 * 253 * @param string $suffix The suffix 254 * @param string $prefix The prefix 255 * 256 * @return string|null The class name 257 * 258 * @since 3.10.0 259 */ 260 protected function getClassName(string $suffix, string $prefix) 261 { 262 if (!$prefix) { 263 $prefix = Factory::getApplication(); 264 } 265 266 $className = trim($this->namespace, '\\') . '\\' . ucfirst($prefix) . '\\' . $suffix; 267 268 if (!class_exists($className)) { 269 return null; 270 } 271 272 return $className; 273 } 274 275 /** 276 * Sets the internal form factory on the given object. 277 * 278 * @param object $object The object 279 * 280 * @return void 281 * 282 * @since 4.0.0 283 */ 284 private function setFormFactoryOnObject($object) 285 { 286 if (!$object instanceof FormFactoryAwareInterface) { 287 return; 288 } 289 290 try { 291 $object->setFormFactory($this->getFormFactory()); 292 } catch (\UnexpectedValueException $e) { 293 // Ignore it 294 } 295 } 296 297 /** 298 * Sets the internal event dispatcher on the given object. 299 * 300 * @param object $object The object 301 * 302 * @return void 303 * 304 * @since 4.2.0 305 */ 306 private function setDispatcherOnObject($object) 307 { 308 if (!$object instanceof DispatcherAwareInterface) { 309 return; 310 } 311 312 try { 313 $object->setDispatcher($this->getDispatcher()); 314 } catch (\UnexpectedValueException $e) { 315 // Ignore it 316 } 317 } 318 319 /** 320 * Sets the internal router on the given object. 321 * 322 * @param object $object The object 323 * 324 * @return void 325 * 326 * @since 4.2.0 327 */ 328 private function setRouterOnObject($object): void 329 { 330 if (!$object instanceof SiteRouterAwareInterface) { 331 return; 332 } 333 334 try { 335 $object->setSiteRouter($this->getSiteRouter()); 336 } catch (\UnexpectedValueException $e) { 337 // Ignore it 338 } 339 } 340 341 /** 342 * Sets the internal cache controller on the given object. 343 * 344 * @param object $object The object 345 * 346 * @return void 347 * 348 * @since 4.2.0 349 */ 350 private function setCacheControllerOnObject($object): void 351 { 352 if (!$object instanceof CacheControllerFactoryAwareInterface) { 353 return; 354 } 355 356 try { 357 $object->setCacheControllerFactory($this->getCacheControllerFactory()); 358 } catch (\UnexpectedValueException $e) { 359 // Ignore it 360 } 361 } 362 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |