Symfony2 - 如何扩展供应商捆绑(例如FOSFacebookBundle) [英] Symfony2 - how to extend a vendor bundle (e.g. of FOSFacebookBundle)
问题描述
场景
我正在使用一个软件包(FOSFacebookBundle),允许我在配置中为一个Facebook应用设置参数。一切工作都很好,但现在我不仅要设置一个应用程序,还可以设置多个。
I'm using a bundle (FOSFacebookBundle) that allows me to set parameters for exactly one facebook app in my configuration. Everything works perfectly fine, but now I need to set not only one app, but multiple.
我的方法:
我创建了一个AcmeFacebookBundle,它允许在像这样的数组中定义多个应用程序(在 Acme\FacebookBundle\DependencyInjection\Configuration 中定义的配置) :
I've created a AcmeFacebookBundle, which allows multiple apps to be defined (configuration defined in Acme\FacebookBundle\DependencyInjection\Configuration) in an array like so:
acme_facebook:
apps:
some_competition:
server_url: %acme.facebook.some_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook.some_competition.app_id%
secret: % acme .facebook.some_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
some_other_competition:
server_url: %acme.facebook. some_other_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook. some_other_competition.app_id%
secret: % acme .facebook. some_other_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
在 Acme\FacebookBundle\DependencyInjection\AcmeFacebookExtension 中,然后循环遍历所有应用程序。这个想法是将server_url参数与当前URL进行比较,并覆盖我的fos_facebook配置。
In Acme\FacebookBundle\DependencyInjection\AcmeFacebookExtension I am then looping through all apps. The idea is to compare the server_url parameter against the current URL and override the fos_facebook configuration with mine.
class AcmeFacebookExtension extends Extension
{
...
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config['apps'] as $app)
{
// check for matching path here?
foreach (array('file', 'app_id', 'secret', 'cookie', 'domain', 'logging', 'culture', 'permissions') as $attribute)
{
$container->setParameter('fos_facebook.' . $attribute, $app[$attribute]);
}
}
}
问题: / strong>
Problem:
但这正是我被卡住的地方。显然,我没有访问Request对象或AcmeFacebookExtension内的DiC进行此比较。
我的做法完全错了?你有什么更好的想法来解决这个问题吗?
But this is exactly where I'm stuck. Obviously, I have no access to the Request object or the DiC from within AcmeFacebookExtension to do this comparison. Am I going completely wrong in my approach? Do you have any better idea on how to tackle this problem?
推荐答案
你想要创建的是一个 CompilerPass
,以便在加载所有其他配置后,您可以操作 Container
。这些应该让你开始:
What you want to create is a CompilerPass
so that you can manipulate the Container
after all other configuration has loaded. These should get you started:
- Symfony2: Service Container Compiler Passes
- Symfony2: Manipulating Service Parameters and Definitions
- Create a
CompilerPass
这篇关于Symfony2 - 如何扩展供应商捆绑(例如FOSFacebookBundle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!