Skip to content

Commit

Permalink
Uniform Package builder and move data insertion jit
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColomb committed Feb 14, 2022
1 parent 869b171 commit ff7f953
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
50 changes: 40 additions & 10 deletions src/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@

class Package extends CompletePackage implements JsonSerializable
{
private \Closure $distCallback;

public function __construct(string $name)
{
parent::__construct(
$name,
'0.0.0.0',
'0.0.0',
);

$this->withMetadata();
$this->withLinks();
$this->withRequires();
$this->withSuggests();
}

/**
Expand All @@ -50,7 +47,7 @@ public function withVersion(string $version): self
return $this;
}

protected function withMetadata(): void
protected function withMetadata(): self
{
$this->setType('wordpress-core');
$this->setDescription('WordPress is web software you can use to create a beautiful website or blog.');
Expand All @@ -66,9 +63,11 @@ protected function withMetadata(): void
'cms'
]);
$this->setLicense(['GPL-2.0-or-later']);

return $this;
}

protected function withLinks(): void
protected function withLinks(): self
{
$this->setHomepage('https://wordpress.org/');
$this->setSupport([
Expand All @@ -86,9 +85,11 @@ protected function withLinks(): void
'url' => 'https://wordpressfoundation.org/donate/'
]
]);

return $this;
}

public function withRequires(string $minPhpVersion = null): void
public function withRequires(string $minPhpVersion = null): self
{
if (is_null($minPhpVersion)) {
$minPhpVersion = self::getMinPhpVersion($this->getVersion());
Expand All @@ -110,9 +111,11 @@ public function withRequires(string $minPhpVersion = null): void
'^1.0'
)
]);

return $this;
}

public function withProvides(): void
public function withProvides(): self
{
if ($this->version === '0.0.0.0') {
throw new \Exception('The version must be set before setting implementations provided');
Expand All @@ -127,14 +130,16 @@ public function withProvides(): void
$this->prettyVersion
)
]);

return $this;
}

/**
* Suggest PHP extensions that WordPress expects to be provided.
*
* @see https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions
*/
protected function withSuggests(): void
protected function withSuggests(): self
{
$this->setSuggests([
'ext-curl' => 'Performs remote request operations.',
Expand All @@ -152,6 +157,8 @@ protected function withSuggests(): void
'ext-xml' => 'Used for XML parsing, such as from a third-party site.',
'ext-zip' => 'Used for decompressing Plugins, Themes, and WordPress update packages.',
]);

return $this;
}

/**
Expand All @@ -173,11 +180,34 @@ protected static function getMinPhpVersion(string $version): string
return '5.6.20';
}

public function setDistCallback(\Closure $callback): void
{
$this->distCallback = $callback;
}

/**
* Just in time metadata insertion before serialization.
*
* @return void
*/
private function prepareData(): void
{
$this->withMetadata();
$this->withLinks();
$this->withSuggests();

if (isset($this->distCallback)) {
($this->distCallback)($this);
}
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
$this->prepareData();

$dump = (new ArrayDumper())->dump($this);
// TODO: figure out why I have to do this in upstream
unset($dump['version_normalized']);
Expand Down
26 changes: 14 additions & 12 deletions tests/Package/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testJsonSerialize()
$json = new JsonFile($this->jsonFile);
$this->assertTrue($json->validateSchema());

$this->assertJsonFileEqualsJsonFile($this->jsonFile, __DIR__ . '/../resources/composer.json');
$this->assertJsonFileEqualsJsonFile($this->jsonFile, __DIR__ . '/../resources/package-composer.json');
}

public function testGreaterThanOrEqualTo()
Expand All @@ -95,16 +95,18 @@ public function testMinPhpVersion()

$builder = new Package('roots/wordpress');

$phpNew1 = $getPhpPackage($builder->clone()->withVersion('5.2.1'));
$phpNew2 = $getPhpPackage($builder->clone()->withVersion('5.2'));
$phpNew3 = $getPhpPackage($builder->clone()->withVersion('5.2-beta1'));
$phpOld1 = $getPhpPackage($builder->clone()->withVersion('5.1'));
$phpOld2 = $getPhpPackage($builder->clone()->withVersion('4.0'));

$this->assertTrue($phpNew1->matches(new Constraint('=', '5.6.20')));
$this->assertTrue($phpNew2->matches(new Constraint('=', '5.6.20')));
$this->assertTrue($phpNew3->matches(new Constraint('=', '5.6.20')));
$this->assertTrue($phpOld1->matches(new Constraint('=', '5.2.4')));
$this->assertTrue($phpOld2->matches(new Constraint('=', '5.2.4')));
$phpNew1 = $getPhpPackage($builder->clone()->withVersion('5.2.1')->withRequires());
$phpNew2 = $getPhpPackage($builder->clone()->withVersion('5.2')->withRequires());
$phpNew3 = $getPhpPackage($builder->clone()->withVersion('5.2-beta1')->withRequires());
$phpOld1 = $getPhpPackage($builder->clone()->withVersion('5.1')->withRequires());
$phpOld2 = $getPhpPackage($builder->clone()->withVersion('4.0')->withRequires());
$phpCustom = $getPhpPackage($builder->clone()->withVersion('7.8')->withRequires('7.7.7'));

$this->assertTrue($phpNew1->matches(new Constraint(Constraint::STR_OP_EQ, '5.6.20')));
$this->assertTrue($phpNew2->matches(new Constraint(Constraint::STR_OP_EQ, '5.6.20')));
$this->assertTrue($phpNew3->matches(new Constraint(Constraint::STR_OP_EQ, '5.6.20')));
$this->assertTrue($phpOld1->matches(new Constraint(Constraint::STR_OP_EQ, '5.2.4')));
$this->assertTrue($phpOld2->matches(new Constraint(Constraint::STR_OP_EQ, '5.2.4')));
$this->assertTrue($phpCustom->matches(new Constraint(Constraint::STR_OP_EQ, '7.7.7')));
}
}
File renamed without changes.

0 comments on commit ff7f953

Please sign in to comment.