FlexServiceProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @package Grav\Common\Service
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Service;
  9. use Grav\Common\Config\Config;
  10. use Grav\Common\Flex\Types\Users\Storage\UserFileStorage;
  11. use Grav\Common\Flex\Types\Users\Storage\UserFolderStorage;
  12. use Grav\Common\Grav;
  13. use Grav\Events\FlexRegisterEvent;
  14. use Grav\Framework\Flex\Flex;
  15. use Grav\Framework\Flex\FlexFormFlash;
  16. use Pimple\Container;
  17. use Pimple\ServiceProviderInterface;
  18. use function is_array;
  19. /**
  20. * Class FlexServiceProvider
  21. * @package Grav\Common\Service
  22. */
  23. class FlexServiceProvider implements ServiceProviderInterface
  24. {
  25. /**
  26. * @param Container $container
  27. * @return void
  28. */
  29. public function register(Container $container)
  30. {
  31. $container['flex'] = function (Grav $container) {
  32. /** @var Config $config */
  33. $config = $container['config'];
  34. $flex = new Flex([], ['object' => $config->get('system.flex', [])]);
  35. FlexFormFlash::setFlex($flex);
  36. $accountsEnabled = $config->get('system.accounts.type', 'regular') === 'flex';
  37. $pagesEnabled = $config->get('system.pages.type', 'regular') === 'flex';
  38. // Add built-in types from Grav.
  39. if ($pagesEnabled) {
  40. $flex->addDirectoryType(
  41. 'pages',
  42. 'blueprints://flex/pages.yaml',
  43. [
  44. 'enabled' => $pagesEnabled
  45. ]
  46. );
  47. }
  48. if ($accountsEnabled) {
  49. $flex->addDirectoryType(
  50. 'user-accounts',
  51. 'blueprints://flex/user-accounts.yaml',
  52. [
  53. 'enabled' => $accountsEnabled,
  54. 'data' => [
  55. 'storage' => $this->getFlexAccountsStorage($config),
  56. ]
  57. ]
  58. );
  59. $flex->addDirectoryType(
  60. 'user-groups',
  61. 'blueprints://flex/user-groups.yaml',
  62. [
  63. 'enabled' => $accountsEnabled
  64. ]
  65. );
  66. }
  67. // Call event to register Flex Directories.
  68. $event = new FlexRegisterEvent($flex);
  69. $container->dispatchEvent($event);
  70. return $flex;
  71. };
  72. }
  73. /**
  74. * @param Config $config
  75. * @return array
  76. */
  77. private function getFlexAccountsStorage(Config $config): array
  78. {
  79. $value = $config->get('system.accounts.storage', 'file');
  80. if (is_array($value)) {
  81. return $value;
  82. }
  83. if ($value === 'folder') {
  84. return [
  85. 'class' => UserFolderStorage::class,
  86. 'options' => [
  87. 'file' => 'user',
  88. 'pattern' => '{FOLDER}/{KEY:2}/{KEY}/{FILE}{EXT}',
  89. 'key' => 'storage_key'
  90. ],
  91. ];
  92. }
  93. if ($value === 'file') {
  94. return [
  95. 'class' => UserFileStorage::class,
  96. 'options' => [
  97. 'pattern' => '{FOLDER}/{KEY}{EXT}',
  98. 'key' => 'username'
  99. ],
  100. ];
  101. }
  102. return [];
  103. }
  104. }