Search code examples
symfonybundlesymfony6

symfony 6.2: EnvParameterException: Environment variables "resolve:..." are never used. Please, check your container's configuration


I have a custom bundle, which I try to configure with an environment variable. I use this technique with other bundles as well, but here I get this unexpected error:

Environment variables "resolve:HTML_TO_PDF_API_KEY" are never used. Please, check your container's configuration.

Full Error Trace

config/packages/schoenef_html_to_pdf.yaml:

schoenef_html_to_pdf:
  provider: pdfrocket
  timeout: 40
  apikey: '%env(resolve:HTML_TO_PDF_API_KEY)%'
  default_options:
    shrinking: false
    dpi: 300
    image_quality: 100
    page_size: A4
    zoom: 1.335

My .env file:

// ...
HTML_TO_PDF_API_KEY=change_me

Is there anything hidden I'm overlooking here?

Update 1

It feels like it is somehow connected to my custom bundle service defintion:

/vendor/schoenef/html-to-pdf-bundle/config/services.yml:

services:
  Schoenef\HtmlToPdfBundle\Service\Html2PdfConnector:
    arguments:
      $connectorConfig: '%schoenef_html_to_pdf%'

results now in: ** You have requested a non-existent parameter "schoenef_html_to_pdf"**

This makes sense, as it is not a parameter, but a native configuration.


Solution

  • So it was a complete mixup during the migration of the bundle from symfony 3 to 6. For this simple bundle to inject a reusable service it is enough to just use the new AbstractBundle. Thx also to this question.

    /vendor/schoenef/html-to-pdf-bundle/src/SchoenefHtmlToPdfBundle.php:

    <?php
    
    namespace Schoenef\HtmlToPdfBundle;
    
    use Schoenef\HtmlToPdfBundle\Service\Html2PdfConnector;
    use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
    use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
    
    class SchoenefHtmlToPdfBundle extends AbstractBundle
    {
    
        public const pageSizes = ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B0', 'B1', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'C5E', 'Comm10E', 'DLE', 'Executive', 'Folio', 'Ledger', 'Legal', 'Letter', 'Tabloid'];
        public const PROVIDER_PDF_ROCKET = 'pdfrocket';
    
        public const KEY_PROVIDER = 'provider';
        public const KEY_TIMEOUT = 'timeout';
        public const KEY_APIKEY = 'apikey';
        public const KEY_DEFAULT_OPTIONS = 'default_options';
    
        public const OPTION_DPI = 'dpi';
        public const OPTION_SHRINKING = 'shrinking';
        public const OPTION_IMAGE_QUALITY = 'image_quality';
        public const OPTION_PAGE_SIZE = 'page_size';
        public const OPTION_ZOOM = 'zoom';
        public const OPTION_JS_DELAY = 'js_delay';
    
        public function configure(DefinitionConfigurator $definition): void
        {
            $definition->rootNode()
                ->children()
                    ->enumNode(self::KEY_PROVIDER)->values([self::PROVIDER_PDF_ROCKET])->defaultValue(self::PROVIDER_PDF_ROCKET)->end()
                    ->integerNode(self::KEY_TIMEOUT)->defaultValue(20)->end()
                    ->scalarNode(self::KEY_APIKEY)->isRequired()->end()
                    ->arrayNode(self::KEY_DEFAULT_OPTIONS)
                        ->children()
                            ->integerNode(self::OPTION_DPI)->end()
                            ->floatNode(self::OPTION_ZOOM)->end()
                            ->integerNode(self::OPTION_JS_DELAY)->end()
                            ->booleanNode(self::OPTION_SHRINKING)->defaultTrue()->end()
                            ->integerNode(self::OPTION_IMAGE_QUALITY)->end()
                            ->enumNode(self::OPTION_PAGE_SIZE)->values(self::pageSizes)->end()
                        ->end()
                    ->end()
                ->end();
        }
    
    
        public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
        {
            // load an XML, PHP or Yaml file
            $container->import('../config/services.yml');
    
            $container->services()->get(Html2PdfConnector::class)->arg(0, $config);
        }
    }
    

    And I skipped everything in DependencyInjection. You can see the complete working bundle here: https://github.com/Andreas-Schoenefeldt/SchoenefHtmlToPdfBundle/tree/dev