/
VersionFetcherServiceLocator.php
111 lines (97 loc) · 3.25 KB
/
VersionFetcherServiceLocator.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
<?php
/**
* LimeSurvey
* Copyright (C) 2007-2015 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
namespace LimeSurvey\ExtensionInstaller;
/**
* Central Yii component to add and retrieve version fetcher strategies.
*
* @since 2018-09-26
* @author Olle Haerstedt
*/
class VersionFetcherServiceLocator
{
/**
* Array of callables that return a version fetcher.
* @var array<string, callable>
*/
protected $versionFetcherCreators;
/**
* All Yii components need an init() method.
* @return void
*/
public function init()
{
// Add RESTVersionFetcher, available by default.
$this->addVersionFetcherType(
'rest',
function (\SimpleXMLElement $updaterXml) {
$vf = new RESTVersionFetcher($updaterXml);
return $vf;
}
);
// TODO: Not implemented.
$this->addVersionFetcherType(
'git',
function (\SimpleXMLElement $updaterXml) {
return new GitVersionFetcher($updaterXml);
}
);
}
/**
* Creates a version fetcher based on xml $updaterXml.
* @param SimpleXMLElement $updaterXml <updater> tag from config.xml.
* @return VersionFetcher
* @throws Exception if version fetcher is not found.
*/
public function createVersionFetcher(\SimpleXMLElement $updaterXml)
{
$this->validateXml($updaterXml);
$type = (string) $updaterXml->type;
if (isset($this->versionFetcherCreators[$type])) {
$versionFetcher = $this->versionFetcherCreators[$type]($updaterXml);
return $versionFetcher;
} else {
throw new \Exception('Did not find version fetcher of type ' . json_encode($type));
}
}
/**
* @param string $name
* @param callable $vfCreator
* @return void
* @throws Exception if version fetcher with name $name already exists.
*/
public function addVersionFetcherType(string $name, callable $vfCreator)
{
if (isset($this->versionFetcherCreators[$name])) {
// NB: Internal error, don't need to translate.
throw new \Exception("Version fetcher with name $name already exists");
}
$this->versionFetcherCreators[$name] = $vfCreator;
}
/**
* @param SimpleXMLElement $xml
* @return void
* @throws Exception on invalid xml.
*/
protected function validateXml(\SimpleXMLElement $xml)
{
if (empty((string) $xml->type)) {
throw new \Exception(gT('Missing type tag in updater xml'));
}
if (empty((string) $xml->source)) {
throw new \Exception(gT('Missing source tag in updater xml'));
}
if ((string) $xml->stable === '') {
throw new \Exception(gT('Missing stable tag in updater xml'));
}
}
}