IHMUserWSManagerClass.php 2.14 KB
<?php

/**
 * @class IHMUserWSManagerClass
 * @brief Manager for IHM user workspace
 * @details
 */
class IHMUserWSManagerClass
{
  private static $version = 1;

  protected $wsInfo      = null;

  public function init()
	{
    //check user workspace
		if (IHMConfigClass::getUserName() == "" || !is_dir(IHMConfigClass::getUserPath()))
			throw new Exception('Cannot find user workspace.');

      //Load info about WS
      if (!$this->loadWSInfoFile())
        throw new Exception('Error to load workspace info file.');

        //Update WS if need
        if (!$this->update())
          throw new Exception('Error during user workspace conversion.');
	}

  public function update()
  {
    error_log(print_r($this->wsInfo,TRUE));
    error_log('BRE - update - '.$this->wsInfo['version']);
    //while (IHMUserWSManagerClass::$version > $this->wsInfo['version']) {
      $updateMethod = "updateFromVersion".$this->wsInfo['version'];
      error_log('BRE - '.$updateMethod);
      if (method_exists($this,$updateMethod)) {
        if (!$this->{$updateMethod}()) {
          return FALSE;
        }
      }
      ++$this->wsInfo['version'];
      error_log('BRE - saveWSInfoFile');
      $this->saveWSInfoFile();
    //}
    return $this->saveWSInfoFile();
  }

  private function loadWSInfoFile()
	{
		if (file_exists(IHMConfigClass::getUserWSInfoFilePath())) {
			$infoContent = file_get_contents(IHMConfigClass::getUserWSInfoFilePath());
			if (empty($infoContent)) {
				return FALSE;
			}
			$this->wsInfo = json_decode($infoContent);
			if (empty($this->wsInfo)) {
				return FALSE;
			}
		}

		//Init info file
		$this->wsInfo = array(
			"version" => 0,
		);

    return $this->saveWSInfoFile();
	}

	private function saveWSInfoFile()
	{
    error_log('BRE - saveWSInfoFile');
		if (empty($this->wsInfo)) {
			return FALSE;
		}

		$json_data = json_encode($this->wsInfo);

		if ($json_data === FALSE) {
			return FALSE;
		}

    error_log(IHMConfigClass::getUserWSInfoFilePath());

		return file_put_contents(IHMConfigClass::getUserWSInfoFilePath(),$json_data);
	}

  private function updateFromVersion0() {
    error_log('BRE - updateFromVersion0 call');
    return TRUE;
  }
}