Symfony6.2中的自定义捆绑包出现无效配置异常

pb3skfrl  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(159)

我试图将我的自定义包从symfony 3迁移到symfony 6.2支持,但是被InvalidConfigurationException卡住了。
我一直在遵循thisthis文档,但似乎遗漏了一个重要部分。
来源:中国科学院网站.php:

<?php

namespace Schoenef\HtmlToPdfBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class SchoenefHtmlToPdfBundle extends AbstractBundle
{

}

//相关性注入/schoenefHtmlToPdfExtension.php:

namespace Schoenef\HtmlToPdfBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class SchoenefHtmlToPdfExtension extends Extension
{

    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
        $loader->load('services.yml');
    }

}

src/DependencyInjection/Configuration.php

<?php
namespace Schoenef\HtmlToPdfBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files
 * this is testing the configuration in the following manner:
 * html2pdf:
 *   provider: defualt pdfrocket
 *   timeout: default 20
 *   apikey: required
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{

    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'];
    const PROVIDER_PDF_ROCKET = 'pdfrocket';

    const KEY_PROVIDER = 'provider';
    const KEY_TIMEOUT = 'timeout';
    const KEY_APIKEY = 'apikey';
    const KEY_DEFAULT_OPTIONS = 'default_options';

    const OPTION_DPI = 'dpi';
    const OPTION_SHRINKING = 'shrinking';
    const OPTION_IMAGE_QUALITY = 'image_quality';
    const OPTION_PAGE_SIZE = 'page_size';
    const OPTION_ZOOM = 'zoom';
    const OPTION_JS_DELAY = 'js_delay';

    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder(): TreeBuilder
    {
        $treeBuilder = new TreeBuilder('schoenef_html_to_pdf');

        $treeBuilder->getRootNode()
            ->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();
        return $treeBuilder;
    }
}

有人能看出这个可能非常明显的错误吗?我只得到:

Unrecognized options "provider, timeout, apikey, default_options" under "schoenef_html_to_pdf". Available options are "".

非常感谢!

k7fdbhmy

k7fdbhmy1#

Symfony 6.1引入了一个新的AbstractBundle类。有了AbstractBundle,你不再需要单独的扩展和配置类。直到看到这个问题,我才意识到AbstractBundle与旧的Bundle类不兼容。
所以这个问题的简短答案就是扩展原始的Bundle类,一切都应该像以前一样工作。

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SchoenefHtmlToPdfBundle extends Bundle

一个稍微长一点的答案是继续并利用AbstractBundle的功能。

class CeradMyBundle extends AbstractBundle
{
  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'];
  const PROVIDER_PDF_ROCKET = 'pdfrocket';

  const KEY_PROVIDER = 'provider';
  const KEY_TIMEOUT = 'timeout';
  const KEY_APIKEY = 'apikey';
  const KEY_DEFAULT_OPTIONS = 'default_options';

  const OPTION_DPI = 'dpi';
  const OPTION_SHRINKING = 'shrinking';
  const OPTION_IMAGE_QUALITY = 'image_quality';
  const OPTION_PAGE_SIZE = 'page_size';
  const OPTION_ZOOM = 'zoom';
  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
  {
    $container->import('../config/services.yaml');
  }
}

您还可以考虑将配置内容移到它自己的config/definition.php文件中。

相关问题