Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Classes/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use RobinTheHood\ModifiedModuleLoaderClient\Loader\ModuleLoader;
use RobinTheHood\ModifiedModuleLoaderClient\Loader\LocalModuleLoader;
use RobinTheHood\ModifiedModuleLoaderClient\Helpers\FileHelper;
use RobinTheHood\ModifiedModuleLoaderClient\Semver\Comparator;
use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser;

class Module extends ModuleInfo
{
Expand Down Expand Up @@ -438,12 +440,26 @@ public function isLoaded(): bool
return false;
}


public function isCompatible(): bool
{
if (!$this->isCompatibleWithModified()) {
return false;
}

if (!$this->isCompatibleWithPhp()) {
return false;
}

return true;
}

/**
* Checks whether this module is compatible with the installed version of modified.
*
* @return bool Returns true if the module is compatible, otherwise false.
*/
public function isCompatible(): bool
public function isCompatibleWithModified(): bool
{
$installedVersion = ShopInfo::getModifiedVersion();
$versions = $this->getModifiedCompatibility();
Expand All @@ -457,6 +473,19 @@ public function isCompatible(): bool
return false;
}

public function isCompatibleWithPhp(): bool
{
$php = $this->getPhp();
$phpVersionContraint = $php['version'] ?? '';
if (!$phpVersionContraint) {
return true;
}

$phpVersionInstalled = phpversion();
$comparator = new Comparator(new Parser());
return $comparator->satisfiesOr($phpVersionInstalled, $phpVersionContraint);
}

public function getTemplateFiles($file): array
{
$files = [];
Expand Down
3 changes: 2 additions & 1 deletion src/Classes/ModuleConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static function convertToArray(Module $module): array
'visibility' => $module->getVisibility(),
'price' => $module->getPrice(),
'autoload' => $module->getAutoload(),
'tags' => $module->getTags()
'tags' => $module->getTags(),
'php' => $module->getPhp()
];

$moduleArray = [
Expand Down
1 change: 1 addition & 0 deletions src/Classes/ModuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static function createFromArray(array $array): Module
$module->setPrice($array['price'] ?? '');
$module->setAutoload($autoload);
$module->setTags($array['tags'] ?? '');
$module->setPhp($array['php'] ?? []);

// Module
$module->setLocalRootPath($array['localRootPath'] ?? '');
Expand Down
22 changes: 22 additions & 0 deletions src/Classes/ModuleInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ class ModuleInfo
*/
protected $tags;

/**
* Mit welchen Version von PHP ist das Modul kompatible.
*
* Beispiel: [
* 'version' => '^7.4 || ^8.0'
* ]
*
* @var array
*/
protected $php;


public function getName(): string
{
return $this->name;
Expand Down Expand Up @@ -350,4 +362,14 @@ public function setTags(string $value): void
{
$this->tags = $value;
}

public function getPhp(): array
{
return $this->php;
}

public function setPhp(array $value): void
{
$this->php = $value;
}
}
17 changes: 17 additions & 0 deletions src/Classes/Semver/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ public function satisfies(string $versionString1, string $constrain): bool
}
}

/**
* Can satisfy multiple constraints with OR / ||
*
* Example: ^7.4 || ^8.0
*/
public function satisfiesOr(string $versionString1, string $constraintOrExpression): bool
{
$constraints = explode('||', $constraintOrExpression);
foreach ($constraints as $constraint) {
$constraint = trim($constraint);
if ($this->satisfies($versionString1, $constraint)) {
return true;
}
}
return false;
}

public function sort(array $versionStrings): array
{
usort($versionStrings, [$this, 'compareAsc']);
Expand Down
21 changes: 21 additions & 0 deletions src/Classes/ViewModels/ModuleViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use RobinTheHood\ModifiedModuleLoaderClient\Module;
use RobinTheHood\ModifiedModuleLoaderClient\ModuleStatus;
use RobinTheHood\ModifiedModuleLoaderClient\ShopInfo;

class ModuleViewModel
{
Expand Down Expand Up @@ -187,4 +188,24 @@ private function getUrl(string $action, string $ref): string
'&version=' . $this->module->getVersion() .
'&ref=' . $ref;
}

/**
* @return string[]
*/
public function getCompatibleStrings(): array
{
$array = [];

if (!$this->module->isCompatibleWithModified()) {
$version = ShopInfo::getModifiedVersion();
$array[] = "Dieses Modul wurde noch nicht mit deiner Version von modified getestet. Du hast modifed Version <strong>$version</strong> installiert.";
}

if (!$this->module->isCompatibleWithPhp()) {
$version = phpversion();
$array[] = "Dieses Modul wurde noch nicht mit deiner PHP Version getestet. Du hast PHP Version <strong>$version</strong> installiert.";
}

return $array;
}
}
24 changes: 19 additions & 5 deletions src/Templates/ModuleInfo.tmpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@
<?php } ?>

<?php if (!$moduleView->isCompatible()) { ?>
<div class="alert alert-warning" role="alert">
<i class="fas fa-exclamation-triangle fa-fw"></i>
Dieses Modul wurde noch nicht mit deiner Version von modified getestet. Du hast modifed
<strong><?= ShopInfo::getModifiedVersion()?></strong> installiert.
</div>
<?php foreach ($moduleView->getCompatibleStrings() as $string) { ?>
<div class="alert alert-warning" role="alert">
<i class="fas fa-exclamation-triangle fa-fw"></i>
<?= $string ?>
</div>
<?php } ?>
<?php } ?>
</div>
</div>
Expand Down Expand Up @@ -270,6 +271,19 @@
</td>
</tr>

<tr>
<td>Kompatibel mit PHP</td>
<td>
<?php if ($module->getPhp()) { ?>
<?php foreach (explode('||', $module->getPhp()['version'] ?? '') as $version) { ?>
<span class="badge badge-secondary"><?= trim($version); ?></span>
<?php } ?>
<?php } else { ?>
unbekannt
<?php } ?>
</td>
</tr>

<?php if ($module->getTags()) { ?>
<tr>
<td>Tags</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit;
namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit\SemverTests;

use PHPUnit\Framework\TestCase;
use RobinTheHood\ModifiedModuleLoaderClient\Semver\Comparator;
Expand Down Expand Up @@ -195,4 +195,10 @@ public function testThatVersionASatisfiesContraint()
$this->assertTrue($this->comparator->satisfies('3.3.3', '3.3.3'));
$this->assertFalse($this->comparator->satisfies('3.3.3', '3.2.3'));
}

public function testThatVersionASatisfiesOrContraint()
{
$this->assertTrue($this->comparator->satisfiesOr('3.3.3', '^2.2.2 || ^3.3.3'));
$this->assertFalse($this->comparator->satisfiesOr('4.4.4', '^2.2.2 || ^3.3.3'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
* file that was distributed with this source code.
*/

namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit;
namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit\SemverTests;

use PHPUnit\Framework\TestCase;
use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser;

class SemverParserTest extends TestCase
{
/** @var Parser */
private $parser;

protected function setUp(): void
{
$this->parser = new Parser();
Expand Down