2018-12-03 23:49:20 +01:00
|
|
|
<?php
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
namespace Shaarli\Updater;
|
|
|
|
|
|
|
|
class UpdaterUtils
|
2018-12-03 23:49:20 +01:00
|
|
|
{
|
2023-05-24 11:35:15 +02:00
|
|
|
/**
|
|
|
|
* Read the updates file, and return already done updates.
|
|
|
|
*
|
|
|
|
* @param string $updatesFilepath Updates file path.
|
|
|
|
*
|
|
|
|
* @return array Already done update methods.
|
|
|
|
*/
|
|
|
|
public static function readUpdatesFile($updatesFilepath)
|
|
|
|
{
|
|
|
|
if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
|
|
|
|
$content = file_get_contents($updatesFilepath);
|
|
|
|
if (! empty($content)) {
|
|
|
|
return explode(';', $content);
|
|
|
|
}
|
2018-12-03 23:49:20 +01:00
|
|
|
}
|
2023-05-24 11:35:15 +02:00
|
|
|
return [];
|
2018-12-03 23:49:20 +01:00
|
|
|
}
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
/**
|
|
|
|
* Write updates file.
|
|
|
|
*
|
|
|
|
* @param string $updatesFilepath Updates file path.
|
|
|
|
* @param array $updates Updates array to write.
|
|
|
|
*
|
|
|
|
* @throws \Exception Couldn't write version number.
|
|
|
|
*/
|
|
|
|
public static function writeUpdatesFile($updatesFilepath, $updates)
|
|
|
|
{
|
|
|
|
if (empty($updatesFilepath)) {
|
|
|
|
throw new \Exception('Updates file path is not set, can\'t write updates.');
|
|
|
|
}
|
2018-12-03 23:49:20 +01:00
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
$res = file_put_contents($updatesFilepath, implode(';', $updates));
|
|
|
|
if ($res === false) {
|
|
|
|
throw new \Exception('Unable to write updates in ' . $updatesFilepath . '.');
|
|
|
|
}
|
2018-12-03 23:49:20 +01:00
|
|
|
}
|
|
|
|
}
|