Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| PharFileFaker | |
100.00% |
44 / 44 |
|
100.00% |
6 / 6 |
12 | |
100.00% |
1 / 1 |
| createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResource | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| createSourceFiles | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| createPhar | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieFileFaker; |
| 5 | use Apie\Faker\SeededFile; |
| 6 | use Faker\Generator; |
| 7 | use Nyholm\Psr7\Stream; |
| 8 | |
| 9 | /** |
| 10 | * Generates a phar archive with some small php classes and a few random data files. |
| 11 | * The phar format is written by hand as the Phar class can not write when phar.readonly is enabled (default). |
| 12 | */ |
| 13 | final class PharFileFaker implements ApieFileFaker |
| 14 | { |
| 15 | private const API_VERSION = 0x1100; |
| 16 | private const FLAG_SIGNATURE = 0x00010000; |
| 17 | private const SIGNATURE_SHA256 = 0x0003; |
| 18 | |
| 19 | private static bool $alreadyCreating = false; |
| 20 | |
| 21 | public function createOriginalFilename(Generator $faker): string |
| 22 | { |
| 23 | return $faker->word() . '.phar'; |
| 24 | } |
| 25 | |
| 26 | public function createMimeType(): string |
| 27 | { |
| 28 | return 'application/x-phar'; |
| 29 | } |
| 30 | |
| 31 | public static function isSupported(): bool |
| 32 | { |
| 33 | return !self::$alreadyCreating; |
| 34 | } |
| 35 | |
| 36 | /** @return resource */ |
| 37 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
| 38 | { |
| 39 | $files = $this->createSourceFiles($faker); |
| 40 | self::$alreadyCreating = true; |
| 41 | try { |
| 42 | for ($i = $faker->numberBetween(0, 3); $i > 0; $i--) { |
| 43 | /** @var SeededFile $file */ |
| 44 | $file = $faker->fakeClass(SeededFile::class); |
| 45 | $files['data/' . $file->getClientFilename()] = (string) $file->getStream(); |
| 46 | } |
| 47 | } finally { |
| 48 | self::$alreadyCreating = false; |
| 49 | } |
| 50 | |
| 51 | return Stream::create($this->createPhar($faker, $originalFilename, $files))->detach(); |
| 52 | } |
| 53 | |
| 54 | /** @return array<string, string> */ |
| 55 | private function createSourceFiles(Generator $faker): array |
| 56 | { |
| 57 | $namespace = 'Faker\\' . ucfirst($faker->word()) . 'App'; |
| 58 | $classes = []; |
| 59 | for ($i = $faker->numberBetween(1, 3); $i > 0; $i--) { |
| 60 | $classes[ucfirst($faker->word()) . 'Service'] = $faker->sentence(); |
| 61 | } |
| 62 | $files = []; |
| 63 | foreach ($classes as $class => $greeting) { |
| 64 | $files['src/' . $class . '.php'] = "<?php\nnamespace " . $namespace . ";\n\nclass " . $class . "\n{\n" |
| 65 | . " public function greet(): string\n {\n return " . var_export($greeting, true) . ";\n }\n}\n"; |
| 66 | } |
| 67 | $index = "<?php\n"; |
| 68 | foreach (array_keys($classes) as $class) { |
| 69 | $index .= "require_once __DIR__ . '/src/" . $class . ".php';\n"; |
| 70 | } |
| 71 | $index .= "\n"; |
| 72 | foreach (array_keys($classes) as $class) { |
| 73 | $index .= 'echo (new \\' . $namespace . '\\' . $class . "())->greet(), PHP_EOL;\n"; |
| 74 | } |
| 75 | $files['index.php'] = $index; |
| 76 | ksort($files); |
| 77 | |
| 78 | return $files; |
| 79 | } |
| 80 | |
| 81 | /** @param array<string, string> $files */ |
| 82 | private function createPhar(Generator $faker, string $alias, array $files): string |
| 83 | { |
| 84 | $stub = "<?php\nPhar::mapPhar(" . var_export($alias, true) . ");\n" |
| 85 | . 'require ' . var_export('phar://' . $alias . '/index.php', true) . ";\n" |
| 86 | . "__HALT_COMPILER(); ?>\r\n"; |
| 87 | $manifest = pack('V', count($files)) . pack('n', self::API_VERSION) . pack('V', self::FLAG_SIGNATURE) |
| 88 | . pack('V', strlen($alias)) . $alias . pack('V', 0); |
| 89 | $contents = ''; |
| 90 | foreach ($files as $name => $data) { |
| 91 | $manifest .= pack('V', strlen($name)) . $name |
| 92 | . pack('V', strlen($data)) |
| 93 | . pack('V', $faker->dateTimeBetween('-2 years')->getTimestamp()) |
| 94 | . pack('V', strlen($data)) |
| 95 | . pack('V', crc32($data)) |
| 96 | . pack('V', 0644) |
| 97 | . pack('V', 0); |
| 98 | $contents .= $data; |
| 99 | } |
| 100 | $phar = $stub . pack('V', strlen($manifest)) . $manifest . $contents; |
| 101 | |
| 102 | return $phar . hash('sha256', $phar, true) . pack('V', self::SIGNATURE_SHA256) . 'GBMB'; |
| 103 | } |
| 104 | } |