Skip to content

Commit

Permalink
Merge pull request #834 from phalcon/3.2.x
Browse files Browse the repository at this point in the history
3.2.4
  • Loading branch information
sergeyklay authored Oct 20, 2017
2 parents 34db963 + 158a247 commit f8595b4
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 3 deletions.
69 changes: 68 additions & 1 deletion Library/Phalcon/Mvc/Model/Behavior/NestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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());
}

/**
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down
85 changes: 85 additions & 0 deletions Library/Phalcon/Traits/EventManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Sergii Svyrydenko <[email protected]> |
+------------------------------------------------------------------------+
*/

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);
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/_ci/install_prereqs_5.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
printf "\n" | pecl install --force apcu-4.0.11 &> /dev/null
printf "\n" | pecl install --force igbinary &> /dev/null
printf "\n" | pecl install --force imagick &> /dev/null
printf "\n" | pecl install --force yaml &> /dev/null
printf "\n" | pecl install --force yaml-1.3.1 &> /dev/null

echo 'extension="mongo.so"' >> "$(phpenv root)/versions/$(phpenv version-name)/etc/php.ini"
echo 'extension="mongodb.so"' >> "$(phpenv root)/versions/$(phpenv version-name)/etc/php.ini"
Expand Down

0 comments on commit f8595b4

Please sign in to comment.