Skip to content

Commit

Permalink
feat: Add the PHAR timestamp to PharMeta (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 21, 2023
1 parent b7472c2 commit f1c6086
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Phar/PharInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public function getNormalizedMetadata(): ?string
return $this->meta->normalizedMetadata;
}

public function getTimestamp(): int
{
return $this->meta->timestamp;
}

public function getSignature(): ?array
{
return $this->meta->signature;
Expand Down
5 changes: 5 additions & 0 deletions src/Phar/PharMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct(
public readonly ?string $stub,
public readonly ?string $version,
public readonly ?string $normalizedMetadata,
public readonly int $timestamp,
public readonly ?string $pubKeyContent,
public readonly array $filesMeta,
) {
Expand All @@ -64,6 +65,7 @@ public static function fromPhar(Phar|PharData $phar, ?string $pubKeyContent): se
$stub = $phar->getStub();
$version = $phar->getVersion();
$metadata = $phar->getMetadata();
$timestamp = $phar->getMTime();

return new self(
false === $compression ? CompressionAlgorithm::NONE : CompressionAlgorithm::from($compression),
Expand All @@ -72,6 +74,7 @@ public static function fromPhar(Phar|PharData $phar, ?string $pubKeyContent): se
'' === $version ? null : $version,
// TODO: check $unserializeOptions here
null === $metadata ? null : var_export($metadata, true),
$timestamp,
$pubKeyContent,
self::collectFilesMeta($phar),
);
Expand All @@ -93,6 +96,7 @@ public static function fromJson(string $json): self
$decodedJson['stub'],
$decodedJson['version'],
$decodedJson['normalizedMetadata'],
$decodedJson['timestamp'],
$decodedJson['pubKeyContent'],
$filesMeta,
);
Expand All @@ -106,6 +110,7 @@ public function toJson(): string
'stub' => $this->stub,
'version' => $this->version,
'normalizedMetadata' => $this->normalizedMetadata,
'timestamp' => $this->timestamp,
'pubKeyContent' => $this->pubKeyContent,
'filesMeta' => $this->filesMeta,
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Console/Command/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private static function pharProvider(): iterable
$oldDefaultPharStub,
'1.1.0',
null,
1559806605,
null,
[
'.hidden' => [
Expand Down Expand Up @@ -128,6 +129,7 @@ private static function pharProvider(): iterable
$oldDefaultPharStub,
'1.1.0',
null,
1559807994,
null,
[
'.hidden' => [
Expand Down Expand Up @@ -160,6 +162,7 @@ private static function pharProvider(): iterable
$sha512Stub,
'1.1.0',
null,
1374531272,
null,
[
'index.php' => [
Expand Down Expand Up @@ -187,6 +190,7 @@ private static function pharProvider(): iterable
$sha512Stub,
'1.1.0',
null,
1374531313,
<<<'EOF'
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuZkrHT54KtuBCTrR36+4tibd+2un9b
Expand Down
17 changes: 17 additions & 0 deletions tests/Phar/PharMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function pharMetaProvider(): iterable
null,
null,
null,
1509920675,
null,
[],
),
Expand All @@ -63,6 +64,7 @@ public static function pharMetaProvider(): iterable
'action' => 'sayHello',
)
EOL,
1509920675,
<<<'EOF'
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuZkrHT54KtuBCTrR36+4tibd+2un9b
Expand Down Expand Up @@ -121,6 +123,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
null,
1680285013,
null,
[
'sample.php' => [
Expand All @@ -143,6 +146,7 @@ public static function pharProvider(): iterable
$oldDefaultPharStub,
'1.1.0',
null,
1509920675,
null,
[
'foo.php' => [
Expand All @@ -165,6 +169,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
null,
1680680933,
null,
[
'sample.php' => [
Expand Down Expand Up @@ -192,6 +197,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
null,
1680469485,
null,
[
'sample.php' => [
Expand All @@ -216,6 +222,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
null,
1680469504,
null,
[
'sample.php' => [
Expand All @@ -239,6 +246,7 @@ public static function pharProvider(): iterable
$sha512Stub,
'1.1.0',
null,
1374531272,
null,
[
'index.php' => [
Expand All @@ -261,6 +269,7 @@ public static function pharProvider(): iterable
$sha512Stub,
'1.1.0',
null,
1374531313,
<<<'EOF'
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKuZkrHT54KtuBCTrR36+4tibd+2un9b
Expand Down Expand Up @@ -289,6 +298,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
"'Hello world!'",
1680366918,
null,
[
'sample.php' => [
Expand All @@ -311,6 +321,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
'-19.8',
1680366947,
null,
[
'sample.php' => [
Expand All @@ -337,6 +348,7 @@ public static function pharProvider(): iterable
'action' => 'sayHello',
)
EOL,
1680367053,
null,
[
'sample.php' => [
Expand All @@ -356,6 +368,7 @@ public static function pharProvider(): iterable
null,
null,
null,
1680284754,
null,
[
'sample.txt' => [
Expand All @@ -376,6 +389,7 @@ public static function pharProvider(): iterable
null,
null,
null,
1680284663,
null,
[
'sample.txt' => [
Expand All @@ -397,6 +411,7 @@ public static function pharProvider(): iterable
null,
null,
null,
1680284660,
null,
[
'sample.txt' => [
Expand All @@ -418,6 +433,7 @@ public static function pharProvider(): iterable
null,
null,
null,
1680284663,
null,
[
'sample.txt' => [
Expand All @@ -441,6 +457,7 @@ public static function pharProvider(): iterable
$defaultStub,
'1.1.0',
null,
1680645848,
null,
[
'.hidden-dir/.hidden-file' => [
Expand Down

0 comments on commit f1c6086

Please sign in to comment.