From 9107b7e1de2c4e5b7339cfc6ac103651d8970e70 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Sun, 18 Jul 2021 18:39:55 +0700 Subject: [PATCH] Clean up code --- .../components/com_weblinks/script.php | 5 ++- .../com_weblinks/services/provider.php | 14 +++++--- .../src/Controller/WeblinkController.php | 2 +- .../src/Helper/AssociationsHelper.php | 2 +- .../com_weblinks/src/Model/CategoryModel.php | 34 +++++++++++++------ 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/administrator/components/com_weblinks/script.php b/src/administrator/components/com_weblinks/script.php index 34234ac..794c867 100644 --- a/src/administrator/components/com_weblinks/script.php +++ b/src/administrator/components/com_weblinks/script.php @@ -9,7 +9,6 @@ defined('_JEXEC') or die; -use Joomla\CMS\Extension\ExtensionHelper; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\File; use Joomla\CMS\Filesystem\Folder; @@ -93,8 +92,8 @@ class Com_WeblinksInstallerScript public function install($parent) { // Initialize a new category - /** @type JTableCategory $category */ - $category = Table::getInstance('Category'); + /** @type Joomla\CMS\Table\Category $category */ + $category = Table::getInstance('Category', 'Joomla\\CMS\\Table\\'); // Check if the Uncategorised category exists before adding it if (!$category->load(array('extension' => 'com_weblinks', 'title' => 'Uncategorised'))) diff --git a/src/administrator/components/com_weblinks/services/provider.php b/src/administrator/components/com_weblinks/services/provider.php index 71ff945..e46b034 100644 --- a/src/administrator/components/com_weblinks/services/provider.php +++ b/src/administrator/components/com_weblinks/services/provider.php @@ -44,19 +44,23 @@ return new class implements ServiceProviderInterface { $container->set(AssociationExtensionInterface::class, new AssociationsHelper); - $container->registerServiceProvider(new CategoryFactory('\\Joomla\\Component\\Weblinks')); - $container->registerServiceProvider(new MVCFactory('\\Joomla\\Component\\Weblinks')); - $container->registerServiceProvider(new ComponentDispatcherFactory('\\Joomla\\Component\\Weblinks')); - $container->registerServiceProvider(new RouterFactory('\\Joomla\\Component\\Weblinks')); + $componentNamespace = '\\Joomla\\Component\\Weblinks'; + + $container->registerServiceProvider(new CategoryFactory($componentNamespace)); + $container->registerServiceProvider(new MVCFactory($componentNamespace)); + $container->registerServiceProvider(new ComponentDispatcherFactory($componentNamespace)); + $container->registerServiceProvider(new RouterFactory($componentNamespace)); + $container->set( ComponentInterface::class, function (Container $container) { $component = new WeblinksComponent($container->get(ComponentDispatcherFactoryInterface::class)); + $component->setRegistry($container->get(Registry::class)); $component->setMVCFactory($container->get(MVCFactoryInterface::class)); $component->setCategoryFactory($container->get(CategoryFactoryInterface::class)); - $component->setRouterFactory($container->get(RouterFactoryInterface::class)); $component->setAssociationExtension($container->get(AssociationExtensionInterface::class)); + $component->setRouterFactory($container->get(RouterFactoryInterface::class)); return $component; } diff --git a/src/administrator/components/com_weblinks/src/Controller/WeblinkController.php b/src/administrator/components/com_weblinks/src/Controller/WeblinkController.php index 0f2a5fc..a5ce117 100644 --- a/src/administrator/components/com_weblinks/src/Controller/WeblinkController.php +++ b/src/administrator/components/com_weblinks/src/Controller/WeblinkController.php @@ -100,7 +100,7 @@ class WeblinkController extends FormController $this->checkToken(); // Set the model - $model = $this->getModel('Weblink', '', array()); + $model = $this->getModel('Weblink', 'Administrator', array()); // Preset the redirect $this->setRedirect(Route::_('index.php?option=com_weblinks&view=weblinks' . $this->getRedirectToListAppend(), false)); diff --git a/src/administrator/components/com_weblinks/src/Helper/AssociationsHelper.php b/src/administrator/components/com_weblinks/src/Helper/AssociationsHelper.php index 60c7977..45d4129 100644 --- a/src/administrator/components/com_weblinks/src/Helper/AssociationsHelper.php +++ b/src/administrator/components/com_weblinks/src/Helper/AssociationsHelper.php @@ -128,7 +128,7 @@ class AssociationsHelper extends AssociationExtensionHelper break; case 'category': - $table = Table::getInstance('Category'); + $table = Table::getInstance('Category', 'Joomla\\CMS\\Table\\'); break; } diff --git a/src/components/com_weblinks/src/Model/CategoryModel.php b/src/components/com_weblinks/src/Model/CategoryModel.php index b6140a2..1ca68cb 100644 --- a/src/components/com_weblinks/src/Model/CategoryModel.php +++ b/src/components/com_weblinks/src/Model/CategoryModel.php @@ -36,6 +36,20 @@ class CategoryModel extends ListModel */ protected $_item = null; + /** + * Category left of this one + * + * @var CategoryNode|null + */ + protected $_leftsibling = null; + + /** + * Category right right of this one + * + * @var CategoryNode|null + */ + protected $_rightsibling = null; + /** * Array of child-categories * @@ -114,7 +128,7 @@ class CategoryModel extends ListModel $viewLevels = Factory::getApplication()->getIdentity()->getAuthorisedViewLevels(); // Create a new query object. - $db = $this->getDbo(); + $db = $this->getDbo(); $query = $db->getQuery(true); // Select required fields from the categories. @@ -174,7 +188,7 @@ class CategoryModel extends ListModel if ($this->getState('filter.publish_date')) { $nullDate = $db->getNullDate(); - $nowDate = Factory::getDate()->toSql(); + $nowDate = Factory::getDate()->toSql(); $query->where('(a.publish_up = :publishUpNullDate OR a.publish_up <= :publishUpNowDate)') ->where('(a.publish_down = :publishDownNullDate OR a.publish_down >= :publishDownNowDate)') ->bind(':publishUpNullDate', $nullDate) @@ -231,7 +245,7 @@ class CategoryModel extends ListModel */ protected function populateState($ordering = null, $direction = null) { - $app = Factory::getApplication(); + $app = Factory::getApplication(); $params = ComponentHelper::getParams('com_weblinks'); // List state information @@ -304,17 +318,17 @@ class CategoryModel extends ListModel $params = new Registry; } - $options = array(); + $options = array(); $options['countItems'] = $params->get('show_cat_num_links_cat', 1) || $params->get('show_empty_categories', 0); - $categories = Categories::getInstance('Weblinks', $options); + $categories = Categories::getInstance('Weblinks', $options); $this->_item = $categories->get($this->getState('category.id', 'root')); if (is_object($this->_item)) { $this->_children = $this->_item->getChildren(); - $this->_parent = false; + $this->_parent = false; if ($this->_item->getParent()) { @@ -322,12 +336,12 @@ class CategoryModel extends ListModel } $this->_rightsibling = $this->_item->getSibling(); - $this->_leftsibling = $this->_item->getSibling(false); + $this->_leftsibling = $this->_item->getSibling(false); } else { $this->_children = false; - $this->_parent = false; + $this->_parent = false; } } @@ -409,8 +423,8 @@ class CategoryModel extends ListModel if ($hitcount) { - $pk = (!empty($pk)) ? $pk : (int) $this->getState('category.id'); - $table = Table::getInstance('Category', 'JTable'); + $pk = (!empty($pk)) ? $pk : (int) $this->getState('category.id'); + $table = Table::getInstance('Category', 'Joomla\\CMS\\Table\\'); $table->load($pk); $table->hit($pk); }