From 66c17a8f5338bd15a032feffbdd92704f7399464 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 12 Jul 2019 12:39:05 +0300 Subject: [PATCH] Added `hasFlexFeature()` method to test if `FlexObject` or `FlexCollection` implements a given feature --- CHANGELOG.md | 1 + .../Grav/Framework/Flex/FlexCollection.php | 19 +++++++++++++++++-- system/src/Grav/Framework/Flex/FlexIndex.php | 17 +++++++++++++++++ system/src/Grav/Framework/Flex/FlexObject.php | 17 +++++++++++++++++ .../Flex/Interfaces/FlexCommonInterface.php | 9 +++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b3ae8484..769ac0964 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 1. [](#new) * 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 1. [](#improved) * Better support for Symfony local server `symfony server:start` * Make `Route` objects immutable diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index 5e5431bbf..2fd593112 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -13,12 +13,11 @@ use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; use Grav\Common\Debugger; use Grav\Common\Grav; +use Grav\Common\Inflector; use Grav\Common\Twig\Twig; use Grav\Common\User\Interfaces\UserInterface; use Grav\Framework\Cache\CacheInterface; -use Grav\Framework\ContentBlock\ContentBlockInterface; use Grav\Framework\ContentBlock\HtmlBlock; -use Grav\Framework\Flex\Interfaces\FlexIndexInterface; use Grav\Framework\Flex\Interfaces\FlexObjectInterface; use Grav\Framework\Object\ObjectCollection; use Grav\Framework\Flex\Interfaces\FlexCollectionInterface; @@ -92,6 +91,22 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface } } + /** + * {@inheritdoc} + * @see FlexCommonInterface::hasFlexFeature() + */ + public function hasFlexFeature($name): bool + { + $implements = class_implements($this); + $list = []; + foreach ($implements as $interface) { + $key = Inflector::hyphenize(preg_replace('/(.*\\\\)(.*?)Interface$/', '\\2', $interface)); + $list[$key] = true; + } + + return is_array($name) ? array_intersect_key($list, array_flip($name)) : $list[$name] ?? false; + } + /** * {@inheritdoc} * @see FlexCollectionInterface::search() diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index 8b96b4a9a..bf4e48442 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -12,6 +12,7 @@ namespace Grav\Framework\Flex; use Grav\Common\Debugger; use Grav\Common\File\CompiledYamlFile; use Grav\Common\Grav; +use Grav\Common\Inflector; use Grav\Common\Session; use Grav\Framework\Cache\CacheInterface; use Grav\Framework\Collection\CollectionInterface; @@ -80,6 +81,22 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde $this->setKeyField(null); } + /** + * {@inheritdoc} + * @see FlexCommonInterface::hasFlexFeature() + */ + public function hasFlexFeature($name): bool + { + $implements = class_implements($this->getFlexDirectory()->getCollectionClass()); + $list = []; + foreach ($implements as $interface) { + $key = Inflector::hyphenize(preg_replace('/(.*\\\\)(.*?)Interface$/', '\\2', $interface)); + $list[$key] = true; + } + + return is_array($name) ? array_intersect_key($list, array_flip($name)) : $list[$name] ?? false; + } + /** * {@inheritdoc} * @see FlexCollectionInterface::search() diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 01f45d079..99af3ca2a 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -12,6 +12,7 @@ namespace Grav\Framework\Flex; use Grav\Common\Data\Blueprint; use Grav\Common\Debugger; use Grav\Common\Grav; +use Grav\Common\Inflector; use Grav\Common\Twig\Twig; use Grav\Common\Utils; use Grav\Framework\Cache\CacheInterface; @@ -110,6 +111,22 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface $this->objectConstruct($elements, $key); } + /** + * {@inheritdoc} + * @see FlexCommonInterface::hasFlexFeature() + */ + public function hasFlexFeature($name): bool + { + $implements = class_implements($this); + $list = []; + foreach ($implements as $interface) { + $key = Inflector::hyphenize(preg_replace('/(.*\\\\)(.*?)Interface$/', '\\2', $interface)); + $list[$key] = true; + } + + return is_array($name) ? array_intersect_key($list, array_flip($name)) : $list[$name] ?? false; + } + /** * {@inheritdoc} * @see FlexObjectInterface::getFlexType() diff --git a/system/src/Grav/Framework/Flex/Interfaces/FlexCommonInterface.php b/system/src/Grav/Framework/Flex/Interfaces/FlexCommonInterface.php index 482aef226..c748a602c 100644 --- a/system/src/Grav/Framework/Flex/Interfaces/FlexCommonInterface.php +++ b/system/src/Grav/Framework/Flex/Interfaces/FlexCommonInterface.php @@ -38,6 +38,15 @@ interface FlexCommonInterface extends RenderInterface */ public function getFlexDirectory(): FlexDirectory; + /** + * Return true if a feature or all listed features have been implemented in the object / collection. + * + * @param string|string[] $feature + * @return bool + * @since 1.7 + */ + public function hasFlexFeature($feature): bool; + /** * Get last updated timestamp for the object / collection. *