public function beforeSave() { $name = $this->getProperty('name', ''); if (empty($name)) { $this->addFieldError('name', $this->modx->lexicon('provider_err_ns_name')); } $serviceUrl = $this->getProperty('service_url', ''); if (empty($serviceUrl)) { $this->addFieldError('service_url', $this->modx->lexicon('provider_err_ns_url')); } $verified = $this->object->verify(); if ($verified !== true) { $this->addFieldError('service_url', $this->modx->lexicon('provider_err_not_verified')); } return parent::beforeSave(); }
/** * Download and install the package from the provider * * @param string $packageName * @param \modTransportProvider $provider * @param array $options * @return bool */ private function download($packageName, $provider, $options = array()) { $this->modx->getVersionData(); $product_version = $this->modx->version['code_name'] . '-' . $this->modx->version['full_version']; $response = $provider->verify(); if ($response !== true) { $this->output->writeln("<error>Could not download {$packageName} because the provider cannot be verified.</error>"); $error = $response; if (!empty($error) && is_string($error)) { $this->output->writeln("Message from Provider: {$error}"); } return false; } $provider->getClient(); $this->output->writeln("Searching <comment>{$provider->get('name')}</comment> for <comment>{$packageName}</comment>..."); // Request package information from the chosen provider $response = $provider->request('package', 'GET', array('supports' => $product_version, 'query' => $packageName)); // Check for a proper response if (!empty($response)) { $foundPackages = simplexml_load_string($response->response); $helper = $this->getHelper('question'); $packages = array(); foreach ($foundPackages as $foundPkg) { $packages[strtolower((string) $foundPkg->name)] = array('name' => (string) $foundPkg->name, 'version' => (string) $foundPkg->version, 'location' => (string) $foundPkg->location, 'signature' => (string) $foundPkg->signature); } // Ensure the exact match is always first if (isset($packages[strtolower($packageName)])) { $packages = array($packageName => $packages[strtolower($packageName)]) + $packages; } foreach ($packages as $package) { if ($this->modx->getCount('transport.modTransportPackage', array('signature' => $package['signature']))) { $this->output->writeln("<info>Package {$package['name']} {$package['version']} is already installed.</info>"); if ($this->interactive) { continue; } else { return true; } } if ($this->interactive) { if (!$helper->ask($this->input, $this->output, new ConfirmationQuestion("Do you want to install <info>{$package['name']} ({$package['version']})</info>? <comment>[Y/n]</comment>: ", true))) { continue; } } // Run the core processor to download the package from the provider $this->output->writeln("<comment>Downloading {$package['name']} ({$package['version']})...</comment>"); $response = $this->modx->runProcessor('workspace/packages/rest/download', array('provider' => $provider->get('id'), 'info' => join('::', array($package['location'], $package['signature'])))); // If we have an error, show it and cancel. if ($response->isError()) { $this->output->writeln("<error>Could not download package {$package['name']}. Reason: {$response->getMessage()}</error>"); return false; } $this->output->writeln("<comment>Installing {$package['name']}...</comment>"); // Grab the package object $obj = $response->getObject(); if ($package = $this->modx->getObject('transport.modTransportPackage', array('signature' => $obj['signature']))) { // Install the package return $package->install($options); } } } return true; }