diff --git a/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php b/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php index 2d8518a17..b1a709f56 100644 --- a/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php +++ b/Library/Phalcon/Mvc/Model/Behavior/NestedSet.php @@ -9,9 +9,21 @@ use Phalcon\Db\AdapterInterface; use Phalcon\Mvc\Model\BehaviorInterface; use Phalcon\Mvc\Model\ResultsetInterface; +use Phalcon\Traits\EventManagerAwareTrait; class NestedSet extends Behavior implements BehaviorInterface { + use EventManagerAwareTrait; + + const EVT_TYPE_QUERY = 'nestedset'; + + const EVT_DESCENDANTS = 'Descendants'; + const EVT_ANCESTORS = 'Ancestors'; + const EVT_PARENT = 'Parent'; + const EVT_PREV = 'Prev'; + const EVT_NEXT = 'Next'; + const EVT_ROOTS = 'Roots'; + /** * @var AdapterInterface|null */ @@ -216,6 +228,16 @@ public function descendants($depth = null, $addSelf = false) $query = $query->andWhere($this->rootAttribute . '=' . $owner->{$this->rootAttribute}); } + $this->fire( + self::EVT_TYPE_QUERY . ':before' . self::EVT_DESCENDANTS, + $query, + [ + 'owner' => $owner, + 'depth' => $depth, + 'addSelf' => $addSelf + ] + ); + return $query->execute(); } @@ -252,6 +274,15 @@ public function ancestors($depth = null) $query = $query->andWhere($this->rootAttribute . '=' . $owner->{$this->rootAttribute}); } + $this->fire( + self::EVT_TYPE_QUERY . ':before' . self::EVT_ANCESTORS, + $query, + [ + 'owner' => $owner, + 'depth' => $depth + ] + ); + return $query->execute(); } @@ -264,7 +295,19 @@ public function roots() { $owner = $this->getOwner(); - return $owner::find($this->leftAttribute . ' = 1'); + $query = $owner::query() + ->andWhere($this->leftAttribute . ' = 1') + ; + + $this->fire( + self::EVT_TYPE_QUERY . ':before' . self::EVT_ROOTS, + $query, + [ + 'owner' => $owner + ] + ); + + return $owner::find($query->getParams()); } /** @@ -286,6 +329,14 @@ public function parent() $query = $query->andWhere($this->rootAttribute . '=' . $owner->{$this->rootAttribute}); } + $this->fire( + self::EVT_TYPE_QUERY . ':before' . self::EVT_PARENT, + $query, + [ + 'owner' => $owner + ] + ); + return $query->execute()->getFirst(); } @@ -304,6 +355,14 @@ public function prev() $query = $query->andWhere($this->rootAttribute . '=' . $owner->{$this->rootAttribute}); } + $this->fire( + self::EVT_TYPE_QUERY . ':before' . self::EVT_PREV, + $query, + [ + 'owner' => $owner + ] + ); + return $query->execute()->getFirst(); } @@ -322,6 +381,14 @@ public function next() $query = $query->andWhere($this->rootAttribute . '=' . $owner->{$this->rootAttribute}); } + $this->fire( + self::EVT_TYPE_QUERY . ':before' . self::EVT_NEXT, + $query, + [ + 'owner' => $owner + ] + ); + return $query->execute()->getFirst(); } diff --git a/Library/Phalcon/Traits/EventManagerTrait.php b/Library/Phalcon/Traits/EventManagerTrait.php new file mode 100644 index 000000000..681a6e4bb --- /dev/null +++ b/Library/Phalcon/Traits/EventManagerTrait.php @@ -0,0 +1,85 @@ + | + +------------------------------------------------------------------------+ +*/ + +namespace Phalcon\Traits; + +use Phalcon\Di; +use Phalcon\Events\Manager as EventsManager; + +/** + * Phalcon\Traits\EventManagerAwareTrait + * + * Trait for event processing + * + * @package Phalcon\Traits + */ + +trait EventManagerAwareTrait +{ + /** + * @var EventsManager + */ + protected $eventsManager = null; + + /** + * set event manager + * + * @param EventsManager $eventsManager + */ + public function setEventsManager(EventsManager $manager) + { + $this->eventsManager = $manager; + } + + /** + * return event manager + * + * @return EventsManager | null + */ + public function getEventsManager() + { + if (!empty($this->eventsManager)) { + $manager = $this->eventsManager; + } elseif (Di::getDefault()->has('eventsManager')) { + $manager = Di::getDefault()->get('eventsManager'); + } + + if (isset($manager) && $manager instanceof EventsManager) { + return $manager; + } + + return null; + } + + /** + * Checking if event manager is defined - fire event + * + * @param string $event + * @param object $source + * @param mixed $data + * @param boolean $cancelable + * + */ + public function fire($event, $source, $data = null, $cancelable = true) + { + if ($manager = $this->getEventsManager()) { + $manager->fire($event, $source, $data, $cancelable); + } + } +} diff --git a/composer.json b/composer.json index 70baa0a28..4f6bb6c0f 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "phpunit/phpunit": "^4.8", "squizlabs/php_codesniffer": "^2.9", "codeception/codeception": "^2.3", - "codeception/mockery-module": "^0.2", + "codeception/mockery-module": "0.2.2", "codeception/aerospike-module": "^1.0", "codeception/specify": "^0.4", "codeception/verify": "^0.3",