Sort of get config to work

This commit is contained in:
Brabli
2022-08-14 15:16:00 +01:00
parent 03b705cf5f
commit 8149b9494d
11 changed files with 111 additions and 33 deletions

View File

@@ -7,5 +7,5 @@ To get started, add a bundle name and description inside of `composer.json`. Add
Change the **class name** and **namespace** of the bundle file in `/src`. Change the **file name** to match the class name also. Change the **class name** and **namespace** of the bundle file in `/src`. Change the **file name** to match the class name also.
# Conventions # Conventions
- `YAML` files must end with `.yml` - `YAML` files must end with `.yaml`
- Namespaces should always start with `Pcm\ExampleBundle` (change `Example` to whatever is appropriate) - Namespaces should always start with `Pcm\ExampleBundle` (change `Example` to whatever is appropriate)

5
config/bundles.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
return [
Pcm\IconBundle\PcmIconBundle::class => ['all' => true],
];

View File

@@ -0,0 +1,19 @@
when@dev:
pcm_icon:
directories:
- '%kernel.project_dir%/public/icons'
palletes: []
when@prod:
pcm_icon:
directories:
- '%kernel.project_dir%/public/icons'
palletes: []
when@test:
pcm_icon:
directories:
- '%kernel.project_dir%/tests/icons'
palletes:
- []

View File

@@ -4,13 +4,12 @@ services:
autoconfigure: true autoconfigure: true
pcm_icon.icon_extension: pcm_icon.icon_extension:
alias: Pcm\IconBundle\Twig\Functions\IconExtension
public: true public: true
class: Pcm\IconBundle\Twig\Functions\IconExtension
arguments:
$directories: []
$palletes: []
Pcm\IconBundle\Twig\Functions\IconExtension: Pcm\IconBundle\Twig\Functions\IconExtension:
tags: ['twig.extension']
public: false public: false
arguments: alias: pcm_icon.icon_extension
$directories:
- '%kernel.project_dir%/public/icons'

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Pcm\IconBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('pcm_icon');
$treeBuilder->getRootNode()
->children()
->arrayNode('directories')
->scalarPrototype()
->end()
->end()
->arrayNode('palletes')->variablePrototype()->end()
->end()
->end()
;
return $treeBuilder;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Pcm\IconBundle\DependencyInjection;
use Pcm\IconBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class PcmIconExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.yaml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$definition = $container->getDefinition('pcm_icon.icon_extension');
$definition->replaceArgument('$directories', $config['directories']);
$definition->replaceArgument('$palletes', $config['palletes']);
dump($configs);
}
}

View File

@@ -4,16 +4,21 @@ declare(strict_types=1);
namespace Pcm\IconBundle; namespace Pcm\IconBundle;
use Symfony\Component\Config\FileLocator; use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle; use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class PcmIconBundle extends AbstractBundle { class PcmIconBundle extends AbstractBundle
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void {
public function getContainerExtension(): ?ExtensionInterface
{ {
$loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/')); if (null === $this->extension) {
$loader->load('services.yml'); $this->extension = new PcmIconExtension();
}
return $this->extension;
} }
} }

View File

@@ -17,7 +17,9 @@ final class IconExtension extends AbstractExtension
'size' => self::DEFAULT_SIZE 'size' => self::DEFAULT_SIZE
]; ];
public function __construct(private array $directories) {} public function __construct(private array $directories, private array $palletes) {
dump($this->directories);
}
/** /**
* @inheritDoc * @inheritDoc

View File

@@ -5,11 +5,10 @@ declare(strict_types=1);
namespace Pcm\IconBundle\Tests; namespace Pcm\IconBundle\Tests;
use Pcm\IconBundle\PcmIconBundle; use Pcm\IconBundle\PcmIconBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\Kernel;
class AppKernel extends Kernel class TestKernel extends Kernel
{ {
/** /**
* @return array * @return array
@@ -17,7 +16,6 @@ class AppKernel extends Kernel
public function registerBundles(): array public function registerBundles(): array
{ {
return [ return [
new FrameworkBundle(),
new PcmIconBundle() new PcmIconBundle()
]; ];
} }
@@ -27,6 +25,6 @@ class AppKernel extends Kernel
*/ */
public function registerContainerConfiguration(LoaderInterface $loader): void public function registerContainerConfiguration(LoaderInterface $loader): void
{ {
$loader->load(__DIR__.'/services_'.$this->getEnvironment().'.yml'); $loader->load(__DIR__.'/../config/packages/pcm_icon.yaml');
} }
} }

View File

@@ -4,7 +4,8 @@ declare(strict_types=1);
namespace Pcm\IconBundle\Tests\Twig\Functions; namespace Pcm\IconBundle\Tests\Twig\Functions;
use Pcm\IconBundle\Tests\AppKernel; use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
use Pcm\IconBundle\Tests\TestKernel;
use Pcm\IconBundle\Twig\Functions\IconExtension; use Pcm\IconBundle\Twig\Functions\IconExtension;
use Pcm\IconBundle\Twig\Functions\IconNotFound; use Pcm\IconBundle\Twig\Functions\IconNotFound;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@@ -20,7 +21,7 @@ class IconExtensionTest extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
$kernel = new AppKernel('test', false); $kernel = new TestKernel('test', true);
$kernel->boot(); $kernel->boot();
$this->icon = $kernel->getContainer()->get('pcm_icon.icon_extension'); $this->icon = $kernel->getContainer()->get('pcm_icon.icon_extension');
} }

View File

@@ -1,13 +0,0 @@
# This config is only here to stop a deprecation notice for Symfony 7
framework:
http_method_override: false
services:
pcm_icon.icon_extension:
alias: Pcm\IconBundle\Twig\Functions\IconExtension
public: true
Pcm\IconBundle\Twig\Functions\IconExtension:
arguments:
$directories:
- '%kernel.project_dir%/tests/icons'