Skip to content

Commit

Permalink
[4.0] Load the component dispatcher through the ComponentInterface (#…
Browse files Browse the repository at this point in the history
…19811)

* Load dispatcher as service

* Reove obsolete

* dispatcher

* dispatcher service provider

* Dispatcher factory

* CS

* doc block

* doc block

* imports

* Revert

* CS

* Make drone happy

* CS

* Load the option correctly
  • Loading branch information
laoneo authored and wilsonge committed Mar 4, 2018
1 parent d36f931 commit 5888634
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;
namespace Joomla\Component\Content\Administrator\Dispatcher;

use Joomla\CMS\Dispatcher\Dispatcher;
defined('JPATH_PLATFORM') or die;

/**
* Dispatcher class for com_content
*
* @since 4.0.0
*/
class ContentDispatcher extends Dispatcher
class Dispatcher extends \Joomla\CMS\Dispatcher\Dispatcher
{
/**
* The extension namespace
Expand Down
7 changes: 6 additions & 1 deletion administrator/components/com_content/services/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

defined('_JEXEC') or die;

use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Dispatcher\DispatcherFactory;
use Joomla\CMS\Dispatcher\DispatcherFactoryInterface;
use Joomla\CMS\Extension\Service\Provider\Component;
use Joomla\Component\Content\Site\Service\Category;
use Joomla\DI\Container;
Expand All @@ -32,7 +35,9 @@ class ContentComponentServiceProvider implements ServiceProviderInterface
*/
public function register(Container $container)
{
$container->set('categories', ['' => new Category]);
$container->set(Categories::class, ['' => new Category]);

$container->set(DispatcherFactoryInterface::class, new DispatcherFactory('\\Joomla\\Component\\Content'));
$container->registerServiceProvider(new Component);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;
namespace Joomla\Component\Content\Site\Dispatcher;

use Joomla\CMS\Dispatcher\Dispatcher;
defined('JPATH_PLATFORM') or die;

/**
* Dispatcher class for com_content
*
* @since 4.0.0
*/
class ContentDispatcher extends Dispatcher
class Dispatcher extends \Joomla\CMS\Dispatcher\Dispatcher
{
/**
* The extension namespace
Expand Down Expand Up @@ -58,9 +58,9 @@ public function dispatch()
}
}

JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');
JLoader::register('ContentHelperQuery', JPATH_SITE . '/components/com_content/helpers/query.php');
JLoader::register('ContentHelperAssociation', JPATH_SITE . '/components/com_content/helpers/association.php');
\JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');
\JLoader::register('ContentHelperQuery', JPATH_SITE . '/components/com_content/helpers/query.php');
\JLoader::register('ContentHelperAssociation', JPATH_SITE . '/components/com_content/helpers/association.php');

parent::dispatch();
}
Expand Down
9 changes: 8 additions & 1 deletion libraries/src/Component/ComponentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,15 @@ public static function renderComponent($option, $params = array())
// Handle template preview outlining.
$contents = null;

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

// Check if we have a dispatcher
if (file_exists(JPATH_COMPONENT . '/dispatcher.php'))
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';
Expand Down
12 changes: 5 additions & 7 deletions libraries/src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Joomla\CMS\Access\Exception\NotAllowed;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Form\FormFactoryAwareInterface;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\Input\Input;
Expand Down Expand Up @@ -82,13 +83,10 @@ public function __construct(CMSApplication $app, Input $input = null)
// If option is not provided, detect it from dispatcher class name, ie ContentDispatcher
if (empty($this->option))
{
$className = get_class($this);
$pos = strpos($className, 'Dispatcher');

if ($pos !== false)
{
$this->option = 'com_' . strtolower(substr($className, 0, $pos));
}
$this->option = ComponentHelper::getComponentName(
$this,
strtolower(str_replace('Dispatcher', '', get_class($this)))
);
}

$this->loadLanguage();
Expand Down
65 changes: 65 additions & 0 deletions libraries/src/Dispatcher/DispatcherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\CMSApplicationInterface;

/**
* Namesapce based implementation of the DispatcherFactoryInterface
*
* @since __DEPLOY_VERSION__
*/
class DispatcherFactory implements DispatcherFactoryInterface
{
/**
* The extension namespace
*
* @var string
*
* @since __DEPLOY_VERSION__
*/
protected $namespace;

/**
* DispatcherFactory constructor.
*
* @param string $namespace The namespace
*
* @since __DEPLOY_VERSION__
*/
public function __construct(string $namespace)
{
$this->namespace = $namespace;
}

/**
* Creates a dispatcher.
*
* @param CMSApplicationInterface $application The application
*
* @return DispatcherInterface
*
* @since __DEPLOY_VERSION__
*/
public function createDispatcher(CMSApplicationInterface $application): DispatcherInterface
{
$name = 'Site';

if ($application->isClient('administrator'))
{
$name = 'Administrator';
}

$className = '\\' . trim($this->namespace, '\\') . '\\' . $name . '\\Dispatcher\\Dispatcher';

return new $className($application, $application->input);
}
}
33 changes: 33 additions & 0 deletions libraries/src/Dispatcher/DispatcherFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @package Joomla.Libraries
* @subpackage Dispatcher
*
* @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\CMSApplicationInterface;

/**
* Dispatcher factory interface
*
* @since __DEPLOY_VERSION__
*/
interface DispatcherFactoryInterface
{
/**
* Creates a dispatcher.
*
* @param CMSApplicationInterface $application The application
*
* @return DispatcherInterface
*
* @since __DEPLOY_VERSION__
*/
public function createDispatcher(CMSApplicationInterface $application): DispatcherInterface;
}
46 changes: 46 additions & 0 deletions libraries/src/Extension/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Dispatcher\DispatcherFactory;
use Joomla\CMS\Dispatcher\DispatcherFactoryInterface;
use Joomla\CMS\Dispatcher\DispatcherInterface;

/**
* Access to component specific services.
Expand All @@ -28,6 +32,48 @@ class Component implements ComponentInterface
*/
private $categories;

/**
* The dispatcher factory.
*
* @var DispatcherFactoryInterface
*
* @since __DEPLOY_VERSION__
*/
private $dispatcherFactory;

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

return $this->dispatcherFactory->createDispatcher($application);
}

/**
* Sets the dispatcher factory.
*
* @param DispatcherFactoryInterface $dispatcherFactory The dispatcher factory
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function setDispatcherFactory(DispatcherFactoryInterface $dispatcherFactory)
{
$this->dispatcherFactory = $dispatcherFactory;
}

/**
* Returns the category service. If the service is not available
* null is returned.
Expand Down
13 changes: 13 additions & 0 deletions libraries/src/Extension/ComponentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Dispatcher\DispatcherInterface;

/**
* Access to component specific services.
Expand All @@ -19,6 +21,17 @@
*/
interface ComponentInterface
{
/**
* Returns the dispatcher for the given application, null if none exists.
*
* @param CMSApplicationInterface $application The application
*
* @return DispatcherInterface|null
*
* @since __DEPLOY_VERSION__
*/
public function getDispatcher(CMSApplicationInterface $application);

/**
* Returns the category service. If the service is not available
* null is returned.
Expand Down
16 changes: 16 additions & 0 deletions libraries/src/Extension/LegacyComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Dispatcher\DispatcherInterface;

/**
* Access to component specific services.
Expand Down Expand Up @@ -38,6 +40,20 @@ public function __construct(string $component)
$this->component = str_replace('com_', '', $component);
}

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

/**
* Returns the category service. If the service is not available
* null is returned.
Expand Down
13 changes: 10 additions & 3 deletions libraries/src/Extension/Service/Provider/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Dispatcher\DispatcherFactoryInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

/**
* Service provider for the component's dispatcher dependency
* Service provider for the service based components.
*
* @since __DEPLOY_VERSION__
*/
Expand All @@ -37,9 +39,14 @@ function (Container $container)
{
$component = new \Joomla\CMS\Extension\Component;

if ($container->has('categories'))
if ($container->has(Categories::class))
{
$component->setCategories($container->get('categories'));
$component->setCategories($container->get(Categories::class));
}

if ($container->has(DispatcherFactoryInterface::class))
{
$component->setDispatcherFactory($container->get(DispatcherFactoryInterface::class));
}

return $component;
Expand Down

0 comments on commit 5888634

Please sign in to comment.