Skip to content

Commit

Permalink
Port back the MVCFactory from 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Aug 23, 2017
1 parent 80a405b commit 1ad8cdf
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions libraries/src/MVC/Factory/MVCFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;

/**
* Factory to create MVC objects based on a namespace.
Expand Down Expand Up @@ -62,7 +63,18 @@ public function __construct($namespace, CMSApplication $application)
*/
public function createModel($name, $prefix = '', array $config = array())
{
return $this->createInstance('Model\\' . ucfirst($name) .'Model', $prefix, $config);
// Clean the parameters
$name = preg_replace('/[^A-Z0-9_]/i', '', $name);
$prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);

$className = $this->getClassName('Model\\' . ucfirst($name) .'Model', $prefix);

if (!$className)
{
return null;
}

return new $className($config, $this);
}

/**
Expand All @@ -80,7 +92,19 @@ public function createModel($name, $prefix = '', array $config = array())
*/
public function createView($name, $prefix = '', $type = '', array $config = array())
{
return $this->createInstance('View\\' . ucfirst($name) . '\\' . ucfirst($type) . 'View', $prefix, $config);
// Clean the parameters
$name = preg_replace('/[^A-Z0-9_]/i', '', $name);
$prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
$type = preg_replace('/[^A-Z0-9_]/i', '', $type);

$className = $this->getClassName('View\\' . ucfirst($name) . '\\' . ucfirst($type) . 'View', $prefix);

if (!$className)
{
return null;
}

return new $className($config);
}

/**
Expand All @@ -97,32 +121,54 @@ public function createView($name, $prefix = '', $type = '', array $config = arra
*/
public function createTable($name, $prefix = '', array $config = array())
{
return $this->createInstance('Table\\' . ucfirst($name) . 'Table', $prefix, $config);
// Clean the parameters
$name = preg_replace('/[^A-Z0-9_]/i', '', $name);
$prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);

$className = $this->getClassName('Table\\' . ucfirst($name) . 'Table', $prefix)
?: $this->getClassName('Table\\' . ucfirst($name) . 'Table', 'Administrator');

if (!$className)
{
return null;
}

if (array_key_exists('dbo', $config))
{
$db = $config['dbo'];
}
else
{
$db = Factory::getDbo();
}

return new $className($db);
}

/**
* Creates a standard classname and returns an instance of this class.
* Returns a standard classname, if the class doesn't exist null is returned.
*
* @param string $suffix The suffix
* @param string $prefix The prefix
* @param array $config The config
*
* @return object The instance
* @return string|null The class name
*
* @since __DEPLOY_VERSION__
*/
private function createInstance($suffix, $prefix, array $config)
private function getClassName($suffix, $prefix)
{
if (!$prefix)
{
$prefix = $this->application->getName();
}

$className = $this->namespace . '\\' . ucfirst($prefix) . '\\' . $suffix;
$className = trim($this->namespace, '\\') . '\\' . ucfirst($prefix) . '\\' . $suffix;

if (!class_exists($className))
{
return null;
}
return new $className($config);

return $className;
}
}

0 comments on commit 1ad8cdf

Please sign in to comment.