Skip to content

Commit

Permalink
Support overrides of namespaced classes
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Mar 18, 2017
1 parent c999b03 commit 2f966ca
Show file tree
Hide file tree
Showing 6 changed files with 806 additions and 3 deletions.
2 changes: 1 addition & 1 deletion administrator/components/com_content/models/articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @since 1.6
*/
class ContentModelArticles extends JModelList
class ContentModelArticles extends \Joomla\Cms\Model\ListModel
{
/**
* Constructor.
Expand Down
36 changes: 36 additions & 0 deletions libraries/cms/class/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,47 @@ public function __construct(ClassLoader $loader)
*/
public function loadClass($class)
{
// Namespaced class
if ($this->loadFromJloader($class))
{
return true;
}

// None namespaced class
if ($this->loadFromJloader('\\' . $class))
{
return true;
}

if ($result = $this->loader->loadClass($class))
{
JLoader::applyAliasFor($class);
}

return $result;
}

/**
* Loads the class from JLoader when possible.
*
* @param string $class The name of the class
*
* @return boolean True if loaded, false otherwise
*
* @since __DEPLOY_VERSION__
*/
private function loadFromJloader($class)
{
// Check if JLoader is able to load the class
if (!key_exists(strtolower($class), JLoader::getClassList()))
{
return false;
}

// Load the class
JLoader::load($class);

JLoader::applyAliasFor($class);
return true;
}
}
17 changes: 17 additions & 0 deletions libraries/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ public static function load($class)
*/
public static function register($class, $path, $force = true)
{
// When an alias exists, register it as well
if (key_exists($class, self::$classAliases))
{
self::register(self::$classAliases[$class], $path, $force);
}

// Sanitize class name.
$class = strtolower($class);

Expand Down Expand Up @@ -636,6 +642,17 @@ class_alias(self::$classAliases[$class], $class);
*/
public static function applyAliasFor($class)
{
// When the class doesn't exists but we have a class available with the key, create the alias
if (!class_exists($class) && $key = array_search($class, self::$classAliases))
{
if (class_exists($key))
{
class_alias($key, $class);
return;
}
}


// Remove the root backslash if present.
if ($class[0] == '\\')
{
Expand Down
2 changes: 1 addition & 1 deletion plugins/system/override/NoneNsListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function getActiveFilters()
*/
public function getItems()
{
\JFactory::getApplication()->enqueueMessage('Override called!!');
\JFactory::getApplication()->enqueueMessage('Override called in none namespaced class!!');

// Get a storage key.
$store = $this->getStoreId();
Expand Down

0 comments on commit 2f966ca

Please sign in to comment.