Skip to content

Commit

Permalink
Dev: Remake updater factory to service locator
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Oct 2, 2018
1 parent 61f0f60 commit 3655a7f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 77 deletions.
7 changes: 5 additions & 2 deletions application/config/internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,12 @@
),
),
),
'extensionUpdaterServiceLocator' => array(
'class' => '\LimeSurvey\ExtensionInstaller\ExtensionUpdaterServiceLocator',
),
'versionFetcherServiceLocator' => array(
'class' => "\\LimeSurvey\\ExtensionInstaller\\VersionFetcherServiceLocator",
)
'class' => '\LimeSurvey\ExtensionInstaller\VersionFetcherServiceLocator',
),
)
);

Expand Down
5 changes: 2 additions & 3 deletions application/controllers/admin/ExtensionUpdaterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ class ExtensionUpdaterController extends Survey_Common_Action
*/
public function checkAll()
{
$factory = new ExtensionUpdaterFactory();
$factory->addUpdaterClassNames($this->updaterClassNames);
$service = \Yii::app()->extensionUpdaterServiceLocator;

// Get one updater class for each extension type (PluginUpdater, ThemeUpdater, etc).
// Only static methods will be used for this updaters.
list($updaters, $errors) = $factory->getAllUpdaters();
list($updaters, $errors) = $service->getAllUpdaters();

$errors = [];
foreach ($updaters as $updater) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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;

/**
* @since 2018-09-26
* @author Olle Haerstedt
*/
class ExtensionUpdaterServiceLocator
{
/**
* @var array<string, callable>
*/
protected $updaters = [];

/**
* All Yii components need an init() method.
* @return void
*/
public function init() : void
{
$this->addUpdater(
'plugin',
function () {
return PluginUpdater::createUpdaters();
}
);
}

/**
* @param string $name Updater class name, like 'PluginUpdater', or 'ExtensionUpdater'.
* @param callable $creator Callable that returns an ExtensionUpdater array.
* @return void
*/
public function addUpdater(string $name, callable $creator) : void
{
if (isset($this->updaters[$name])) {
throw new \Exception("Extension installer with name $name already exists");
}
$this->updaters[$name] = $creator;
}

/**
* @param string $name
* @return ExtensionUpdater
*/
public function getUpdater(string $name) : ?ExtensionUpdater
{
if (isset($this->updaters[$name])) {
$updater = $this->updaters[$name]();
return $updater;
} else {
return null;
}
}

/**
* @return ExtensionUpdater[]
*/
public function getAllUpdaters()
{
// Get an extension updater for each extension installed.
$updaters = [];
$errors = [];
foreach ($this->updaters as $name => $creator) {
$updaters = array_merge($creator(), $updaters);
}
return $updaters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,6 @@ public function getVersionFetcher(\SimpleXMLElement $updaterXml) : VersionFetche
} 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;
*/
}

/**
Expand Down

0 comments on commit 3655a7f

Please sign in to comment.