|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Collects the important data from all XML files of one type |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package XML |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\xml; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Collects the important data from all XML files of one type |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package XML |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
abstract class Metadata |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Local path |
|
33
|
|
|
**/ |
|
34
|
|
|
protected $path = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Service loader object |
|
38
|
|
|
**/ |
|
39
|
|
|
protected $loader = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Cache driver object |
|
43
|
|
|
**/ |
|
44
|
|
|
protected $cache = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Language shorthandle for translation |
|
48
|
|
|
**/ |
|
49
|
|
|
protected $language = ''; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Type of registry |
|
53
|
|
|
**/ |
|
54
|
|
|
protected $driver = ''; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Load XML registry |
|
58
|
|
|
* |
|
59
|
|
|
* @return \csphere\core\xml\Metadata |
|
60
|
|
|
**/ |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function __construct() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->path = \csphere\core\init\path(); |
|
65
|
|
|
$this->loader = \csphere\core\service\Locator::get(); |
|
66
|
|
|
$this->cache = $this->loader->load('cache'); |
|
67
|
|
|
$this->language = \csphere\core\translation\Fetch::lang(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Generate registry from XML files |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
**/ |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
protected function generate() |
|
77
|
|
|
{ |
|
78
|
|
|
// Try to load metadata from cache |
|
79
|
|
|
$key = 'metadata_' . $this->driver; |
|
80
|
|
|
$reg = $this->cache->load($key); |
|
81
|
|
|
|
|
82
|
|
|
// If cache loading fails load it and create cache file |
|
83
|
|
|
if ($reg == false) { |
|
84
|
|
|
|
|
85
|
|
|
// Get a directory listing of all files |
|
86
|
|
|
$origin = $this->path . 'csphere/' . $this->driver . 's'; |
|
87
|
|
|
$files = \csphere\core\files\File::search($origin); |
|
88
|
|
|
|
|
89
|
|
|
// Load XML content into an array |
|
90
|
|
|
$xml = $this->loader->load('xml', $this->driver); |
|
91
|
|
|
$reg = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach ($files AS $name) { |
|
94
|
|
|
|
|
95
|
|
|
$reg[$name] = $xml->source($this->driver, $name); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->cache->save($key, $reg); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $reg; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Check if an entry exists |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $short Short name for entry |
|
108
|
|
|
* |
|
109
|
|
|
* @return boolean |
|
110
|
|
|
**/ |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
public function exists($short) |
|
113
|
|
|
{ |
|
114
|
|
|
// Check if entry is part of registry |
|
115
|
|
|
$reg = $this->generate(); |
|
116
|
|
|
$result = isset($reg[$short]) ? true : false; |
|
117
|
|
|
|
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Lists all entries with most important information |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
**/ |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
public function details() |
|
128
|
|
|
{ |
|
129
|
|
|
// Try to load entries from cache |
|
130
|
|
|
$key = 'metadata_' . $this->driver . '_details'; |
|
131
|
|
|
$ent = $this->cache->load($key); |
|
132
|
|
|
|
|
133
|
|
|
// If cache loading fails load it and create cache file |
|
134
|
|
|
if ($ent == false) { |
|
135
|
|
|
|
|
136
|
|
|
// Get registered entries from cache |
|
137
|
|
|
$reg = $this->generate(); |
|
138
|
|
|
$ent = []; |
|
139
|
|
|
|
|
140
|
|
|
// Create a list of entries with some important details |
|
141
|
|
|
foreach ($reg AS $short => $info) { |
|
142
|
|
|
|
|
143
|
|
|
$ent[$short] = ['short' => $short, |
|
144
|
|
|
'version' => $info['version'], |
|
145
|
|
|
'pub' => $info['published'], |
|
146
|
|
|
'name' => $info['name'], |
|
147
|
|
|
'icon' => $info['icon']['value'], |
|
148
|
|
|
'icon_url' => $info['icon']['url']]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->cache->save($key, $ent); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $ent; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.