Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Phalcon\Events\Manager->attach('event') fails when preceded by ->dettach('event'). #1331

Closed
temuri416 opened this issue Oct 3, 2013 · 5 comments

Comments

@temuri416
Copy link
Contributor

Attaching event listeners by event name fails if preceded by detachment of all listeners for that type.

The code:

class AuthWrapper
{
    private $em;

    function setEventsManager(Phalcon\Events\Manager $em)
    {
        $this->em = $em;
    }

    function getEventsManager()
    {
        return $this->em;
    }
}

class LogToFile {}
class LogToDb {}

/**
 * Configure DI.
 */
$di = new Phalcon\Di;
$di->set('auth', function() use ($di) {
    $auth = new AuthWrapper;
    $eventsManager = new Phalcon\Events\Manager;
    $eventsManager->attach('log', $di->get('fileBased'));
    $auth->setEventsManager($eventsManager);
    return $auth;
});
$di->set('fileBased', 'LogToFile');
$di->set('dbBased', 'LogToDb');

/**
 * Fetch AuthWrapper with default file-based log writer.
 *
 * @var AuthWrapper
 */
$auth = $di->get('auth');

/**
 * Clear all existing file-based log listeners.
 */
$auth->getEventsManager()->dettachAll('log');

// * $auth->em->_events->log is now NULL.

/**
 * And use DB-based log listeners instead.
 */
$auth->getEventsManager()->attach('log', $di->get('dbBased'));

Execution of above code results in:

PHP Warning: Cannot use a scalar value as an array in AttachDetach.php

No listener is set for 'log' event. _$auth->em->events->log remains NULL.

I am on Phalcon 1.3.0.

Thanks!

@temuri416
Copy link
Contributor Author

On a side note - would be nice if ->dettach() is renamed to ->detach() :-)

@Cinderella-Man
Copy link
Contributor

I debugged the flow of class and in file ext/events/manager.c, before line 154 there should be a check is priority_queue an array (after 'dettach' it's null) and this causes phalcon_array_append to print the warning and stop assigning new values. I will try to add fix today, as long as somebody from Phalcon team won't do that before me :)

All the best,
Kamil

@temuri416
Copy link
Contributor Author

@Cinderella-Man

Dzieki!

@Cinderella-Man
Copy link
Contributor

Fix is already merged to 1.3.0 - happy coding!

@temuri416
Copy link
Contributor Author

Thanks!

@phalcon phalcon closed this as completed Oct 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants