?
avatar Denitz
Denitz
6 Aug 2020

Is it possible to include and implement CategoryFactory, CategoryFactoryInterface and CategoryInterface in 3.10 to provide easier code refactoring for Joomla 4?

avatar Denitz Denitz - open - 6 Aug 2020
avatar joomla-cms-bot joomla-cms-bot - change - 6 Aug 2020
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 6 Aug 2020
avatar zero-24
zero-24 - comment - 9 Aug 2020

Hi, backporting sounds good to me but I'm not sure wherether that line here can work in 3.x given that services are 4.x only right?
https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Categories/CategoryFactory.php#L55

@laoneo can we get your advice here on a backport?

avatar laoneo
laoneo - comment - 10 Aug 2020

I guess you have then to backport the whole container setup as well. Means all the interfaces have to be rewritten for PHP 5.3, not sure if you really want to do that.

avatar zero-24
zero-24 - comment - 11 Aug 2020

hmm thanks for your feeback. I agree backporting the container setup seems to me to be a bit to big here.

avatar Denitz
Denitz - comment - 12 Aug 2020

The following files can be easily included from J4's /libraries/src/categories - just a copy! No need for any container!

CategoryFactory.php
CategoryFactoryInterface.php
CategoryInterface.php

It helps to re-use the J4 category service as J3 category helper and really helps with having old category helper working on J4 code.

Here is an example J3 category helper /components/com_sample/helpers/category.php which is actually aliasing the new J4 category service /components/com_sample/src/Service/Category.php as J3 category helper but currently it requires own copies of three files mentioned above (we keep them in /components/com_sample/compat39/Categories).

use Joomla\CMS\Categories\CategoryFactory;
use Joomla\CMS\Categories\CategoryFactoryInterface;
use Joomla\CMS\Categories\CategoryInterface;

defined('_JEXEC') or die('Restricted access');

JLoader::register(CategoryFactory::class, JPATH_SITE . '/components/com_sample/compat39/Categories/CategoryFactory.php');
JLoader::register(CategoryFactoryInterface::class, JPATH_SITE . '/components/com_sample/compat39/Categories/CategoryFactoryInterface.php');
JLoader::register(CategoryInterface::class, JPATH_SITE . '/components/com_sample/compat39/Categories/CategoryInterface.php');

require_once __DIR__ . '/../src/Service/Category.php';

class_alias(Joomla\Component\Sample\Site\Service\Category::class, 'SampleCategories');

Once we have these 3 files in /libraries/src/Categories, there is no need for keeping them in own com_sample, we have the code for both J3 and J4, the actual code is located in J4 files and once the J3 support is stopped for our component, we can just remove the old category helper without any issues with the code for J4.

Here is the simple code for mentioned J4 category service which is also working on J3 as category helper:

namespace Joomla\Component\Sample\Site\Service;

use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Categories\CategoryInterface;
use Joomla\CMS\Categories\CategoryNode;

defined('_JEXEC') or die;

class Category extends Categories implements CategoryInterface
{

	/**
	 * @inheritDoc
	 *
	 * @since   1.0.0
	 */
	public function __construct($options = [])
	{
		$options['table']     = '#__sample_items';
		$options['extension'] = 'com_sample';

		parent::__construct($options);
	}
}

CategoryFactory and CategoryFactoryInterface can be also now used in J4 Router service code which is working on J3 easily! Same approach: we alias the router service in J3 component's router.php

avatar laoneo
laoneo - comment - 12 Aug 2020

The question then is how you load the category as in J4 you don't need getInstance anymore as it can be loaded through the component class? But try to make a pr, perhaps I get something wrong.

avatar Denitz
Denitz - comment - 12 Aug 2020

We can use ported CategoryServiceTrait::getCategory() etc., will make a PR...

avatar Denitz Denitz - change - 12 Aug 2020
Title
[3.10] libraries/src/Categories
[3.10] Backport Categories
avatar Denitz Denitz - edited - 12 Aug 2020
avatar Denitz
Denitz - comment - 12 Aug 2020
avatar richard67 richard67 - change - 12 Aug 2020
Status New Closed
Closed_Date 0000-00-00 00:00:00 2020-08-12 16:47:13
Closed_By richard67
avatar richard67 richard67 - close - 12 Aug 2020
avatar richard67
richard67 - comment - 12 Aug 2020

Closing as having a Pull Request. See #30346 .

Add a Comment

Login with GitHub to post a comment