Add reference hooks

This commit is contained in:
Frederic Guillot 2016-08-13 14:23:53 -04:00
parent 29820bf83b
commit 4ffaba2ba0
No known key found for this signature in database
GPG Key ID: 92D77191BA7FBC99
8 changed files with 97 additions and 31 deletions

View File

@ -3,6 +3,7 @@ Version 1.0.33 (unreleased)
Improvements:
* Add "reference" hooks
* Show project name in task forms
* Convert vanilla CSS to SASS

View File

@ -51,7 +51,7 @@ class LexerBuilder
*/
public function __construct()
{
$this->lexer = new Lexer;
$this->lexer = new Lexer();
$this->queryBuilder = new QueryBuilder();
}

View File

@ -96,4 +96,21 @@ class Hook
return null;
}
/**
* Hook with reference
*
* @access public
* @param string $hook
* @param mixed $param
* @return mixed
*/
public function reference($hook, &$param)
{
foreach ($this->getListeners($hook) as $listener) {
$listener($param);
}
return $param;
}
}

View File

@ -44,6 +44,13 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface
{
$swimlanes = $this->swimlaneModel->getSwimlanes($this->projectId);
$columns = $this->columnModel->getAll($this->projectId);
if (empty($swimlanes) || empty($columns)) {
return array();
}
$this->hook->reference('formatter:board:query', $this->query);
$tasks = $this->query
->eq(TaskModel::TABLE.'.project_id', $this->projectId)
->asc(TaskModel::TABLE.'.position')
@ -52,10 +59,6 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface
$task_ids = array_column($tasks, 'id');
$tags = $this->taskTagModel->getTagsByTasks($task_ids);
if (empty($swimlanes) || empty($columns)) {
return array();
}
return BoardSwimlaneFormatter::getInstance($this->container)
->withSwimlanes($swimlanes)
->withColumns($columns)

View File

@ -26,11 +26,14 @@ class SubtaskPagination extends Base
*/
public function getDashboardPaginator($user_id, $method, $max)
{
$query = $this->subtaskModel->getUserQuery($user_id, array(SubtaskModel::STATUS_TODO, SubtaskModel::STATUS_INPROGRESS));
$this->hook->reference('pagination:dashboard:subtask:query', $query);
return $this->paginator
->setUrl('DashboardController', $method, array('pagination' => 'subtasks', 'user_id' => $user_id))
->setMax($max)
->setOrder(TaskModel::TABLE.'.id')
->setQuery($this->subtaskModel->getUserQuery($user_id, array(SubtaskModel::STATUS_TODO, SubtaskModel::STATUS_INPROGRESS)))
->setQuery($query)
->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks');
}
}

View File

@ -25,11 +25,14 @@ class TaskPagination extends Base
*/
public function getDashboardPaginator($user_id, $method, $max)
{
$query = $this->taskFinderModel->getUserQuery($user_id);
$this->hook->reference('pagination:dashboard:task:query', $query);
return $this->paginator
->setUrl('DashboardController', $method, array('pagination' => 'tasks', 'user_id' => $user_id))
->setMax($max)
->setOrder(TaskModel::TABLE.'.id')
->setQuery($this->taskFinderModel->getUserQuery($user_id))
->setQuery($query)
->calculateOnlyIf($this->request->getStringParam('pagination') === 'tasks');
}
}

View File

@ -115,6 +115,31 @@ List of asset Hooks:
- `template:layout:css`
- `template:layout:js`
Reference hooks
---------------
Reference hooks are passing a variable by reference.
Example:
```php
$this->hook->on('formatter:board:query', function (\PicoDb\Table &query) {
$query->eq('color_id', 'red');
});
```
The code above will show only tasks in red on the board.
List of reference hooks:
| Hook | Description |
|--------------------------------------------|---------------------------------------------------------------|
| `formatter:board:query` | Alter database query before rendering board |
| `pagination:dashboard:task:query` | Alter database query for tasks pagination on the dashboard |
| `pagination:dashboard:subtask:query` | Alter database query for subtasks pagination on the dashboard |
Template Hooks
--------------

View File

@ -8,89 +8,103 @@ class HookTest extends Base
{
public function testGetListeners()
{
$h = new Hook;
$this->assertEmpty($h->getListeners('myhook'));
$hook = new Hook;
$this->assertEmpty($hook->getListeners('myhook'));
$h->on('myhook', 'A');
$h->on('myhook', 'B');
$hook->on('myhook', 'A');
$hook->on('myhook', 'B');
$this->assertEquals(array('A', 'B'), $h->getListeners('myhook'));
$this->assertEquals(array('A', 'B'), $hook->getListeners('myhook'));
}
public function testExists()
{
$h = new Hook;
$this->assertFalse($h->exists('myhook'));
$hook = new Hook;
$this->assertFalse($hook->exists('myhook'));
$h->on('myhook', 'A');
$hook->on('myhook', 'A');
$this->assertTrue($h->exists('myhook'));
$this->assertTrue($hook->exists('myhook'));
}
public function testMergeWithNoBinding()
{
$h = new Hook;
$hook = new Hook;
$values = array('A', 'B');
$result = $h->merge('myhook', $values, array('p' => 'c'));
$result = $hook->merge('myhook', $values, array('p' => 'c'));
$this->assertEquals($values, $result);
}
public function testMergeWithBindings()
{
$h = new Hook;
$hook = new Hook;
$values = array('A', 'B');
$expected = array('A', 'B', 'c', 'D');
$h->on('myhook', function ($p) {
$hook->on('myhook', function ($p) {
return array($p);
});
$h->on('myhook', function () {
$hook->on('myhook', function () {
return array('D');
});
$result = $h->merge('myhook', $values, array('p' => 'c'));
$result = $hook->merge('myhook', $values, array('p' => 'c'));
$this->assertEquals($expected, $result);
$this->assertEquals($expected, $values);
}
public function testMergeWithBindingButReturningBadData()
{
$h = new Hook;
$hook = new Hook;
$values = array('A', 'B');
$expected = array('A', 'B');
$h->on('myhook', function () {
$hook->on('myhook', function () {
return 'string';
});
$result = $h->merge('myhook', $values);
$result = $hook->merge('myhook', $values);
$this->assertEquals($expected, $result);
$this->assertEquals($expected, $values);
}
public function testFirstWithNoBinding()
{
$h = new Hook;
$hook = new Hook;
$result = $h->first('myhook', array('p' => 2));
$result = $hook->first('myhook', array('p' => 2));
$this->assertEquals(null, $result);
}
public function testFirstWithMultipleBindings()
{
$h = new Hook;
$hook = new Hook;
$h->on('myhook', function ($p) {
$hook->on('myhook', function ($p) {
return $p + 1;
});
$h->on('myhook', function ($p) {
$hook->on('myhook', function ($p) {
return $p;
});
$result = $h->first('myhook', array('p' => 3));
$result = $hook->first('myhook', array('p' => 3));
$this->assertEquals(4, $result);
}
public function testHookWithReference()
{
$hook = new Hook();
$hook->on('myhook', function (&$p) {
$p = 2;
});
$param = 123;
$result = $hook->reference('myhook', $param);
$this->assertSame(2, $result);
$this->assertSame(2, $param);
}
}