/
Listener.php
executable file
·60 lines (48 loc) · 1.89 KB
/
Listener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace mirolabs\phalcon\Framework\Compile\Plugin;
use mirolabs\collection\ArrayList;
use mirolabs\phalcon\Framework\Compile\Plugin\Model\Listener as ModelListener;
use mirolabs\phalcon\Framework\Compile\AnnotationParser as AnnotationParser;
class Listener implements \mirolabs\phalcon\Framework\Compile\Plugin
{
const EVENT_ANNOTATION = 'Listener';
const CACHE_FILE = '/listeners.php';
/**
* @var Config
*/
private $config;
/**
* @var ArrayList
*/
private $listeners = [];
public function __construct() {
$this->listeners = new ArrayList('\mirolabs\phalcon\Framework\Compile\Plugin\Model\Listener');
}
public function getConfig() {
return $this->config;
}
public function setConfig(\Phalcon\Config $config) {
$this->config = $config;
}
public function createCache($cacheDir) {
$file = "<?php\n\n";
$file .= "\tfunction _loadListeners(\$di) {\n";
$file .= implode("\n", $this->buildListeners());
$file .= "\n\t}\n";
file_put_contents($cacheDir . self::CACHE_FILE, $file);
}
public function parseFile(\Phalcon\Annotations\Adapter $adapter, $className, $module) {
$parser = new AnnotationParser($adapter->get($className));
if ($parser->isExistsAnnotationClass(Service::CLASS_ANNOTATION)) {
$this->listeners->add(new ModelListener($parser, $className));
}
}
private function buildListeners() {
$result = [];
$this->listeners
->map(function (ModelListener $model) { return $model->getListeners(); })
->filter(function ($data) { return count($data) > 0;})
->each(function ($data) use (&$result) { $result = array_merge($result, $data); });
return $result;
}
}