Skip to content

Commit

Permalink
Backport the MVC classes
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Feb 8, 2018
1 parent 328643c commit 9d42e72
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
4 changes: 2 additions & 2 deletions libraries/src/MVC/Controller/BaseController.php
Expand Up @@ -720,7 +720,7 @@ public function getModel($name = '', $prefix = '', $config = array())
$name = $this->getName();
}

if (empty($prefix))
if (empty($prefix) && $this->factory instanceof LegacyFactory)
{
$prefix = $this->model_prefix;
}
Expand Down Expand Up @@ -823,7 +823,7 @@ public function getView($name = '', $type = '', $prefix = '', $config = array())
$name = $this->getName();
}

if (empty($prefix))
if (empty($prefix) && $this->factory instanceof LegacyFactory)
{
$prefix = $this->getName() . 'View';
}
Expand Down
16 changes: 13 additions & 3 deletions libraries/src/MVC/Controller/FormController.php
Expand Up @@ -10,6 +10,7 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
// Guess the option as com_NameOfController
if (empty($this->option))
{
$this->option = 'com_' . strtolower($this->getName());
$this->option = ComponentHelper::getComponentName($this, $this->getName());
}

// Guess the \JText message prefix. Defaults to the option.
Expand All @@ -91,12 +92,21 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
{
$r = null;

if (!preg_match('/(.*)Controller(.*)/i', get_class($this), $r))
$match = 'Controller';

// If there is a namespace append a backslash
if (strpos(get_class($this), '\\'))
{
$match .= '\\\\';
}

if (!preg_match('/(.*)' . $match . '(.*)/i', get_class($this), $r))
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
}

$this->context = strtolower($r[2]);
// Remove the backslashes and the suffix controller
$this->context = str_replace(array('\\', 'controller'), '', strtolower($r[2]));
}

// Guess the item view as the context.
Expand Down
6 changes: 4 additions & 2 deletions libraries/src/MVC/Factory/MVCFactory.php
Expand Up @@ -23,14 +23,16 @@ class MVCFactory implements MVCFactoryInterface
/**
* The namespace to create the objects from.
*
* @var string
* @var string
* @since __DEPLOY_VERSION__
*/
private $namespace = null;

/**
* The application.
*
* @var CMSApplication
* @var CMSApplication
* @since __DEPLOY_VERSION__
*/
private $application = null;

Expand Down
25 changes: 23 additions & 2 deletions libraries/src/MVC/Model/BaseDatabaseModel.php
Expand Up @@ -10,7 +10,9 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Factory\LegacyFactory;
use Joomla\CMS\MVC\Factory\MVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\Utilities\ArrayHelper;

Expand Down Expand Up @@ -233,7 +235,7 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
}

$this->option = 'com_' . strtolower($r[1]);
$this->option = ComponentHelper::getComponentName($this, $r[1]);
}

// Set the view name
Expand Down Expand Up @@ -299,6 +301,19 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
$this->event_clean_cache = 'onContentCleanCache';
}

if (!$factory)
{
$reflect = new \ReflectionClass($this);
if ($reflect->getNamespaceName())
{
// Guess the root namespace
$ns = explode('\\', $reflect->getNamespaceName());
$ns = implode('\\', array_slice($ns, 0, 3));

$factory = new MVCFactory($ns, \JFactory::getApplication());
}
}

$this->factory = $factory ? : new LegacyFactory;
}

Expand Down Expand Up @@ -427,7 +442,7 @@ public function getName()
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
}

$this->name = strtolower($r[1]);
$this->name = str_replace(array('\\', 'model'), '', strtolower($r[1]));
}

return $this->name;
Expand Down Expand Up @@ -476,6 +491,12 @@ public function getTable($name = '', $prefix = 'Table', $options = array())
$name = $this->getName();
}

// We need this ugly code to deal with non-namespaced MVC code
if (empty($prefix) && $this->factory instanceof LegacyFactory)
{
$prefix = 'Table';
}

if ($table = $this->_createTable($name, $prefix, $options))
{
return $table;
Expand Down
1 change: 1 addition & 0 deletions libraries/src/MVC/Model/FormModel.php
Expand Up @@ -220,6 +220,7 @@ protected function loadForm($name, $source = null, $options = array(), $clear =
}

// Get the form.
\JForm::addFormPath(JPATH_COMPONENT . '/forms');
\JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
\JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
\JForm::addFormPath(JPATH_COMPONENT . '/model/form');
Expand Down
5 changes: 3 additions & 2 deletions libraries/src/MVC/Model/ListModel.php
Expand Up @@ -348,9 +348,9 @@ public function getFilterForm($data = array(), $loadData = true)
{
$classNameParts = explode('Model', get_called_class());

if (count($classNameParts) == 2)
if (count($classNameParts) >= 2)
{
$this->filterFormName = 'filter_' . strtolower($classNameParts[1]);
$this->filterFormName = 'filter_' . str_replace('\\', '', strtolower($classNameParts[1]));
}
}

Expand Down Expand Up @@ -392,6 +392,7 @@ protected function loadForm($name, $source = null, $options = array(), $clear =
}

// Get the form.
\JForm::addFormPath(JPATH_COMPONENT . '/forms');
\JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
\JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');

Expand Down
42 changes: 35 additions & 7 deletions libraries/src/MVC/View/HtmlView.php
Expand Up @@ -182,14 +182,26 @@ public function __construct($config = array())
// User-defined dirs
$this->_setPath('template', $config['template_path']);
}
elseif (is_dir($this->_basePath . '/view'))
elseif (is_dir($this->_basePath . '/View/' . $this->getName() . '/tmpl'))
{
$this->_setPath('template', $this->_basePath . '/View/' . $this->getName() . '/tmpl');
}
elseif (is_dir($this->_basePath . '/view/' . $this->getName() . '/tmpl'))
{
$this->_setPath('template', $this->_basePath . '/view/' . $this->getName() . '/tmpl');
}
else
elseif (is_dir($this->_basePath . '/tmpl/' . $this->getName()))
{
$this->_setPath('template', $this->_basePath . '/tmpl/' . $this->getName());
}
elseif (is_dir($this->_basePath . '/views/' . $this->getName() . '/tmpl'))
{
$this->_setPath('template', $this->_basePath . '/views/' . $this->getName() . '/tmpl');
}
else
{
$this->_setPath('template', $this->_basePath . '/views/' . $this->getName());
}

// Set the default helper search path
if (array_key_exists('helper_path', $config))
Expand Down Expand Up @@ -490,15 +502,31 @@ public function getName()
{
if (empty($this->_name))
{
$classname = get_class($this);
$viewpos = strpos($classname, 'View');
$reflection = new \ReflectionClass($this);
if ($viewNamespace = $reflection->getNamespaceName())
{
$pos = strrpos($viewNamespace, '\\');

if ($pos !== false)
{
$this->_name = strtolower(substr($viewNamespace, $pos + 1));
}
}
else
{
$className = get_class($this);
$viewPos = strpos($className, 'View');

if ($viewpos === false)
if ($viewPos != false)
{
$this->_name = strtolower(substr($className, $viewPos + 4));
}
}

if (empty($this->_name))
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_VIEW_GET_NAME'), 500);
}

$this->_name = strtolower(substr($classname, $viewpos + 4));
}

return $this->_name;
Expand Down

0 comments on commit 9d42e72

Please sign in to comment.