2013-09-04 7 views
5

私はsymfonyの標準版(2.3)でsymfonysのコンソールイベントにフックしようとしていますが、動作しません。symfonyでコンソールイベントを聞くにはどうしたらいいですか?

私は彼らのexampleに応じてリスナーを作成し、guides on event registration従ってください:メーリングリスト上

namespace Acme\DemoBundle\EventListener; 

use Symfony\Component\Console\Event\ConsoleCommandEvent; 
use Symfony\Component\Console\ConsoleEvents; 

class AcmeCommandListener 
{ 
    public function onConsoleCommand(ConsoleCommandEvent $event) { 
     // get the output instance 
     $output = $event->getOutput(); 

     // get the command to be executed 
     $command = $event->getCommand(); 

     // write something about the command 
     $output->writeln(sprintf('Before running command <info>%s</info>', $command->getName())); 
    } 
} 

と誰かがサービスコンテナ内のイベントとして登録するために私に言いました。だから私はこれをした:

services: 
    kernel.listener.command_dispatch: 
     class: Acme\DemoBundle\EventListener\AcmeCommandListener 
     tags: 
      - { name: kernel.event_listener, event: console.command } 

しかし、明らかにタグ付けが正しくないと私はそれの正しい名前を見つけることができません。どうすればいい?

+0

あなたは 'php app/console'またはカスタムアプリケーションを使用していますか? – Touki

+0

私はSymfonyの標準版を使用しており、 'app/console'コマンドが実行される前にイベントの発生を追加したいと考えています。私はすでにsymfonyのGoogleグループに尋ねました。誰かが私が上で試したようにサービスを使って設定する必要があると言いました。 – acme

答えて

1

私はついにそれを手に入れました。上記のコードは元の投稿では完全に動作していますが、私のservices.ymlはアプリケーションの設定ではなくapp/config.ymlです。つまり、構成はロードされませんでした。私は、コンテナの拡張機能を経由して設定をインポートする必要がありました:

# Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php 
namespace Acme\DemoBundle\DependencyInjection; 

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

class AcmeDemoExtension extends Extension 
{ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

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

# Acme/DemoBundle/DependencyInjection/Configuration.php 
namespace Acme\DemoBundle\DependencyInjection; 

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

class Configuration implements ConfigurationInterface 
{ 
    public function getConfigTreeBuilder() 
    { 
     $treeBuilder = new TreeBuilder(); 
     $rootNode = $treeBuilder->root('headwork_legacy'); 
     return $treeBuilder; 
    } 
} 

私はあなたにも$configuration = new Configuration();一部とConfigurationクラスを除外することができますねけど。

関連する問題