forked from aimeos/aimeos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArcavias.php
334 lines (266 loc) · 7.96 KB
/
Arcavias.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* @copyright Copyright (c) Metaways Infosystems GmbH, 2011
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
*/
/**
* Global starting point for applicatons.
*/
class Arcavias
{
private $_manifests = array();
private $_extensions = array();
private $_extensionsDone = array();
private $_dependencies = array();
private static $_includePaths = array();
private static $_autoloader = false;
/**
* Initialises the object.
*
* @param array $extdirs List of directories to look for manifest files (or sub-directories thereof)
* @param boolean $defaultdir If default extension directory should be included automatically
* @param string|null $basedir Arcavias core path (optional, dirname(__FILE__) if null)
*/
public function __construct( array $extdirs = array(), $defaultdir = true, $basedir = null )
{
if( $basedir === null ) {
$basedir = dirname( __FILE__ );
}
if( $defaultdir === true && is_dir( $basedir . DIRECTORY_SEPARATOR . 'ext' ) === true ) {
$extdirs[] = $basedir . DIRECTORY_SEPARATOR . 'ext';
}
$this->_manifests[$basedir] = $this->_getManifestFile( $basedir );
self::$_includePaths = $this->getIncludePaths();
$this->_registerAutoloader();
foreach( $this->_getManifests( $extdirs ) as $location => $manifest )
{
if( isset( $this->_extensions[$manifest['name']] ) )
{
$location2 = $this->_extensions[$manifest['name']]['location'];
$msg = 'Extension "%1$s" exists twice in "%2$s" and in "%3$s"';
throw new Exception( sprintf( $msg, $manifest['name'], $location, $location2 ) );
}
if( !isset( $manifest['depends'] ) || !is_array( $manifest['depends'] ) ) {
throw new Exception( sprintf( 'Incorrect dependency configuration in manifest "%1$s"', $location ) );
}
$manifest['location'] = $location;
$this->_extensions[$manifest['name']] = $manifest;
foreach( $manifest['depends'] as $name ) {
$this->_dependencies[$manifest['name']][$name] = $name;
}
}
$this->_addManifests( $this->_dependencies );
self::$_includePaths = $this->getIncludePaths();
}
/**
* Loads the class files for a given class name.
*
* @param string $className Name of the class
* @return boolean True if file was found, false if not
*/
public static function autoload( $className )
{
$fileName = strtr( ltrim( $className, '\\' ), '\\_', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR ) . '.php';
foreach( self::$_includePaths as $path )
{
$file = $path . DIRECTORY_SEPARATOR . $fileName;
if( file_exists( $file ) === true && ( include_once $file ) !== false ) {
return true;
}
}
foreach( explode( PATH_SEPARATOR, get_include_path() ) as $path )
{
$file = $path . DIRECTORY_SEPARATOR . $fileName;
if( file_exists( $file ) === true && ( include_once $file ) !== false ) {
return true;
}
}
return false;
}
/**
* Returns the list of paths for each domain where the translation files are located.
*
* @return array Associative list of i18n domains and lists of absolute paths to the translation directories
*/
public function getI18nPaths()
{
$paths = array();
foreach( $this->_manifests as $basePath => $manifest )
{
if( !isset( $manifest['i18n'] ) ) {
continue;
}
foreach( $manifest['i18n'] as $domain => $location ) {
$paths[$domain][] = $basePath . DIRECTORY_SEPARATOR . $location;
}
}
return $paths;
}
/**
* Returns the include paths containing the required class files.
*
* @return array List of include paths
*/
public function getIncludePaths()
{
$includes = array();
foreach( $this->_manifests as $path => $manifest )
{
if( !isset( $manifest['include'] ) ) {
continue;
}
foreach( $manifest['include'] as $paths ) {
$includes[] = $path . DIRECTORY_SEPARATOR . $paths;
}
}
return $includes;
}
/**
* Returns the paths containing the required configuration files.
*
* @param string $dbtype Name of the database type, e.g. "mysql"
* @return string[] List of configuration paths
*/
public function getConfigPaths( $dbtype )
{
$confpaths = array();
foreach( $this->_manifests as $path => $manifest )
{
if( !isset( $manifest['config'][$dbtype] ) ) {
continue;
}
foreach( $manifest['config'][$dbtype] as $paths ) {
$confpaths[] = $path . DIRECTORY_SEPARATOR . $paths;
}
}
return $confpaths;
}
/**
* Returns the paths stored in the manifest file for the given custom section.
*
* @param string $section Name of the section like in the manifest file
* @return array List of paths
*/
public function getCustomPaths( $section )
{
$paths = array();
foreach( $this->_manifests as $path => $manifest )
{
if( isset( $manifest['custom'][$section] ) ) {
$paths[$path] = $manifest['custom'][$section];
}
}
return $paths;
}
/**
* Returns the list of paths where setup tasks are stored.
*
* @param string $site Name of the site like "default", "unitperf" and "unittest"
* @return array List of setup paths
*/
public function getSetupPaths( $site )
{
$setupPaths = array();
foreach( $this->_manifests as $path => $manifest )
{
if( !isset( $manifest['setup'] ) ) {
continue;
}
foreach( $manifest['setup'] as $relpath )
{
$setupPaths[] = $path . DIRECTORY_SEPARATOR . $relpath;
$sitePath = $path . DIRECTORY_SEPARATOR . $relpath . DIRECTORY_SEPARATOR . $site;
if( is_dir( realpath( $sitePath ) ) ) {
$setupPaths[] = $sitePath;
}
}
}
return $setupPaths;
}
/**
* Returns the configurations of the manifest files in the given directories.
*
* @param array $directories List of directories where the manifest files are stored
* @return array Associative list of directory / configuration array pairs
*/
protected function _getManifests( array $directories )
{
$manifests = array();
foreach( $directories as $directory )
{
$manifest = $this->_getManifestFile( $directory );
if( $manifest !== false )
{
$manifests[$directory] = $manifest;
continue;
}
$dir = new DirectoryIterator( $directory );
foreach( $dir as $dirinfo )
{
if( $dirinfo->isDot() !== false ) {
continue;
}
$manifest = $this->_getManifestFile( $dirinfo->getPathName() );
if( $manifest === false ) {
continue;
}
$manifests[$dirinfo->getPathName()] = $manifest;
}
}
return $manifests;
}
/**
* Loads the manifest file from the given directory.
*
* @param string $dir Directory that includes the manifest file
* @return array|false Associative list of configurations or false if the file doesn't exist
*/
protected function _getManifestFile( $dir )
{
$manifestFile = $dir . DIRECTORY_SEPARATOR . 'manifest.php';
if( file_exists( $manifestFile ) ) {
return include $manifestFile;
}
return false;
}
/**
* Registers the Arcavias autoloader.
*/
protected function _registerAutoloader()
{
if( self::$_autoloader === false )
{
spl_autoload_register( array( $this, 'autoload' ), true, false );
self::$_autoloader = true;
}
}
/**
* Re-order the given dependencies of each manifest configuration.
*
* @param array $deps List of dependencies
* @param array $stack List of task names that are scheduled after this task
* @todo version checks
*/
private function _addManifests( array $deps, array $stack = array( ) )
{
foreach( $deps as $extName => $name )
{
if( in_array( $extName, $this->_extensionsDone ) ) {
continue;
}
if( in_array( $extName, $stack ) ) {
$msg = sprintf( 'Circular dependency for "%1$s" detected', $extName );
throw new Exception( $msg );
}
$stack[] = $extName;
/** @todo test for expression object or array when implementing version checks */
if( isset( $this->_dependencies[$extName] ) ) {
$this->_addManifests( (array) $this->_dependencies[$extName], $stack );
}
if( isset( $this->_extensions[$extName] ) ) {
$this->_manifests[$this->_extensions[$extName]['location']] = $this->_extensions[$extName];
}
$this->_extensionsDone[] = $extName;
}
}
}