Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add url for install ps_mbo && update module #203

Merged
merged 14 commits into from
Dec 11, 2023
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ composer.phar:
vendor: composer.phar
./composer.phar install --no-dev -o;

tools/vendor: composer.phar
tools/vendor: composer.phar vendor
./composer.phar install --working-dir tools -o;
sed -i -e 's|%currentWorkingDirectory%/vendor|%currentWorkingDirectory%/tools/vendor|g' ./tools/vendor/prestashop/php-dev-tools/phpstan/ps-module-extension.neon

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"psr/http-message": "<1.1",
"symfony/config": "^3.4",
"symfony/dependency-injection": "^3.4",
"symfony/yaml": "^3.4"
"symfony/yaml": "^3.4",
"prestashop/module-lib-mbo-installer": "^0.1.0"
}
}
58 changes: 56 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@ imports:
- { resource: ../common.yml }
- { resource: ../front/repository.yml }
- { resource: ../front/services.yml }

services:
ps_eventbus.service.presenter:
class: 'PrestaShop\Module\PsEventbus\Service\PresenterService'
public: true

ps_eventbus.helper.module:
class: 'PrestaShop\Module\PsEventbus\Helper\ModuleHelper'
arguments:
- "@ps_eventbus"
public: true

ps_eventbus.module.upgrade:
class: 'PrestaShop\Module\PsEventbus\Module\Upgrade'
arguments:
- "@ps_eventbus"
public: true

6 changes: 6 additions & 0 deletions config/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ps_eventbus_api_resolver:
path: pseventbus/api/{query}
methods: [GET, POST]
defaults:
query: ""
_controller: 'PrestaShop\Module\PsEventbus\Controller\Admin\PsEventbusResolverController::resolve'
98 changes: 98 additions & 0 deletions src/Controller/Admin/PsEventbusResolverController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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 Academic Free License version 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/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 and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\PsEventbus\Controller\Admin;

use PrestaShop\Module\PsEventbus\Module\Upgrade;
use Prestashop\ModuleLibMboInstaller\Installer as MBOInstaller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class PsEventbusResolverController extends FrameworkBundleAdminController
{
/** @var \Ps_eventbus */
private $module;

public function __construct()
{
/** @var \Ps_eventbus $psEventbus */
$psEventbus = \Module::getInstanceByName('ps_eventbus');
$this->module = $psEventbus;
}

/**
* Api endpoint
*
* @param Request $request
* @param string $query
*
* @return Response
*
* @throws \Exception
*/
public function resolve(Request $request, string $query)
{
try {
if (is_callable([$this, $query])) {
/** @var callable $args */
$args = [$this, $query];

/** @var Response $result */
$result = call_user_func($args);

return $result;
}
} catch (\Throwable $th) {
throw new \Exception('#001 Message : ' . $th->getMessage());
}

return new Response('Not found', 404);
}

/**
* Install ps_mbo module
*
* @return Response
*/
public function installPsMbo(): Response
{
$mboInstaller = new MBOInstaller(_PS_VERSION_);

return new JsonResponse($mboInstaller->installModule(), 200);
}

/**
* Upgrade a module
*
* @return Response
*/
public function upgradeModule()
{
/** @var Upgrade $upgrade */
$upgrade = $this->module->getService('ps_eventbus.module.upgrade');

return new Response($upgrade->upgradePsEventbus(), 200, [
'Content-Type' => 'application/json',
]);
}
}
158 changes: 158 additions & 0 deletions src/Helper/ModuleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace PrestaShop\Module\PsEventbus\Helper;

use PrestaShopBundle\Service\Routing\Router;

class ModuleHelper
{
/** @var \Ps_eventbus */
private $module;

public function __construct(\Ps_eventbus $module)
{
$this->module = $module;
}

/**
* @param string $moduleName
*
* @return bool
*/
public function isInstalled(string $moduleName)
{
return \ModuleCore::isInstalled($moduleName);
}

/**
* @param string $moduleName
*
* @return bool
*/
public function isEnabled(string $moduleName)
{
return \ModuleCore::isEnabled($moduleName);
}

/**
* @param string $moduleName
*
* @return false|\ModuleCore
*/
public function getInstanceByName(string $moduleName)
{
return \ModuleCore::getInstanceByName($moduleName);
}

/**
* returns the installation link of the module if it is not installed. If installed, returns an empty string
*
* @param string $moduleName
*
* @return string
*/
public function getInstallLink(string $moduleName)
{
if (true === $this->isInstalled($moduleName)) {
return '';
}
/** @var Router $router * */
$router = $this->module->get('router');

if ($moduleName === 'ps_mbo') {
return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) .
$router->generate('ps_eventbus_api_resolver', [
'query' => 'installPsMbo',
]);
}

return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) .
$router->generate('admin_module_manage_action', [
'action' => 'install',
'module_name' => $moduleName,
]);
}

/**
* returns the enable link of the module if it is not enabled. If enabled, returns an empty string
*
* @param string $moduleName
*
* @return string
*/
public function getEnableLink(string $moduleName)
{
if (true === $this->isEnabled($moduleName)) {
return '';
}

/** @var Router $router * */
$router = $this->module->get('router');

return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) .
$router->generate('admin_module_manage_action', [
'action' => 'enable',
'module_name' => $moduleName,
]);
}

/**
* returns the update link of the module if it is not enabled. If enabled, returns an empty string
*
* @param string $moduleName
*
* @return string
*/
public function getUpdateLink(string $moduleName)
{
/** @var Router $router * */
$router = $this->module->get('router');

return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) .
$router->generate('admin_module_manage_action', [
'action' => 'upgrade',
'module_name' => $moduleName,
]);
}

/**
* get ps_analytics module version
*
* @param string $moduleName
*
* @return string
*/
public function getModuleVersion(string $moduleName)
{
if (false === $this->isInstalled($moduleName)) {
return '0.0.0';
}

$module = \Module::getInstanceByName($moduleName);

if (false === $module) {
return '0.0.0';
}

return $module->version;
}

/**
* Build informations about module
*
* @param string $moduleName
*
* @return array
*/
public function buildModuleInformations(string $moduleName)
{
return [
'isInstalled' => $this->isInstalled($moduleName),
'isEnabled' => $this->isEnabled($moduleName),
'linkEnable' => $this->getEnableLink($moduleName),
'linkInstall' => $this->getInstallLink($moduleName),
'linkUpdate' => $this->getUpdateLink($moduleName),
'version' => $this->getModuleVersion($moduleName),
];
}
}
Loading