Skip to content

Commit

Permalink
Move component categories helper to services
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Feb 13, 2018
1 parent 26d7a45 commit 5e73d6b
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 72 deletions.
24 changes: 24 additions & 0 deletions administrator/components/com_content/services/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

$container->share(
'ContentContainer',
function (\Joomla\DI\Container $parent)
{
$container = new \Joomla\CMS\Component\ComponentContainer($parent);
$container->registerServiceProvider(
new \Joomla\CMS\Component\Service\Provider\Categories(['table' => '#__content', 'extension' => 'com_content'])
);

return $container;
},
true
);
2 changes: 1 addition & 1 deletion administrator/components/com_fields/Model/FieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ protected function preprocessForm(\JForm $form, $data, $group = 'content')
}

// Setting the context for the category field
$cat = \JCategories::getInstance(str_replace('com_', '', $component));
$cat = $this->bootComponent($component)->getCategories();

if ($cat && $cat->get('root')->hasChildren())
{
Expand Down
33 changes: 0 additions & 33 deletions components/com_content/helpers/category.php

This file was deleted.

86 changes: 48 additions & 38 deletions libraries/src/Categories/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;

Expand Down Expand Up @@ -90,7 +91,7 @@ class Categories
* @var array
* @since 1.6
*/
protected $_options = null;
protected $_options = [];

/**
* Class constructor
Expand All @@ -101,20 +102,7 @@ class Categories
*/
public function __construct($options)
{
$this->_extension = $options['extension'];
$this->_table = $options['table'];
$this->_field = isset($options['field']) && $options['field'] ? $options['field'] : 'catid';
$this->_key = isset($options['key']) && $options['key'] ? $options['key'] : 'id';
$this->_statefield = $options['statefield'] ?? 'state';

$options['access'] = $options['access'] ?? 'true';
$options['published'] = $options['published'] ?? 1;
$options['countItems'] = $options['countItems'] ?? 0;
$options['currentlang'] = Multilanguage::isEnabled() ? Factory::getLanguage()->getTag() : 0;

$this->_options = $options;

return true;
$this->setOptions($options);
}

/**
Expand All @@ -125,7 +113,8 @@ public function __construct($options)
*
* @return Categories|boolean Categories object on success, boolean false if an object does not exist
*
* @since 1.6
* @since 1.6
* @deprecated 5.0 Use the ComponentContainerInterface to get the categories
*/
public static function getInstance($extension, $options = array())
{
Expand All @@ -136,33 +125,17 @@ public static function getInstance($extension, $options = array())
return self::$instances[$hash];
}

$parts = explode('.', $extension);
$component = 'com_' . strtolower($parts[0]);
$section = count($parts) > 1 ? $parts[1] : '';
$classname = ucfirst(substr($component, 4)) . ucfirst($section) . 'Categories';

if (!class_exists($classname))
{
$path = JPATH_SITE . '/components/' . $component . '/helpers/category.php';

if (!is_file($path))
{
return false;
}
$parts = explode('.', $extension, 2);

include_once $path;
}
$categories = ComponentHelper::boot($parts[0])->getCategories(count($parts) > 1 ? $parts[1] : '');

// Check for a possible service from the container otherwise manually instantiate the class
if (\JFactory::getContainer()->exists($classname))
{
self::$instances[$hash] = \JFactory::getContainer()->get($classname);
}
else
if ($categories)
{
self::$instances[$hash] = new $classname($options);
$categories->setOptions($options);
}

self::$instances[$hash] = $categories;

return self::$instances[$hash];
}

Expand Down Expand Up @@ -389,4 +362,41 @@ protected function _load($id)
$this->_nodes[$id] = null;
}
}

/**
* Sets the options for this service. The given options do overwrite the
* internal options with the same index. The existing options are not cleared
* when they are not specified in the given options array.
*
* @param array $options The new options
*
* @since __DEPLOY_VERSION__
*/
public function setOptions(array $options)
{
if ($this->_options == $options)
{
return;
}

// Merge the options to not loose the base config
$options = array_merge($this->_options, $options);

$this->_extension = $options['extension'];
$this->_table = $options['table'];
$this->_field = isset($options['field']) && $options['field'] ? $options['field'] : 'catid';
$this->_key = isset($options['key']) && $options['key'] ? $options['key'] : 'id';
$this->_statefield = $options['statefield'] ?? 'state';

$options['access'] = $options['access'] ?? 'true';
$options['published'] = $options['published'] ?? 1;
$options['countItems'] = $options['countItems'] ?? 0;
$options['currentlang'] = Multilanguage::isEnabled() ? Factory::getLanguage()->getTag() : 0;

$this->_options = $options;

// Reset the cache
$this->_nodes = [];
$this->_checkedCategories = [];
}
}
50 changes: 50 additions & 0 deletions libraries/src/Component/ComponentContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.txt
*/

namespace Joomla\CMS\Component;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Categories\Categories;
use Joomla\DI\Container;
use Psr\Container\ContainerInterface;

/**
* Access to component specific services.
*
* @since __DEPLOY_VERSION__
*/
class ComponentContainer extends Container implements ComponentContainerInterface
{
/**
* Returns the category service. If the service is not available
* null is returned.
*
* @param string $section The section
*
* @return Categories|null
*
* @since __DEPLOY_VERSION__
*/
public function getCategories($section = ''): Categories
{
$serviceName = 'categories';

if ($section)
{
$serviceName .= '.' .strtolower($section);
}

if (!$this->has($serviceName))
{
return null;
}

return $this->get($serviceName);
}
}
33 changes: 33 additions & 0 deletions libraries/src/Component/ComponentContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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.txt
*/

namespace Joomla\CMS\Component;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Categories\Categories;

/**
* Access to component specific services.
*
* @since __DEPLOY_VERSION__
*/
interface ComponentContainerInterface
{
/**
* Returns the category service. If the service is not available
* null is returned.
*
* @param string $section The section
*
* @return Categories|null
*
* @since __DEPLOY_VERSION__
*/
public function getCategories($section = ''): Categories;
}
47 changes: 47 additions & 0 deletions libraries/src/Component/ComponentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

use Joomla\CMS\Access\Access;
use Joomla\CMS\Component\Exception\MissingComponentException;
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;
use Joomla\CMS\Dispatcher\DispatcherInterface;
use Psr\Container\ContainerInterface;

/**
* Component helper class
Expand Down Expand Up @@ -552,4 +554,49 @@ public static function getComponentName($object, string $alternativeName): strin

return 'com_' . strtolower(substr($reflect->getNamespaceName(), $from + 11, $to));
}

/**
* Boots the component with the given name.
*
* @param string $component The component name, eg. com_content.
* @param ContainerInterface $container The container.
*
* @return ComponentContainerInterface The service container
*
* @since __DEPLOY_VERSION__
*/
public static function boot($component, ContainerInterface $container = null): ComponentContainerInterface
{
// Normalize the component name
$component = strtolower(str_replace('com_', '', $component));

// The service name
$serviceName = ucfirst($component) . 'Container';

// The container which holds the component container
$container = $container ? : Factory::getContainer();

// Check if the service is already available
if ($container->has($serviceName))
{
return $container->get($serviceName);
}

// Allow components to register services
$path = JPATH_ADMINISTRATOR . '/components/com_' . $component . '/services/services.php';

if (file_exists($path))
{
// Load the services file
require_once $path;
}

if (!$container->has($serviceName))
{
$container->set($serviceName, new LegacyComponentContainer($component));
}

// Return the child container
return $container->get($serviceName);
}
}
Loading

0 comments on commit 5e73d6b

Please sign in to comment.