From c19e8072bdcf372e40cffdf0fa95a8eae3f1107d Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 23 May 2023 15:08:47 +0200 Subject: [PATCH] Convert the content plugins to service providers (#40561) * Convert the content plugins to service providers * functions * namespaces * doc * get application from plugin (#29) * get application from plugin * Revert pagenavigation --------- Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com> --- plugins/content/finder/finder.xml | 5 +- plugins/content/finder/services/provider.php | 47 +++++++ .../{finder.php => src/Extension/Finder.php} | 17 ++- plugins/content/joomla/joomla.xml | 4 +- plugins/content/joomla/services/provider.php | 51 ++++++++ .../{joomla.php => src/Extension/Joomla.php} | 120 ++++++++---------- plugins/content/loadmodule/loadmodule.xml | 4 +- .../content/loadmodule/services/provider.php | 47 +++++++ .../Extension/LoadModule.php} | 25 ++-- plugins/content/pagebreak/pagebreak.xml | 5 +- .../content/pagebreak/services/provider.php | 47 +++++++ .../Extension/PageBreak.php} | 33 ++--- plugins/content/pagebreak/tmpl/navigation.php | 3 +- plugins/content/vote/services/provider.php | 47 +++++++ .../vote/{vote.php => src/Extension/Vote.php} | 37 ++---- plugins/content/vote/tmpl/rating.php | 2 +- plugins/content/vote/vote.xml | 4 +- 17 files changed, 357 insertions(+), 141 deletions(-) create mode 100644 plugins/content/finder/services/provider.php rename plugins/content/finder/{finder.php => src/Extension/Finder.php} (85%) create mode 100644 plugins/content/joomla/services/provider.php rename plugins/content/joomla/{joomla.php => src/Extension/Joomla.php} (82%) create mode 100644 plugins/content/loadmodule/services/provider.php rename plugins/content/loadmodule/{loadmodule.php => src/Extension/LoadModule.php} (92%) create mode 100644 plugins/content/pagebreak/services/provider.php rename plugins/content/pagebreak/{pagebreak.php => src/Extension/PageBreak.php} (91%) create mode 100644 plugins/content/vote/services/provider.php rename plugins/content/vote/{vote.php => src/Extension/Vote.php} (80%) diff --git a/plugins/content/finder/finder.xml b/plugins/content/finder/finder.xml index 1e6cf2ad55247..fd2b69748ff35 100644 --- a/plugins/content/finder/finder.xml +++ b/plugins/content/finder/finder.xml @@ -9,9 +9,10 @@ www.joomla.org 3.0.0 PLG_CONTENT_FINDER_XML_DESCRIPTION - + Joomla\Plugin\Content\Finder - finder.php + services + src language/en-GB/plg_content_finder.ini diff --git a/plugins/content/finder/services/provider.php b/plugins/content/finder/services/provider.php new file mode 100644 index 0000000000000..0034506e62d8c --- /dev/null +++ b/plugins/content/finder/services/provider.php @@ -0,0 +1,47 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\PluginInterface; +use Joomla\CMS\Factory; +use Joomla\CMS\Plugin\PluginHelper; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Plugin\Content\Finder\Extension\Finder; + +return new class () implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container): void + { + $container->set( + PluginInterface::class, + function (Container $container) { + $dispatcher = $container->get(DispatcherInterface::class); + $plugin = new Finder( + $dispatcher, + (array) PluginHelper::getPlugin('content', 'finder') + ); + $plugin->setApplication(Factory::getApplication()); + + return $plugin; + } + ); + } +}; diff --git a/plugins/content/finder/finder.php b/plugins/content/finder/src/Extension/Finder.php similarity index 85% rename from plugins/content/finder/finder.php rename to plugins/content/finder/src/Extension/Finder.php index 7e0b5ff24a767..463f925827c36 100644 --- a/plugins/content/finder/finder.php +++ b/plugins/content/finder/src/Extension/Finder.php @@ -6,11 +6,10 @@ * * @copyright (C) 2011 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt - - * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace */ -use Joomla\CMS\Factory; +namespace Joomla\Plugin\Content\Finder\Extension; + use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Plugin\PluginHelper; @@ -23,7 +22,7 @@ * * @since 2.5 */ -class PlgContentFinder extends CMSPlugin +final class Finder extends CMSPlugin { /** * Smart Search after save content method. @@ -43,7 +42,7 @@ public function onContentAfterSave($context, $article, $isNew): void PluginHelper::importPlugin('finder'); // Trigger the onFinderAfterSave event. - Factory::getApplication()->triggerEvent('onFinderAfterSave', [$context, $article, $isNew]); + $this->getApplication()->triggerEvent('onFinderAfterSave', [$context, $article, $isNew]); } /** @@ -63,7 +62,7 @@ public function onContentBeforeSave($context, $article, $isNew) PluginHelper::importPlugin('finder'); // Trigger the onFinderBeforeSave event. - Factory::getApplication()->triggerEvent('onFinderBeforeSave', [$context, $article, $isNew]); + $this->getApplication()->triggerEvent('onFinderBeforeSave', [$context, $article, $isNew]); } /** @@ -82,7 +81,7 @@ public function onContentAfterDelete($context, $article): void PluginHelper::importPlugin('finder'); // Trigger the onFinderAfterDelete event. - Factory::getApplication()->triggerEvent('onFinderAfterDelete', [$context, $article]); + $this->getApplication()->triggerEvent('onFinderAfterDelete', [$context, $article]); } /** @@ -104,7 +103,7 @@ public function onContentChangeState($context, $pks, $value) PluginHelper::importPlugin('finder'); // Trigger the onFinderChangeState event. - Factory::getApplication()->triggerEvent('onFinderChangeState', [$context, $pks, $value]); + $this->getApplication()->triggerEvent('onFinderChangeState', [$context, $pks, $value]); } /** @@ -125,6 +124,6 @@ public function onCategoryChangeState($extension, $pks, $value) PluginHelper::importPlugin('finder'); // Trigger the onFinderCategoryChangeState event. - Factory::getApplication()->triggerEvent('onFinderCategoryChangeState', [$extension, $pks, $value]); + $this->getApplication()->triggerEvent('onFinderCategoryChangeState', [$extension, $pks, $value]); } } diff --git a/plugins/content/joomla/joomla.xml b/plugins/content/joomla/joomla.xml index 272bdd471bd58..2e82d7d3fba5f 100644 --- a/plugins/content/joomla/joomla.xml +++ b/plugins/content/joomla/joomla.xml @@ -9,8 +9,10 @@ www.joomla.org 3.0.0 PLG_CONTENT_JOOMLA_XML_DESCRIPTION + Joomla\Plugin\Content\Joomla - joomla.php + services + src language/en-GB/plg_content_joomla.ini diff --git a/plugins/content/joomla/services/provider.php b/plugins/content/joomla/services/provider.php new file mode 100644 index 0000000000000..c85bce318799b --- /dev/null +++ b/plugins/content/joomla/services/provider.php @@ -0,0 +1,51 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\PluginInterface; +use Joomla\CMS\Factory; +use Joomla\CMS\Plugin\PluginHelper; +use Joomla\CMS\User\UserFactoryInterface; +use Joomla\Database\DatabaseInterface; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Plugin\Content\Joomla\Extension\Joomla; + +return new class () implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container): void + { + $container->set( + PluginInterface::class, + function (Container $container) { + $dispatcher = $container->get(DispatcherInterface::class); + $plugin = new Joomla( + $dispatcher, + (array) PluginHelper::getPlugin('content', 'joomla') + ); + $plugin->setApplication(Factory::getApplication()); + $plugin->setDatabase($container->get(DatabaseInterface::class)); + $plugin->setUserFactory($container->get(UserFactoryInterface::class)); + + return $plugin; + } + ); + } +}; diff --git a/plugins/content/joomla/joomla.php b/plugins/content/joomla/src/Extension/Joomla.php similarity index 82% rename from plugins/content/joomla/joomla.php rename to plugins/content/joomla/src/Extension/Joomla.php index ba4deb8f58e02..24c44f84419d6 100644 --- a/plugins/content/joomla/joomla.php +++ b/plugins/content/joomla/src/Extension/Joomla.php @@ -6,23 +6,24 @@ * * @copyright (C) 2010 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt - - * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace */ -use Joomla\CMS\Application\CMSApplicationInterface; +namespace Joomla\Plugin\Content\Joomla\Extension; + +use Exception; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Language\Language; use Joomla\CMS\Language\Text; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Table\CoreContent; -use Joomla\CMS\User\User; +use Joomla\CMS\User\UserFactoryAwareTrait; use Joomla\CMS\Workflow\WorkflowServiceInterface; use Joomla\Component\Workflow\Administrator\Table\StageTable; use Joomla\Component\Workflow\Administrator\Table\WorkflowTable; -use Joomla\Database\DatabaseDriver; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; use Joomla\Utilities\ArrayHelper; +use RuntimeException; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -33,23 +34,10 @@ * * @since 1.6 */ -class PlgContentJoomla extends CMSPlugin +final class Joomla extends CMSPlugin { - /** - * Application object - * - * @var CMSApplicationInterface - * @since 4.0.0 - */ - protected $app; - - /** - * Database Driver Instance - * - * @var DatabaseDriver - * @since 4.0.0 - */ - protected $db; + use DatabaseAwareTrait; + use UserFactoryAwareTrait; /** * The save event. @@ -83,10 +71,10 @@ public function onContentBeforeSave($context, $table, $isNew, $data) if ($item->$publishedField > 0 && isset($data[$publishedField]) && $data[$publishedField] < 1) { switch ($context) { case 'com_workflow.workflow': - return $this->_workflowNotUsed($item->id); + return $this->workflowNotUsed($item->id); case 'com_workflow.stage': - return $this->_stageNotUsed($item->id); + return $this->stageNotUsed($item->id); } } @@ -123,7 +111,7 @@ public function onContentAfterSave($context, $article, $isNew): void return; } - $db = $this->db; + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('id')) ->from($db->quoteName('#__users')) @@ -136,17 +124,17 @@ public function onContentAfterSave($context, $article, $isNew): void return; } - $user = $this->app->getIdentity(); + $user = $this->getApplication()->getIdentity(); // Messaging for new items $default_language = ComponentHelper::getParams('com_languages')->get('administrator'); - $debug = $this->app->get('debug_lang'); + $debug = $this->getApplication()->get('debug_lang'); foreach ($users as $user_id) { if ($user_id != $user->id) { // Load language for messaging - $receiver = User::getInstance($user_id); + $receiver = $this->getUserFactory()->loadUserById($user_id); $lang = Language::getInstance($receiver->getParam('admin_language', $default_language), $debug); $lang->load('com_content'); $message = [ @@ -154,7 +142,7 @@ public function onContentAfterSave($context, $article, $isNew): void 'subject' => $lang->_('COM_CONTENT_NEW_ARTICLE'), 'message' => sprintf($lang->_('COM_CONTENT_ON_NEW_CONTENT'), $user->get('name'), $article->title), ]; - $model_message = $this->app->bootComponent('com_messages')->getMVCFactory() + $model_message = $this->getApplication()->bootComponent('com_messages')->getMVCFactory() ->createModel('Message', 'Administrator'); $model_message->save($message); } @@ -180,13 +168,13 @@ public function onContentBeforeDelete($context, $data) switch ($context) { case 'com_categories.category': - return $this->_canDeleteCategories($data); + return $this->canDeleteCategories($data); case 'com_workflow.workflow': - return $this->_workflowNotUsed($data->id); + return $this->workflowNotUsed($data->id); case 'com_workflow.stage': - return $this->_stageNotUsed($data->id); + return $this->stageNotUsed($data->id); } } @@ -212,11 +200,11 @@ public function onContentBeforeChangeState($context, $pks, $value) foreach ($pks as $id) { switch ($context) { case 'com_workflow.workflow': - $result = $result && $this->_workflowNotUsed($id); + $result = $result && $this->workflowNotUsed($id); break; case 'com_workflow.stage': - $result = $result && $this->_stageNotUsed($id); + $result = $result && $this->stageNotUsed($id); break; } } @@ -231,14 +219,14 @@ public function onContentBeforeChangeState($context, $pks, $value) * * @return boolean */ - private function _canDeleteCategories($data) + private function canDeleteCategories($data) { // Check if this function is enabled. if (!$this->params->def('check_categories', 1)) { return true; } - $extension = $this->app->getInput()->getString('extension'); + $extension = $this->getApplication()->getInput()->getString('extension'); // Default to true if not a core extension $result = true; @@ -258,7 +246,7 @@ private function _canDeleteCategories($data) $table = $tableInfo[$extension]['table_name']; // See if this category has any content items - $count = $this->_countItemsInCategory($table, $data->get('id')); + $count = $this->countItemsInCategory($table, $data->get('id')); // Return false if db error if ($count === false) { @@ -268,20 +256,20 @@ private function _canDeleteCategories($data) if ($count > 0) { $msg = Text::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) . ' ' . Text::plural('COM_CATEGORIES_N_ITEMS_ASSIGNED', $count); - $this->app->enqueueMessage($msg, 'error'); + $this->getApplication()->enqueueMessage($msg, 'error'); $result = false; } // Check for items in any child categories (if it is a leaf, there are no child categories) if (!$data->isLeaf()) { - $count = $this->_countItemsInChildren($table, $data->get('id'), $data); + $count = $this->countItemsInChildren($table, $data->get('id'), $data); if ($count === false) { $result = false; } elseif ($count > 0) { $msg = Text::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) . ' ' . Text::plural('COM_CATEGORIES_HAS_SUBCATEGORY_ITEMS', $count); - $this->app->enqueueMessage($msg, 'error'); + $this->getApplication()->enqueueMessage($msg, 'error'); $result = false; } } @@ -300,10 +288,10 @@ private function _canDeleteCategories($data) * * @since 4.0.0 */ - private function _workflowNotUsed($pk) + private function workflowNotUsed($pk) { // Check if this workflow is the default stage - $table = new WorkflowTable($this->db); + $table = new WorkflowTable($this->getDatabase()); $table->load($pk); @@ -312,12 +300,12 @@ private function _workflowNotUsed($pk) } if ($table->default) { - throw new Exception(Text::_('COM_WORKFLOW_MSG_DELETE_IS_DEFAULT')); + throw new Exception($this->getApplication()->getLanguage()->_('COM_WORKFLOW_MSG_DELETE_IS_DEFAULT')); } $parts = explode('.', $table->extension); - $component = $this->app->bootComponent($parts[0]); + $component = $this->getApplication()->bootComponent($parts[0]); $section = ''; @@ -331,7 +319,7 @@ private function _workflowNotUsed($pk) } /** @var \Joomla\Component\Workflow\Administrator\Model\StagesModel $model */ - $model = $this->app->bootComponent('com_workflow')->getMVCFactory() + $model = $this->getApplication()->bootComponent('com_workflow')->getMVCFactory() ->createModel('Stages', 'Administrator', ['ignore_request' => true]); $model->setState('filter.workflow_id', $pk); @@ -341,11 +329,11 @@ private function _workflowNotUsed($pk) $stage_ids = array_column($stages, 'id'); - $result = $this->_countItemsInStage($stage_ids, $table->extension); + $result = $this->countItemsInStage($stage_ids, $table->extension); // Return false if db error if ($result > 0) { - throw new Exception(Text::_('COM_WORKFLOW_MSG_DELETE_WORKFLOW_IS_ASSIGNED')); + throw new Exception($this->getApplication()->getLanguage()->_('COM_WORKFLOW_MSG_DELETE_WORKFLOW_IS_ASSIGNED')); } return true; @@ -360,9 +348,9 @@ private function _workflowNotUsed($pk) * * @since 4.0.0 */ - private function _stageNotUsed($pk) + private function stageNotUsed($pk) { - $table = new StageTable($this->db); + $table = new StageTable($this->getDatabase()); $table->load($pk); @@ -372,10 +360,10 @@ private function _stageNotUsed($pk) // Check if this stage is the default stage if ($table->default) { - throw new Exception(Text::_('COM_WORKFLOW_MSG_DELETE_IS_DEFAULT')); + throw new Exception($this->getApplication()->getLanguage()->_('COM_WORKFLOW_MSG_DELETE_IS_DEFAULT')); } - $workflow = new WorkflowTable($this->db); + $workflow = new WorkflowTable($this->getDatabase()); $workflow->load($table->workflow_id); @@ -385,7 +373,7 @@ private function _stageNotUsed($pk) $parts = explode('.', $workflow->extension); - $component = $this->app->bootComponent($parts[0]); + $component = $this->getApplication()->bootComponent($parts[0]); // No core interface => we're ok if (!$component instanceof WorkflowServiceInterface) { @@ -394,11 +382,11 @@ private function _stageNotUsed($pk) $stage_ids = [$table->id]; - $result = $this->_countItemsInStage($stage_ids, $workflow->extension); + $result = $this->countItemsInStage($stage_ids, $workflow->extension); // Return false if db error if ($result > 0) { - throw new Exception(Text::_('COM_WORKFLOW_MSG_DELETE_STAGE_IS_ASSIGNED')); + throw new Exception($this->getApplication()->getLanguage()->_('COM_WORKFLOW_MSG_DELETE_STAGE_IS_ASSIGNED')); } return true; @@ -414,9 +402,9 @@ private function _stageNotUsed($pk) * * @since 1.6 */ - private function _countItemsInCategory($table, $catid) + private function countItemsInCategory($table, $catid) { - $db = $this->db; + $db = $this->getDatabase(); $query = $db->getQuery(true); // Count the items in this category @@ -429,7 +417,7 @@ private function _countItemsInCategory($table, $catid) try { $count = $db->loadResult(); } catch (RuntimeException $e) { - $this->app->enqueueMessage($e->getMessage(), 'error'); + $this->getApplication()->enqueueMessage($e->getMessage(), 'error'); return false; } @@ -447,9 +435,9 @@ private function _countItemsInCategory($table, $catid) * * @since 4.0.0 */ - private function _countItemsInStage(array $stageIds, string $extension): bool + private function countItemsInStage(array $stageIds, string $extension): bool { - $db = $this->db; + $db = $this->getDatabase(); $parts = explode('.', $extension); @@ -462,7 +450,7 @@ private function _countItemsInStage(array $stageIds, string $extension): bool $section = $parts[1]; } - $component = $this->app->bootComponent($parts[0]); + $component = $this->getApplication()->bootComponent($parts[0]); $table = $component->getWorkflowTableBySection($section); @@ -483,7 +471,7 @@ private function _countItemsInStage(array $stageIds, string $extension): bool try { return (int) $db->setQuery($query)->loadResult(); } catch (Exception $e) { - $this->app->enqueueMessage($e->getMessage(), 'error'); + $this->getApplication()->enqueueMessage($e->getMessage(), 'error'); } return false; @@ -500,9 +488,9 @@ private function _countItemsInStage(array $stageIds, string $extension): bool * * @since 1.6 */ - private function _countItemsInChildren($table, $catid, $data) + private function countItemsInChildren($table, $catid, $data) { - $db = $this->db; + $db = $this->getDatabase(); // Create subquery for list of child categories $childCategoryTree = $data->getTree(); @@ -527,7 +515,7 @@ private function _countItemsInChildren($table, $catid, $data) try { $count = $db->loadResult(); } catch (RuntimeException $e) { - $this->app->enqueueMessage($e->getMessage(), 'error'); + $this->getApplication()->enqueueMessage($e->getMessage(), 'error'); return false; } @@ -555,7 +543,7 @@ public function onContentChangeState($context, $pks, $value) if ($context === 'com_workflow.stage' && $value < 1) { foreach ($pks as $pk) { - if (!$this->_stageNotUsed($pk)) { + if (!$this->stageNotUsed($pk)) { return false; } } @@ -563,7 +551,7 @@ public function onContentChangeState($context, $pks, $value) return true; } - $db = $this->db; + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('core_content_id')) ->from($db->quoteName('#__ucm_content')) @@ -607,7 +595,7 @@ private function checkMenuItemBeforeSave($context, $table, $isNew, $data) $params = json_decode($table->params, true); if ($params['enable_category'] == 1 && empty($params['catid'])) { - $table->setError(Text::_('COM_CONTENT_CREATE_ARTICLE_ERROR')); + $table->setError($this->getApplication()->getLanguage()->_('COM_CONTENT_CREATE_ARTICLE_ERROR')); return false; } diff --git a/plugins/content/loadmodule/loadmodule.xml b/plugins/content/loadmodule/loadmodule.xml index c62080c283466..3eeb863e4a9a2 100644 --- a/plugins/content/loadmodule/loadmodule.xml +++ b/plugins/content/loadmodule/loadmodule.xml @@ -9,8 +9,10 @@ www.joomla.org 3.0.0 PLG_LOADMODULE_XML_DESCRIPTION + Joomla\Plugin\Content\LoadModule - loadmodule.php + services + src language/en-GB/plg_content_loadmodule.ini diff --git a/plugins/content/loadmodule/services/provider.php b/plugins/content/loadmodule/services/provider.php new file mode 100644 index 0000000000000..50dbb6eeecfba --- /dev/null +++ b/plugins/content/loadmodule/services/provider.php @@ -0,0 +1,47 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\PluginInterface; +use Joomla\CMS\Factory; +use Joomla\CMS\Plugin\PluginHelper; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Plugin\Content\LoadModule\Extension\LoadModule; + +return new class () implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container): void + { + $container->set( + PluginInterface::class, + function (Container $container) { + $dispatcher = $container->get(DispatcherInterface::class); + $plugin = new LoadModule( + $dispatcher, + (array) PluginHelper::getPlugin('content', 'loadmodule') + ); + $plugin->setApplication(Factory::getApplication()); + + return $plugin; + } + ); + } +}; diff --git a/plugins/content/loadmodule/loadmodule.php b/plugins/content/loadmodule/src/Extension/LoadModule.php similarity index 92% rename from plugins/content/loadmodule/loadmodule.php rename to plugins/content/loadmodule/src/Extension/LoadModule.php index ce5074f81902b..78c4a3845b7f2 100644 --- a/plugins/content/loadmodule/loadmodule.php +++ b/plugins/content/loadmodule/src/Extension/LoadModule.php @@ -6,11 +6,10 @@ * * @copyright (C) 2006 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt - - * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace */ -use Joomla\CMS\Factory; +namespace Joomla\Plugin\Content\LoadModule\Extension; + use Joomla\CMS\Helper\ModuleHelper; use Joomla\CMS\Plugin\CMSPlugin; @@ -24,7 +23,7 @@ * * @since 1.5 */ -class PlgContentLoadmodule extends CMSPlugin +final class LoadModule extends CMSPlugin { protected static $modules = []; @@ -88,7 +87,7 @@ public function onContentPrepare($context, &$article, &$params, $page = 0) $position = trim($matcheslist[0]); $style = trim($matcheslist[1]); - $output = $this->_load($position, $style); + $output = $this->load($position, $style); // We should replace only first occurrence in order to allow positions with the same name to regenerate their content: if (($start = strpos($article->text, $match[0])) !== false) { @@ -124,7 +123,7 @@ public function onContentPrepare($context, &$article, &$params, $page = 0) $stylemod = trim($matchesmodlist[2]); } - $output = $this->_loadmod($module, $title, $stylemod); + $output = $this->loadModule($module, $title, $stylemod); // We should replace only first occurrence in order to allow positions with the same name to regenerate their content: if (($start = strpos($article->text, $matchmod[0])) !== false) { @@ -142,7 +141,7 @@ public function onContentPrepare($context, &$article, &$params, $page = 0) if ($matchesmodid) { foreach ($matchesmodid as $match) { $id = trim($match[1]); - $output = $this->_loadid($id); + $output = $this->loadID($id); // We should replace only first occurrence in order to allow positions with the same name to regenerate their content: if (($start = strpos($article->text, $match[0])) !== false) { @@ -163,9 +162,9 @@ public function onContentPrepare($context, &$article, &$params, $page = 0) * * @since 1.6 */ - protected function _load($position, $style = 'none') + private function load($position, $style = 'none') { - $document = Factory::getDocument(); + $document = $this->getApplication()->getDocument(); $renderer = $document->loadRenderer('module'); $modules = ModuleHelper::getModules($position); $params = ['style' => $style]; @@ -190,9 +189,9 @@ protected function _load($position, $style = 'none') * * @since 1.6 */ - protected function _loadmod($module, $title, $style = 'none') + private function loadModule($module, $title, $style = 'none') { - $document = Factory::getDocument(); + $document = $this->getApplication()->getDocument(); $renderer = $document->loadRenderer('module'); $mod = ModuleHelper::getModule($module, $title); @@ -222,9 +221,9 @@ protected function _loadmod($module, $title, $style = 'none') * * @since 3.9.0 */ - protected function _loadid($id) + private function loadID($id) { - $document = Factory::getDocument(); + $document = $this->getApplication()->getDocument(); $renderer = $document->loadRenderer('module'); $modules = ModuleHelper::getModuleById($id); $params = ['style' => 'none']; diff --git a/plugins/content/pagebreak/pagebreak.xml b/plugins/content/pagebreak/pagebreak.xml index 3d91dff6c0f9e..c447ce17a7152 100644 --- a/plugins/content/pagebreak/pagebreak.xml +++ b/plugins/content/pagebreak/pagebreak.xml @@ -9,8 +9,11 @@ www.joomla.org 3.0.0 PLG_CONTENT_PAGEBREAK_XML_DESCRIPTION + Joomla\Plugin\Content\PageBreak - pagebreak.php + services + src + tmpl language/en-GB/plg_content_pagebreak.ini diff --git a/plugins/content/pagebreak/services/provider.php b/plugins/content/pagebreak/services/provider.php new file mode 100644 index 0000000000000..cb10f0e198ac3 --- /dev/null +++ b/plugins/content/pagebreak/services/provider.php @@ -0,0 +1,47 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\PluginInterface; +use Joomla\CMS\Factory; +use Joomla\CMS\Plugin\PluginHelper; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Plugin\Content\PageBreak\Extension\PageBreak; + +return new class () implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container): void + { + $container->set( + PluginInterface::class, + function (Container $container) { + $dispatcher = $container->get(DispatcherInterface::class); + $plugin = new PageBreak( + $dispatcher, + (array) PluginHelper::getPlugin('content', 'pagebreak') + ); + $plugin->setApplication(Factory::getApplication()); + + return $plugin; + } + ); + } +}; diff --git a/plugins/content/pagebreak/pagebreak.php b/plugins/content/pagebreak/src/Extension/PageBreak.php similarity index 91% rename from plugins/content/pagebreak/pagebreak.php rename to plugins/content/pagebreak/src/Extension/PageBreak.php index 6d4ec0c6b7b85..7794434d24db2 100644 --- a/plugins/content/pagebreak/pagebreak.php +++ b/plugins/content/pagebreak/src/Extension/PageBreak.php @@ -6,11 +6,11 @@ * * @copyright (C) 2006 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt - - * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace */ -use Joomla\CMS\Factory; +namespace Joomla\Plugin\Content\PageBreak\Extension; + +use Exception; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Pagination\Pagination; @@ -19,6 +19,7 @@ use Joomla\CMS\Utility\Utility; use Joomla\Component\Content\Site\Helper\RouteHelper; use Joomla\String\StringHelper; +use stdClass; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -39,7 +40,7 @@ * * @since 1.6 */ -class PlgContentPagebreak extends CMSPlugin +final class PageBreak extends CMSPlugin { /** * The navigation list with all page objects if parameter 'multipage_toc' is active. @@ -74,7 +75,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) // Expression to search for. $regex = '##iU'; - $input = Factory::getApplication()->getInput(); + $input = $this->getApplication()->getInput(); $print = $input->getBool('print'); $showall = $input->getBool('showall'); @@ -92,7 +93,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) // Simple performance check to determine whether bot should process further. if (StringHelper::strpos($row->text, 'class="system-pagebreak') === false) { if ($page > 0) { - throw new Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404); + throw new Exception($this->getApplication()->getLanguage()->_('JERROR_PAGE_NOT_FOUND'), 404); } return; @@ -124,7 +125,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) if ($hasToc) { // Display TOC. $page = 1; - $this->_createToc($row, $matches, $page); + $this->createToc($row, $matches, $page); } else { $row->toc = ''; } @@ -138,7 +139,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) $text = preg_split($regex, $row->text); if (!isset($text[$page])) { - throw new Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404); + throw new Exception($this->getApplication()->getLanguage()->_('JERROR_PAGE_NOT_FOUND'), 404); } // Count the number of pages. @@ -164,7 +165,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) if ($style === 'pages') { // Display TOC. if ($hasToc) { - $this->_createToc($row, $matches, $page); + $this->createToc($row, $matches, $page); } else { $row->toc = ''; } @@ -189,7 +190,7 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) // Adds navigation between pages to bottom of text. if ($hasToc) { - $this->_createNavigation($row, $page, $n); + $this->createNavigation($row, $page, $n); } // Page links shown at bottom of page if TOC disabled. @@ -260,16 +261,16 @@ public function onContentPrepare($context, &$row, &$params, $page = 0) * * @since 1.6 */ - protected function _createToc(&$row, &$matches, &$page) + private function createToc(&$row, &$matches, &$page) { - $heading = $row->title ?? Text::_('PLG_CONTENT_PAGEBREAK_NO_TITLE'); - $input = Factory::getApplication()->getInput(); + $heading = $row->title ?? $this->getApplication()->getLanguage()->_('PLG_CONTENT_PAGEBREAK_NO_TITLE'); + $input = $this->getApplication()->getInput(); $limitstart = $input->getUint('limitstart', 0); $showall = $input->getInt('showall', 0); $headingtext = ''; if ($this->params->get('article_index', 1) == 1) { - $headingtext = Text::_('PLG_CONTENT_PAGEBREAK_ARTICLE_INDEX'); + $headingtext = $this->getApplication()->getLanguage()->_('PLG_CONTENT_PAGEBREAK_ARTICLE_INDEX'); if ($this->params->get('article_index_text')) { $headingtext = htmlspecialchars($this->params->get('article_index_text'), ENT_QUOTES, 'UTF-8'); @@ -310,7 +311,7 @@ protected function _createToc(&$row, &$matches, &$page) if ($this->params->get('showall')) { $this->list[$i] = new stdClass(); $this->list[$i]->link = RouteHelper::getArticleRoute($row->slug, $row->catid, $row->language) . '&showall=1'; - $this->list[$i]->title = Text::_('PLG_CONTENT_PAGEBREAK_ALL_PAGES'); + $this->list[$i]->title = $this->getApplication()->getLanguage()->_('PLG_CONTENT_PAGEBREAK_ALL_PAGES'); $this->list[$i]->active = ($limitstart === $i - 1); } @@ -332,7 +333,7 @@ protected function _createToc(&$row, &$matches, &$page) * * @since 1.6 */ - protected function _createNavigation(&$row, $page, $n) + private function createNavigation(&$row, $page, $n) { $links = [ 'next' => '', diff --git a/plugins/content/pagebreak/tmpl/navigation.php b/plugins/content/pagebreak/tmpl/navigation.php index 7e7a7ab8836ad..19794c861cce8 100644 --- a/plugins/content/pagebreak/tmpl/navigation.php +++ b/plugins/content/pagebreak/tmpl/navigation.php @@ -10,7 +10,6 @@ defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Router\Route; @@ -19,7 +18,7 @@ * @var $page integer The page number */ -$lang = Factory::getLanguage(); +$lang = $this->getApplication()->getLanguage(); ?>