|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/manganel |
|
7
|
|
|
* @license AGPL-3.0-only, proprietary |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* @link https://maslosoft.com/manganel/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
|
14
|
|
|
|
|
15
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
16
|
|
|
use Maslosoft\Mangan\Events\Event; |
|
17
|
|
|
use Maslosoft\Mangan\Events\ModelEvent; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\TrashInterface; |
|
19
|
|
|
use Maslosoft\Mangan\Signals\AfterDelete; |
|
20
|
|
|
use Maslosoft\Mangan\Signals\AfterSave; |
|
21
|
|
|
use Maslosoft\Mangan\Signals\ConfigInit; |
|
22
|
|
|
use Maslosoft\Mangan\Traits\Model\TrashableTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Receiver of Mangan signals |
|
26
|
|
|
* TODO: Should remove from index on trash |
|
27
|
|
|
* TODO: Should add to index on trash restore |
|
28
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Receiver implements AnnotatedInterface |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @SlotFor(AfterSave) |
|
35
|
|
|
* @param AfterSave $signal |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function onSave(AfterSave $signal) |
|
38
|
|
|
{ |
|
39
|
6 |
|
(new IndexManager($signal->model))->index(); |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @SlotFor(AfterDelete) |
|
44
|
|
|
* @param AfterDelete $signal |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function onDelete(AfterDelete $signal) |
|
47
|
|
|
{ |
|
48
|
1 |
|
(new IndexManager($signal->model))->delete(); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @SlotFor(ConfigInit) |
|
53
|
|
|
* @codeCoverageIgnore Configuration is tested on Index/SearchArray test |
|
54
|
|
|
* @param ConfigInit $signal |
|
55
|
|
|
*/ |
|
56
|
|
|
public function onInit(ConfigInit $signal) |
|
57
|
|
|
{ |
|
58
|
|
|
$signal->apply(require __DIR__ . '/config/mangan.cfg.php'); |
|
59
|
|
|
|
|
60
|
|
|
// Trash does not emit signals |
|
61
|
|
|
$this->attachTrashHandlers(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @staticvar boolean $once |
|
66
|
|
|
*/ |
|
67
|
|
|
private function attachTrashHandlers() |
|
68
|
|
|
{ |
|
69
|
|
|
// @codeCoverageIgnoreStart |
|
70
|
|
|
static $once = true; |
|
71
|
|
|
if ($once) |
|
72
|
|
|
{ |
|
73
|
|
|
$handler = function(ModelEvent $event) |
|
74
|
|
|
{ |
|
75
|
|
|
// @codeCoverageIgnoreEnd |
|
76
|
|
|
/* @var $event ModelEvent */ |
|
77
|
|
|
$model = $event->sender; |
|
78
|
|
|
$this->onDelete(new AfterDelete($model)); |
|
79
|
|
|
$event->handled = true; |
|
80
|
|
|
$event->isValid = true; |
|
81
|
|
|
// @codeCoverageIgnoreStart |
|
82
|
|
|
}; |
|
83
|
|
|
$handler->bindTo($this); |
|
84
|
|
|
Event::on(TrashableTrait::class, TrashInterface::EventAfterTrash, $handler); |
|
85
|
|
|
$once = false; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|