-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track module upgrades on Distribution API
- Loading branch information
Showing
8 changed files
with
322 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DISTRIBUTION_API_URL="http://localhost:3000" |
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
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 |
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
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
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
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
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()); | ||
} | ||
} |
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
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; | ||
} | ||
} |
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
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; | ||
} |