From 8149b9494defae6db82853bff027fb29570c44ea Mon Sep 17 00:00:00 2001 From: Brabli <67018167+Brabli@users.noreply.github.com> Date: Sun, 14 Aug 2022 15:16:00 +0100 Subject: [PATCH] Sort of get config to work --- README.md | 2 +- config/bundles.php | 5 +++ config/packages/pcm_icon.yaml | 19 ++++++++++++ config/{services.yml => services.yaml} | 11 +++---- src/DependencyInjection/Configuration.php | 30 ++++++++++++++++++ src/DependencyInjection/PcmIconExtension.php | 32 ++++++++++++++++++++ src/PcmIconBundle.php | 17 +++++++---- src/Twig/Functions/IconExtension.php | 4 ++- tests/{AppKernel.php => TestKernel.php} | 6 ++-- tests/Twig/Functions/IconExtensionTest.php | 5 +-- tests/services_test.yml | 13 -------- 11 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 config/bundles.php create mode 100644 config/packages/pcm_icon.yaml rename config/{services.yml => services.yaml} (55%) create mode 100644 src/DependencyInjection/Configuration.php create mode 100644 src/DependencyInjection/PcmIconExtension.php rename tests/{AppKernel.php => TestKernel.php} (72%) delete mode 100644 tests/services_test.yml diff --git a/README.md b/README.md index a850bc0..13acf09 100644 --- a/README.md +++ b/README.md @@ -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. # 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) diff --git a/config/bundles.php b/config/bundles.php new file mode 100644 index 0000000..9d76e09 --- /dev/null +++ b/config/bundles.php @@ -0,0 +1,5 @@ + ['all' => true], +]; diff --git a/config/packages/pcm_icon.yaml b/config/packages/pcm_icon.yaml new file mode 100644 index 0000000..d610470 --- /dev/null +++ b/config/packages/pcm_icon.yaml @@ -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: + - [] + diff --git a/config/services.yml b/config/services.yaml similarity index 55% rename from config/services.yml rename to config/services.yaml index 7e447ae..fd90929 100644 --- a/config/services.yml +++ b/config/services.yaml @@ -4,13 +4,12 @@ services: autoconfigure: true pcm_icon.icon_extension: - alias: Pcm\IconBundle\Twig\Functions\IconExtension public: true - + class: Pcm\IconBundle\Twig\Functions\IconExtension + arguments: + $directories: [] + $palletes: [] Pcm\IconBundle\Twig\Functions\IconExtension: - tags: ['twig.extension'] public: false - arguments: - $directories: - - '%kernel.project_dir%/public/icons' + alias: pcm_icon.icon_extension diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..afd042b --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,30 @@ +getRootNode() + ->children() + ->arrayNode('directories') + ->scalarPrototype() + ->end() + ->end() + ->arrayNode('palletes')->variablePrototype()->end() + + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/src/DependencyInjection/PcmIconExtension.php b/src/DependencyInjection/PcmIconExtension.php new file mode 100644 index 0000000..43d645b --- /dev/null +++ b/src/DependencyInjection/PcmIconExtension.php @@ -0,0 +1,32 @@ +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); + } +} diff --git a/src/PcmIconBundle.php b/src/PcmIconBundle.php index d2c5edd..17eb2ce 100644 --- a/src/PcmIconBundle.php +++ b/src/PcmIconBundle.php @@ -4,16 +4,21 @@ declare(strict_types=1); 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\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\HttpKernel\Bundle\AbstractBundle; -class PcmIconBundle extends AbstractBundle { - public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void +class PcmIconBundle extends AbstractBundle +{ + public function getContainerExtension(): ?ExtensionInterface { - $loader = new YamlFileLoader($builder, new FileLocator(__DIR__.'/../config/')); - $loader->load('services.yml'); + if (null === $this->extension) { + $this->extension = new PcmIconExtension(); + } + + return $this->extension; } } diff --git a/src/Twig/Functions/IconExtension.php b/src/Twig/Functions/IconExtension.php index f39e842..85a4367 100644 --- a/src/Twig/Functions/IconExtension.php +++ b/src/Twig/Functions/IconExtension.php @@ -17,7 +17,9 @@ final class IconExtension extends AbstractExtension 'size' => self::DEFAULT_SIZE ]; - public function __construct(private array $directories) {} + public function __construct(private array $directories, private array $palletes) { + dump($this->directories); + } /** * @inheritDoc diff --git a/tests/AppKernel.php b/tests/TestKernel.php similarity index 72% rename from tests/AppKernel.php rename to tests/TestKernel.php index 432fa8b..39e28fe 100644 --- a/tests/AppKernel.php +++ b/tests/TestKernel.php @@ -5,11 +5,10 @@ declare(strict_types=1); namespace Pcm\IconBundle\Tests; use Pcm\IconBundle\PcmIconBundle; -use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\HttpKernel\Kernel; -class AppKernel extends Kernel +class TestKernel extends Kernel { /** * @return array @@ -17,7 +16,6 @@ class AppKernel extends Kernel public function registerBundles(): array { return [ - new FrameworkBundle(), new PcmIconBundle() ]; } @@ -27,6 +25,6 @@ class AppKernel extends Kernel */ public function registerContainerConfiguration(LoaderInterface $loader): void { - $loader->load(__DIR__.'/services_'.$this->getEnvironment().'.yml'); + $loader->load(__DIR__.'/../config/packages/pcm_icon.yaml'); } } diff --git a/tests/Twig/Functions/IconExtensionTest.php b/tests/Twig/Functions/IconExtensionTest.php index e633c39..1d1a392 100644 --- a/tests/Twig/Functions/IconExtensionTest.php +++ b/tests/Twig/Functions/IconExtensionTest.php @@ -4,7 +4,8 @@ declare(strict_types=1); 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\IconNotFound; use PHPUnit\Framework\TestCase; @@ -20,7 +21,7 @@ class IconExtensionTest extends TestCase protected function setUp(): void { - $kernel = new AppKernel('test', false); + $kernel = new TestKernel('test', true); $kernel->boot(); $this->icon = $kernel->getContainer()->get('pcm_icon.icon_extension'); } diff --git a/tests/services_test.yml b/tests/services_test.yml deleted file mode 100644 index 9a55bfc..0000000 --- a/tests/services_test.yml +++ /dev/null @@ -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'