I have command that extends Symfony Command class and I want to create and call event in it. In Nette\Object I would just create onProperty and @method onProperty with needed params.

How to make the same thing without Nette\Object? In particular, how would onProperty method would look like? Thanks

You can have injected EventManager and call dispatch() on it.

Great, I also found solution in Nette forum.

Full code looks like this:

class ProductImport extends Command
{
    /** @var EventManager */
    private $evm;


    public function __construct(EventManager $evm)
    {
        $this->evm = $evm;
    }


    protected function execute(...)
    {
        // ...
        $this->onImport($products, $output);
    }


    public function onImport($products, $output)
    {
        $this->evm->dispatchEvent(__METHOD__, new EventArgsList([$products, $output]));
    }

}

Hi I have exactly the same code but the listener's method isn't called. Any idea what could be a problem?

namespace App\Console;

use Kdyby\Events\EventManager;
use Kdyby\Events\EventArgsList;

class PaymentsMatchCommand extends Command
{
    /** @var EventManager */
   private $evm;

    public $onPaid = [];

    public function __construct(...  EventManager $evm, $name = NULL)
    {
        parent::__construct($name);
        ...
        $this->evm = $evm;
    }

protected function execute(InputInterface $input, OutputInterface $output)
   {
              ...
        $this->matchPayments($input->getArgument('from'), $input->getArgument('to'));
   }

private function matchPayments($from = '-1 month', $to = 'now')
   {
      $this->onPaid($waitingPayment->userMembership);
}

public function onPaid($userMembershipEntity)
   {
    $this->evm->dispatchEvent(__METHOD__, new EventArgsList([$userMembershipEntity]));
   }

neon:

services:
    paymentsMatchCommand:
        class: App\Console\PaymentsMatchCommand
        tags: [kdyby.console.command]

listener: jeho metoda se nezavolá

...
public function onPaid(\App\Model\Database\Entity\UserMembership $userMembershipEntity)
    {  \Tracy\Debugger::barDump('test');
        $this->paidEmailFactory->setUserMembership($userMembershipEntity)
                ->send();
    }
  • Comments:

  • Jak máš definované getSubscribedEvents() ?

    by Filip Procházka at 15.5.2015 18:21
public function getSubscribedEvents()
    {
        return [
            'App\Model\PasswordProcess::onForgottenPasswordRequested' => 'onForgottenPasswordRequested',
            'App\Model\SubscriptionProcess::onAfterSubscribed' => 'onAfterSubscribed',
            'App\Console\MatchPaymentsCommand::onPaid' => 'onPaid'
        ];
    }
  • Comments:

  • PaymentsMatchCommand vs. MatchPaymentsCommand

    by Michal Vyšinský at 18.5.2015 9:36
  • :-) ach jo.. díky

    by Oggy at 18.5.2015 11:43
  • Comments:

  • používám Sublime Text na macu..

    by Oggy at 18.5.2015 13:09

You must first log in to participate in this discussion