LemonStand Forum: Phpr_Events Fixes and Priority - LemonStand Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Phpr_Events Fixes and Priority

#1 User is offline   activeholdingco 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 177
  • Joined: 23-September 10

Posted 06 May 2011 - 07:52 AM

Can we add a priority parameter to Phpr_Events. I have multiple modules which tap into the same events and it would be great to be able to specify the priority they need to execute in.

What happens now if two events return two different sets a data for an event?

Lets say cms:onGetCustomerGroupId returns array(1) and another function returns array(2). Which value is it? According to the events it would be $groups_id = array( array(1), array(2) ); Should you merge the arrays in the events?

Heres the untested priority add on:

	class Phpr_Events extends Phpr_Extension
	{
		public $_eventList = array();

		public function addEvent($Name)
		{
			$args = func_get_args();
			$handler = extractFunctionArg($args, 1);
			$priority = (int)extractFunctionArg($args, 2);
			if( $priority == 0 )
				$priority = 100;

			if (!isset($this->_eventList[$Name]))
				$this->_eventList[$Name] = array();

			$this->_eventList[$Name][] = array('handler'=>$handler, 'priority'=>$priority);
		}

		public function fireEvent($Name)
		{
			if (!isset($this->_eventList[$Name]))
				return array();

			$Params = func_get_args();
//			array_shift($Params);
			array_shift($Params);
//			array_unshift($Params, $baseObject);

			$result = array();

			//sort the event list by priority
			uasort($this->_eventList[$Name], array($this, 'cmp'));

			foreach ($this->_eventList[$Name] as $event)
				$result[] = callFunction($event['handler'], $Params);

			return $result;
		}

		private function cmp($a, $B)
		{
			if ($a['priority'] == $b['priority']) {
				return 0;
			}
 			return ($a['priority'] < $b['priority']) ? -1 : 1;
		}

0

#2 User is online   Aleksey 

  • Co-Founder
  • Group: +Administrators
  • Posts: 3,626
  • Joined: 31-October 09

Posted 08 May 2011 - 03:42 PM

Hi!

Good idea. Added to the roadmap.

Thank you

#3 User is offline   Eric 

  • Developer
  • PipPipPip
  • Group: Members
  • Posts: 1,290
  • Joined: 04-August 10
  • LocationVancouver, Canada

Posted 10 January 2012 - 11:48 AM

Implemented. Refer to the tracker: http://forum.lemonst...d__429#entry429
0

Share this topic:


Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users