-
Notifications
You must be signed in to change notification settings - Fork 988
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dev: Remake factory into global service locator (Yii component)
- Loading branch information
1 parent
f3512fa
commit 6d9a080
Showing
8 changed files
with
171 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
application/libraries/ExtensionInstaller/VersionFetcherFactory.php
This file was deleted.
Oops, something went wrong.
130 changes: 130 additions & 0 deletions
130
application/libraries/ExtensionInstaller/VersionFetcherServiceLocator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?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 $strategies; | ||
|
||
/** | ||
* All Yii components need an init() method. | ||
* @return void | ||
*/ | ||
public function init() : void | ||
{ | ||
// Add RESTVersionFetcher, available by default. | ||
$this->addVersionFetcher( | ||
'rest', | ||
function () { | ||
return new RESTVersionFetcher(); | ||
} | ||
); | ||
|
||
// TODO: Not implemented. | ||
$this->addVersionFetcher( | ||
'git', | ||
function () { | ||
return new GitVersionFetcher(); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param SimpleXMLElement $updaterXml <updater> tag from config.xml. | ||
* @return VersionFetcher | ||
* @throws Exception if version fetcher is not found. | ||
*/ | ||
public function getVersionFetcher(\SimpleXMLElement $updaterXml) : VersionFetcher | ||
{ | ||
$this->validateXml($updaterXml); | ||
|
||
$type = (string) $updaterXml->type; | ||
|
||
if (isset($this->strategies[$type])) { | ||
$fileFetcher = $this->strategies[$type](); | ||
return $fileFetcher; | ||
} else { | ||
throw new \Exception('Did not find version fetcher of type ' . json_encode($type)); | ||
} | ||
|
||
/* | ||
switch ($type) { | ||
case 'rest': | ||
$fetcher = new RESTVersionFetcher(); | ||
break; | ||
case 'git': | ||
$fetcher = new GitVersionFetcher(); | ||
break; | ||
default: | ||
throw new \Exception('Did not find version fetcher of type ' . json_encode($type)); | ||
} | ||
$source = (string) $updaterXml->source; | ||
$stable = (string) $updaterXml->stable; | ||
$fetcher->setSource($source); | ||
$fetcher->setStable($stable === "1"); | ||
return $fetcher; | ||
*/ | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param callable $vfCreator | ||
* @return void | ||
* @throws Exception if version fetcher with name $name already exists. | ||
*/ | ||
public function addVersionFetcher(string $name, callable $vfCreator) : void | ||
{ | ||
if (isset($this->strategies[$name])) { | ||
// NB: Internal error, don't need to translate. | ||
throw new \Exception("Version fetcher with name $name already exists"); | ||
} | ||
|
||
$this->strategies[$name] = $vfCreator; | ||
} | ||
|
||
/** | ||
* @param SimpleXMLElement $xml | ||
* @return void | ||
* @throws Exception on invalid xml. | ||
*/ | ||
protected function validateXml(\SimpleXMLElement $xml) : void | ||
{ | ||
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')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters