|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Playwright PHP community project. |
| 7 | + * For the full copyright and license information, please view |
| 8 | + * the LICENSE file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +$jsonPath = __DIR__.'/../data/deviceDescriptorsSource.json'; |
| 12 | +$outputPath = __DIR__.'/../data/devices.php'; |
| 13 | + |
| 14 | +$devices = json_decode(file_get_contents($jsonPath), true); |
| 15 | + |
| 16 | +// Enforce isMobile and hasTouch defaults |
| 17 | +foreach ($devices as $name => &$properties) { |
| 18 | + if (str_starts_with($name, 'Desktop')) { |
| 19 | + $properties['isMobile'] = false; |
| 20 | + $properties['hasTouch'] = false; |
| 21 | + } else { |
| 22 | + unset($properties['isMobile']); |
| 23 | + unset($properties['hasTouch']); |
| 24 | + } |
| 25 | +} |
| 26 | +unset($properties); // Unset reference |
| 27 | + |
| 28 | +// Consolidate landscape and portrait devices |
| 29 | +$consolidatedDevices = []; |
| 30 | +foreach ($devices as $name => $properties) { |
| 31 | + if (str_ends_with($name, ' landscape')) { |
| 32 | + $baseName = str_replace(' landscape', '', $name); |
| 33 | + if (isset($consolidatedDevices[$baseName])) { |
| 34 | + $consolidatedDevices[$baseName]['viewportLandscape'] = $properties['viewport']; |
| 35 | + } |
| 36 | + } else { |
| 37 | + $consolidatedDevices[$name] = $properties; |
| 38 | + $consolidatedDevices[$name]['viewportLandscape'] = null; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Remap to short keys and string dimensions for smaller file size |
| 43 | +$minifiedDevices = []; |
| 44 | +foreach ($consolidatedDevices as $name => $properties) { |
| 45 | + $minified = [ |
| 46 | + 'ua' => $properties['userAgent'], |
| 47 | + 'dbt' => $properties['defaultBrowserType'], |
| 48 | + 'sf' => $properties['deviceScaleFactor'], |
| 49 | + 'vp' => $properties['viewport']['width'].'x'.$properties['viewport']['height'], |
| 50 | + ]; |
| 51 | + |
| 52 | + if (isset($properties['screen'])) { |
| 53 | + $minified['sc'] = $properties['screen']['width'].'x'.$properties['screen']['height']; |
| 54 | + } |
| 55 | + if (isset($properties['viewportLandscape'])) { |
| 56 | + $minified['vp_l'] = $properties['viewportLandscape']['width'].'x'.$properties['viewportLandscape']['height']; |
| 57 | + } |
| 58 | + if (isset($properties['isMobile'])) { |
| 59 | + $minified['m'] = $properties['isMobile']; |
| 60 | + } |
| 61 | + if (isset($properties['hasTouch'])) { |
| 62 | + $minified['t'] = $properties['hasTouch']; |
| 63 | + } |
| 64 | + |
| 65 | + $minifiedDevices[$name] = $minified; |
| 66 | +} |
| 67 | + |
| 68 | +// Generate the PHP file |
| 69 | +$phpCode = "<?php\n\nreturn ".var_export($minifiedDevices, true).";\n"; |
| 70 | + |
| 71 | +$csFixerPath = __DIR__.'/../vendor/bin/php-cs-fixer'; |
| 72 | + |
| 73 | +// Run php-cs-fixer (single pass, as string formatting is handled) |
| 74 | +if (file_exists($csFixerPath)) { |
| 75 | + file_put_contents($outputPath, $phpCode); // Write initial content |
| 76 | + $command = escapeshellarg($csFixerPath).' fix '.escapeshellarg($outputPath); |
| 77 | + echo "Running php-cs-fixer...\n"; |
| 78 | + $output = shell_exec($command); |
| 79 | + echo "php-cs-fixer output:\n".$output; |
| 80 | +} else { |
| 81 | + file_put_contents($outputPath, $phpCode); // Just write if fixer not found |
| 82 | +} |
| 83 | + |
| 84 | +echo "Successfully generated consolidated data/devices.php.\n"; |
| 85 | + |
| 86 | +echo "Script finished.\n"; |
0 commit comments