82 lines
2.8 KiB
PHP
82 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pcm\IconBundle\DependencyInjection;
|
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
|
|
|
class Configuration implements ConfigurationInterface
|
|
{
|
|
public const DEFAULT_SIZE = 32;
|
|
public const DEFAULT_COLOUR = 'primary';
|
|
|
|
public function getConfigTreeBuilder(): TreeBuilder
|
|
{
|
|
$treeBuilder = new TreeBuilder('pcm_icon');
|
|
$this->addValidationRules($treeBuilder->getRootNode());
|
|
|
|
return $treeBuilder;
|
|
}
|
|
|
|
private function addValidationRules(ArrayNodeDefinition $rootNode): void
|
|
{
|
|
// I've split the tree up like this because Intelephense was crying.
|
|
// Plus I think it's a little easier to read.
|
|
|
|
$rootNode
|
|
->children()
|
|
->arrayNode('default')
|
|
->addDefaultsIfNotSet()
|
|
->children()
|
|
->scalarNode('colour')->defaultValue(self::DEFAULT_COLOUR)->end()
|
|
->integerNode('size')->defaultValue(self::DEFAULT_SIZE)->end()
|
|
->end()
|
|
->end()
|
|
->end()
|
|
;
|
|
|
|
$rootNode
|
|
->children()
|
|
->arrayNode('directories')
|
|
->validate()
|
|
->ifEmpty()
|
|
->thenInvalid("Directories cannot be empty!")
|
|
->end()
|
|
->scalarPrototype()
|
|
->validate()
|
|
->ifTrue(fn($path) => !is_dir($path))
|
|
->thenInvalid('%s is not a directory!')
|
|
->end()
|
|
->end()
|
|
->end()
|
|
->end()
|
|
;
|
|
|
|
$rootNode
|
|
->children()
|
|
->arrayNode('colours')
|
|
->validate()
|
|
->ifEmpty()
|
|
->thenInvalid('Colours cannot be empty!')
|
|
->end()
|
|
|
|
->arrayPrototype()
|
|
->normalizeKeys(false) # Don't normalize hyphens into underscores etc
|
|
->children()
|
|
->scalarNode('fill')->isRequired()->end()
|
|
->scalarNode('stroke')->isRequired()->end()
|
|
->scalarNode('fill-hover')->isRequired()->end()
|
|
->scalarNode('stroke-hover')->isRequired()->end()
|
|
->scalarNode('fill-group-hover')->isRequired()->end()
|
|
->scalarNode('stroke-group-hover')->isRequired()->end()
|
|
->end()
|
|
->end()
|
|
->end()
|
|
->end()
|
|
;
|
|
}
|
|
}
|