Added hasFlexFeature() method to test if FlexObject or FlexCollection implements a given feature

This commit is contained in:
Matias Griese
2019-07-12 12:39:05 +03:00
parent 7172da8ed6
commit 66c17a8f53
5 changed files with 61 additions and 2 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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()

View File

@@ -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()

View File

@@ -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.
*