public function prepare()
 {
     $id = $this->getProperty('id');
     if ($id == null) {
         return $this->failure();
     }
     $this->object = $this->modx->getObject('GitPackage', array('id' => $id));
     if (!$this->object) {
         return $this->failure();
     }
     $this->packagePath = rtrim($this->modx->getOption('gitpackagemanagement.packages_dir', null, null), '/') . '/';
     if ($this->packagePath == null) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_ns_packages_dir');
     }
     $packagePath = $this->packagePath . $this->object->dir_name;
     $configFile = $packagePath . $this->modx->gitpackagemanagement->configPath;
     if (!file_exists($configFile)) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_url_config_nf');
     }
     $config = file_get_contents($configFile);
     $config = $this->modx->fromJSON($config);
     $this->config = new GitPackageConfig($this->modx, $packagePath);
     if ($this->config->parseConfig($config) == false) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_url_config_nf');
     }
     $this->loadSmarty();
     return true;
 }
 private function setConfig($packagePath)
 {
     $configFile = $packagePath . $this->modx->gitpackagemanagement->configPath;
     if (!file_exists($configFile)) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_url_config_nfif');
     }
     $this->config = new GitPackageConfig($this->modx, $packagePath);
     if ($this->config->parseConfig($this->modx->fromJSON(file_get_contents($configFile))) == false) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_url_config_nf');
     }
     return true;
 }
 /**
  * Parse config file to objects
  * @param $package string Path to folder of cloned repository
  * @return bool
  */
 private function setConfig($package)
 {
     $configFile = $package . $this->modx->gitpackagemanagement->configPath;
     if (!file_exists($configFile)) {
         $this->addFieldError('folderName', $this->modx->lexicon('gitpackagemanagement.package_err_url_config_nf'));
         return false;
     }
     $configContent = $this->modx->fromJSON(file_get_contents($configFile));
     $this->config = new GitPackageConfig($this->modx, $package);
     if ($this->config->parseConfig($configContent) == false) {
         $this->addFieldError('folderName', $this->modx->lexicon('Config file is invalid.'));
         $this->modx->log(modX::LOG_LEVEL_ERROR, 'Config file is invalid.');
         $this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED');
         return false;
     }
     $dependencies = $this->config->checkDependencies();
     if ($dependencies !== true) {
         $this->addFieldError('folderName', $this->modx->lexicon('gitpackagemanagement.package_err_dependencies'));
         $this->modx->log(modX::LOG_LEVEL_ERROR, 'Dependencies are not matching!');
         foreach ($dependencies as $dependency) {
             $this->modx->log(modX::LOG_LEVEL_ERROR, 'Package ' . $dependency . ' not found!');
         }
         $this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED');
         return false;
     }
     $this->object->set('config', $this->modx->toJSON($configContent));
     $this->object->save();
     $this->modx->log(modX::LOG_LEVEL_INFO, 'Config file is valid.');
     return true;
 }
 public function beforeSet()
 {
     $this->packagePath = rtrim($this->modx->getOption('gitpackagemanagement.packages_dir', null, null), '/');
     if ($this->packagePath == null) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_ns_packages_dir');
     }
     $this->packagePath .= '/';
     $packagePath = $this->packagePath . $this->object->dir_name;
     $configFile = $packagePath . $this->modx->gitpackagemanagement->configPath;
     if (!file_exists($configFile)) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_url_config_nf');
     }
     $config = file_get_contents($configFile);
     $config = $this->modx->fromJSON($config);
     if (is_null($config)) {
         return 'JSON config file is not valid.';
     }
     $this->newConfig = new GitPackageConfig($this->modx, $packagePath);
     $this->newConfig->parseConfig($config);
     if ($this->newConfig->error->hasErrors()) {
         return implode('<br />', $this->newConfig->error->getErrors());
     }
     $dependencies = $this->newConfig->checkDependencies();
     if ($dependencies !== true) {
         $msg = '<strong>Dependencies check failed!</strong><br />';
         foreach ($dependencies as $dependency) {
             $msg .= 'Package ' . $dependency . ' not found!<br />';
         }
         return $msg;
     }
     $this->oldConfig = new GitPackageConfig($this->modx, $packagePath);
     $this->oldConfig->parseConfig($this->modx->fromJSON($this->object->config));
     $this->recreateDatabase = $this->getProperty('recreateDatabase', 0);
     $this->alterDatabase = $this->getProperty('alterDatabase', 0);
     $update = $this->update();
     if ($update !== true) {
         return $update;
     }
     $this->setProperty('config', $this->modx->toJSON($config));
     return parent::beforeSet();
 }