diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 64946113d..4f87a7642 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -63,6 +63,7 @@ class Grav extends Container 'Grav\Common\Service\ConfigServiceProvider', 'Grav\Common\Service\ErrorServiceProvider', 'Grav\Common\Service\FilesystemServiceProvider', + 'Grav\Common\Service\FlexServiceProvider', 'Grav\Common\Service\InflectorServiceProvider', 'Grav\Common\Service\LoggerServiceProvider', 'Grav\Common\Service\OutputServiceProvider', diff --git a/system/src/Grav/Common/Service/FlexServiceProvider.php b/system/src/Grav/Common/Service/FlexServiceProvider.php new file mode 100644 index 000000000..ba9607976 --- /dev/null +++ b/system/src/Grav/Common/Service/FlexServiceProvider.php @@ -0,0 +1,91 @@ + $config->get('system.flex', [])]); + + $accountsEnabled = $config->get('system.accounts.type', 'regular') === 'flex'; + $pagesEnabled = $config->get('system.pages.type', 'regular') === 'flex'; + + // Add built-in types from Grav. + if ($pagesEnabled) { + $flex->addDirectoryType( + 'grav-pages', + 'blueprints://flex/pages.yaml', + [ + 'enabled' => $pagesEnabled + ] + ); + } + if ($accountsEnabled) { + $flex->addDirectoryType( + 'grav-accounts', + 'blueprints://flex/accounts.yaml', + [ + 'enabled' => $accountsEnabled, + 'data' => [ + 'storage' => $this->getFlexAccountsStorage($config), + ] + ] + ); + $flex->addDirectoryType( + 'grav-user-groups', + 'blueprints://flex/user-groups.yaml', + [ + 'enabled' => $accountsEnabled + ] + ); + } + + // Call event to register Flex Directories. + $event = new FlexRegisterEvent($flex); + $container->dispatchEvent($event); + + return $flex; + }; + } + + private function getFlexAccountsStorage(Config $config) + { + $value = $config->get('system.accounts.storage', 'file'); + if (\is_array($value)) { + return $value; + } + + if ($value === 'folder') { + return [ + 'class' => UserFolderStorage::class, + 'options' => [ + 'file' => 'user', + 'pattern' => '{FOLDER}/{KEY:2}/{KEY}/{FILE}{EXT}', + 'key' => 'storage_key', + ], + ]; + } + + return []; + } +} diff --git a/system/src/Grav/Events/FlexRegisterEvent.php b/system/src/Grav/Events/FlexRegisterEvent.php new file mode 100644 index 000000000..406a2f149 --- /dev/null +++ b/system/src/Grav/Events/FlexRegisterEvent.php @@ -0,0 +1,44 @@ +flex = $flex; + } + + /** + * @return array + */ + public function __debugInfo(): array + { + return (array)$this; + } +} diff --git a/system/src/Grav/Framework/Flex/Flex.php b/system/src/Grav/Framework/Flex/Flex.php index bdf44f053..da0abf8a3 100644 --- a/system/src/Grav/Framework/Flex/Flex.php +++ b/system/src/Grav/Framework/Flex/Flex.php @@ -14,6 +14,7 @@ namespace Grav\Framework\Flex; use Grav\Common\Debugger; use Grav\Common\Grav; use Grav\Framework\Flex\Interfaces\FlexCollectionInterface; +use Grav\Framework\Flex\Interfaces\FlexInterface; use Grav\Framework\Flex\Interfaces\FlexObjectInterface; use Grav\Framework\Object\ObjectCollection; @@ -21,7 +22,7 @@ use Grav\Framework\Object\ObjectCollection; * Class Flex * @package Grav\Framework\Flex */ -class Flex implements \Countable +class Flex implements FlexInterface { /** @var array */ protected $config; diff --git a/system/src/Grav/Framework/Flex/Interfaces/FlexInterface.php b/system/src/Grav/Framework/Flex/Interfaces/FlexInterface.php new file mode 100644 index 000000000..64c4c7b43 --- /dev/null +++ b/system/src/Grav/Framework/Flex/Interfaces/FlexInterface.php @@ -0,0 +1,96 @@ + + */ + public function getDirectories(array $types = null, bool $keepMissing = false): array; + + /** + * @param string $type + * @return FlexDirectory|null + */ + public function getDirectory(string $type): ?FlexDirectory; + + /** + * @param string $type + * @param array|null $keys + * @param string|null $keyField + * @return FlexCollectionInterface|null + */ + public function getCollection(string $type, array $keys = null, string $keyField = null): ?FlexCollectionInterface; + + /** + * @param array $keys + * @param array $options In addition to the options in getObjects(), following options can be passed: + * collection_class: Class to be used to create the collection. Defaults to ObjectCollection. + * @return FlexCollectionInterface + * @throws \RuntimeException + */ + public function getMixedCollection(array $keys, array $options = []): FlexCollectionInterface; + + /** + * @param array $keys + * @param array $options Following optional options can be passed: + * types: List of allowed types. + * type: Allowed type if types isn't defined, otherwise acts as default_type. + * default_type: Set default type for objects given without type (only used if key_field isn't set). + * keep_missing: Set to true if you want to return missing objects as null. + * key_field: Key field which is used to match the objects. + * @return array + */ + public function getObjects(array $keys, array $options = []): array; + + /** + * @param string $key + * @param string|null $type + * @param string|null $keyField + * @return FlexObjectInterface|null + */ + public function getObject(string $key, string $type = null, string $keyField = null): ?FlexObjectInterface; + + /** + * @return int + */ + public function count(): int; +}