Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[3.0] New Event system #153

Closed
wants to merge 5 commits into from

5 participants

@guilhermeblanco

No description provided.

lib/Doctrine/Common/Event/Event.php
((197 lines not shown))
+ return $this->propagationStopped;
+ }
+
+ /**
+ * If an event is cancelable, the preventDefault method is used to signify that the event is to
+ * be canceled, meaning any default action normally taken by the implementation as a result of
+ * the event will not occur. If, during any stage of event flow, the preventDefault method is
+ * called the event is canceled. Any default action associated with the event will not occur.
+ * Calling this method for a non-cancelable event has no effect. Once preventDefault has been
+ * called it will remain in effect throughout the remainder of the event's propagation. This
+ * method may be used during any stage of event flow.
+ *
+ */
+ public function preventDefault()
+ {
+ if ($this->cancellable) {
@schmittjoh Collaborator

Should we throw an exception if not? Something seems to be wrong in that case in the calling code, no?

@guilhermeblanco Owner

Yes. This is one of the things I was considering originally.
The point here is whether we should do that or not. If it's not cancellable, then it should still execute the default behavior, not block it. Sometimes I think we should just ignore, others we should shout at user.
The way that CallableEventListener is implemented, it automatically triggers preventDefault if user defined to return a value there. This may heavily affect this behavior if we throw Exception. I don't know....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/Common/Event/EventSubscriber.php
((32 lines not shown))
+ */
+abstract class EventSubscriber implements EventSubscriberInterface
+{
+ /**
+ * @see EventSubscriberInterface::addEventListeners
+ *
+ * @api
+ */
+ abstract public function addEventListeners(EventTarget $target);
+
+ /**
+ * @see EventSubscriberInterface::removeEventListeners
+ *
+ * @api
+ */
+ abstract public function removeEventListeners(EventTarget $target);
@stof Collaborator
stof added a note

why defining abstract methods for the method of the interface ? This is useless as the interface already enforces them (making the whole class useless btw)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on the diff
lib/Doctrine/Common/Event/EventTarget.php
((19 lines not shown))
+
+namespace Doctrine\Common\Event;
+
+/**
+ * Allow binding specific on its subclasses instances. Allow registration and
+ * removal of {@link EventListener}s on an EventTarget and dispatch events to
+ * that EventTarget.
+ *
+ * @api
+ *
+ * @abstract
+ * @link www.doctrine-project.org
+ * @since 2.3
+ * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
+ */
+abstract class EventTarget
@stof Collaborator
stof added a note

why making it abstract and then extending it with an empty class ?
Btw, you should probably add an interface

@stof Collaborator
stof added a note

ok, there is an interface, but you should implement it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/Common/Event/CallableEventListener.php
((65 lines not shown))
+ * @param mixed $target
+ */
+ public function setTarget($target)
+ {
+ $this->target = $target;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function handleEvent(Event $event)
+ {
+ $event->setCurrentTarget($this->getTarget());
+
+ // Automatically prevent default if return is used in callable
+ if (call_user_func($this->handler, $event)) {
@stof Collaborator
stof added a note

wouldn't it be better to prevent the default when the return value is false instead of doing it when true is returned ? The interface of your event system looks like the DOM event system so it would be logical to use the same way to prevent the default too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/Common/Event/CallableEventListener.php
((65 lines not shown))
+ * @param mixed $target
+ */
+ public function setTarget($target)
+ {
+ $this->target = $target;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function handleEvent(Event $event)
+ {
+ $event->setCurrentTarget($this->getTarget());
+
+ // Automatically prevent default if return is used in callable
+ if ( ! call_user_func($this->handler, $event) && $event->isCancellable()) {
@stof Collaborator
stof added a note

I would use false === to cancel only if the user returned false. currently, you cancel also on null, meaning the default behavior for an handler is cancelling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@guilhermeblanco guilhermeblanco Started tests on new Event system. Added generic support for lists (p…
…lanning for cached support). General optimizations adnd changes. Simplified API.
c440291
@Ocramius Ocramius commented on the diff
lib/Doctrine/Common/Event/EventListenerList.php
((69 lines not shown))
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function size($type)
+ {
+ return count($this->listenerList[$type]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function get($type)
+ {
+ if (isset($this->sortedListenerList[$type]) && ! $this->sortedListenerList[$type]) {
@Ocramius Owner

What about using an SplPriorityQueue instead of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius Ocramius commented on the diff
lib/Doctrine/Common/Event/EventListenerList.php
((37 lines not shown))
+ * @var array
+ */
+ protected $listenerList = array();
+
+ /**
+ * @var array
+ */
+ private $sortedListenerList = array();
+
+ /**
+ * {@inheritdoc}
+ */
+ public function add(EventListenerInterface $listener)
+ {
+ $type = $listener->getType();
+ $hash = spl_object_hash($listener);
@Ocramius Owner

Is it by design that you cannot add the same listener twice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@henrikbjorn henrikbjorn commented on the diff
lib/Doctrine/Common/Event/Event.php
((80 lines not shown))
+ *
+ * @var boolean Flag holding if event is assigned to stop propagating to other EventListeners.
+ */
+ protected $propagationStopped = false;
+
+ /**
+ * Constructor.
+ *
+ * @param string $type Event type
+ * @param boolean $cancellable Specifies whether or not the event's default action can be prevented.
+ */
+ public function __construct($type, $cancellable = false)
+ {
+ $this->type = $type;
+ $this->timeStamp = microtime(true);
+ $this->cancelable = $cancellable;

Typo cancellable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius
Owner

Closing: after 3 years, I think we might first look at how the ecosystem may have evolved before going and building our own (again)

@Ocramius Ocramius closed this
@Ocramius Ocramius self-assigned this
@Ocramius Ocramius deleted the event branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 15, 2012
  1. @guilhermeblanco
  2. @guilhermeblanco
Commits on Jun 19, 2012
  1. @guilhermeblanco
Commits on Jun 24, 2012
  1. @guilhermeblanco

    Started tests on new Event system. Added generic support for lists (p…

    guilhermeblanco authored
    …lanning for cached support). General optimizations adnd changes. Simplified API.
Commits on Jul 23, 2012
  1. @guilhermeblanco
Something went wrong with that request. Please try again.