-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
documentation & config validator as services, used by databox.
- Loading branch information
Showing
20 changed files
with
528 additions
and
272 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
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
use Alchemy\RenditionFactory\DocumentationDumper as RenditionFactoryDocumentationDumper; | ||
|
||
|
||
#[AsCommand('app:documentation:dump')] | ||
class DocumentationDumperCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly RenditionFactoryDocumentationDumper $renditionFactoryDocumentationDumper, | ||
) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
parent::configure(); | ||
|
||
$this | ||
->setName('app:documentation:dump') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('# rendition factory'); | ||
$output->writeln($this->renditionFactoryDocumentationDumper->dump()); | ||
|
||
return 0; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
databox/api/src/Validator/ValidRenditionDefinitionConstraint.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Validator; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
class ValidRenditionDefinitionConstraint extends Constraint | ||
{ | ||
public function getTargets(): string|array | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
databox/api/src/Validator/ValidRenditionDefinitionConstraintValidator.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Validator; | ||
|
||
|
||
use Alchemy\RenditionFactory\Config\Validator; | ||
use Alchemy\RenditionFactory\Config\YamlLoader; | ||
use App\Entity\Core\RenditionDefinition; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
class ValidRenditionDefinitionConstraintValidator extends ConstraintValidator | ||
{ | ||
public function __construct(private readonly YamlLoader $yamlLoader, private readonly Validator $validator) | ||
{ | ||
} | ||
|
||
/** | ||
* @param RenditionDefinition $value | ||
* @param ValidRenditionDefinitionConstraint $constraint | ||
*/ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
try { | ||
$config = $this->yamlLoader->parse($value->getDefinition()); | ||
$this->validator->validate($config); | ||
} catch (\Throwable $e) { | ||
$this->context | ||
->buildViolation($e->getMessage()) | ||
->addViolation(); | ||
} | ||
} | ||
} |
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
123 changes: 0 additions & 123 deletions
123
lib/php/rendition-factory/src/Command/ConfigCommand.php
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
lib/php/rendition-factory/src/Command/ConfigurationValidateCommand.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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Alchemy\RenditionFactory\Command; | ||
|
||
use Alchemy\RenditionFactory\Config\Validator; | ||
use Alchemy\RenditionFactory\Config\YamlLoader; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand('alchemy:rendition-factory:conf:validate')] | ||
class ConfigurationValidateCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly YamlLoader $yamlLoader, | ||
private readonly Validator $validator, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
parent::configure(); | ||
|
||
$this->addArgument('config', InputArgument::REQUIRED, 'A build config YAML file to validate') | ||
->setHelp('Validate a config file.') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$config = $this->yamlLoader->load($input->getArgument('config')); | ||
$this->validator->validate($config); | ||
|
||
$output->writeln('Configuration is valid.'); | ||
|
||
return 0; | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace Alchemy\RenditionFactory\Config; | ||
|
||
use Alchemy\RenditionFactory\DTO\BuildConfig\BuildConfig; | ||
use Alchemy\RenditionFactory\DTO\FamilyEnum; | ||
use Alchemy\RenditionFactory\Transformer\TransformerModuleInterface; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
|
||
class Validator | ||
{ | ||
public function __construct( | ||
#[TaggedLocator(TransformerModuleInterface::TAG, defaultIndexMethod: 'getName')] | ||
private readonly ServiceLocator $transformers, | ||
) { | ||
|
||
} | ||
|
||
public function getTransformers(): ServiceLocator | ||
{ | ||
return $this->transformers; | ||
} | ||
|
||
public function validate(BuildConfig $config): void | ||
{ | ||
foreach (FamilyEnum::cases() as $family) { | ||
$familyConfig = $config->getFamily($family); | ||
if (null === $familyConfig) { | ||
continue; | ||
} | ||
foreach ($familyConfig->getTransformations() as $transformation) { | ||
$transformerName = $transformation->getModule(); | ||
|
||
/** @var TransformerModuleInterface $transformer */ | ||
$transformer = $this->transformers->get($transformerName); | ||
|
||
try { | ||
$this->checkTransformerConfiguration($transformer, $transformation->toArray()); | ||
} catch (\Throwable $e) { | ||
$msg = sprintf("Error in module \"%s\"\n%s", $transformerName, $e->getMessage()); | ||
throw new InvalidConfigurationException($msg); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private function checkTransformerConfiguration(TransformerModuleInterface $transformer, array $options): void | ||
{ | ||
$documentation = $transformer->getDocumentation(); | ||
$treeBuilder = $documentation->getTreeBuilder(); | ||
|
||
$processor = new Processor(); | ||
$processor->process($treeBuilder->buildTree(), ['root' => $options]); | ||
} | ||
} |
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
Oops, something went wrong.