From f593ab782da0da17070e1292da8aa401654a82f9 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Wed, 14 Jun 2023 14:24:25 +0200 Subject: [PATCH] Introduce a document aware interface (#40662) * Introduce a document aware interface * cs --------- --- .../src/View/Association/HtmlView.php | 2 +- .../com_banners/src/View/Tracks/RawView.php | 2 +- .../src/View/Categories/HtmlView.php | 2 +- .../src/View/Category/HtmlView.php | 2 +- .../com_privacy/src/View/Export/XmlView.php | 4 +- .../src/View/Application/JsonapiView.php | 10 +-- .../src/View/Component/JsonapiView.php | 10 +-- .../src/View/Strings/JsonapiView.php | 4 +- .../com_menus/src/View/Items/JsonapiView.php | 4 +- .../src/View/Consents/JsonapiView.php | 6 +- .../src/View/Requests/JsonapiView.php | 6 +- .../com_config/src/View/Config/HtmlView.php | 4 +- .../src/View/Templates/HtmlView.php | 6 +- .../com_contact/src/View/Contact/HtmlView.php | 8 +- .../com_contact/src/View/Contact/VcfView.php | 2 +- .../src/View/Featured/HtmlView.php | 4 +- .../com_contact/src/View/Form/HtmlView.php | 6 +- .../com_content/src/View/Archive/HtmlView.php | 4 +- .../com_content/src/View/Article/HtmlView.php | 12 +-- .../src/View/Category/HtmlView.php | 10 +-- .../src/View/Featured/FeedView.php | 4 +- .../src/View/Featured/HtmlView.php | 12 +-- .../com_content/src/View/Form/HtmlView.php | 4 +- .../com_finder/src/View/Search/FeedView.php | 6 +- .../com_finder/src/View/Search/HtmlView.php | 16 ++-- .../src/View/Search/OpensearchView.php | 12 +-- .../src/View/Newsfeed/HtmlView.php | 10 +-- .../com_privacy/src/View/Confirm/HtmlView.php | 4 +- .../com_privacy/src/View/Remind/HtmlView.php | 4 +- .../com_privacy/src/View/Request/HtmlView.php | 4 +- components/com_tags/src/View/Tag/FeedView.php | 8 +- components/com_tags/src/View/Tag/HtmlView.php | 16 ++-- .../com_tags/src/View/Tags/FeedView.php | 10 +-- .../com_tags/src/View/Tags/HtmlView.php | 14 ++-- .../com_users/src/View/Login/HtmlView.php | 4 +- .../com_users/src/View/Profile/HtmlView.php | 4 +- .../src/View/Registration/HtmlView.php | 4 +- .../com_users/src/View/Remind/HtmlView.php | 4 +- .../com_users/src/View/Reset/HtmlView.php | 4 +- .../com_wrapper/src/View/Wrapper/HtmlView.php | 4 +- .../src/Document/DocumentAwareInterface.php | 33 +++++++++ libraries/src/Document/DocumentAwareTrait.php | 61 ++++++++++++++++ .../src/MVC/Controller/BaseController.php | 11 ++- libraries/src/MVC/View/AbstractView.php | 38 +++++++++- libraries/src/MVC/View/CategoriesView.php | 4 +- libraries/src/MVC/View/CategoryFeedView.php | 2 +- libraries/src/MVC/View/CategoryView.php | 12 +-- libraries/src/MVC/View/HtmlView.php | 2 +- libraries/src/MVC/View/JsonApiView.php | 16 ++-- libraries/src/MVC/View/JsonView.php | 2 +- .../Cms/Document/DocumentAwareTraitTest.php | 73 +++++++++++++++++++ 51 files changed, 361 insertions(+), 149 deletions(-) create mode 100644 libraries/src/Document/DocumentAwareInterface.php create mode 100644 libraries/src/Document/DocumentAwareTrait.php create mode 100644 tests/Unit/Libraries/Cms/Document/DocumentAwareTraitTest.php diff --git a/administrator/components/com_associations/src/View/Association/HtmlView.php b/administrator/components/com_associations/src/View/Association/HtmlView.php index 8b6409f76c6..105163a6135 100644 --- a/administrator/components/com_associations/src/View/Association/HtmlView.php +++ b/administrator/components/com_associations/src/View/Association/HtmlView.php @@ -318,7 +318,7 @@ class HtmlView extends BaseHtmlView * Let's put the target src into a variable to use in the javascript code * to avoid race conditions when the reference iframe loads. */ - $this->document->addScriptOptions('targetSrc', Route::_($this->editUri . '&task=' . $task . '&id=' . (int) $this->targetId)); + $this->getDocument()->addScriptOptions('targetSrc', Route::_($this->editUri . '&task=' . $task . '&id=' . (int) $this->targetId)); $this->form->setValue('itemlanguage', '', $this->targetLanguage . ':' . $this->targetId . ':' . $this->targetAction); } diff --git a/administrator/components/com_banners/src/View/Tracks/RawView.php b/administrator/components/com_banners/src/View/Tracks/RawView.php index dbafaabd131..55c26ebd477 100644 --- a/administrator/components/com_banners/src/View/Tracks/RawView.php +++ b/administrator/components/com_banners/src/View/Tracks/RawView.php @@ -53,7 +53,7 @@ class RawView extends BaseHtmlView throw new GenericDataException(implode("\n", $errors), 500); } - $this->document->setMimeEncoding($mimeType); + $this->getDocument()->setMimeEncoding($mimeType); /** @var CMSApplication $app */ $app = Factory::getApplication(); diff --git a/administrator/components/com_categories/src/View/Categories/HtmlView.php b/administrator/components/com_categories/src/View/Categories/HtmlView.php index 60bf6850239..41496204fb2 100644 --- a/administrator/components/com_categories/src/View/Categories/HtmlView.php +++ b/administrator/components/com_categories/src/View/Categories/HtmlView.php @@ -188,7 +188,7 @@ class HtmlView extends BaseHtmlView // Load specific css component /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */ - $wa = $this->document->getWebAssetManager(); + $wa = $this->getDocument()->getWebAssetManager(); $wa->getRegistry()->addExtensionRegistryFile($component); if ($wa->assetExists('style', $component . '.admin-categories')) { diff --git a/administrator/components/com_categories/src/View/Category/HtmlView.php b/administrator/components/com_categories/src/View/Category/HtmlView.php index d349deacd59..94714879ce3 100644 --- a/administrator/components/com_categories/src/View/Category/HtmlView.php +++ b/administrator/components/com_categories/src/View/Category/HtmlView.php @@ -173,7 +173,7 @@ class HtmlView extends BaseHtmlView // Load specific css component /** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */ - $wa = $this->document->getWebAssetManager(); + $wa = $this->getDocument()->getWebAssetManager(); $wa->getRegistry()->addExtensionRegistryFile($component); if ($wa->assetExists('style', $component . '.admin-categories')) { diff --git a/administrator/components/com_privacy/src/View/Export/XmlView.php b/administrator/components/com_privacy/src/View/Export/XmlView.php index 6378732e568..0e3d595303b 100644 --- a/administrator/components/com_privacy/src/View/Export/XmlView.php +++ b/administrator/components/com_privacy/src/View/Export/XmlView.php @@ -53,8 +53,8 @@ class XmlView extends AbstractView $requestId = $model->getState($model->getName() . '.request_id'); // This document should always be downloaded - $this->document->setDownload(true); - $this->document->setName('export-request-' . $requestId); + $this->getDocument()->setDownload(true); + $this->getDocument()->setName('export-request-' . $requestId); echo PrivacyHelper::renderDataAsXml($exportData); } diff --git a/api/components/com_config/src/View/Application/JsonapiView.php b/api/components/com_config/src/View/Application/JsonapiView.php index d4f09fac9f6..c9a762672d8 100644 --- a/api/components/com_config/src/View/Application/JsonapiView.php +++ b/api/components/com_config/src/View/Application/JsonapiView.php @@ -60,7 +60,7 @@ class JsonapiView extends BaseApiView $items = array_splice($items, $offset, $limit); - $this->document->addMeta('total-pages', $totalPagesAvailable) + $this->getDocument()->addMeta('total-pages', $totalPagesAvailable) ->addLink('self', (string) $currentUrl); // Check for first and previous pages @@ -76,7 +76,7 @@ class JsonapiView extends BaseApiView $previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0; $previousPage->setVar('page', $previousPageQuery); - $this->document->addLink('first', $this->queryEncode((string) $firstPage)) + $this->getDocument()->addLink('first', $this->queryEncode((string) $firstPage)) ->addLink('previous', $this->queryEncode((string) $previousPage)); } @@ -93,16 +93,16 @@ class JsonapiView extends BaseApiView $lastPageQuery['offset'] = ($totalPagesAvailable - 1) * $limit; $lastPage->setVar('page', $lastPageQuery); - $this->document->addLink('next', $this->queryEncode((string) $nextPage)) + $this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage)) ->addLink('last', $this->queryEncode((string) $lastPage)); } $collection = (new Collection($items, new JoomlaSerializer($this->type))); // Set the data into the document and render it - $this->document->setData($collection); + $this->getDocument()->setData($collection); - return $this->document->render(); + return $this->getDocument()->render(); } /** diff --git a/api/components/com_config/src/View/Component/JsonapiView.php b/api/components/com_config/src/View/Component/JsonapiView.php index 604a36517d3..0cf690523a6 100644 --- a/api/components/com_config/src/View/Component/JsonapiView.php +++ b/api/components/com_config/src/View/Component/JsonapiView.php @@ -72,7 +72,7 @@ class JsonapiView extends BaseApiView $items = array_splice($items, $offset, $limit); - $this->document->addMeta('total-pages', $totalPagesAvailable) + $this->getDocument()->addMeta('total-pages', $totalPagesAvailable) ->addLink('self', (string) $currentUrl); // Check for first and previous pages @@ -88,7 +88,7 @@ class JsonapiView extends BaseApiView $previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0; $previousPage->setVar('page', $previousPageQuery); - $this->document->addLink('first', $this->queryEncode((string) $firstPage)) + $this->getDocument()->addLink('first', $this->queryEncode((string) $firstPage)) ->addLink('previous', $this->queryEncode((string) $previousPage)); } @@ -105,16 +105,16 @@ class JsonapiView extends BaseApiView $lastPageQuery['offset'] = ($totalPagesAvailable - 1) * $limit; $lastPage->setVar('page', $lastPageQuery); - $this->document->addLink('next', $this->queryEncode((string) $nextPage)) + $this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage)) ->addLink('last', $this->queryEncode((string) $lastPage)); } $collection = (new Collection($items, new JoomlaSerializer($this->type))); // Set the data into the document and render it - $this->document->setData($collection); + $this->getDocument()->setData($collection); - return $this->document->render(); + return $this->getDocument()->render(); } /** diff --git a/api/components/com_languages/src/View/Strings/JsonapiView.php b/api/components/com_languages/src/View/Strings/JsonapiView.php index 9c14d17d7c5..e0302321d37 100644 --- a/api/components/com_languages/src/View/Strings/JsonapiView.php +++ b/api/components/com_languages/src/View/Strings/JsonapiView.php @@ -77,9 +77,9 @@ class JsonapiView extends BaseApiView ->fields([$this->type => $this->fieldsToRenderList]); // Set the data into the document and render it - $this->document->setData($collection); + $this->getDocument()->setData($collection); - return $this->document->render(); + return $this->getDocument()->render(); } /** diff --git a/api/components/com_menus/src/View/Items/JsonapiView.php b/api/components/com_menus/src/View/Items/JsonapiView.php index 3e641a48dda..acd6065da5f 100644 --- a/api/components/com_menus/src/View/Items/JsonapiView.php +++ b/api/components/com_menus/src/View/Items/JsonapiView.php @@ -178,7 +178,7 @@ class JsonapiView extends BaseApiView $collection = (new Collection($items, new JoomlaSerializer('menutypes'))); // Set the data into the document and render it - $this->document->addMeta('total-pages', $totalPagesAvailable) + $this->getDocument()->addMeta('total-pages', $totalPagesAvailable) ->setData($collection) ->addLink('self', (string) $currentUrl) ->addLink('first', (string) $firstPage) @@ -186,7 +186,7 @@ class JsonapiView extends BaseApiView ->addLink('previous', (string) $previousPage) ->addLink('last', (string) $lastPage); - return $this->document->render(); + return $this->getDocument()->render(); } /** diff --git a/api/components/com_privacy/src/View/Consents/JsonapiView.php b/api/components/com_privacy/src/View/Consents/JsonapiView.php index e1ef4308e68..3f80696da0e 100644 --- a/api/components/com_privacy/src/View/Consents/JsonapiView.php +++ b/api/components/com_privacy/src/View/Consents/JsonapiView.php @@ -112,9 +112,9 @@ class JsonapiView extends BaseApiView $element = (new Resource($displayItem, $serializer)) ->fields([$this->type => $this->fieldsToRenderItem]); - $this->document->setData($element); - $this->document->addLink('self', Uri::current()); + $this->getDocument()->setData($element); + $this->getDocument()->addLink('self', Uri::current()); - return $this->document->render(); + return $this->getDocument()->render(); } } diff --git a/api/components/com_privacy/src/View/Requests/JsonapiView.php b/api/components/com_privacy/src/View/Requests/JsonapiView.php index ff91b6bab6d..d4b8caf6839 100644 --- a/api/components/com_privacy/src/View/Requests/JsonapiView.php +++ b/api/components/com_privacy/src/View/Requests/JsonapiView.php @@ -65,9 +65,9 @@ class JsonapiView extends BaseApiView $serializer = new JoomlaSerializer('export'); $element = (new Resource($exportData, $serializer)); - $this->document->setData($element); - $this->document->addLink('self', Uri::current()); + $this->getDocument()->setData($element); + $this->getDocument()->addLink('self', Uri::current()); - return $this->document->render(); + return $this->getDocument()->render(); } } diff --git a/components/com_config/src/View/Config/HtmlView.php b/components/com_config/src/View/Config/HtmlView.php index c4cbeed183a..adbb16beef0 100644 --- a/components/com_config/src/View/Config/HtmlView.php +++ b/components/com_config/src/View/Config/HtmlView.php @@ -121,11 +121,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($params->get('page_title', '')); if ($params->get('menu-meta_description')) { - $this->document->setDescription($params->get('menu-meta_description')); + $this->getDocument()->setDescription($params->get('menu-meta_description')); } if ($params->get('robots')) { - $this->document->setMetaData('robots', $params->get('robots')); + $this->getDocument()->setMetaData('robots', $params->get('robots')); } // Escape strings for HTML output diff --git a/components/com_config/src/View/Templates/HtmlView.php b/components/com_config/src/View/Templates/HtmlView.php index 42fa606f653..384f875469a 100644 --- a/components/com_config/src/View/Templates/HtmlView.php +++ b/components/com_config/src/View/Templates/HtmlView.php @@ -94,7 +94,7 @@ class HtmlView extends BaseHtmlView $view = $factory->createView('Style', 'Administrator', 'Json'); $view->setModel($factory->createModel('Style', 'Administrator'), true); - $view->document = $this->document; + $view->document = $this->getDocument(); $json = $view->display(); @@ -141,11 +141,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($params->get('page_title', '')); if ($params->get('menu-meta_description')) { - $this->document->setDescription($params->get('menu-meta_description')); + $this->getDocument()->setDescription($params->get('menu-meta_description')); } if ($params->get('robots')) { - $this->document->setMetaData('robots', $params->get('robots')); + $this->getDocument()->setMetaData('robots', $params->get('robots')); } // Escape strings for HTML output diff --git a/components/com_contact/src/View/Contact/HtmlView.php b/components/com_contact/src/View/Contact/HtmlView.php index 17c55854414..762130d70f5 100644 --- a/components/com_contact/src/View/Contact/HtmlView.php +++ b/components/com_contact/src/View/Contact/HtmlView.php @@ -447,20 +447,20 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($title); if ($this->item->metadesc) { - $this->document->setDescription($this->item->metadesc); + $this->getDocument()->setDescription($this->item->metadesc); } elseif ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } $mdata = $this->item->metadata->toArray(); foreach ($mdata as $k => $v) { if ($v) { - $this->document->setMetaData($k, $v); + $this->getDocument()->setMetaData($k, $v); } } } diff --git a/components/com_contact/src/View/Contact/VcfView.php b/components/com_contact/src/View/Contact/VcfView.php index 45312326317..40821068062 100644 --- a/components/com_contact/src/View/Contact/VcfView.php +++ b/components/com_contact/src/View/Contact/VcfView.php @@ -51,7 +51,7 @@ class VcfView extends AbstractView throw new GenericDataException(implode("\n", $errors), 500); } - $this->document->setMimeEncoding('text/directory', true); + $this->getDocument()->setMimeEncoding('text/directory', true); // Compute lastname, firstname and middlename $item->name = trim($item->name); diff --git a/components/com_contact/src/View/Featured/HtmlView.php b/components/com_contact/src/View/Featured/HtmlView.php index 86afcf5a325..51ef02becb4 100644 --- a/components/com_contact/src/View/Featured/HtmlView.php +++ b/components/com_contact/src/View/Featured/HtmlView.php @@ -163,11 +163,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_contact/src/View/Form/HtmlView.php b/components/com_contact/src/View/Form/HtmlView.php index 89b86683ed6..b6473ea8c73 100644 --- a/components/com_contact/src/View/Form/HtmlView.php +++ b/components/com_contact/src/View/Form/HtmlView.php @@ -164,15 +164,15 @@ class HtmlView extends BaseHtmlView $pathway->addItem($title, ''); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('menu-meta_keywords')) { - $this->document->setMetaData('keywords', $this->params->get('menu-meta_keywords')); + $this->getDocument()->setMetaData('keywords', $this->params->get('menu-meta_keywords')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_content/src/View/Archive/HtmlView.php b/components/com_content/src/View/Archive/HtmlView.php index 07b2903317a..b30110dd506 100644 --- a/components/com_content/src/View/Archive/HtmlView.php +++ b/components/com_content/src/View/Archive/HtmlView.php @@ -245,11 +245,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_content/src/View/Article/HtmlView.php b/components/com_content/src/View/Article/HtmlView.php index 2f5d9332085..50f42ecf977 100644 --- a/components/com_content/src/View/Article/HtmlView.php +++ b/components/com_content/src/View/Article/HtmlView.php @@ -310,25 +310,25 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($title); if ($this->item->metadesc) { - $this->document->setDescription($this->item->metadesc); + $this->getDocument()->setDescription($this->item->metadesc); } elseif ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } if ($app->get('MetaAuthor') == '1') { $author = $this->item->created_by_alias ?: $this->item->author; - $this->document->setMetaData('author', $author); + $this->getDocument()->setMetaData('author', $author); } $mdata = $this->item->metadata->toArray(); foreach ($mdata as $k => $v) { if ($v) { - $this->document->setMetaData($k, $v); + $this->getDocument()->setMetaData($k, $v); } } @@ -341,7 +341,7 @@ class HtmlView extends BaseHtmlView } if ($this->print) { - $this->document->setMetaData('robots', 'noindex, nofollow'); + $this->getDocument()->setMetaData('robots', 'noindex, nofollow'); } } } diff --git a/components/com_content/src/View/Category/HtmlView.php b/components/com_content/src/View/Category/HtmlView.php index 6183348601d..3fb4c8779b6 100644 --- a/components/com_content/src/View/Category/HtmlView.php +++ b/components/com_content/src/View/Category/HtmlView.php @@ -154,13 +154,13 @@ class HtmlView extends CategoryView $this->setDocumentTitle($title); if ($this->category->metadesc) { - $this->document->setDescription($this->category->metadesc); + $this->getDocument()->setDescription($this->category->metadesc); } elseif ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } if (!is_object($this->category->metadata)) { @@ -168,14 +168,14 @@ class HtmlView extends CategoryView } if (($app->get('MetaAuthor') == '1') && $this->category->get('author', '')) { - $this->document->setMetaData('author', $this->category->get('author', '')); + $this->getDocument()->setMetaData('author', $this->category->get('author', '')); } $mdata = $this->category->metadata->toArray(); foreach ($mdata as $k => $v) { if ($v) { - $this->document->setMetaData($k, $v); + $this->getDocument()->setMetaData($k, $v); } } diff --git a/components/com_content/src/View/Featured/FeedView.php b/components/com_content/src/View/Featured/FeedView.php index 1e260f2169a..9f8e7a38b6d 100644 --- a/components/com_content/src/View/Featured/FeedView.php +++ b/components/com_content/src/View/Featured/FeedView.php @@ -45,7 +45,7 @@ class FeedView extends AbstractView $feedEmail = $app->get('feed_email', 'none'); $siteEmail = $app->get('mailfrom'); - $this->document->link = Route::_('index.php?option=com_content&view=featured'); + $this->getDocument()->link = Route::_('index.php?option=com_content&view=featured'); // Get some data from the model $app->getInput()->set('limit', $app->get('feed_limit')); @@ -109,7 +109,7 @@ class FeedView extends AbstractView $item->description = '
' . $description . '
'; // Loads item info into rss array - $this->document->addItem($item); + $this->getDocument()->addItem($item); } } } diff --git a/components/com_content/src/View/Featured/HtmlView.php b/components/com_content/src/View/Featured/HtmlView.php index d034c38ac5f..50a850425e3 100644 --- a/components/com_content/src/View/Featured/HtmlView.php +++ b/components/com_content/src/View/Featured/HtmlView.php @@ -229,20 +229,20 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } // Add feed links if ($this->params->get('show_feed_link', 1)) { $link = '&format=feed&limitstart='; - $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); - $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); } } } diff --git a/components/com_content/src/View/Form/HtmlView.php b/components/com_content/src/View/Form/HtmlView.php index 2e5f08a6733..9addcf6f41e 100644 --- a/components/com_content/src/View/Form/HtmlView.php +++ b/components/com_content/src/View/Form/HtmlView.php @@ -223,11 +223,11 @@ class HtmlView extends BaseHtmlView $app->getPathway()->addItem($title); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_finder/src/View/Search/FeedView.php b/components/com_finder/src/View/Search/FeedView.php index fa891ae860e..b0a0959ccf3 100644 --- a/components/com_finder/src/View/Search/FeedView.php +++ b/components/com_finder/src/View/Search/FeedView.php @@ -59,11 +59,11 @@ class FeedView extends BaseHtmlView // Configure the document description. if (!empty($explained)) { - $this->document->setDescription(html_entity_decode(strip_tags($explained), ENT_QUOTES, 'UTF-8')); + $this->getDocument()->setDescription(html_entity_decode(strip_tags($explained), ENT_QUOTES, 'UTF-8')); } // Set the document link. - $this->document->link = Route::_($query->toUri()); + $this->getDocument()->link = Route::_($query->toUri()); // If we don't have any results, we are done. if (empty($results)) { @@ -82,7 +82,7 @@ class FeedView extends BaseHtmlView $item->date = (int) $result->start_date ? HTMLHelper::_('date', $result->start_date, 'U') : $result->indexdate; // Loads item info into RSS array - $this->document->addItem($item); + $this->getDocument()->addItem($item); } } } diff --git a/components/com_finder/src/View/Search/HtmlView.php b/components/com_finder/src/View/Search/HtmlView.php index aba0c9f0025..eacf584afaf 100644 --- a/components/com_finder/src/View/Search/HtmlView.php +++ b/components/com_finder/src/View/Search/HtmlView.php @@ -295,13 +295,13 @@ class HtmlView extends BaseHtmlView implements SiteRouterAwareInterface // Configure the document meta-description. if (!empty($this->explained)) { $explained = $this->escape(html_entity_decode(strip_tags($this->explained), ENT_QUOTES, 'UTF-8')); - $this->document->setDescription($explained); + $this->getDocument()->setDescription($explained); } elseif ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } // Check for OpenSearch @@ -310,7 +310,7 @@ class HtmlView extends BaseHtmlView implements SiteRouterAwareInterface 'opensearch_name', Text::_('COM_FINDER_OPENSEARCH_NAME') . ' ' . $app->get('sitename') ); - $this->document->addHeadLink( + $this->getDocument()->addHeadLink( Uri::getInstance()->toString(['scheme', 'host', 'port']) . Route::_('index.php?option=com_finder&view=search&format=opensearch'), 'search', 'rel', @@ -321,14 +321,14 @@ class HtmlView extends BaseHtmlView implements SiteRouterAwareInterface // Add feed link to the document head. if ($this->params->get('show_feed_link', 1) == 1) { // Add the RSS link. - $props = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->document->getTitle())]; + $props = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; $route = Route::_($this->query->toUri() . '&format=feed&type=rss'); - $this->document->addHeadLink($route, 'alternate', 'rel', $props); + $this->getDocument()->addHeadLink($route, 'alternate', 'rel', $props); // Add the ATOM link. - $props = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->document->getTitle())]; + $props = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; $route = Route::_($this->query->toUri() . '&format=feed&type=atom'); - $this->document->addHeadLink($route, 'alternate', 'rel', $props); + $this->getDocument()->addHeadLink($route, 'alternate', 'rel', $props); } } } diff --git a/components/com_finder/src/View/Search/OpensearchView.php b/components/com_finder/src/View/Search/OpensearchView.php index 795a85e2ef3..d690bf11224 100644 --- a/components/com_finder/src/View/Search/OpensearchView.php +++ b/components/com_finder/src/View/Search/OpensearchView.php @@ -42,8 +42,8 @@ class OpensearchView extends AbstractView $app = Factory::getApplication(); $params = ComponentHelper::getParams('com_finder'); - $this->document->setShortName($params->get('opensearch_name', $app->get('sitename', ''))); - $this->document->setDescription($params->get('opensearch_description', $app->get('MetaDesc', ''))); + $this->getDocument()->setShortName($params->get('opensearch_name', $app->get('sitename', ''))); + $this->getDocument()->setDescription($params->get('opensearch_description', $app->get('MetaDesc', ''))); // Prevent any output when OpenSearch Support is disabled if (!$params->get('opensearch', 1)) { @@ -64,26 +64,26 @@ class OpensearchView extends AbstractView // Add the HTML result view $htmlSearch = new OpensearchUrl(); $htmlSearch->template = $baseUrl . Route::_($searchUri, false); - $this->document->addUrl($htmlSearch); + $this->getDocument()->addUrl($htmlSearch); // Add the RSS result view $htmlSearch = new OpensearchUrl(); $htmlSearch->template = $baseUrl . Route::_($searchUri . '&format=feed&type=rss', false); $htmlSearch->type = 'application/rss+xml'; - $this->document->addUrl($htmlSearch); + $this->getDocument()->addUrl($htmlSearch); // Add the Atom result view $htmlSearch = new OpensearchUrl(); $htmlSearch->template = $baseUrl . Route::_($searchUri . '&format=feed&type=atom', false); $htmlSearch->type = 'application/atom+xml'; - $this->document->addUrl($htmlSearch); + $this->getDocument()->addUrl($htmlSearch); // Add suggestions URL if ($params->get('show_autosuggest', 1)) { $htmlSearch = new OpensearchUrl(); $htmlSearch->template = $baseUrl . Route::_($suggestionsUri, false); $htmlSearch->type = 'application/x-suggestions+json'; - $this->document->addUrl($htmlSearch); + $this->getDocument()->addUrl($htmlSearch); } } } diff --git a/components/com_newsfeeds/src/View/Newsfeed/HtmlView.php b/components/com_newsfeeds/src/View/Newsfeed/HtmlView.php index e3f4dd187b8..e02e4a038db 100644 --- a/components/com_newsfeeds/src/View/Newsfeed/HtmlView.php +++ b/components/com_newsfeeds/src/View/Newsfeed/HtmlView.php @@ -281,24 +281,24 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($title); if ($this->item->metadesc) { - $this->document->setDescription($this->item->metadesc); + $this->getDocument()->setDescription($this->item->metadesc); } elseif ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } if ($app->get('MetaAuthor') == '1') { - $this->document->setMetaData('author', $this->item->author); + $this->getDocument()->setMetaData('author', $this->item->author); } $mdata = $this->item->metadata->toArray(); foreach ($mdata as $k => $v) { if ($v) { - $this->document->setMetaData($k, $v); + $this->getDocument()->setMetaData($k, $v); } } } diff --git a/components/com_privacy/src/View/Confirm/HtmlView.php b/components/com_privacy/src/View/Confirm/HtmlView.php index 636d8e3348f..bea3a5084a8 100644 --- a/components/com_privacy/src/View/Confirm/HtmlView.php +++ b/components/com_privacy/src/View/Confirm/HtmlView.php @@ -114,11 +114,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_privacy/src/View/Remind/HtmlView.php b/components/com_privacy/src/View/Remind/HtmlView.php index 9428da823a4..9d1dca99479 100644 --- a/components/com_privacy/src/View/Remind/HtmlView.php +++ b/components/com_privacy/src/View/Remind/HtmlView.php @@ -114,11 +114,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_privacy/src/View/Request/HtmlView.php b/components/com_privacy/src/View/Request/HtmlView.php index ade63031228..8ac66d71264 100644 --- a/components/com_privacy/src/View/Request/HtmlView.php +++ b/components/com_privacy/src/View/Request/HtmlView.php @@ -123,11 +123,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_tags/src/View/Tag/FeedView.php b/components/com_tags/src/View/Tag/FeedView.php index 89a4042fc7f..739a1aeeb59 100644 --- a/components/com_tags/src/View/Tag/FeedView.php +++ b/components/com_tags/src/View/Tag/FeedView.php @@ -53,17 +53,17 @@ class FeedView extends BaseHtmlView $i++; } - $this->document->link = Route::_('index.php?option=com_tags&view=tag&' . $tagIds); + $this->getDocument()->link = Route::_('index.php?option=com_tags&view=tag&' . $tagIds); $app->getInput()->set('limit', $app->get('feed_limit')); $siteEmail = $app->get('mailfrom'); $fromName = $app->get('fromname'); $feedEmail = $app->get('feed_email', 'none'); - $this->document->editor = $fromName; + $this->getDocument()->editor = $fromName; if ($feedEmail !== 'none') { - $this->document->editorEmail = $siteEmail; + $this->getDocument()->editorEmail = $siteEmail; } // Get some data from the model @@ -96,7 +96,7 @@ class FeedView extends BaseHtmlView } // Loads item info into RSS array - $this->document->addItem($feeditem); + $this->getDocument()->addItem($feeditem); } } } diff --git a/components/com_tags/src/View/Tag/HtmlView.php b/components/com_tags/src/View/Tag/HtmlView.php index 444ba85a832..27f12a9cfe9 100644 --- a/components/com_tags/src/View/Tag/HtmlView.php +++ b/components/com_tags/src/View/Tag/HtmlView.php @@ -279,30 +279,30 @@ class HtmlView extends BaseHtmlView foreach ($this->item as $itemElement) { if ($itemElement->metadesc) { - $this->document->setDescription($itemElement->metadesc); + $this->getDocument()->setDescription($itemElement->metadesc); } elseif ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } if (count($this->item) === 1) { foreach ($this->item[0]->metadata->toArray() as $k => $v) { if ($v) { - $this->document->setMetaData($k, $v); + $this->getDocument()->setMetaData($k, $v); } } } if ($this->params->get('show_feed_link', 1) == 1) { $link = '&format=feed&limitstart='; - $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); - $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); } } diff --git a/components/com_tags/src/View/Tags/FeedView.php b/components/com_tags/src/View/Tags/FeedView.php index 33105c288e8..9035bdad85c 100644 --- a/components/com_tags/src/View/Tags/FeedView.php +++ b/components/com_tags/src/View/Tags/FeedView.php @@ -35,18 +35,18 @@ class FeedView extends BaseHtmlView */ public function display($tpl = null) { - $app = Factory::getApplication(); - $this->document->link = Route::_('index.php?option=com_tags&view=tags'); + $app = Factory::getApplication(); + $this->getDocument()->link = Route::_('index.php?option=com_tags&view=tags'); $app->getInput()->set('limit', $app->get('feed_limit')); $siteEmail = $app->get('mailfrom'); $fromName = $app->get('fromname'); $feedEmail = $app->get('feed_email', 'none'); - $this->document->editor = $fromName; + $this->getDocument()->editor = $fromName; if ($feedEmail !== 'none') { - $this->document->editorEmail = $siteEmail; + $this->getDocument()->editorEmail = $siteEmail; } // Get some data from the model @@ -80,7 +80,7 @@ class FeedView extends BaseHtmlView } // Loads item info into RSS array - $this->document->addItem($feeditem); + $this->getDocument()->addItem($feeditem); } } } diff --git a/components/com_tags/src/View/Tags/HtmlView.php b/components/com_tags/src/View/Tags/HtmlView.php index 7fada1b94dd..694a77978bc 100644 --- a/components/com_tags/src/View/Tags/HtmlView.php +++ b/components/com_tags/src/View/Tags/HtmlView.php @@ -151,23 +151,23 @@ class HtmlView extends BaseHtmlView // Set metadata for all tags menu item if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } // Respect configuration Sitename Before/After for TITLE in views All Tags. - $this->setDocumentTitle($this->document->getTitle()); + $this->setDocumentTitle($this->getDocument()->getTitle()); // Add alternative feed link if ($this->params->get('show_feed_link', 1) == 1) { $link = '&format=feed&limitstart='; - $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); - $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); } } } diff --git a/components/com_users/src/View/Login/HtmlView.php b/components/com_users/src/View/Login/HtmlView.php index f38e7bf104a..a0ee07b61d3 100644 --- a/components/com_users/src/View/Login/HtmlView.php +++ b/components/com_users/src/View/Login/HtmlView.php @@ -149,11 +149,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_users/src/View/Profile/HtmlView.php b/components/com_users/src/View/Profile/HtmlView.php index 64d6151b6c5..3d73ee6ebcb 100644 --- a/components/com_users/src/View/Profile/HtmlView.php +++ b/components/com_users/src/View/Profile/HtmlView.php @@ -179,11 +179,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_users/src/View/Registration/HtmlView.php b/components/com_users/src/View/Registration/HtmlView.php index 08840947af8..e1e0c40de21 100644 --- a/components/com_users/src/View/Registration/HtmlView.php +++ b/components/com_users/src/View/Registration/HtmlView.php @@ -151,11 +151,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_users/src/View/Remind/HtmlView.php b/components/com_users/src/View/Remind/HtmlView.php index e35f15092a5..32b4e80bb5f 100644 --- a/components/com_users/src/View/Remind/HtmlView.php +++ b/components/com_users/src/View/Remind/HtmlView.php @@ -116,11 +116,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_users/src/View/Reset/HtmlView.php b/components/com_users/src/View/Reset/HtmlView.php index 16675307b7d..15409693417 100644 --- a/components/com_users/src/View/Reset/HtmlView.php +++ b/components/com_users/src/View/Reset/HtmlView.php @@ -122,11 +122,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/components/com_wrapper/src/View/Wrapper/HtmlView.php b/components/com_wrapper/src/View/Wrapper/HtmlView.php index 4af7c5d0840..8b1da760f87 100644 --- a/components/com_wrapper/src/View/Wrapper/HtmlView.php +++ b/components/com_wrapper/src/View/Wrapper/HtmlView.php @@ -68,11 +68,11 @@ class HtmlView extends BaseHtmlView $this->setDocumentTitle($params->get('page_title', '')); if ($params->get('menu-meta_description')) { - $this->document->setDescription($params->get('menu-meta_description')); + $this->getDocument()->setDescription($params->get('menu-meta_description')); } if ($params->get('robots')) { - $this->document->setMetaData('robots', $params->get('robots')); + $this->getDocument()->setMetaData('robots', $params->get('robots')); } $wrapper = new \stdClass(); diff --git a/libraries/src/Document/DocumentAwareInterface.php b/libraries/src/Document/DocumentAwareInterface.php new file mode 100644 index 00000000000..043cc35d2f3 --- /dev/null +++ b/libraries/src/Document/DocumentAwareInterface.php @@ -0,0 +1,33 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\CMS\Document; + +// phpcs:disable PSR1.Files.SideEffects +\defined('_JEXEC') or die; +// phpcs:enable PSR1.Files.SideEffects + +/** + * Interface to be implemented by classes depending on a document. + * + * @since __DEPLOY_VERSION__ + */ +interface DocumentAwareInterface +{ + /** + * Set the document to use. + * + * @param Document $document The document to use. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function setDocument(Document $document): void; +} diff --git a/libraries/src/Document/DocumentAwareTrait.php b/libraries/src/Document/DocumentAwareTrait.php new file mode 100644 index 00000000000..8b6effb264b --- /dev/null +++ b/libraries/src/Document/DocumentAwareTrait.php @@ -0,0 +1,61 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\CMS\Document; + +// phpcs:disable PSR1.Files.SideEffects +\defined('_JEXEC') or die; +// phpcs:enable PSR1.Files.SideEffects + +/** + * Defines the trait for a document aware class. + * + * @since __DEPLOY_VERSION__ + */ +trait DocumentAwareTrait +{ + /** + * Document + * + * @var Document + * @since __DEPLOY_VERSION__ + */ + private $document; + + /** + * Get the Document. + * + * @return Document + * + * @since __DEPLOY_VERSION__ + * @throws \UnexpectedValueException May be thrown if the document has not been set. + */ + protected function getDocument(): Document + { + if ($this->document) { + return $this->document; + } + + throw new \UnexpectedValueException('Document not set in ' . __CLASS__); + } + + /** + * Set the document to use. + * + * @param Document $document The document to use + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function setDocument(Document $document): void + { + $this->document = $document; + } +} diff --git a/libraries/src/MVC/Controller/BaseController.php b/libraries/src/MVC/Controller/BaseController.php index b6276722eba..95a87b02c69 100644 --- a/libraries/src/MVC/Controller/BaseController.php +++ b/libraries/src/MVC/Controller/BaseController.php @@ -11,6 +11,7 @@ namespace Joomla\CMS\MVC\Controller; use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; +use Joomla\CMS\Document\DocumentAwareInterface; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Filter\InputFilter; @@ -613,7 +614,15 @@ class BaseController implements ControllerInterface, DispatcherAwareInterface $view->setModel($model, true); } - $view->document = $document; + if ($view instanceof DocumentAwareInterface && $document) { + $view->setDocument($this->app->getDocument()); + } else { + @trigger_error( + 'View should implement document aware interface.', + E_USER_DEPRECATED + ); + $view->document = $document; + } // Display the view if ($cachable && $viewType !== 'feed' && $this->app->get('caching') >= 1) { diff --git a/libraries/src/MVC/View/AbstractView.php b/libraries/src/MVC/View/AbstractView.php index 70bd24f83f1..0b7e1d605f5 100644 --- a/libraries/src/MVC/View/AbstractView.php +++ b/libraries/src/MVC/View/AbstractView.php @@ -10,6 +10,8 @@ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Document\Document; +use Joomla\CMS\Document\DocumentAwareInterface; +use Joomla\CMS\Document\DocumentAwareTrait; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Model\BaseDatabaseModel; @@ -30,7 +32,7 @@ use Joomla\Event\EventInterface; * * @since 2.5.5 */ -abstract class AbstractView extends CMSObject implements ViewInterface, DispatcherAwareInterface +abstract class AbstractView extends CMSObject implements ViewInterface, DispatcherAwareInterface, DocumentAwareInterface { use DispatcherAwareTrait; @@ -39,6 +41,9 @@ abstract class AbstractView extends CMSObject implements ViewInterface, Dispatch * * @var Document * @since 3.0 + * + * @deprecated __DEPLOY_VERSION__ will be removed in 6.0 + * Use $this->getDocument() instead */ public $document; @@ -234,6 +239,37 @@ abstract class AbstractView extends CMSObject implements ViewInterface, Dispatch return $this->_name; } + /** + * Get the Document. + * + * @return Document + * + * @since __DEPLOY_VERSION__ + * @throws \UnexpectedValueException May be thrown if the document has not been set. + */ + protected function getDocument(): Document + { + if ($this->document) { + return $this->document; + } + + throw new \UnexpectedValueException('Document not set in ' . __CLASS__); + } + + /** + * Set the document to use. + * + * @param Document $document The document to use + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function setDocument(Document $document): void + { + $this->document = $document; + } + /** * Dispatches the given event on the internal dispatcher, does a fallback to the global one. * diff --git a/libraries/src/MVC/View/CategoriesView.php b/libraries/src/MVC/View/CategoriesView.php index 0c771bb23f7..d2c378c568d 100644 --- a/libraries/src/MVC/View/CategoriesView.php +++ b/libraries/src/MVC/View/CategoriesView.php @@ -122,11 +122,11 @@ class CategoriesView extends HtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } diff --git a/libraries/src/MVC/View/CategoryFeedView.php b/libraries/src/MVC/View/CategoryFeedView.php index a95f2e0492e..91c6058c087 100644 --- a/libraries/src/MVC/View/CategoryFeedView.php +++ b/libraries/src/MVC/View/CategoryFeedView.php @@ -40,7 +40,7 @@ class CategoryFeedView extends HtmlView public function display($tpl = null) { $app = Factory::getApplication(); - $document = Factory::getDocument(); + $document = $this->getDocument(); $extension = $app->getInput()->getString('option'); $contentType = $extension . '.' . $this->viewName; diff --git a/libraries/src/MVC/View/CategoryView.php b/libraries/src/MVC/View/CategoryView.php index 5945b7a61d3..947393d1384 100644 --- a/libraries/src/MVC/View/CategoryView.php +++ b/libraries/src/MVC/View/CategoryView.php @@ -286,11 +286,11 @@ class CategoryView extends HtmlView $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { - $this->document->setDescription($this->params->get('menu-meta_description')); + $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { - $this->document->setMetaData('robots', $this->params->get('robots')); + $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } @@ -305,10 +305,10 @@ class CategoryView extends HtmlView { if ($this->params->get('show_feed_link', 1) == 1) { $link = '&format=feed&limitstart='; - $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); - $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->document->getTitle())]; - $this->document->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); + $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; + $this->getDocument()->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); } } } diff --git a/libraries/src/MVC/View/HtmlView.php b/libraries/src/MVC/View/HtmlView.php index 9a559e18076..6741b4845bb 100644 --- a/libraries/src/MVC/View/HtmlView.php +++ b/libraries/src/MVC/View/HtmlView.php @@ -590,6 +590,6 @@ class HtmlView extends AbstractView implements CurrentUserInterface $title = Text::sprintf('JPAGETITLE', $title, $app->get('sitename')); } - $this->document->setTitle($title); + $this->getDocument()->setTitle($title); } } diff --git a/libraries/src/MVC/View/JsonApiView.php b/libraries/src/MVC/View/JsonApiView.php index 91db120317d..7726f08edc0 100644 --- a/libraries/src/MVC/View/JsonApiView.php +++ b/libraries/src/MVC/View/JsonApiView.php @@ -142,7 +142,7 @@ abstract class JsonApiView extends JsonView // Set up links for pagination $totalItemsCount = ($pagination->pagesTotal * $pagination->limit); - $this->document->addMeta('total-pages', $pagination->pagesTotal) + $this->getDocument()->addMeta('total-pages', $pagination->pagesTotal) ->addLink('self', (string) $currentUrl); // Check for first and previous pages @@ -158,7 +158,7 @@ abstract class JsonApiView extends JsonView $previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0; $previousPage->setVar('page', $previousPageQuery); - $this->document->addLink('first', $this->queryEncode((string) $firstPage)) + $this->getDocument()->addLink('first', $this->queryEncode((string) $firstPage)) ->addLink('previous', $this->queryEncode((string) $previousPage)); } @@ -175,7 +175,7 @@ abstract class JsonApiView extends JsonView $lastPageQuery['offset'] = ($pagination->pagesTotal - 1) * $pagination->limit; $lastPage->setVar('page', $lastPageQuery); - $this->document->addLink('next', $this->queryEncode((string) $nextPage)) + $this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage)) ->addLink('last', $this->queryEncode((string) $lastPage)); } @@ -193,9 +193,9 @@ abstract class JsonApiView extends JsonView } // Set the data into the document and render it - $this->document->setData($collection); + $this->getDocument()->setData($collection); - return $this->document->render(); + return $this->getDocument()->render(); } /** @@ -246,10 +246,10 @@ abstract class JsonApiView extends JsonView $element->with($eventResult->getAllRelationsToRender()); } - $this->document->setData($element); - $this->document->addLink('self', Uri::current()); + $this->getDocument()->setData($element); + $this->getDocument()->addLink('self', Uri::current()); - return $this->document->render(); + return $this->getDocument()->render(); } /** diff --git a/libraries/src/MVC/View/JsonView.php b/libraries/src/MVC/View/JsonView.php index 75300105b5b..5ed41e3ace1 100644 --- a/libraries/src/MVC/View/JsonView.php +++ b/libraries/src/MVC/View/JsonView.php @@ -96,6 +96,6 @@ class JsonView extends AbstractView $result = json_encode($this->_output); // Pushing output to the document - $this->document->setBuffer($result); + $this->getDocument()->setBuffer($result); } } diff --git a/tests/Unit/Libraries/Cms/Document/DocumentAwareTraitTest.php b/tests/Unit/Libraries/Cms/Document/DocumentAwareTraitTest.php new file mode 100644 index 00000000000..dc373e6b369 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Document/DocumentAwareTraitTest.php @@ -0,0 +1,73 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Libraries\Cms\Document; + +use Joomla\CMS\Document\DocumentAwareTrait; +use Joomla\CMS\Document\Document; +use Joomla\Tests\Unit\UnitTestCase; +use UnexpectedValueException; + +/** + * Test class for \Joomla\CMS\Document\DocumentAwareTrait + * + * @package Joomla.UnitTest + * @subpackage MVC + * @since __DEPLOY_VERSION__ + */ +class DocumentAwareTraitTest extends UnitTestCase +{ + /** + * @testdox The user document can be set and accessed by the trait + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testGetSetDocument() + { + $document = new Document(); + $trait = new class () { + use DocumentAwareTrait; + + public function get(): Document + { + return $this->getDocument(); + } + }; + + $trait->setDocument($document); + + $this->assertEquals($document, $trait->get()); + } + + /** + * @testdox The user document can be set and accessed by the trait + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testGetDocumentThrowsException() + { + $this->expectException(UnexpectedValueException::class); + + $trait = new class () { + use DocumentAwareTrait; + + public function get(): Document + { + return $this->getDocument(); + } + }; + + $trait->get(); + } +}