2

リレーションシップの関連付けを含むバンドルを作成しています。ResolveTargetEntities物事を抽象的に保つために、Doctrine の新しいリスナーを使用したいと思います。

問題は、リスナーのセットアップを自動化したいということです。そのため、私のバンドルを使用する将来の開発者は、リスナー自体を構成する必要がありません。

私のバンドルには、リスナーdata_classのセットアップに使用したいという構成パラメーターがあります。ResolveTargetEntities

# app/config/config.yml
my_bundle:
    City:
        data_class: Acme\DemoBundle\Entity\City

このパラメーターを使用してリスナーを構成するために、バンドル内のサービスまたは構成ファイルをセットアップするにはどうすればよいですか? このようなもの:

resolve_target_entities:
    Dev\MyBundle\Model\City: %my_bundle.City.data_class%

編集:

上記の構成例は、ドクトリンによって達成されるべきことを示すために提供されていますが、この質問の目的はResolveTargetEntities、サービス、依存性注入コンテナー、またはエンドユーザーを必要とするその他の方法を使用して、リスナーを自動的にセットアップする方法を見つけることです。my_bundle名前空間の下に 1 つのパラメーターのみを指定するには、次のようにします。data_class

4

3 に答える 3

4

そのために、DependancyInjection フォルダーで Bundle Extension クラスを使用できます。カスタム構成を別のバンドル構成に追加するオプションがあります ( http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html )。同じことを行う私のコードは以下のとおりです。

<?php

namespace Zveen\CmsBundle\DependencyInjection;

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

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class ZveenCmsExtension extends Extension implements PrependExtensionInterface
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('zveen.cmsbundle.tenant.class', $config['tenant']['class']);
        $container->setParameter('zveen.cmsbundle.tenant.dql', $config['tenant']['class']);

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

    /**
     * Allow an extension to prepend the extension configurations.
     *
     * @param ContainerBuilder $container
     */
    public function prepend(ContainerBuilder $container)
    {
        // get all Bundles
        $bundles = $container->getParameter('kernel.bundles');
        if (isset($bundles['DoctrineBundle'])) {
            // Get configuration of our own bundle
            $configs = $container->getExtensionConfig($this->getAlias());
            $config = $this->processConfiguration(new Configuration(), $configs);

            // Prepare for insertion
            $forInsertion = array(
                'orm' => array(
                    'resolve_target_entities' => array(
                        'Zveen\CmsBundle\Entity\TenantInterface' => $config['tenant']['class']
                    )
                )
            );
            foreach ($container->getExtensions() as $name => $extension) {
                switch ($name) {
                    case 'doctrine':
                        $container->prependExtensionConfig($name, $forInsertion);
                        break;
                }
            }
        }
    }
}
于 2014-02-21T10:49:28.397 に答える
0

書いてみてください

my_bundle.City.data_class: Acme\DemoBundle\Entity\City

parameters.yml ファイルでは、うまくいきました。

于 2013-01-21T02:27:20.160 に答える