Skip to content

Commit

Permalink
Track module upgrades on Distribution API
Browse files Browse the repository at this point in the history
  • Loading branch information
sowbiba committed Jan 17, 2023
1 parent 26a384c commit 4e05e08
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DISTRIBUTION_API_URL="http://localhost:3000"
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.php_cs.cache
config_*.xml
/vendor/
!/vendor/index.php
!/vendor/index.php
.env
.env.*
!.env.dist
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_mbo</name>
<displayName><![CDATA[PrestaShop Marketplace in your Back Office]]></displayName>
<version><![CDATA[2.0.2]]></version>
<version><![CDATA[2.0.3]]></version>
<description><![CDATA[Discover the best PrestaShop modules to optimize your online store.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[administration]]></tab>
Expand Down
7 changes: 7 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ services:
- '@request_stack'
- '@mbo.externalcontent.provider'
- '@mbo.addons_selection_link_provider'

mbo.event_subscriber.module_event_subsciber:
class: PrestaShop\Module\Mbo\EventSubscriber\ModuleEventSubscriber
arguments:
- '@logger'
tags:
- { name: kernel.event_subscriber }
2 changes: 1 addition & 1 deletion ps_mbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class ps_mbo extends Module
public function __construct()
{
$this->name = 'ps_mbo';
$this->version = '2.0.2';
$this->version = '2.0.3';
$this->author = 'PrestaShop';
$this->tab = 'administration';
$this->module_key = '6cad5414354fbef755c7df4ef1ab74eb';
Expand Down
76 changes: 76 additions & 0 deletions src/EventSubscriber/ModuleEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\Module\Mbo\EventSubscriber;

use PrestaShop\Module\Mbo\UpgradeTracker;
use PrestaShopBundle\Event\ModuleManagementEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Listen upgrade events from module manager, and push event to distribution event for tracking purpose.
*/
class ModuleEventSubscriber implements EventSubscriberInterface
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @param LoggerInterface $logger
*/
public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
ModuleManagementEvent::UPGRADE => 'pushDistributionTracking',
];
}

/**
* @param ModuleManagementEvent $event
*/
public function pushDistributionTracking(ModuleManagementEvent $event)
{
$module = $event->getModule();
$moduleName = $module->get('name');
if ('ps_mbo' !== $moduleName) {
return;
}

(new UpgradeTracker())->postTracking($module->getInstance());
}
}
197 changes: 197 additions & 0 deletions src/UpgradeTracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\Module\Mbo;

use Configuration;
use Db;
use Module;
use Shop;
use Symfony\Component\Dotenv\Dotenv;

class UpgradeTracker
{
/**
* @param Module $module
*
* @return bool
*/
public function postTracking(Module $module, ?string $nextVersion = null)
{
try {
// Load env variables
$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../.env');

$apiBaseUrl = getenv('DISTRIBUTION_API_URL');

if (is_string($apiBaseUrl) && !empty($apiBaseUrl)) {
// Execute the API call
$this->sendRequest(
$apiBaseUrl . '/api/shops/track-upgrade-mbo',
[
'shop_url' => $this->getShopUrl(),
'ps_version' => _PS_VERSION_,
// At this point, the module is not changed in DB yet but module->database_version is null. So we make DB query
'from_mbo_version' => $this->getCurrentModuleVersion(),
'to_mbo_version' => $nextVersion ?: $this->getNextModuleVersion(),
]
);
}
} catch (\Exception $e) {
return false;
}

return true;
}

/**
* @param string $url
* @param array $data
*/
private function sendRequest($url, $data)
{
if (function_exists('curl_init') && defined('CURLOPT_POST')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
$content = curl_exec($curl);
$httpcode = curl_getinfo($curl);

$error = curl_error($curl);
if ($error) {
curl_close($curl);
throw new \Exception($error, curl_errno($curl));
}

curl_close($curl);
}
}

/**
* The Tools::usingSecureMode used by Shop::getBaseUrl seems to not work in all situations
* To be sure to have the correct data, use shop configuration
*
* @return string|null
*/
private function getShopUrl()
{
$shopUrl = null;
$singleShop = $this->getSingleShop();
$useSecureProtocol = $this->isUsingSecureProtocol();
$domainConfigKey = $useSecureProtocol ? 'PS_SHOP_DOMAIN_SSL' : 'PS_SHOP_DOMAIN';

$domain = Configuration::get(
$domainConfigKey,
null,
$singleShop->id_shop_group,
$singleShop->id
);

if ($domain) {
$domain = preg_replace('#(https?://)#', '', $domain);
$shopUrl = ($useSecureProtocol ? 'https://' : 'http://') . $domain;
}

return $shopUrl;
}

/**
* @return bool
*/
private function isUsingSecureProtocol()
{
$singleShop = $this->getSingleShop();

return (bool) Configuration::get(
'PS_SSL_ENABLED',
null,
$singleShop->id_shop_group,
$singleShop->id
);
}

/**
* @return Shop
*/
private function getSingleShop()
{
$shops = Shop::getShops(false, null, true);

return new Shop((int) reset($shops));
}

/**
* @return string
*/
private function getCurrentModuleVersion()
{
return (string) Db::getInstance()->getValue(sprintf("SELECT `version` FROM `%smodule` WHERE `name`='ps_mbo'", _DB_PREFIX_));
}

/**
* Because there is an issue after upgrade : the version in module->version is the old one.
*
* @return string
*
* @throws \Exception
*/
private function getNextModuleVersion()
{
$moduleMainFile = sprintf('%s/ps_mbo/ps_mbo.php', rtrim(_PS_MODULE_DIR_, '/'));
if (!file_exists($moduleMainFile)) {
throw new \Exception(sprintf('Could not find module main file at %s', $moduleMainFile));
}

$moduleClassContent = file_get_contents($moduleMainFile);
preg_match('/const[ ]+VERSION[ ]*=[ ]*\'(.+)\'/', $moduleClassContent, $matches);

if (!empty($matches)) {
$version = $matches[1];
} else {
preg_match('/this->version[ ]*=[ ]*\'(.+)\'/', $moduleClassContent, $matches);

if (!empty($matches)) {
$version = $matches[1];
}
}

if (empty($version)) {
throw new \Exception(sprintf('Could not find module version in the main file at %s', $moduleMainFile));
}

return (string) $version;
}
}
35 changes: 35 additions & 0 deletions upgrade/upgrade-2.0.3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

use PrestaShop\Module\Mbo\UpgradeTracker;

/**
* @param ps_mbo $module
*
* @return bool
*/
function upgrade_module_2_0_3($module)
{
$return = true;

$return &= (new UpgradeTracker())->postTracking($module, $module->version);

return true;
}

0 comments on commit 4e05e08

Please sign in to comment.