-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Added ApplicationConfig REST Value * Added ApplicationConfigController * Added ApplicationConfigRestResolver * Added ApplicationConfigVisitor * [PHPStan] Regenerated baseline * Fixed Resolvers service definition * Fixed phpdoc for ApplicationConfig::getConfig * Fixes after review * Fixed variables name * Reworked generating custom output
- Loading branch information
Showing
12 changed files
with
406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Bundle\AdminUi\Controller; | ||
|
||
use Ibexa\AdminUi\REST\Value\ApplicationConfig; | ||
use Ibexa\AdminUi\UI\Config\Aggregator; | ||
use Ibexa\Rest\Server\Controller; | ||
|
||
final class ApplicationConfigController extends Controller | ||
{ | ||
private Aggregator $aggregator; | ||
|
||
public function __construct(Aggregator $aggregator) | ||
{ | ||
$this->aggregator = $aggregator; | ||
} | ||
|
||
public function loadConfigAction(): ApplicationConfig | ||
{ | ||
return new ApplicationConfig($this->aggregator->getConfig()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/contracts/REST/ApplicationConfigRestGeneratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\AdminUi\REST; | ||
|
||
use Ibexa\Contracts\Rest\Output\Generator; | ||
use Ibexa\Contracts\Rest\Output\Visitor; | ||
|
||
interface ApplicationConfigRestGeneratorInterface | ||
{ | ||
public function supportsNamespace(string $namespace): bool; | ||
|
||
public function supportsParameter(string $parameterName): bool; | ||
|
||
/** | ||
* @param mixed $parameter | ||
*/ | ||
public function generate($parameter, Generator $generator, Visitor $visitor): void; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/contracts/REST/ApplicationConfigRestGeneratorRegistryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\AdminUi\REST; | ||
|
||
interface ApplicationConfigRestGeneratorRegistryInterface | ||
{ | ||
public function hasGenerator(string $namespace, string $parameter): bool; | ||
|
||
public function hasGenerators(string $namespace): bool; | ||
|
||
public function getGenerator(string $namespace, string $parameter): ApplicationConfigRestGeneratorInterface; | ||
|
||
/** | ||
* @return iterable<\Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface> | ||
*/ | ||
public function getGenerators(string $namespace): iterable; | ||
} |
90 changes: 90 additions & 0 deletions
90
src/lib/REST/Generator/ApplicationConfigRestGeneratorRegistry.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\AdminUi\REST\Generator; | ||
|
||
use Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface; | ||
use Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorRegistryInterface; | ||
use RuntimeException; | ||
|
||
final class ApplicationConfigRestGeneratorRegistry implements ApplicationConfigRestGeneratorRegistryInterface | ||
{ | ||
/** @var iterable<\Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface> */ | ||
private iterable $generators; | ||
|
||
/** | ||
* @param iterable<\Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface> $generators | ||
*/ | ||
public function __construct(iterable $generators) | ||
{ | ||
$this->generators = $generators; | ||
} | ||
|
||
public function hasGenerator( | ||
string $namespace, | ||
string $parameter | ||
): bool { | ||
foreach ($this->generators as $generator) { | ||
if ( | ||
$generator->supportsNamespace($namespace) | ||
&& $generator->supportsParameter($parameter) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function hasGenerators(string $namespace): bool | ||
{ | ||
foreach ($this->generators as $generator) { | ||
if ($generator->supportsNamespace($namespace)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getGenerator(string $namespace, string $parameter): ApplicationConfigRestGeneratorInterface | ||
{ | ||
foreach ($this->generators as $generator) { | ||
if ( | ||
$generator->supportsNamespace($namespace) | ||
&& $generator->supportsParameter($parameter) | ||
) { | ||
return $generator; | ||
} | ||
} | ||
|
||
throw new RuntimeException( | ||
sprintf( | ||
'No Generator found for Configuration in namespace \'%s\' and parameter \'%s\'', | ||
$namespace, | ||
$parameter | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<\Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface> | ||
*/ | ||
public function getGenerators(string $namespace): iterable | ||
{ | ||
foreach ($this->generators as $generator) { | ||
if ($generator->supportsNamespace($namespace)) { | ||
yield $generator; | ||
} | ||
} | ||
|
||
throw new RuntimeException( | ||
'No Generators found for Configuration in namespace \'' . $namespace . '\'', | ||
); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/lib/REST/Generator/ProfilePictureFieldConfigRestGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\AdminUi\REST\Generator; | ||
|
||
use Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Field; | ||
use Ibexa\Contracts\Rest\Output\Generator; | ||
use Ibexa\Contracts\Rest\Output\Visitor; | ||
|
||
final class ProfilePictureFieldConfigRestGenerator implements ApplicationConfigRestGeneratorInterface | ||
{ | ||
private const NAMESPACE = 'user'; | ||
private const PARAMETER = 'profile_picture_field'; | ||
|
||
public function supportsNamespace(string $namespace): bool | ||
{ | ||
return self::NAMESPACE === $namespace; | ||
} | ||
|
||
public function supportsParameter(string $parameterName): bool | ||
{ | ||
return self::PARAMETER === $parameterName; | ||
} | ||
|
||
public function generate($parameter, Generator $generator, Visitor $visitor): void | ||
{ | ||
if ($parameter instanceof Field) { | ||
$generator->startHashElement(self::PARAMETER); | ||
$visitor->visitValueObject($parameter); | ||
$generator->endHashElement(self::PARAMETER); | ||
|
||
return; | ||
} | ||
|
||
$generator->generateFieldTypeHash(self::PARAMETER, $parameter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\AdminUi\REST\Generator; | ||
|
||
use Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorInterface; | ||
use Ibexa\Contracts\Core\Repository\Values\User\User; | ||
use Ibexa\Contracts\Rest\Output\Generator; | ||
use Ibexa\Contracts\Rest\Output\Visitor; | ||
|
||
final class UserConfigRestGenerator implements ApplicationConfigRestGeneratorInterface | ||
{ | ||
private const NAMESPACE = 'user'; | ||
private const PARAMETER = 'user'; | ||
|
||
public function supportsNamespace(string $namespace): bool | ||
{ | ||
return self::NAMESPACE === $namespace; | ||
} | ||
|
||
public function supportsParameter(string $parameterName): bool | ||
{ | ||
return self::PARAMETER === $parameterName; | ||
} | ||
|
||
public function generate($parameter, Generator $generator, Visitor $visitor): void | ||
{ | ||
if ($parameter instanceof User) { | ||
$generator->startHashElement(self::PARAMETER); | ||
$visitor->visitValueObject($parameter); | ||
$generator->endHashElement(self::PARAMETER); | ||
|
||
return; | ||
} | ||
|
||
$generator->generateFieldTypeHash(self::PARAMETER, $parameter); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/lib/REST/Output/ValueObjectVisitor/ApplicationConfigVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\AdminUi\REST\Output\ValueObjectVisitor; | ||
|
||
use Ibexa\Contracts\AdminUi\REST\ApplicationConfigRestGeneratorRegistryInterface; | ||
use Ibexa\Contracts\Rest\Output\Generator; | ||
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor; | ||
use Ibexa\Contracts\Rest\Output\Visitor; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ApplicationConfigVisitor extends ValueObjectVisitor | ||
{ | ||
private ApplicationConfigRestGeneratorRegistryInterface $applicationConfigRestGeneratorRegistry; | ||
|
||
public function __construct(ApplicationConfigRestGeneratorRegistryInterface $applicationConfigRestGeneratorRegistry) | ||
{ | ||
$this->applicationConfigRestGeneratorRegistry = $applicationConfigRestGeneratorRegistry; | ||
} | ||
|
||
/** | ||
* @param \Ibexa\AdminUi\REST\Value\ApplicationConfig $data | ||
*/ | ||
public function visit(Visitor $visitor, Generator $generator, $data): void | ||
{ | ||
$generator->startObjectElement('ApplicationConfig'); | ||
$visitor->setHeader('Content-Type', $generator->getMediaType('ApplicationConfig')); | ||
|
||
foreach ($data->getConfig() as $namespace => $config) { | ||
// Checks if namespace has internal generators to generate custom output. | ||
if ($this->applicationConfigRestGeneratorRegistry->hasGenerators($namespace)) { | ||
$this->visitInternalGenerator( | ||
$visitor, | ||
$generator, | ||
$namespace, | ||
$config | ||
); | ||
|
||
continue; | ||
} | ||
|
||
$generator->generateFieldTypeHash($namespace, $config); | ||
} | ||
|
||
$generator->endObjectElement('ApplicationConfig'); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function visitInternalGenerator( | ||
Visitor $visitor, | ||
Generator $generator, | ||
string $namespace, | ||
array $config | ||
): void { | ||
$generator->startHashElement($namespace); | ||
|
||
foreach ($config as $name => $value) { | ||
if (!$this->applicationConfigRestGeneratorRegistry->hasGenerator($namespace, $name)) { | ||
$generator->generateFieldTypeHash($name, $value); | ||
|
||
continue; | ||
} | ||
|
||
$this->applicationConfigRestGeneratorRegistry | ||
->getGenerator($namespace, $name) | ||
->generate($value, $generator, $visitor); | ||
} | ||
|
||
$generator->endHashElement($namespace); | ||
} | ||
} |
Oops, something went wrong.