-
-/**
- * Class UpdaterException.
- */
-class UpdaterException extends Exception
-{
- /**
- * @var string Method where the error occurred.
- */
- protected $method;
-
- /**
- * @var Exception The parent exception.
- */
- protected $previous;
-
- /**
- * Constructor.
- *
- * @param string $message Force the error message if set.
- * @param string $method Method where the error occurred.
- * @param Exception|bool $previous Parent exception.
- */
- public function __construct($message = '', $method = '', $previous = false)
- {
- $this->method = $method;
- $this->previous = $previous;
- $this->message = $this->buildMessage($message);
- }
-
- /**
- * Build the exception error message.
- *
- * @param string $message Optional given error message.
- *
- * @return string The built error message.
- */
- private function buildMessage($message)
- {
- $out = '';
- if (! empty($message)) {
- $out .= $message . PHP_EOL;
- }
-
- if (! empty($this->method)) {
- $out .= t('An error occurred while running the update ') . $this->method . PHP_EOL;
- }
-
- if (! empty($this->previous)) {
- $out .= ' '. $this->previous->getMessage();
- }
-
- return $out;
- }
-}
-
-/**
- * Read the updates file, and return already done updates.
- *
- * @param string $updatesFilepath Updates file path.
- *
- * @return array Already done update methods.
- */
-function read_updates_file($updatesFilepath)
-{
- if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
- $content = file_get_contents($updatesFilepath);
- if (! empty($content)) {
- return explode(';', $content);
- }
- }
- return array();
-}
-
-/**
- * Write updates file.
- *
- * @param string $updatesFilepath Updates file path.
- * @param array $updates Updates array to write.
- *
- * @throws Exception Couldn't write version number.
- */
-function write_updates_file($updatesFilepath, $updates)
-{
- if (empty($updatesFilepath)) {
- throw new Exception(t('Updates file path is not set, can\'t write updates.'));
- }
-
- $res = file_put_contents($updatesFilepath, implode(';', $updates));
- if ($res === false) {
- throw new Exception(t('Unable to write updates in '. $updatesFilepath . '.'));
- }
-}