From 0e3a3f5566323d3720db412c351112ee2f5fd7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 22 Oct 2023 14:21:04 +0200 Subject: [PATCH] refactor: Move the dumped PHAR meta JSON to .phar --- requirement-checker/Makefile | 2 +- src/Console/Command/Extract.php | 2 +- src/Console/PharInfoRenderer.php | 9 +++++++-- src/Phar/PharInfo.php | 3 ++- tests/Console/Command/DiffTest.php | 4 ++-- tests/Console/Command/ExtractTest.php | 28 +++++++++++++-------------- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/requirement-checker/Makefile b/requirement-checker/Makefile index 1349ceb9e..26eaef625 100644 --- a/requirement-checker/Makefile +++ b/requirement-checker/Makefile @@ -235,7 +235,7 @@ $(PHAR): $(PHAR_SRC_FILES) $(BOX_BIN) $(PHAR_PREFIX) $(PHAR_EXTRACT): $(PHAR) $(BOX_BIN) $(BOX) extract $(PHAR) $(PHAR_EXTRACT) - rm -rf $(PHAR_EXTRACT)/.phar_meta.json + rm -rf $(PHAR_EXTRACT)/.phar touch -c $@ $(BOX_BIN): diff --git a/src/Console/Command/Extract.php b/src/Console/Command/Extract.php index 4ead856d3..bf37e1f6d 100644 --- a/src/Console/Command/Extract.php +++ b/src/Console/Command/Extract.php @@ -39,7 +39,7 @@ final class Extract implements Command { public const STUB_PATH = '.phar/stub.php'; - public const PHAR_META_PATH = '.phar_meta.json'; + public const PHAR_META_PATH = '.phar/meta.json'; private const PHAR_ARG = 'phar'; private const OUTPUT_ARG = 'output'; diff --git a/src/Console/PharInfoRenderer.php b/src/Console/PharInfoRenderer.php index 2a813c013..2d5dca0d4 100644 --- a/src/Console/PharInfoRenderer.php +++ b/src/Console/PharInfoRenderer.php @@ -61,9 +61,14 @@ public static function renderShortSummary( self::renderContentsSummary(...), ]; - foreach ($methods as $method) { + $lastIndex = count($methods) - 1; + + foreach ($methods as $index => $method) { $method($pharInfo, $io); - $separator(); + + if ($index !== $lastIndex) { + $separator(); + } } } diff --git a/src/Phar/PharInfo.php b/src/Phar/PharInfo.php index d3023e75d..b0d5baa2e 100644 --- a/src/Phar/PharInfo.php +++ b/src/Phar/PharInfo.php @@ -62,6 +62,7 @@ use function iter\toArrayWithKeys; use function random_bytes; use function sprintf; +use const DIRECTORY_SEPARATOR; /** * @private @@ -321,7 +322,7 @@ private static function loadDumpedPharFiles(string $tmp): array ), ); - $meta = PharMeta::fromJson($dumpedFiles[Extract::PHAR_META_PATH]->getContents()); + $meta = PharMeta::fromJson(FS::getFileContents($tmp.DIRECTORY_SEPARATOR.Extract::PHAR_META_PATH)); unset($dumpedFiles[Extract::PHAR_META_PATH]); return [$meta, $dumpedFiles]; diff --git a/tests/Console/Command/DiffTest.php b/tests/Console/Command/DiffTest.php index 89e0ed992..962f3fd48 100644 --- a/tests/Console/Command/DiffTest.php +++ b/tests/Console/Command/DiffTest.php @@ -724,7 +724,7 @@ public static function GNUDiffPharsProvider(): iterable // Comparing the two archives contents (gnu diff)... Common subdirectories: simple-phar-bar.phar/.phar and simple-phar-baz.phar/.phar - diff --exclude=.phar_meta.json simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php + diff --exclude=.phar/meta.json simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php 3c3 < echo "Hello world!"; --- @@ -770,7 +770,7 @@ public static function GNUDiffPharsProvider(): iterable // Comparing the two archives contents (gnu diff)... Common subdirectories: simple-phar-bar.phar/.phar and simple-phar-baz.phar/.phar - diff '--exclude=.phar_meta.json' simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php + diff '--exclude=.phar/meta.json' simple-phar-bar.phar/bar.php simple-phar-baz.phar/bar.php 3c3 < echo "Hello world!"; --- diff --git a/tests/Console/Command/ExtractTest.php b/tests/Console/Command/ExtractTest.php index d240c2a3e..cd7aa24af 100644 --- a/tests/Console/Command/ExtractTest.php +++ b/tests/Console/Command/ExtractTest.php @@ -99,10 +99,10 @@ private static function pharProvider(): iterable ); $expectedSimplePharFiles = [ + '.phar/meta.json' => $pharMeta->toJson(), '.phar/stub.php' => $oldDefaultPharStub, '.hidden' => 'baz', 'foo' => 'bar', - '.phar_meta.json' => $pharMeta->toJson(), ]; yield 'simple PHAR' => [ @@ -122,7 +122,7 @@ private static function pharProvider(): iterable '.phar/stub.php' => $oldDefaultPharStub, '.hidden' => 'baz', 'foo' => 'bar', - '.phar_meta.json' => (new PharMeta( + '.phar/meta.json' => (new PharMeta( CompressionAlgorithm::NONE, [ 'hash' => '3CCDA01B80C1CAC91494EA59BBAFA479E38CD120', @@ -151,12 +151,7 @@ private static function pharProvider(): iterable yield 'sha512 signed PHAR' => [ self::FIXTURES_DIR.'/sha512.phar', [ - '.phar/stub.php' => $sha512Stub, - 'index.php' => <<<'PHP' - (new PharMeta( + '.phar/meta.json' => (new PharMeta( CompressionAlgorithm::NONE, [ 'hash' => 'B4CAE177138A773283A748C8770A7142F0CC36D6EE88E37900BCF09A92D840D237CE3F3B47C2C7B39AC2D2C0F9A16D63FE70E1A455723DD36840B6E2E64E2130', @@ -174,18 +169,18 @@ private static function pharProvider(): iterable ], ], ))->toJson(), + '.phar/stub.php' => $sha512Stub, + 'index.php' => <<<'PHP' + [ self::FIXTURES_DIR.'/openssl.phar', [ - '.phar/stub.php' => $sha512Stub, - 'index.php' => <<<'PHP' - (new PharMeta( + '.phar/meta.json' => (new PharMeta( CompressionAlgorithm::NONE, [ 'hash' => '54AF1D4E5459D3A77B692E46FDB9C965D1C7579BD1F2AD2BECF4973677575444FE21E104B7655BA3D088090C28DF63D14876B277C423C8BFBCDB9E3E63F9D61A', @@ -209,6 +204,11 @@ private static function pharProvider(): iterable ], ], ))->toJson(), + '.phar/stub.php' => $sha512Stub, + 'index.php' => <<<'PHP' +