Skip to content

Commit

Permalink
fix: clean up entry file and remove created upgrade file after upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 16, 2024
1 parent 9f510f3 commit cae07fc
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 90 deletions.
105 changes: 15 additions & 90 deletions myparcelnl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

declare(strict_types=1);

use MyParcelNL\Pdk\Base\FileSystemInterface;
use MyParcelNL\Pdk\Base\Pdk as PdkInstance;
use MyParcelNL\Pdk\Facade\Installer;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Facade\MyParcelModule;
use MyParcelNL\PrestaShop\Hooks\HasModuleUpgradeOverrides;
use MyParcelNL\PrestaShop\Hooks\HasPdkCheckoutDeliveryOptionsHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkCheckoutHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkOrderGridHooks;
Expand All @@ -29,6 +29,11 @@
*/
class MyParcelNL extends CarrierModule
{
use HasModuleUpgradeOverrides;

/**
* Module hooks
*/
use HasPdkCheckoutDeliveryOptionsHooks;
use HasPdkCheckoutHooks;
use HasPdkOrderGridHooks;
Expand All @@ -39,14 +44,10 @@ class MyParcelNL extends CarrierModule
use HasPsCarrierHooks;
use HasPsShippingCostHooks;

/**
* @var bool
*/
private $hasPdk;
private static ?string $versionFromComposer = null;

private bool $hasPdk = false;

/**
* @throws \JsonException
*/
public function __construct()
{
// Suppress deprecation warning from Pdk HasAttributes
Expand Down Expand Up @@ -83,72 +84,18 @@ function () {

/**
* @return string
* @throws \JsonException
*/
protected static function getVersionFromComposer(): string
{
$filename = __DIR__ . '/composer.json';
$composerData = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR);

return $composerData['version'];
}

/**
* @param string $moduleName
* @param string $moduleVersion
* @param string $registeredVersion
*
* @return bool
* @noinspection PhpMissingReturnTypeInspection
* @noinspection ReturnTypeCanBeDeclaredInspection
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
*/
protected static function loadUpgradeVersionList($moduleName, $moduleVersion, $registeredVersion)
{
try {
// Trigger pdk setup to use facades
new MyParcelNL();

self::writeUpgradeFile();
} catch (Throwable $e) {
Logger::error("Failed to write upgrade file: {$e->getMessage()}", [
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace(),
]);
if (! self::$versionFromComposer) {
$filename = __DIR__ . '/composer.json';
/** @noinspection JsonEncodingApiUsageInspection */
$composerData = json_decode(file_get_contents($filename), true);

return false;
self::$versionFromComposer = $composerData['version'];
}

return parent::loadUpgradeVersionList($moduleName, $moduleVersion, $registeredVersion);
}

/**
* When the module is upgraded, PrestaShop checks to see if upgrade files exist. We need every update ever to
* trigger MyParcelModule::install(). So, whenever PrestaShop checks our module for upgrade files, write a new
* upgrade file for the current version to trigger the install method.
*
* @return void
* @throws \JsonException
*/
private static function writeUpgradeFile(): void
{
/** @var \MyParcelNL\Pdk\Base\FileSystemInterface $fileSystem */
$fileSystem = Pdk::get(FileSystemInterface::class);

$version = str_replace('-', '_', static::getVersionFromComposer());
$content = '<?php function upgrade_module___VERSION__($module): bool { return \\MyParcelNL\\PrestaShop\\Facade\\MyParcelModule::install($module); }';

$upgradeDir = sprintf('%s/upgrade', __DIR__);

$fileSystem->mkdir($upgradeDir, true);

$fileSystem->put(
sprintf('%s/upgrade-%s.php', $upgradeDir, $version),
strtr($content, [
'__VERSION__' => str_replace(['.', '-'], '_', $version),
])
);
return self::$versionFromComposer;
}

/**
Expand Down Expand Up @@ -195,28 +142,6 @@ public function install(): bool
});
}

/**
* For some reason the cache is cleared halfway throughout the upgrade process when running it via the CLI.
* PrestaShop then proceeds to throw errors because these properties are not set.
*
* @return array
*/
public function runUpgradeModule(): array
{
$upgrade = &static::$modules_cache[$this->name]['upgrade'];

$upgrade['success'] ??= false;
$upgrade['available_upgrade'] ??= 0;
$upgrade['number_upgraded'] ??= 0;
$upgrade['number_upgrade_left'] ??= 0;
$upgrade['upgrade_file_left'] ??= [];
$upgrade['version_fail'] ??= 0;
$upgrade['upgraded_from'] ??= 0;
$upgrade['upgraded_to'] ??= 0;

return parent::runUpgradeModule();
}

/**
* @return bool
*/
Expand Down
137 changes: 137 additions & 0 deletions src/Hooks/HasModuleUpgradeOverrides.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Hooks;

use MyParcelNL;
use MyParcelNL\Pdk\Base\FileSystemInterface;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Facade\Pdk;
use Throwable;

/**
* @mixin \Module
*/
trait HasModuleUpgradeOverrides
{
/**
* @param $name
* @param $version
*
* @return bool
*/
public static function upgradeModuleVersion($name, $version): bool

Check warning on line 24 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L24

Added line #L24 was not covered by tests
{
$result = parent::upgradeModuleVersion($name, $version);

Check warning on line 26 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L26

Added line #L26 was not covered by tests

try {
/** @var \MyParcelNL\Pdk\Base\FileSystemInterface $fileSystem */
$fileSystem = Pdk::get(FileSystemInterface::class);
$filename = static::getUpgradeFileName();

Check warning on line 31 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L30-L31

Added lines #L30 - L31 were not covered by tests

if ($fileSystem->fileExists($filename)) {
$fileSystem->unlink($filename);

Check warning on line 34 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L33-L34

Added lines #L33 - L34 were not covered by tests
}
} catch (Throwable $e) {
Logger::error("Failed to remove upgrade file: {$e->getMessage()}", [
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace(),
]);

Check warning on line 41 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L36-L41

Added lines #L36 - L41 were not covered by tests
}

return $result;

Check warning on line 44 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L44

Added line #L44 was not covered by tests
}

/**
* @param string $moduleName
* @param string $moduleVersion
* @param string $registeredVersion
*
* @return bool
* @noinspection PhpMissingReturnTypeInspection
* @noinspection ReturnTypeCanBeDeclaredInspection
*/
protected static function loadUpgradeVersionList($moduleName, $moduleVersion, $registeredVersion)
{
try {
// Trigger pdk setup to use facades
new MyParcelNL();

static::writeUpgradeFile();
} catch (Throwable $e) {
Logger::error("Failed to write upgrade file: {$e->getMessage()}", [
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace(),
]);

Check warning on line 68 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L63-L68

Added lines #L63 - L68 were not covered by tests

return false;

Check warning on line 70 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L70

Added line #L70 was not covered by tests
}

return parent::loadUpgradeVersionList($moduleName, $moduleVersion, $registeredVersion);
}

/**
* When the module is upgraded, PrestaShop checks to see if upgrade files exist. We need every update ever to
* trigger MyParcelModule::install(). So, whenever PrestaShop checks our module for upgrade files, write a new
* upgrade file for the current version to trigger the install method.
*
* @return void
*/
protected static function writeUpgradeFile(): void
{
/** @var \MyParcelNL\Pdk\Base\FileSystemInterface $fileSystem */
$fileSystem = Pdk::get(FileSystemInterface::class);

$fileSystem->mkdir(static::getUpgradeDir(), true);

$content = '<?php function upgrade_module___VERSION__($module): bool { return \\MyParcelNL\\PrestaShop\\Facade\\MyParcelModule::install($module); }';

$fileSystem->put(static::getUpgradeFileName(), strtr($content, [
'__VERSION__' => str_replace(['.', '-'], '_', static::getVersionFromComposer()),
]));
}

/**
* @return string
*/
private static function getUpgradeDir(): string
{
return sprintf('%s/../../upgrade', __DIR__);
}

/**
* @return string
*/
private static function getUpgradeFileName(): string
{
$upgradeDir = static::getUpgradeDir();
$version = str_replace('-', '_', static::getVersionFromComposer());

return sprintf('%s/upgrade-%s.php', $upgradeDir, $version);
}

/**
* For some reason the cache is cleared halfway throughout the upgrade process when running it via the CLI.
* PrestaShop then proceeds to throw errors because these properties are not set.
*
* @return array
*/
public function runUpgradeModule(): array

Check warning on line 122 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L122

Added line #L122 was not covered by tests
{
$upgrade = &static::$modules_cache[$this->name]['upgrade'];

Check notice on line 124 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Hooks/HasModuleUpgradeOverrides.php#L124

Avoid using undefined variables such as '::$modules_cache' which will lead to PHP notices.

Check warning on line 124 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Hooks/HasModuleUpgradeOverrides.php#L124

The variable $modules_cache is not named in camelCase.

Check warning on line 124 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L124

Added line #L124 was not covered by tests

$upgrade['success'] ??= false;
$upgrade['available_upgrade'] ??= 0;
$upgrade['number_upgraded'] ??= 0;
$upgrade['number_upgrade_left'] ??= 0;
$upgrade['upgrade_file_left'] ??= [];
$upgrade['version_fail'] ??= 0;
$upgrade['upgraded_from'] ??= 0;
$upgrade['upgraded_to'] ??= 0;

Check warning on line 133 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L126-L133

Added lines #L126 - L133 were not covered by tests

return parent::runUpgradeModule();

Check warning on line 135 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Hooks/HasModuleUpgradeOverrides.php#L135

Added line #L135 was not covered by tests
}
}

Check notice on line 137 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Hooks/HasModuleUpgradeOverrides.php#L137

Expected 1 newline at end of file; 0 found

Check notice on line 137 in src/Hooks/HasModuleUpgradeOverrides.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Hooks/HasModuleUpgradeOverrides.php#L137

File must end with a newline character

0 comments on commit cae07fc

Please sign in to comment.