private function update()
 {
     //        $vc = version_compare($this->oldConfig->getVersion(), $this->newConfig->getVersion());
     //        if($vc != -1){
     //            return $this->modx->lexicon('gitpackagemanagement.package_err_nvil');
     //        }
     if ($this->oldConfig->getName() != $this->newConfig->getName()) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_ccn');
     }
     if ($this->oldConfig->getLowCaseName() != $this->newConfig->getLowCaseName()) {
         return $this->modx->lexicon('gitpackagemanagement.package_err_ccln');
     }
     $this->object->set('description', $this->newConfig->getDescription());
     $this->object->set('version', $this->newConfig->getVersion());
     /** @var modCategory category */
     $this->category = $this->modx->getObject('modCategory', array('category' => $this->newConfig->getName()));
     if (!$this->category) {
         $this->category = $this->modx->newObject('modCategory');
         $this->category->set('category', $this->newConfig->getName());
         $this->category->save();
     }
     $this->updateDatabase();
     $this->updateActionsAndMenus();
     $this->updateExtensionPackage();
     $this->updateSystemSettings();
     $notUsedCategories = array();
     $this->updateCategories($notUsedCategories);
     $this->updateElements();
     $this->removeNotUsedCategories($notUsedCategories);
     $this->updateResources();
     $this->clearCache();
     return true;
 }
 public function process()
 {
     $prepare = $this->prepare();
     if ($prepare !== true) {
         return $prepare;
     }
     $this->setPaths();
     $this->builder = new GitPackageBuilder($this->modx, $this->smarty, $this->packagePath);
     $buildOptions = $this->config->getBuild();
     $objectAttributes = $buildOptions->getAttributes();
     foreach ($objectAttributes as $element => $attributes) {
         $this->builder->updateCategoryAttribute($element, $attributes);
     }
     $version = explode('-', $this->config->getVersion());
     if (count($version) == 1) {
         $version[1] = 'pl';
     }
     $this->builder->getTPBuilder()->directory = $this->config->getPackagePath() . '/_packages/';
     $this->builder->getTPBuilder()->createPackage($this->config->getLowCaseName(), $version[0], $version[1]);
     $this->builder->registerNamespace($this->config->getLowCaseName(), false, true, '{core_path}components/' . $this->config->getLowCaseName() . '/', '{assets_path}components/' . $this->config->getLowCaseName() . '/');
     $vehicle = $this->addCategory();
     $resolver = $buildOptions->getResolver();
     $resolversDir = $resolver->getResolversDir();
     $resolversDir = trim($resolversDir, '/');
     $resolversDir = $this->packagePath . '_build/' . $resolversDir . '/';
     $before = $resolver->getBefore();
     foreach ($before as $script) {
         $vehicle->addPHPResolver($resolversDir . ltrim($script, '/'));
     }
     if (is_dir($this->assetsPath)) {
         $vehicle->addAssetsResolver($this->assetsPath);
     }
     if (is_dir($this->corePath)) {
         $vehicle->addCoreResolver($this->corePath);
     }
     $fileResolvers = $resolver->getFileResolvers();
     foreach ($fileResolvers as $fileResolver) {
         $source = $fileResolver['source'];
         $source = str_replace('[[+corePath]]', $this->corePath, $source);
         $source = str_replace('[[+assetsPath]]', $this->assetsPath, $source);
         $source = str_replace('[[+packagePath]]', $this->packagePath, $source);
         $vehicle->addFileResolver($source, $fileResolver['target']);
     }
     $db = $this->config->getDatabase();
     if ($db != null) {
         $tables = $db->getTables();
         if (!empty($tables)) {
             $vehicle->addTableResolver($this->packagePath . '_build/gpm_resolvers', $tables);
         }
     }
     $extensionPackage = $this->config->getExtensionPackage();
     if ($extensionPackage !== false) {
         if ($extensionPackage === true) {
             $vehicle->addExtensionPackageResolver($this->packagePath . '_build/gpm_resolvers');
         } else {
             $vehicle->addExtensionPackageResolver($this->packagePath . '_build/gpm_resolvers', $extensionPackage['serviceName'], $extensionPackage['serviceClass']);
         }
     }
     if (!empty($this->tvMap)) {
         $vehicle->addTVResolver($this->packagePath . '_build/gpm_resolvers', $this->tvMap);
     }
     $resources = $this->config->getResources();
     $resourcesArray = array();
     foreach ($resources as $resource) {
         $resourcesArray[] = $resource->toRawArray();
     }
     if (!empty($resourcesArray)) {
         $vehicle->addResourceResolver($this->packagePath . '_build/gpm_resolvers', $resourcesArray);
     }
     $this->addWidgets();
     $this->addSystemSettings();
     $after = $resolver->getAfter();
     foreach ($after as $script) {
         $vehicle->addPHPResolver($resolversDir . ltrim($script, '/'));
     }
     $this->builder->putVehicle($vehicle);
     $this->addMenus();
     $packageAttributes = array();
     $license = ltrim($buildOptions->getLicense(), '/');
     if (!empty($license) && file_exists($this->corePath . $license)) {
         $packageAttributes['license'] = file_get_contents($this->corePath . $license);
     }
     $readme = ltrim($buildOptions->getReadme(), '/');
     if (!empty($readme) && file_exists($this->corePath . $readme)) {
         $packageAttributes['readme'] = file_get_contents($this->corePath . $readme);
     }
     $changeLog = ltrim($buildOptions->getChangeLog(), '/');
     if (!empty($changeLog) && file_exists($this->corePath . $changeLog)) {
         $packageAttributes['changelog'] = file_get_contents($this->corePath . $changeLog);
     }
     $setupOptions = $buildOptions->getSetupOptions();
     if (!empty($setupOptions) && isset($setupOptions['source']) && !empty($setupOptions['source'])) {
         $file = $this->packagePath . '_build/' . $setupOptions['source'];
         if (file_exists($file)) {
             $setupOptions['source'] = $file;
             $packageAttributes['setup-options'] = $setupOptions;
         }
     }
     $dependencies = $this->config->getDependencies();
     if (!empty($dependencies)) {
         $packageAttributes['requires'] = array();
         foreach ($dependencies as $dependency) {
             $packageAttributes['requires'][$dependency['name']] = $dependency['version'];
         }
     }
     $this->builder->setPackageAttributes($packageAttributes);
     $this->builder->pack();
     return $this->success();
 }
 public function beforeSave()
 {
     //        $url = $this->getProperty('url');
     $folderName = $this->getProperty('folderName');
     /**
      * Check if is set packages dir in MODx system settings
      */
     $packagePath = rtrim($this->modx->getOption('gitpackagemanagement.packages_dir', null, null), '/');
     if ($packagePath == null) {
         $this->addFieldError('folderName', $this->modx->lexicon('gitpackagemanagement.package_err_ns_packages_dir'));
         $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('gitpackagemanagement.package_err_ns_packages_dir'));
         $this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED');
         return false;
     }
     $packagePath .= '/';
     /**
      * Check if is filled folder name
      */
     if (empty($folderName)) {
         $this->addFieldError('folderName', $this->modx->lexicon('gitpackagemanagement.package_err_ns_folder_name'));
         $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('gitpackagemanagement.package_err_ns_folder_name'));
     }
     /**
      * Check if build config and core config are writable
      */
     if (!$this->checkConfig($packagePath . $folderName . '/config.core.php')) {
         $this->addFieldError('folderName', $this->modx->lexicon('gitpackagemanagement.package_err_cc_nw', array('package' => $packagePath . $folderName)));
         $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('gitpackagemanagement.package_err_cc_nw', array('package' => $packagePath . $folderName)));
     }
     if (!$this->checkConfig($packagePath . $folderName . '/_build/build.config.php')) {
         $this->addFieldError('folderName', $this->modx->lexicon('gitpackagemanagement.package_err_bc_nw', array('package' => $packagePath . $folderName)));
         $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('gitpackagemanagement.package_err_bc_nw', array('package' => $packagePath . $folderName)));
     }
     /**
      * If no error was added in block above, cloning and installation part begins
      */
     if (!$this->hasErrors()) {
         /**
          * Parse config file to objects
          */
         if ($this->setConfig($packagePath . $folderName) == false) {
             return false;
         }
         /** @var string packageCorePath Path to core of cloned repository*/
         $this->packageCorePath = $packagePath . $folderName . "/core/components/" . $this->config->getLowCaseName() . "/";
         $this->packageCorePath = str_replace('\\', '/', $this->packageCorePath);
         /** @var string packageAssetsPath Path to assets of cloned repository */
         $this->packageAssetsPath = $packagePath . $folderName . "/assets/components/" . $this->config->getLowCaseName() . "/";
         $this->packageAssetsPath = str_replace('\\', '/', $this->packageAssetsPath);
         /** @var string $packagesUrl Base url for packages directory */
         $packagesUrl = $this->modx->getOption('gitpackagemanagement.packages_base_url', null, null);
         /** @var string packageAssetsUrl URL of assets of cloned repository */
         $this->packageAssetsUrl = $packagesUrl . $folderName . '/assets/components/' . $this->config->getLowCaseName() . '/';
         $this->modx->log(modX::LOG_LEVEL_INFO, '<br /><strong>INSTALLATION START</strong>');
         /**
          * Start installation process
          */
         $this->installPackage($packagePath . $folderName);
         /**
          * Create database record for cloned repository
          */
         $this->object->set('version', $this->config->getVersion());
         $this->object->set('description', $this->config->getDescription());
         $this->object->set('author', $this->config->getAuthor());
         $this->object->set('name', $this->config->getName());
         $this->object->set('dir_name', $folderName);
     }
     $this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED');
     return parent::beforeSave();
 }