Added $grav['flex'] to access all registered Flex Directories

This commit is contained in:
Matias Griese
2020-01-21 10:06:58 +02:00
parent 664447a67b
commit bee5abfbf0
5 changed files with 234 additions and 1 deletions

View File

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

View File

@@ -0,0 +1,91 @@
<?php
/**
* @package Grav\Common\Service
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Common\Service;
use Grav\Common\Config\Config;
use Grav\Common\Flex\Users\Storage\UserFolderStorage;
use Grav\Common\Grav;
use Grav\Events\FlexRegisterEvent;
use Grav\Framework\Flex\Flex;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
class FlexServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['flex'] = function (Grav $container) {
/** @var Config $config */
$config = $container['config'];
$flex = new Flex([], ['object' => $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 [];
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* @package Grav\Events
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Events;
use Grav\Framework\Flex\Flex;
/**
* Flex Register Event
*
* This event is called the first time $grav['flex'] is being called.
*
* Use this event to register enabled Directories to Flex.
*
* @property Flex $flex Flex instance.
*/
class FlexRegisterEvent
{
/** @var Flex */
public $flex;
/**
* FlexRegisterEvent constructor.
* @param Flex $flex
*/
public function __construct(Flex $flex)
{
$this->flex = $flex;
}
/**
* @return array
*/
public function __debugInfo(): array
{
return (array)$this;
}
}

View File

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

View File

@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
/**
* @package Grav\Framework\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Flex\Interfaces;
use Grav\Framework\Flex\FlexDirectory;
/**
* Interface FlexInterface
* @package Grav\Framework\Flex\Interfaces
*/
interface FlexInterface extends \Countable
{
/**
* @param string $type
* @param string $blueprint
* @param array $config
* @return $this
*/
public function addDirectoryType(string $type, string $blueprint, array $config = []);
/**
* @param FlexDirectory $directory
* @return $this
*/
public function addDirectory(FlexDirectory $directory);
/**
* @param string $type
* @return bool
*/
public function hasDirectory(string $type): bool;
/**
* @param array|string[]|null $types
* @param bool $keepMissing
* @return array<FlexDirectory|null>
*/
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;
}