1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\service; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\Extension; |
5
|
|
|
use keeko\core\model\ExtensionQuery; |
6
|
|
|
use phootwork\collection\ArrayList; |
7
|
|
|
use phootwork\collection\Map; |
8
|
|
|
use phootwork\json\Json; |
9
|
|
|
|
10
|
|
|
class ExtensionRegistry { |
11
|
|
|
|
12
|
|
|
/** @var Map */ |
13
|
|
|
private $extensions; |
14
|
|
|
|
15
|
|
|
/** @var Map */ |
16
|
|
|
private $packages; |
17
|
|
|
|
18
|
|
|
public function __construct() { |
19
|
|
|
$this->extensions = new Map(); |
20
|
|
|
$this->packages = new Map(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
// load up all extensions |
23
|
|
|
$exts = ExtensionQuery::create()->joinPackage()->find(); |
24
|
|
|
foreach ($exts as $ext) { |
25
|
|
|
/* @var $ext Extension */ |
26
|
|
|
$key = $ext->getKey(); |
|
|
|
|
27
|
|
|
$packageName = $ext->getPackage()->getName(); |
28
|
|
|
$data = Json::decode($ext->getData($ext->getData())); |
|
|
|
|
29
|
|
|
|
30
|
|
|
// add to global extensions |
31
|
|
|
if (!$this->extensions->has($key)) { |
32
|
|
|
$this->extensions->set($key, new ArrayList()); |
33
|
|
|
} |
34
|
|
|
$this->extensions->get($key)->add($data); |
35
|
|
|
|
36
|
|
|
// add to package extensions |
37
|
|
|
if (!$this->packages->has($packageName)) { |
38
|
|
|
$this->packages->set($packageName, new Map()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$pkg = $this->packages->get($packageName); |
42
|
|
|
if (!$pkg->has($key)) { |
43
|
|
|
$pkg->set($key, new ArrayList()); |
44
|
|
|
} |
45
|
|
|
$pkg->get($key)->add($data); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the extension for the given key |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getExtensions($key) { |
56
|
|
|
if ($this->extensions->has($key)) { |
57
|
|
|
return $this->extensions->get($key)->toArray(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the extension for the given key by a given packageName |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @param string $packageName |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getExtensionsByPackage($key, $packageName) { |
71
|
|
|
if ($this->packages->has($packageName)) { |
72
|
|
|
$pkg = $this->packages->get($packageName); |
73
|
|
|
if ($pkg->has($key)) { |
74
|
|
|
return $pkg->get($key)->toArray(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return []; |
79
|
|
|
} |
80
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.