-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4.0] Move component categories helper to services #19667
Changes from 18 commits
5e73d6b
7d7bc87
fdcf139
1a119fd
efd56db
166c511
44c1140
3282e31
db25ff4
5f126d3
27f89f2
ff0000f
80931ab
9eadbb9
09e4574
ca128f7
91c907d
75b29ce
d5b6c8a
82a0e73
bf25d1c
8d8d41b
16b7c71
3a3328e
5a9912c
161e90a
8ee0093
dfb3a61
a2447d3
084e0a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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( | ||
'ContentComponentContainer', | ||
function (\Joomla\DI\Container $parent) | ||
{ | ||
$container = new \Joomla\CMS\Extension\ComponentContainer($parent); | ||
$container->set('categories', new \Joomla\Component\Content\Site\Service\Category); | ||
|
||
return $container; | ||
}, | ||
true | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ class Categories | |
* @var array | ||
* @since 1.6 | ||
*/ | ||
protected $_options = null; | ||
protected $_options = []; | ||
|
||
/** | ||
* Class constructor | ||
|
@@ -101,20 +101,20 @@ class Categories | |
*/ | ||
public function __construct($options) | ||
{ | ||
// Required 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; | ||
// Default some optional options | ||
$this->_options['access'] = 'true'; | ||
$this->_options['published'] = 1; | ||
$this->_options['countItems'] = 0; | ||
$this->_options['currentlang'] = Multilanguage::isEnabled() ? Factory::getLanguage()->getTag() : 0; | ||
|
||
$this->_options = $options; | ||
|
||
return true; | ||
$this->setOptions($options); | ||
} | ||
|
||
/** | ||
|
@@ -125,7 +125,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()) | ||
{ | ||
|
@@ -136,32 +137,11 @@ 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 = Factory::getApplication()->bootComponent($parts[0])->getCategories($options, 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 | ||
{ | ||
self::$instances[$hash] = new $classname($options); | ||
} | ||
self::$instances[$hash] = $categories; | ||
|
||
return self::$instances[$hash]; | ||
} | ||
|
@@ -389,4 +369,36 @@ protected function _load($id) | |
$this->_nodes[$id] = null; | ||
} | ||
} | ||
|
||
/** | ||
* Allows to set some optional options, eg. if the access level should be considered. | ||
* Also clears the internal children cache. | ||
* | ||
* @param array $options The new options | ||
* | ||
* @return void | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function setOptions(array $options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this back to the constructor please. I know this is a bit simpler to view but I risking people call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to do it that way as in the ComponentContainer here , it is not possible to pass the options. If there is a way to set the options, then I can change it back to the constructor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You make this an anonymous function like we do in all the service providers for the main DI Container https://github.com/joomla/joomla-cms/pull/19667/files#diff-a56b3bb3afcd610a7b1560ed05d8a146R17 |
||
{ | ||
if (isset($options['access'])) | ||
{ | ||
$this->_options['access'] = $options['access']; | ||
} | ||
|
||
if (isset($options['published'])) | ||
{ | ||
$this->_options['published'] = $options['published']; | ||
} | ||
|
||
if (isset($options['countItems'])) | ||
{ | ||
$this->_options['countItems'] = $options['countItems']; | ||
} | ||
|
||
// Reset the cache | ||
$this->_nodes = []; | ||
$this->_checkedCategories = []; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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\Extension; | ||
|
||
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 array $options The options | ||
* @param string $section The section | ||
* | ||
* @return Categories|null | ||
* | ||
* @see Categories::setOptions() | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function getCategories(array $options = [], $section = '') | ||
{ | ||
$serviceName = 'categories'; | ||
|
||
if ($section) | ||
{ | ||
$serviceName .= '.' . strtolower($section); | ||
} | ||
|
||
if (!$this->has($serviceName)) | ||
{ | ||
return null; | ||
} | ||
|
||
$categories = $this->get($serviceName); | ||
$categories->setOptions($options); | ||
|
||
return $categories; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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\Extension; | ||
|
||
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 array $options The options | ||
* @param string $section The section | ||
* | ||
* @return Categories|null | ||
* | ||
* @see Categories::setOptions() | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function getCategories(array $options = [], $section = ''); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Services should be application independent. So why
...\Site\...
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can even be removed, but for now George wants to have it in place, see the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with the class itself so far - I just don't see, why it is in a namespace that semantically restricts it to 'site'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have application agnostic component code. So we'd need a solution to that to get around that (mildly important in some ways) semantic detail.