tao-test/app/tao/models/classes/extension/UpdateExtensions.php

150 lines
5.2 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
*/
namespace oat\tao\model\extension;
use common_ext_ManifestException as ManifestException;
use common_ext_UpdaterNotFoundException as UpdaterNotFoundException;
use common_report_Report as Report;
use oat\oatbox\event\EventManagerAwareTrait;
use oat\oatbox\log\LoggerAggregator;
use oat\oatbox\service\ServiceNotFoundException;
use oat\tao\model\asset\AssetService;
use oat\tao\model\event\TaoUpdateEvent;
use oat\tao\model\migrations\MigrationsService;
use common_ext_ExtensionsManager as ExtensionManager;
use common_ext_Extension as Extension;
/**
* Extends the generis updater to take into account
* the translation files
*/
class UpdateExtensions extends \common_ext_UpdateExtensions
{
use EventManagerAwareTrait;
/**
* (non-PHPdoc)
* @see \oat\oatbox\action\Action::__invoke()
*/
public function __invoke($params)
{
try {
$loggers = [
$this->getLogger(),
$this->getServiceLocator()->get(UpdateLogger::SERVICE_ID)
];
$this->setLogger(new LoggerAggregator($loggers));
} catch (ServiceNotFoundException $e) {
// update script to add update logger hasn't run yet, ignore
}
$report = parent::__invoke($params);
$migrationsReport = $this->getServiceLocator()->get(MigrationsService::class)->migrate();
$this->logInfo(\helpers_Report::renderToCommandline($migrationsReport, false));
$report->add($migrationsReport);
// regenerate locales
$files = \tao_models_classes_LanguageService::singleton()->generateAll();
if (count($files) > 0) {
$report->add(new Report(Report::TYPE_SUCCESS, __('Successfully updated %s client translation bundles', count($files))));
} else {
$report->add(new Report(Report::TYPE_ERROR, __('No client translation bundles updated')));
}
$updateId = $this->generateUpdateId();
$this->updateCacheBuster($report, $updateId);
$postUpdateReport = $this->runPostUpdateScripts();
$report->add($postUpdateReport);
$report->add(new Report(Report::TYPE_INFO, __('Update ID : %s', $updateId)));
$this->getEventManager()->trigger(new TaoUpdateEvent($report));
return $report;
}
/**
* Generate a unique ID per update
* @return string the new id
*/
protected function generateUpdateId()
{
return uniqid();
}
/**
* Update the asset service to save the cache buster value (the update id)
*
* @param Report $report
* @param string $updateid
*/
private function updateCacheBuster(Report $report, $updateid)
{
try {
$assetService = $this->getServiceLocator()->get(AssetService::SERVICE_ID);
$assetService->setCacheBuster($updateid);
$this->getServiceLocator()->register(AssetService::SERVICE_ID, $assetService);
} catch (\Exception $e) {
\common_Logger::e($e->getMessage());
$report->add(
new Report(Report::TYPE_WARNING, __('Unable to update the asset service')));
}
}
/**
* @throws \common_exception_Error
*/
private function runPostUpdateScripts()
{
$report = new Report(Report::TYPE_INFO, 'Post update actions:');
$extManager = $this->getServiceLocator()->get(ExtensionManager::SERVICE_ID);
$sorted = \helpers_ExtensionHelper::sortByDependencies($extManager->getInstalledExtensions());
foreach ($sorted as $ext) {
$postUpdateExtensionReport = $this->runPostUpdateScript($ext);
if ($postUpdateExtensionReport !== null) {
$report->add($postUpdateExtensionReport);
}
}
if (!$report->hasChildren()) {
$report->add(
new Report(Report::TYPE_INFO, 'No actions to be executed'));
}
return $report;
}
/**
* @param Extension $ext
*
* @return Report|null
*/
private function runPostUpdateScript(Extension $ext): ?Report
{
try {
return $ext->getUpdater()->postUpdate();
} catch (UpdaterNotFoundException $e) {
return Report::createSuccess(sprintf('No postprocessing defined for %s', $ext->getName()));
} catch (ManifestException $e) {
return new Report(Report::TYPE_WARNING, $e->getMessage());
}
}
}