php Symfony:不带捆绑包的加载扩展

vh0rcniy  于 2023-11-16  发布在  PHP
关注(0)|答案(1)|浏览(131)

我有一个Symfony 5.4应用程序。我在我的config/packages/文件夹中创建了一个sso_settings.yml
没有bundle结构,所以我在src/文件夹下创建了一个DependencyInjection文件夹,其中包含文件AppConfiguration.php和文件AppExtension.php
因为我没有bundle,所以我在Kernel.php类中注册了我的Extension,

protected function build(ContainerBuilder $container): void
    {
        $container->registerExtension(new AppExtension());
    }

字符串
AppConfiguration.php

<?php

declare(strict_types=1);

namespace App\DependencyInjection;

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

class AppConfiguration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('sso_settings');

        $treeBuilder->getRootNode()
            ->children()
                ->arrayNode('siam_role_hierarchy')
                    ->useAttributeAsKey('id')
                    ->prototype('scalar')
                    ->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}


AppExtension.php

<?php

declare(strict_types=1);

namespace App\DependencyInjection;

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

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new AppConfiguration();
        $processedConfig = $this->processConfiguration($configuration, $configs);

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

    public function getAlias(): string
    {
        return 'sso_settings';
    }
}


sso_settings.yaml的路径是正确的。
但现在我得到了这个错误:

There is no extension able to load the configuration for "sso_settings" (in "/var/www/app/src/DependencyInjection/../../config/packages/sso_settings.yaml"). Looked for namespace "sso_settings", found "none".


有人能帮我吗?我找不到问题。

vcirk6k6

vcirk6k61#

我找到了一个正确的方法来创建一个全局应用程序配置。以下是使其工作所需的类和文件。
src/DependencyInjection/AppExtension.php处创建外部类:

<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class AppExtension extends Extension
{
    public function getAlias(): string
    {
        return 'app';
    }

    public function load(array $configs, ContainerBuilder $container)
    {
        /*
        $configs = [
            0 => [
               "hello_world" => "!"
            ]
        ]
        */
    }

    public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
    {
        return new AppConfiguration();
    }
}

字符串
src/DependencyInjection/AppConfiguration.php上创建相关的配置类:

<?php

namespace App\DependencyInjection;

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

class AppConfiguration implements ConfigurationInterface
{
    public function getConfigTreeBuilder(): TreeBuilder
    {
        $treeBuilder = new TreeBuilder('app');

        $treeBuilder->getRootNode()
            ->children()
                ->scalarNode('hello_world')->end()
            ->end()
        ;

        return $treeBuilder;
    }
}


在准备内核时通过编辑src/AppKernel.php(或您的内核位置)注册扩展:

<?php

namespace App;

// use ...many classes...

class Kernel extends BaseKernel
{
    // ...cut..
    protected function prepareContainer(ContainerBuilder $container)
    {
        $container->registerExtension(new AppExtension());

        parent::prepareContainer($container);
    }
    // ...cut..
}


最后,在config/packages/app.php(或者yaml,如果你喜欢的话)下使用你的配置:

<?php

use Symfony\Config\AppConfig;

return static function (AppConfig $config): void {
    $config->helloWorld('!');
};


请注意,我使用了一个通用的“app”别名,来回答这个问题,并根据“sso_settings”上下文进行调整。
希望这对你有帮助!

相关问题