From be3ecf1377e7c85ec3a31f9525ca460c513f1e4b Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 15 Nov 2019 21:15:38 +0200 Subject: [PATCH] DEPRECATED `$page->modular()` in favor of `$page->isModule()` for better readability --- CHANGELOG.md | 5 +++-- .../Grav/Common/Flex/Pages/PageCollection.php | 12 ++++++++++-- system/src/Grav/Common/Flex/Pages/PageIndex.php | 2 +- .../src/Grav/Common/Flex/Pages/PageObject.php | 2 +- .../Flex/Pages/Traits/PageContentTrait.php | 2 +- system/src/Grav/Common/Page/Collection.php | 4 ++-- .../Page/Interfaces/PageContentInterface.php | 7 +++++++ .../Page/Interfaces/PageLegacyInterface.php | 3 +-- system/src/Grav/Common/Page/Page.php | 17 +++++++++++++---- system/src/Grav/Common/Page/Pages.php | 6 +++--- .../src/Grav/Framework/Flex/FlexDirectory.php | 3 +-- .../Flex/Pages/Traits/PageContentTrait.php | 12 ++++++++++-- .../Flex/Pages/Traits/PageLegacyTrait.php | 6 ++++-- .../Flex/Pages/Traits/PageRoutableTrait.php | 2 +- 14 files changed, 58 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbc7385f8..5cb0f88ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ * Added `Utils::isAssoc()` and `Utils::isNegative()` helper methods * Changed `UserInterface::authorize()` to return `null` having the same meaning as `false` if access is denied because of no matching rule * Moved all Flex type classes under `Grav\Common\Flex` - * Deprecated `Grav\Common\User\Group` in favor of `$grav['user_groups']`, which contains Flex UserGroup collection + * DEPRECATED `Grav\Common\User\Group` in favor of `$grav['user_groups']`, which contains Flex UserGroup collection + * DEPRECATED `$page->modular()` in favor of `$page->isModule()` for better readability * Fixed phpstan issues in all code up to level 3 1. [](#improved) * Improved twig `|array` filter to work with iterators and objects with `toArray()` method @@ -129,7 +130,7 @@ * Added a new `bin/grav server` CLI command to easily run Symfony or PHP built-in webservers * Added `hasFlexFeature()` method to test if `FlexObject` or `FlexCollection` implements a given feature * Added `getFlexFeatures()` method to return all features that `FlexObject` or `FlexCollection` implements - * Deprecated `FlexDirectory::update()` and `FlexDirectory::remove()` + * DEPRECATED `FlexDirectory::update()` and `FlexDirectory::remove()` * Added `FlexStorage::getMetaData()` to get updated object meta information for listed keys * Added `Language::getPageExtensions()` to get full list of supported page language extensions * Added `$grav->close()` method to properly terminate the request with a response diff --git a/system/src/Grav/Common/Flex/Pages/PageCollection.php b/system/src/Grav/Common/Flex/Pages/PageCollection.php index c25e890eb..9a61749c2 100644 --- a/system/src/Grav/Common/Flex/Pages/PageCollection.php +++ b/system/src/Grav/Common/Flex/Pages/PageCollection.php @@ -272,8 +272,12 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa public function modular() { $entries = []; + /** + * @var int|string $key + * @var PageInterface|null $object + */ foreach ($this as $key => $object) { - if ($object && $object->modular()) { + if ($object && $object->isModule()) { $entries[$key] = $object; } } @@ -289,8 +293,12 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa public function nonModular() { $entries = []; + /** + * @var int|string $key + * @var PageInterface|null $object + */ foreach ($this as $key => $object) { - if ($object && !$object->modular()) { + if ($object && !$object->isModule()) { $entries[$key] = $object; } } diff --git a/system/src/Grav/Common/Flex/Pages/PageIndex.php b/system/src/Grav/Common/Flex/Pages/PageIndex.php index 2741e7b1f..9c9a5c032 100644 --- a/system/src/Grav/Common/Flex/Pages/PageIndex.php +++ b/system/src/Grav/Common/Flex/Pages/PageIndex.php @@ -393,7 +393,7 @@ class PageIndex extends FlexPageIndex // TODO: maybe icon by home/modular/page/folder (or even from blueprints) and color by visibility etc.. if ($child->home()) { $icon = 'home'; - } elseif ($child->modular()) { + } elseif ($child->isModule()) { $icon = 'modular'; } elseif ($child->visible()) { $icon = 'visible'; diff --git a/system/src/Grav/Common/Flex/Pages/PageObject.php b/system/src/Grav/Common/Flex/Pages/PageObject.php index c3ba54d52..8e83aea4c 100644 --- a/system/src/Grav/Common/Flex/Pages/PageObject.php +++ b/system/src/Grav/Common/Flex/Pages/PageObject.php @@ -219,7 +219,7 @@ class PageObject extends FlexPageObject /** @var Admin|null $admin */ $admin = Grav::instance()['admin'] ?? null; if ($admin) { - $template = $this->modular() ? 'modular_raw' : 'raw'; + $template = $this->isModule() ? 'modular_raw' : 'raw'; return $admin->blueprints("admin/pages/{$template}"); } diff --git a/system/src/Grav/Common/Flex/Pages/Traits/PageContentTrait.php b/system/src/Grav/Common/Flex/Pages/Traits/PageContentTrait.php index 4c41fcd21..b0e0ebb08 100644 --- a/system/src/Grav/Common/Flex/Pages/Traits/PageContentTrait.php +++ b/system/src/Grav/Common/Flex/Pages/Traits/PageContentTrait.php @@ -68,6 +68,6 @@ trait PageContentTrait public function isPage(): bool { // FIXME: needs to be better - return !$this->exists() || !empty($this->getLanguages()) || $this->modular(); + return !$this->exists() || !empty($this->getLanguages()) || $this->isModule(); } } diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index 36f00e66a..c5afaf7cb 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -404,7 +404,7 @@ class Collection extends Iterator implements PageCollectionInterface foreach ($this->items as $path => $slug) { $page = $this->pages->get($path); - if ($page !== null && $page->modular()) { + if ($page !== null && $page->isModule()) { $modular[$path] = $slug; } } @@ -424,7 +424,7 @@ class Collection extends Iterator implements PageCollectionInterface foreach ($this->items as $path => $slug) { $page = $this->pages->get($path); - if ($page !== null && !$page->modular()) { + if ($page !== null && !$page->isModule()) { $modular[$path] = $slug; } } diff --git a/system/src/Grav/Common/Page/Interfaces/PageContentInterface.php b/system/src/Grav/Common/Page/Interfaces/PageContentInterface.php index c7de508e4..6dbacec21 100644 --- a/system/src/Grav/Common/Page/Interfaces/PageContentInterface.php +++ b/system/src/Grav/Common/Page/Interfaces/PageContentInterface.php @@ -228,6 +228,13 @@ interface PageContentInterface */ public function shouldProcess($process); + /** + * Returns true if page is a module. + * + * @return bool + */ + public function isModule(): bool; + /** * Returns whether or not this Page object has a .md file associated with it or if its just a directory. * diff --git a/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php b/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php index 9cdccff63..35d6d2180 100644 --- a/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php +++ b/system/src/Grav/Common/Page/Interfaces/PageLegacyInterface.php @@ -349,8 +349,8 @@ interface PageLegacyInterface * Gets and sets the modular var that helps identify this page is a modular child * * @param bool $var true if modular_twig - * * @return bool true if modular_twig + * @deprecated 1.7 Use ->isModule() or ->modularTwig() method instead. */ public function modular($var = null); @@ -462,7 +462,6 @@ interface PageLegacyInterface * @param string|array $value * @param bool $only_published * @return mixed - * @internal */ public function evaluate($value, $only_published = true); diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 06e17f59a..de690e86a 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -933,7 +933,7 @@ class Page implements PageInterface $pattern = '%(' . preg_quote($language, '%') . ')?\.md$%'; $name = preg_replace($pattern, '', $name); - if ($this->modular()) { + if ($this->isModule()) { return 'modular/' . $name; } @@ -1294,7 +1294,7 @@ class Page implements PageInterface $this->template = $var; } if (empty($this->template)) { - $this->template = ($this->modular() ? 'modular/' : '') . str_replace($this->extension(), '', $this->name()); + $this->template = ($this->isModule() ? 'modular/' : '') . str_replace($this->extension(), '', $this->name()); } return $this->template; @@ -2220,11 +2220,13 @@ class Page implements PageInterface * Gets and sets the modular var that helps identify this page is a modular child * * @param bool $var true if modular_twig - * * @return bool true if modular_twig + * @deprecated 1.7 Use ->isModule() or ->modularTwig() method instead. */ public function modular($var = null) { + user_error(__METHOD__ . '() is deprecated since Grav 1.7, use ->isModule() or ->modularTwig() method instead', E_USER_DEPRECATED); + return $this->modularTwig($var); } @@ -2233,7 +2235,6 @@ class Page implements PageInterface * twig processing handled differently from a regular page. * * @param bool $var true if modular_twig - * * @return bool true if modular_twig */ public function modularTwig($var = null) @@ -2639,6 +2640,14 @@ class Page implements PageInterface return !$this->isPage(); } + /** + * @return bool + */ + public function isModule(): bool + { + return $this->modularTwig(); + } + /** * Returns whether the page exists in the filesystem. * diff --git a/system/src/Grav/Common/Page/Pages.php b/system/src/Grav/Common/Page/Pages.php index 2b030a1ee..71cd2ec92 100644 --- a/system/src/Grav/Common/Page/Pages.php +++ b/system/src/Grav/Common/Page/Pages.php @@ -407,7 +407,7 @@ class Pages foreach ($params['taxonomies'] ?? [] as $taxonomy => $items) { foreach ($collection as $page) { // Don't filter modular pages - if ($page->modular()) { + if ($page->isModule()) { continue; } @@ -1070,7 +1070,7 @@ class Pages if ($limitLevels === false || ($level+1 < $limitLevels)) { foreach ($current->children() as $next) { - if ($showAll || $next->routable() || ($next->modular() && $showModular)) { + if ($showAll || $next->routable() || ($next->isModule() && $showModular)) { $list = array_merge($list, $this->getList($next, $level + 1, $rawRoutes, $showAll, $showFullpath, $showSlug, $showModular, $limitLevels)); } } @@ -1180,7 +1180,7 @@ class Pages /** @var PageInterface|null $page */ $page = $admin->page(); - $type = $page && $page->modular() ? 'modular' : 'standard'; + $type = $page && $page->isModule() ? 'modular' : 'standard'; } switch ($type) { diff --git a/system/src/Grav/Framework/Flex/FlexDirectory.php b/system/src/Grav/Framework/Flex/FlexDirectory.php index fac148010..1327241ba 100644 --- a/system/src/Grav/Framework/Flex/FlexDirectory.php +++ b/system/src/Grav/Framework/Flex/FlexDirectory.php @@ -165,7 +165,6 @@ class FlexDirectory implements FlexAuthorizeInterface * @param string $type * @param string $context * @return Blueprint - * @internal */ public function getBlueprint(string $type = '', string $context = '') { @@ -699,7 +698,7 @@ class FlexDirectory implements FlexAuthorizeInterface $object = $call['object']; if ($function === '\Grav\Common\Page\Pages::pageTypes') { - $params = [$object instanceof PageInterface && $object->modular() ? 'modular' : 'standard']; + $params = [$object instanceof PageInterface && $object->isModule() ? 'modular' : 'standard']; } $data = null; diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageContentTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageContentTrait.php index 23cac7c0d..6e144a124 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageContentTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageContentTrait.php @@ -223,7 +223,7 @@ trait PageContentTrait 'visible', $var, function ($value) { - return ($value ?? $this->order() !== false) && !$this->modular(); + return ($value ?? $this->order() !== false) && !$this->isModule(); } ); @@ -479,6 +479,14 @@ trait PageContentTrait return !$this->isPage(); } + /** + * @return bool + */ + public function isModule(): bool + { + return $this->modularTwig(); + } + /** * @inheritdoc */ @@ -648,7 +656,7 @@ trait PageContentTrait $config = $grav['config']; $process_markdown = $this->shouldProcess('markdown'); - $process_twig = $this->shouldProcess('twig') || $this->modularTwig(); + $process_twig = $this->shouldProcess('twig') || $this->isModule(); $cache_enable = $this->getNestedProperty('header.cache_enable') ?? $config->get('system.cache.enabled', true); $twig_first = $this->getNestedProperty('header.twig_first') ?? $config->get('system.pages.twig_first', true); diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php index bac8acfc3..39408e852 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php @@ -477,7 +477,7 @@ trait PageLegacyTrait 'template', $var, function ($value) { - return trim($value ?? (($this->modular() ? 'modular/' : '') . str_replace($this->extension(), '', $this->name()))); + return trim($value ?? (($this->isModule() ? 'modular/' : '') . str_replace($this->extension(), '', $this->name()))); } ); } @@ -807,11 +807,13 @@ trait PageLegacyTrait * Gets and sets the modular var that helps identify this page is a modular child * * @param bool $var true if modular_twig - * * @return bool true if modular_twig + * @deprecated 1.7 Use ->isModule() or ->modularTwig() method instead. */ public function modular($var = null): bool { + user_error(__METHOD__ . '() is deprecated since Grav 1.7, use ->isModule() or ->modularTwig() method instead', E_USER_DEPRECATED); + return $this->modularTwig($var); } diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php index f04597eeb..64ffddae1 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php @@ -61,7 +61,7 @@ trait PageRoutableTrait } ); - return $value && $this->published() && !$this->modular() && $this->getLanguages(true); + return $value && $this->published() && !$this->isModule() && $this->getLanguages(true); } /**