Skip to content

Commit

Permalink
remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
leedavis81 committed Sep 20, 2014
1 parent 1bff1b7 commit e232166
Showing 1 changed file with 62 additions and 50 deletions.
112 changes: 62 additions & 50 deletions src/VentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ private function registerEvent(
}
return $params;
};

$this->_ventFunctions['getListeners'] = function (Event $event) {
return (isset($this->_ventEventEmitter) && $this->_ventEventEmitter->hasListeners($event->getName()))
? $this->_ventEventEmitter->getListeners($event->getName())
: [];
};
}

foreach (array_filter(
Expand Down Expand Up @@ -106,25 +112,27 @@ function ($item) {
public function __set($name, $value)
{
$event = new Event('write.' . $name);
if (isset($this->_ventEventEmitter) && $this->_ventEventEmitter->hasListeners($event->getName())) {
foreach ($this->_ventEventEmitter->getListeners($event->getName()) as $listener) {
/** @var CallbackListener $listener */
// replace reserved keywords
$listener->setParams(
call_user_func(
$this->_ventFunctions['replaceReservedParams'],
$listener->getParams(),
$name,
$value
)
);

if (($response = $listener->handle($event)) !== null) {
return $this->_ventVariables[$name] = $response;
}
foreach (call_user_func(
$this->_ventFunctions['getListeners'],
$event
) as $listener) {
/** @var CallbackListener $listener */
// replace reserved keywords
$listener->setParams(
call_user_func(
$this->_ventFunctions['replaceReservedParams'],
$listener->getParams(),
$name,
$value
)
);

if (($response = $listener->handle($event)) !== null) {
return $this->_ventVariables[$name] = $response;
}
}


return $this->_ventVariables[$name] = $value;
}

Expand All @@ -138,26 +146,28 @@ public function __set($name, $value)
public function &__get($name)
{
$event = new Event('read.' . $name);
if (isset($this->_ventEventEmitter) && $this->_ventEventEmitter->hasListeners($event->getName())) {
foreach ($this->_ventEventEmitter->getListeners($event->getName()) as $listener) {
/**
* @var CallbackListener $listener
*/
// replace reserved keywords
$listener->setParams(
call_user_func(
$this->_ventFunctions['replaceReservedParams'],
$listener->getParams(),
$name
)
);

if (($response = $listener->handle($event)) !== null) {
return $response;
}
foreach (call_user_func(
$this->_ventFunctions['getListeners'],
$event
) as $listener) {
/**
* @var CallbackListener $listener
*/
// replace reserved keywords
$listener->setParams(
call_user_func(
$this->_ventFunctions['replaceReservedParams'],
$listener->getParams(),
$name
)
);

if (($response = $listener->handle($event)) !== null) {
return $response;
}
}


// If the variable is unset, or a Null instance return a local object reference;
if (!isset($this->_ventVariables[$name]) || $this->_ventVariables[$name] instanceof Null) {
return $this->$name;
Expand All @@ -175,25 +185,27 @@ public function &__get($name)
public function __unset($name)
{
$event = new Event('delete.' . $name);
if (isset($this->_ventEventEmitter) && $this->_ventEventEmitter->hasListeners($event->getName())) {
foreach ($this->_ventEventEmitter->getListeners($event->getName()) as $listener) {
/**
* @var CallbackListener $listener
*/
// replace reserved keywords
$listener->setParams(
call_user_func(
$this->_ventFunctions['replaceReservedParams'],
$listener->getParams(),
$name
)
);

if (($response = $listener->handle($event)) !== null) {
return $response;
}
foreach (call_user_func(
$this->_ventFunctions['getListeners'],
$event
) as $listener) {
/**
* @var CallbackListener $listener
*/
// replace reserved keywords
$listener->setParams(
call_user_func(
$this->_ventFunctions['replaceReservedParams'],
$listener->getParams(),
$name
)
);

if (($response = $listener->handle($event)) !== null) {
return $response;
}
}

unset($this->_ventVariables[$name]);
return null;
}
Expand Down

0 comments on commit e232166

Please sign in to comment.