Skip to content

Commit

Permalink
[4.0] Legacy dispatcher (#19887)
Browse files Browse the repository at this point in the history
* Legacy dispatcher

* no null

* Run isolated
  • Loading branch information
laoneo authored and wilsonge committed Mar 22, 2018
1 parent eefde8c commit 387c251
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 86 deletions.
79 changes: 3 additions & 76 deletions libraries/src/Component/ComponentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,47 +346,9 @@ public static function renderComponent($option, $params = array())
throw new MissingComponentException(\JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
}

// Handle template preview outlining.
$contents = null;

$dispatcher = $app->bootComponent($option)->getDispatcher($app);

// Check if we have a dispatcher
if ($dispatcher)
{
$contents = static::dispatchComponent($dispatcher);
}
// Will be removed once transition of all components is done
elseif (file_exists(JPATH_COMPONENT . '/dispatcher.php'))
{
require_once JPATH_COMPONENT . '/dispatcher.php';
$class = ucwords($file) . 'Dispatcher';

// Check the class exists and implements the dispatcher interface
if (!class_exists($class) || !in_array(DispatcherInterface::class, class_implements($class)))
{
throw new \LogicException(\JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD', $option), 500);
}

// Dispatch the component.
$contents = static::dispatchComponent(new $class($app, $app->input, new MVCFactoryFactory('Joomla\\Component\\' . ucwords($file))));
}
else
{
$path = JPATH_COMPONENT . '/' . $file . '.php';

// If component file doesn't exist throw error
if (!file_exists($path))
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
}

// Load common and local language files.
$lang->load($option, JPATH_BASE, null, false, true) || $lang->load($option, JPATH_COMPONENT, null, false, true);

// Execute the component.
$contents = static::executeComponent($path);
}
ob_start();
$app->bootComponent($option)->getDispatcher($app)->dispatch();
$contents = ob_get_clean();

// Revert the scope
$app->scope = $scope;
Expand All @@ -399,41 +361,6 @@ public static function renderComponent($option, $params = array())
return $contents;
}

/**
* Execute the component.
*
* @param string $path The component path.
*
* @return string The component output
*
* @since 1.7
*/
protected static function executeComponent($path)
{
ob_start();
require_once $path;

return ob_get_clean();
}

/**
* Dispatch the component.
*
* @param DispatcherInterface $dispatcher The dispatcher class.
*
* @return string The component output
*
* @since 4.0.0
*/
protected static function dispatchComponent(DispatcherInterface $dispatcher): string
{
ob_start();
$dispatcher->dispatch();
$contents = ob_get_clean();

return $contents;
}

/**
* Load the installed components into the components property.
*
Expand Down
96 changes: 96 additions & 0 deletions libraries/src/Dispatcher/LegacyDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\CMS\Dispatcher;

defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\MVC\Factory\MVCFactoryFactory;
use Joomla\Input\Input;

/**
* Base class for a legacy Joomla Dispatcher
*
* Executes the single entry file of a legacy component.
*
* @since __DEPLOY_VERSION__
*/
class LegacyDispatcher implements DispatcherInterface
{
/**
* The application instance
*
* @var CMSApplication
* @since __DEPLOY_VERSION__
*/
private $app;

/**
* Constructor for Dispatcher
*
* @param CMSApplication $app The application instance
*
* @since __DEPLOY_VERSION__
*/
public function __construct(CMSApplication $app)
{
$this->app = $app;
}

/**
* Dispatch a controller task. Redirecting the user if appropriate.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function dispatch()
{
$name = substr($this->app->scope, 4);

// Will be removed once transition of all components is done
if (file_exists(JPATH_COMPONENT . '/dispatcher.php'))
{
require_once JPATH_COMPONENT . '/dispatcher.php';

$class = ucwords($name) . 'Dispatcher';

// Check the class exists and implements the dispatcher interface
if (!class_exists($class) || !in_array(DispatcherInterface::class, class_implements($class)))
{
throw new \LogicException(\JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD', $this->app->scope), 500);
}

// Dispatch the component.
$dispatcher = new $class($this->app, $this->app->input, new MVCFactoryFactory('Joomla\\Component\\' . ucwords($name)));
$dispatcher->dispatch();

return;
}

$path = JPATH_COMPONENT . '/' . $name . '.php';

// If component file doesn't exist throw error
if (!file_exists($path))
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
}

$lang = $this->app->getLanguage();

// Load common and local language files.
$lang->load($this->app->scope, JPATH_BASE, null, false, true) || $lang->load($this->app->scope, JPATH_COMPONENT, null, false, true);

// Execute the component
$loader = static function($path){
require_once $path;
};
$loader($path);
}
}
6 changes: 3 additions & 3 deletions libraries/src/Extension/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ class Component implements ComponentInterface
private $associationExtension;

/**
* Returns the dispatcher for the given application, null if none exists.
* Returns the dispatcher for the given application.
*
* @param CMSApplicationInterface $application The application
*
* @return DispatcherInterface|null
* @return DispatcherInterface
*
* @since __DEPLOY_VERSION__
*/
public function getDispatcher(CMSApplicationInterface $application)
public function getDispatcher(CMSApplicationInterface $application): DispatcherInterface
{
if ($this->dispatcherFactory === null)
{
Expand Down
6 changes: 3 additions & 3 deletions libraries/src/Extension/ComponentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
interface ComponentInterface
{
/**
* Returns the dispatcher for the given application, null if none exists.
* Returns the dispatcher for the given application.
*
* @param CMSApplicationInterface $application The application
*
* @return DispatcherInterface|null
* @return DispatcherInterface
*
* @since __DEPLOY_VERSION__
*/
public function getDispatcher(CMSApplicationInterface $application);
public function getDispatcher(CMSApplicationInterface $application): DispatcherInterface;

/**
* Returns an MVCFactory.
Expand Down
9 changes: 5 additions & 4 deletions libraries/src/Extension/LegacyComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\Association\AssociationExtensionInterface;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Dispatcher\DispatcherInterface;
use Joomla\CMS\Dispatcher\LegacyDispatcher;
use Joomla\CMS\MVC\Factory\LegacyFactory;
use Joomla\CMS\MVC\Factory\MVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
Expand Down Expand Up @@ -45,17 +46,17 @@ public function __construct(string $component)
}

/**
* Returns the dispatcher for the given application, null if none exists.
* Returns the dispatcher for the given application.
*
* @param CMSApplicationInterface $application The application
*
* @return DispatcherInterface|null
* @return DispatcherInterface
*
* @since __DEPLOY_VERSION__
*/
public function getDispatcher(CMSApplicationInterface $application)
public function getDispatcher(CMSApplicationInterface $application): DispatcherInterface
{
return null;
return new LegacyDispatcher($application);
}

/**
Expand Down

0 comments on commit 387c251

Please sign in to comment.