Skip to content

Commit

Permalink
[Feature][WIP] Settings (#64)
Browse files Browse the repository at this point in the history
* Add system settings controller

* Add settings provider concept

* Apply php-cs-fixer changes

* Consistency check

---------

Co-authored-by: mattamon <[email protected]>
  • Loading branch information
mattamon and mattamon authored May 23, 2024
1 parent 300dbd2 commit 6c74dc8
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 0 deletions.
25 changes: 25 additions & 0 deletions config/settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Setting\Controller\:
resource: '../src/Setting/Controller'
public: true
tags: [ 'controller.service_arguments' ]


Pimcore\Bundle\StudioBackendBundle\Setting\Service\SettingProviderLoaderInterface:
class: Pimcore\Bundle\StudioBackendBundle\Setting\Service\Loader\TaggedIteratorAdapter

Pimcore\Bundle\StudioBackendBundle\Setting\Service\SettingsServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Setting\Service\SettingsService

Pimcore\Bundle\StudioBackendBundle\Setting\Provider\ConfigSettingsProvider:
tags: [ 'pimcore.studio_backend.settings_provider' ]

Pimcore\Bundle\StudioBackendBundle\Setting\Provider\SystemSettingsProvider:
tags: [ 'pimcore.studio_backend.settings_provider' ]
46 changes: 46 additions & 0 deletions src/DependencyInjection/CompilerPass/SettingsProviderPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\DependencyInjection\CompilerPass;

use Pimcore\Bundle\StudioBackendBundle\Exception\MustImplementInterfaceException;
use Pimcore\Bundle\StudioBackendBundle\Setting\Provider\SettingsProviderInterface;
use Pimcore\Bundle\StudioBackendBundle\Setting\Service\Loader\TaggedIteratorAdapter;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\MustImplementInterfaceTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @internal
*/
final class SettingsProviderPass implements CompilerPassInterface
{
use MustImplementInterfaceTrait;

/**
* @throws MustImplementInterfaceException
*/
public function process(ContainerBuilder $container): void
{
$taggedServices = array_keys(
$container->findTaggedServiceIds(TaggedIteratorAdapter::SETTINGS_PROVIDER_TAG),
);

foreach ($taggedServices as $environmentType) {
$this->checkInterface($environmentType, SettingsProviderInterface::class);
}
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/PimcoreStudioBackendExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('properties.yaml');
$loader->load('security.yaml');
$loader->load('services.yaml');
$loader->load('settings.yaml');
$loader->load('translation.yaml');
$loader->load('versions.yaml');

Expand Down
5 changes: 5 additions & 0 deletions src/OpenApi/Config/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
name: Tags::Translation->name,
description: 'Get translations either for a single key or multiple keys'
)]
#[Tag(
name: Tags::Settings->name,
description: 'Get Settings'
)]
#[Tag(
name: Tags::Versions->name,
description: 'Versions operations to get/list/publish/delete and cleanup versions'
Expand All @@ -72,6 +76,7 @@ enum Tags: string
case NotesForElement = 'Notes for Element';
case Properties = 'Properties';
case PropertiesForElement = 'Properties for Element';
case Settings = 'Settings';
case Translation = 'Translation';
case Versions = 'Versions';
}
2 changes: 2 additions & 0 deletions src/PimcoreStudioBackendBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\PimcoreGenericDataIndexBundle;
use Pimcore\Bundle\StaticResolverBundle\PimcoreStaticResolverBundle;
use Pimcore\Bundle\StudioBackendBundle\DependencyInjection\CompilerPass\FilterPass;
use Pimcore\Bundle\StudioBackendBundle\DependencyInjection\CompilerPass\SettingsProviderPass;
use Pimcore\Extension\Bundle\AbstractPimcoreBundle;
use Pimcore\Extension\Bundle\Installer\InstallerInterface;
use Pimcore\HttpKernel\Bundle\DependentBundleInterface;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function getInstaller(): ?InstallerInterface
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new FilterPass());
$container->addCompilerPass(new SettingsProviderPass());
}

public static function registerDependentBundles(BundleCollection $collection): void
Expand Down
66 changes: 66 additions & 0 deletions src/Setting/Controller/GetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Setting\Controller;

use OpenApi\Attributes\Get;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnprocessableContentResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Setting\Service\SettingsServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class GetController extends AbstractApiController
{
private const ROUTE = '/settings';

public function __construct(
SerializerInterface $serializer,
private readonly SettingsServiceInterface $settingsService,
) {
parent::__construct($serializer);
}

#[Route(path: self::ROUTE, name: 'pimcore_studio_api_settings', methods: ['GET'])]
#[Get(
path: self::API_PATH . self::ROUTE,
operationId: 'getSystemSettings',
description: 'Get system settings',
summary: 'Get system settings',
security: self::SECURITY_SCHEME,
tags: [Tags::Settings->name]
)]
#[SuccessResponse(
description: 'System settings',
content: new JsonContent(type: 'object', additionalProperties: true)
)]
#[MethodNotAllowedResponse]
#[UnsupportedMediaTypeResponse]
#[UnprocessableContentResponse]
public function getSystemSettings(): JsonResponse
{
return $this->jsonResponse($this->settingsService->getSettings());
}
}
45 changes: 45 additions & 0 deletions src/Setting/Provider/ConfigSettingsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Setting\Provider;

use Pimcore\Config;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;

/**
* @internal
*/
#[AsTaggedItem('pimcore.studio_backend.settings_provider')]
final readonly class ConfigSettingsProvider implements SettingsProviderInterface
{
public function __construct(
private Config $config
)
{

}

public function getSettings(): array
{
return [
'asset_tree_paging_limit' => $this->config['assets']['tree_paging_limit'],
'document_tree_paging_limit' => $this->config['documents']['tree_paging_limit'],
'object_tree_paging_limit' => $this->config['objects']['tree_paging_limit'],
'timezone' => $this->config['general']['timezone'],
];

}
}
25 changes: 25 additions & 0 deletions src/Setting/Provider/SettingsProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Setting\Provider;

/**
* @internal
*/
interface SettingsProviderInterface
{
public function getSettings(): array;
}
50 changes: 50 additions & 0 deletions src/Setting/Provider/SystemSettingsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Setting\Provider;

use Pimcore\SystemSettingsConfig;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;

/**
* @internal
*/
#[AsTaggedItem('pimcore.studio_backend.settings_provider')]
final readonly class SystemSettingsProvider implements SettingsProviderInterface
{
private array $systemSettings;

public function __construct(
SystemSettingsConfig $systemSettingsConfig,
)
{
$this->systemSettings = $systemSettingsConfig->getSystemSettingsConfig();
}

public function getSettings(): array
{
$requiredLanguages =
$this->systemSettings['general']['required_languages'] ??
$this->systemSettings['general']['valid_languages'];


return [
'requiredLanguages' => $requiredLanguages,
'debug_admin_translations' => (bool)$this->systemSettings['general']['debug_admin_translations'],
'main_domain' => $this->systemSettings['general']['domain'],
];
}
}
39 changes: 39 additions & 0 deletions src/Setting/Service/Loader/TaggedIteratorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Setting\Service\Loader;

use Pimcore\Bundle\StudioBackendBundle\Setting\Service\SettingProviderLoaderInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

/**
* @internal
*/
final class TaggedIteratorAdapter implements SettingProviderLoaderInterface
{
public const SETTINGS_PROVIDER_TAG = 'pimcore.studio_backend.settings_provider';

public function __construct(
#[TaggedIterator(self::SETTINGS_PROVIDER_TAG)]
private readonly iterable $taggedSettingProviders,
) {
}

public function loadSettingProviders(): array
{
return [...$this->taggedSettingProviders];
}
}
25 changes: 25 additions & 0 deletions src/Setting/Service/SettingProviderLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Setting\Service;

/**
* @internal
*/
interface SettingProviderLoaderInterface
{
public function loadSettingProviders(): array;
}
Loading

0 comments on commit 6c74dc8

Please sign in to comment.