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

Add feature to disable modules #340

Closed
wants to merge 7 commits into from

Conversation

jolelievre
Copy link
Contributor

This PR fixes the issue PrestaShop/PrestaShop#14779

The module can now disable custom modules by moving them into another folder
This feature was added inside the CoreUpgrader (bound to an option "Disable non native modules"
And a new controller was aded so that this new feature can be used via an API

public function getCustomModulesOnDisk($versions)
{
$modulesOnDisk = $this->getModulesOnDisk();
$nativeModules = array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mix short array and array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! I'm used to short array now, but in this module I think use the array() notation since we're not sure of the PHP version? wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short array came with 5.4 and this module require 5.6+.

/**
* Class AddonsCurlClient is a simple Addons client that uses Curl to perform its request.
*/
class AddonsCurlClient implements AddonsClientInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this class a copy from the Core?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an exact copy, but the code comes from the core yes, but I prefer to have a dedicated class to manage this call inside the module. Since we don't know on which version the module is installed we can't rely on the core classes because they are modified from a version to another.
I think that's why the Tools class was copied in Tools14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way I think I copied this code from the Tools class but I can't remember which version (since I was working on different versions at the moment). I also removed a few switch cases because the API simply didn't work with these parameters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only asking because of snake case and some parts about services and hosted_module 😅

$disabledModulesDir,
AddonsClientInterface $addonsClient
) {
$this->modulesDir = rtrim($modulesDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to not use DIRECTORY_SEPARATOR constant in PHP.
If you use DIRECTORY_SEPARATOR, which resolves to \ on windows, things tend to break. For example:\ gets converted to %5C in urls.

*/
public function setOriginVersion($originVersion)
{
$this->originVersion = $originVersion;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->originVersion = $originVersion;
$this->originVersion = (string) $originVersion;

deactivate_custom_modules();
$customModules = $this->moduleRepository->getCustomModulesOnDisk([$this->originVersion, $this->upgradeVersion]);
foreach ($customModules as $moduleName) {
$this->moduleDisabler->disableModuleFromDatabase($moduleName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this class is not final, always prefer getter function

Suggested change
$this->moduleDisabler->disableModuleFromDatabase($moduleName);
$this->getModuleDisabler()->disableModuleFromDatabase($moduleName);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case it's overridden?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, even for us we we have another Disabler based on this class. If we have setter / getter, always prefer them.

Copy link
Contributor Author

@jolelievre jolelievre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @PierreRambaud
Thanks for the review, I have a few answers/questions for you ;)

classes/AutoupgradeException.php Outdated Show resolved Hide resolved
classes/Module/AddonsClientInterface.php Outdated Show resolved Hide resolved
classes/Module/AddonsClientInterface.php Outdated Show resolved Hide resolved
/**
* Class AddonsCurlClient is a simple Addons client that uses Curl to perform its request.
*/
class AddonsCurlClient implements AddonsClientInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an exact copy, but the code comes from the core yes, but I prefer to have a dedicated class to manage this call inside the module. Since we don't know on which version the module is installed we can't rely on the core classes because they are modified from a version to another.
I think that's why the Tools class was copied in Tools14

/**
* Class AddonsCurlClient is a simple Addons client that uses Curl to perform its request.
*/
class AddonsCurlClient implements AddonsClientInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way I think I copied this code from the Tools class but I can't remember which version (since I was working on different versions at the moment). I also removed a few switch cases because the API simply didn't work with these parameters.

classes/Module/ModuleRepository.php Outdated Show resolved Hide resolved
classes/UpgradeContainer.php Show resolved Hide resolved
deactivate_custom_modules();
$customModules = $this->moduleRepository->getCustomModulesOnDisk([$this->originVersion, $this->upgradeVersion]);
foreach ($customModules as $moduleName) {
$this->moduleDisabler->disableModuleFromDatabase($moduleName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case it's overridden?

tests/ModuleRepositoryTest.php Show resolved Hide resolved
upgrade/upgrade-5-0-0.php Show resolved Hide resolved
@PierreRambaud
Copy link
Contributor

ping @Quetzacoalt91 Can you also have a check on it?

Comment on lines +85 to +87
break;

break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2 break ?

private function parseRequest($requestContent)
{
if (empty($requestContent)) {
false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
false;
return false;

@@ -0,0 +1,33 @@
<?php
/**
* 2007-2019 PrestaShop SA and Contributors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update headers

*
* @return self
*/
public function setOriginVersion($originVersion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function setOriginVersion($originVersion)
public function setOriginVersion(string $originVersion)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do this kind of changes in the autoupgrade module, it must be compatible with older PS versions :)

@Progi1984
Copy link
Contributor

Ping @jolelievre

Comment on lines +23 to +24
<a target="_blank" href="{{ link.getAdminLink('AutoupgradeModule') }}&version=1.6.1">List modules 1.6.1</a>
<a target="_blank" href="{{ link.getAdminLink('AutoupgradeModule') }}&version=1.7.6">List modules 1.7.6</a>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<a target="_blank" href="{{ link.getAdminLink('AutoupgradeModule') }}&version=1.6.1">List modules 1.6.1</a>
<a target="_blank" href="{{ link.getAdminLink('AutoupgradeModule') }}&version=1.7.6">List modules 1.7.6</a>
<a target="_blank" href="{{ link.getAdminLink('AutoupgradeModule') }}&amp;version=1.6.1">List modules 1.6.1</a>
<a target="_blank" href="{{ link.getAdminLink('AutoupgradeModule') }}&amp;version=1.7.6">List modules 1.7.6</a>

{
parent::setUp();
$this->fileSystem = new Filesystem();
$this->tempDir = sys_get_temp_dir() . '/module_disabler';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just notice you should use a test directory, instead of the system directory, some container doesn't have /tmp 😅

@matks
Copy link
Contributor

matks commented Dec 3, 2021

Hi @jolelievre

Since we had no news from you for more than 30 days (and some more), I will follow our guidelines and I'll close this pull request as it seems stale. Feel free to reopen or open another one if you think it's still relevant 😉

Thanks!

@matks matks closed this Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants