Skip to content

Commit

Permalink
Introduce cache controller service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Oct 17, 2018
1 parent b63fcdf commit e99353f
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 19 deletions.
5 changes: 3 additions & 2 deletions libraries/src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ public function __construct($options)
*
* @return CacheController
*
* @since 1.7.0
* @since 1.7.0
* @deprecated 5.0 Use the cache controller factory instead
*/
public static function getInstance($type = 'output', $options = array())
{
return CacheController::getInstance($type, $options);
return Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController($type, $options);
}

/**
Expand Down
23 changes: 9 additions & 14 deletions libraries/src/Cache/CacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,21 @@ public function __call($name, $arguments)
*
* @since 1.7.0
* @throws \RuntimeException
* @deprecated 5.0 Use the cache controller factory instead
*/
public static function getInstance($type = 'output', $options = array())
{
self::addIncludePath(__DIR__ . '/Controller');

$type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));

$class = __NAMESPACE__ . '\\Controller\\' . ucfirst($type) . 'Controller';

if (!class_exists($class))
try
{
return Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController($type, $options);
}
catch (\RuntimeException $e)
{
$class = 'JCacheController' . ucfirst($type);
}

$type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
$class = 'JCacheController' . ucfirst($type);

if (!class_exists($class))
{
// Search for the class file in the Cache include paths.
Expand All @@ -115,12 +116,6 @@ public static function getInstance($type = 'output', $options = array())
}
}

// Check for a possible service from the container otherwise manually instantiate the class
if (Factory::getContainer()->exists($class))
{
return Factory::getContainer()->get($class);
}

return new $class($options);
}

Expand Down
50 changes: 50 additions & 0 deletions libraries/src/Cache/CacheControllerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Cache;

defined('_JEXEC') or die;

/**
* Default factory for creating CacheController objects
*
* @since __DEPLOY_VERSION__
*/
class CacheControllerFactory implements CacheControllerFactoryInterface
{
/**
* Method to get an instance of a cache controller.
*
* @param string $type The cache object type to instantiate
* @param array $options Array of options
*
* @return CacheController
*
* @since __DEPLOY_VERSION__
* @throws \RuntimeException
*/
public function createCacheController($type = 'output', $options = array()): CacheController
{
$type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));

$class = __NAMESPACE__ . '\\Controller\\' . ucfirst($type) . 'Controller';

if ($type == '')
{
$class = CacheController::class;
}

// The class should now be loaded
if (!class_exists($class))
{
throw new \RuntimeException('Unable to load Cache Controller: ' . $type, 500);
}

return new $class($options);
}
}
32 changes: 32 additions & 0 deletions libraries/src/Cache/CacheControllerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Cache;

defined('_JEXEC') or die;

/**
* Interface defining a factory which can create CacheController objects
*
* @since __DEPLOY_VERSION__
*/
interface CacheControllerFactoryInterface
{
/**
* Method to get an instance of a cache controller.
*
* @param string $type The cache object type to instantiate
* @param array $options Array of options
*
* @return CacheController
*
* @since __DEPLOY_VERSION__
* @throws \RuntimeException
*/
public function createCacheController($type = 'output', $options = array()): CacheController;
}
9 changes: 6 additions & 3 deletions libraries/src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Cache\Cache;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Document\Document;
use Joomla\CMS\Document\FactoryInterface;
Expand Down Expand Up @@ -310,8 +311,9 @@ public static function getUser($id = null)
*
* @return \Joomla\CMS\Cache\CacheController object
*
* @see JCache
* @since 1.7.0
* @see Cache
* @since 1.7.0
* @deprecated 5.0 Use the cache controller factory instead
*/
public static function getCache($group = '', $handler = 'callback', $storage = null)
{
Expand All @@ -331,7 +333,7 @@ public static function getCache($group = '', $handler = 'callback', $storage = n
$options['storage'] = $storage;
}

$cache = Cache::getInstance($handler, $options);
$cache = self::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController($handler, $options);

self::$cache[$hash] = $cache;

Expand Down Expand Up @@ -502,6 +504,7 @@ protected static function createContainer(): Container
$container = (new Container)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Application)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Authentication)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\CacheController)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Config)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Console)
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Database)
Expand Down
47 changes: 47 additions & 0 deletions libraries/src/Service/Provider/CacheController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Service\Provider;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Cache\CacheControllerFactory;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

/**
* Service provider for the cache controller dependency
*
* @since 4.0
*/
class CacheController implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since 4.0
*/
public function register(Container $container)
{
$container->alias('cachecontroller.factory', CacheControllerFactoryInterface::class)
->alias(CacheControllerFactory::class, CacheControllerFactoryInterface::class)
->share(
CacheControllerFactoryInterface::class,
function (Container $container)
{
return new CacheControllerFactory;
},
true
);
}
}

0 comments on commit e99353f

Please sign in to comment.