Created
January 14, 2019 07:51
-
-
Save torunar/0400394e012af84c05017fe3d9353db3 to your computer and use it in GitHub Desktop.
app/addons/sample_addon/Tygh/UpgradeCenter/Connectors/SampleAddon/Connector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tygh\UpgradeCenter\Connectors\SampleAddon; | |
use Tygh\Addons\SchemesManager; | |
use Tygh\Http; | |
use Tygh\Registry; | |
use Tygh\Settings; | |
use Tygh\Tools\Url; | |
use Tygh\UpgradeCenter\Connectors\BaseAddonConnector; | |
use Tygh\UpgradeCenter\Connectors\IConnector; | |
/** | |
* Class Connector implements Upgrade center connector for the sample add-on. | |
* | |
* @package Tygh\UpgradeCenter\Connectors\SampleAddon | |
*/ | |
class Connector extends BaseAddonConnector implements IConnector | |
{ | |
/** | |
* @var string HTTP request parameter name that indicates what action is requested on the updates server | |
*/ | |
const ACTION_PARAM = 'dispatch'; | |
/** | |
* @var string 'Check updates' action value for Update center action parameter | |
*/ | |
const ACTION_CHECK_UPDATES = 'updates.check'; | |
/** | |
* @var string 'Download package' action value for Update center action parameter | |
*/ | |
const ACTION_DOWNLOAD_PACKAGE = 'updates.download_package'; | |
/** | |
* @var string Add-on's unique identifier | |
*/ | |
protected $addon_id = 'sample_addon'; | |
/** | |
* @var string Current add-on version | |
*/ | |
protected $addon_version; | |
/** | |
* @var string Store URL | |
*/ | |
protected $product_url; | |
public function __construct() | |
{ | |
parent::__construct(); | |
// updates server address must be specified in the Connector constructor | |
$this->updates_server = 'https://updates.example.com'; | |
// add-on data | |
$addon = SchemesManager::getScheme($this->addon_id); | |
$this->addon_version = isset($addon->version) ? $addon->version : '1.0'; | |
// value of the 'license_number' setting specified in the add-on scheme | |
$this->license_number = (string) Settings::instance()->getValue('license_number', $this->addon_id); | |
// store data | |
$this->product_name = PRODUCT_NAME; | |
$this->product_version = PRODUCT_VERSION; | |
$this->product_build = PRODUCT_BUILD; | |
$this->product_edition = PRODUCT_EDITION; | |
$this->product_url = Registry::get('config.current_location'); | |
} | |
/** | |
* Provides data to connect to the addon's updates server. | |
* Is called when checking for available updates. | |
* | |
* @return array | |
*/ | |
public function getConnectionData() | |
{ | |
// store license can be accessed via $this->uc_settings['license_number'] | |
$data = [ | |
self::ACTION_PARAM => self::ACTION_CHECK_UPDATES, | |
'addon_id' => $this->addon_id, | |
'addon_version' => $this->addon_version, | |
'license_number' => $this->license_number, | |
'product_name' => $this->product_name, | |
'product_version' => $this->product_version, | |
'product_build' => $this->product_build, | |
'product_edition' => $this->product_edition, | |
'product_url' => $this->product_url, | |
]; | |
$headers = []; | |
return [ | |
'method' => 'get', | |
'url' => $this->updates_server, | |
'data' => $data, | |
'headers' => $headers, | |
]; | |
} | |
/** | |
* Downloads an add-on update package. | |
* | |
* @param array $schema Update package schema | |
* @param string $package_path Absolute path on the server to put the update package to | |
* | |
* @return array | |
*/ | |
public function downloadPackage($schema, $package_path) | |
{ | |
$download_url = new Url($this->updates_server); | |
$download_url->setQueryParams(array_merge($download_url->getQueryParams(), [ | |
self::ACTION_PARAM => self::ACTION_DOWNLOAD_PACKAGE, | |
'package_id' => $schema['package_id'], | |
'addon_id' => $this->addon_id, | |
'license_number' => $this->license_number, | |
])); | |
$download_url = $download_url->build(); | |
$request_result = Http::get($download_url, [], [ | |
'write_to_file' => $package_path, | |
]); | |
if (!$request_result || strlen($error = Http::getError())) { | |
$download_result = [false, __('text_uc_cant_download_package')]; | |
fn_rm($package_path); | |
} else { | |
$download_result = [true, '']; | |
} | |
return $download_result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment