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

EventedMapper as a proxy #74 #3

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

EventedMapper as a proxy #74 #3

wants to merge 9 commits into from

Conversation

nawarian
Copy link

As described under this issue under Respect\Relational there's an implementation of a Proxy interface to add events onto Respect\Data.

This could be user as this:

$db = new \PDO('sqlite:test.db');
$aRelationalMapper = new Respect\Relational\Mapper($db);

$mapper = new Respect\Data\EventedMapper($aRelationalMapper);

// From now on we'll be using $mapper only

// First implementation, possibly a back-end one

// $eventName = "collectionName:eventName:suffix";
$mapper->on( 'person:insert:post', function($newPerson, $collection) {
    echo "New person created: [{$newPerson->id}] {$newPerson->name}";
} );

$person = new \stdClass();
$person->name = 'Nawarian';

$mapper->person->persist($person);
$mapper->flush(); // echoes "New person created: [1] Nawarian"

Same goes for every events: insert, update, delete. With suffixes: pre and post.
So there are six events to be listened:

  • insert:pre
  • insert:post
  • update:pre
  • update:post
  • delete:pre
  • delete:post

default:
$message = 'Invalid type of action provided for event manager';
throw new \InvalidArgumentException($message, 400);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this switch?

Can we use if instead?

    if (!(is_callable($action)) || !($action instanceof Listener)) {
        $message = 'Invalid type of action provided for event manager';
        throw new \InvalidArgumentException($message, 400);
    }

    // The block below you can use for control or remove it
    $events = $this->getEvents();
    if (array_key_exists("attach", $events)) {
        $this->dispatch("attach", array($eventName, $action));
    }

    $this->events[$eventName][] = $action;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably for future implementations or checks. I don't see any trouble by changing it.
@Rafael-BP could you?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no problem.

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

Successfully merging this pull request may close these issues.

None yet

3 participants