Skip to content

Commit ee4ba03

Browse files
committed
Initial commit
0 parents  commit ee4ba03

14 files changed

Lines changed: 1276 additions & 0 deletions

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.git* export-ignore
2+
/docs/ export-ignore
3+
/tests/ export-ignore
4+
/.php-cs-fixer* export-ignore
5+
/phpstan* export-ignore
6+
/phpunit* export-ignore

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.phpunit.cache/
2+
/vendor/
3+
/.php-cs-fixer.cache
4+
/composer.lock
5+
/phpstan.neon
6+
/phpunit.xml

.php-cs-fixer.dist.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$license = <<<LICENSE
4+
This file is part of the Playwright PHP community project.
5+
For the full copyright and license information, please view
6+
the LICENSE file that was distributed with this source code.
7+
LICENSE;
8+
9+
$finder = (new PhpCsFixer\Finder())
10+
->in(__DIR__);
11+
12+
return (new PhpCsFixer\Config())
13+
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
14+
->setFinder($finder)
15+
->setRiskyAllowed(true)
16+
->setRules([
17+
'@Symfony' => true,
18+
'declare_strict_types' => true,
19+
'header_comment' => ['header' => $license],
20+
])
21+
;

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
MIT License
2+
3+
Copyright (c) 2025-present Simon André & Playwright PHP
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
---
24+
25+
Third-Party Notice
26+
27+
This project depends on Playwright, an open-source project by Microsoft,
28+
licensed under Apache License 2.0 (https://github.com/microsoft/playwright/blob/main/LICENSE).
29+
"Playwright" is a trademark of Microsoft. This project is independent and not
30+
affiliated with or endorsed by Microsoft.

bin/converter.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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";

composer.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "playwright-php/devices",
3+
"description": "Playwright device descriptors in PHP (mobile & desktop screens, user agents, viewports, scale factors, touch/mobile flags).",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": [
7+
"playwright",
8+
"devices",
9+
"browsers",
10+
"user-agent",
11+
"descriptors",
12+
"mobile",
13+
"tablet",
14+
"desktop",
15+
"emulation",
16+
"viewport"
17+
],
18+
"authors": [
19+
{
20+
"name": "Simon André",
21+
"email": "smn.andre@gmail.com",
22+
"homepage": "https://github.com/smnandre"
23+
},
24+
{
25+
"name": "Playwright PHP Contributors",
26+
"homepage": "https://github.com/playwright-php/playwright"
27+
}
28+
],
29+
"require": {
30+
"php": "^8.3"
31+
},
32+
"require-dev": {
33+
"friendsofphp/php-cs-fixer": "^3.40",
34+
"phpstan/phpstan": "^2.1",
35+
"phpunit/phpunit": "^12.3"
36+
},
37+
"minimum-stability": "stable",
38+
"prefer-stable": true,
39+
"autoload": {
40+
"psr-4": {
41+
"Playwright\\Device\\": "src/"
42+
}
43+
},
44+
"autoload-dev": {
45+
"psr-4": {
46+
"Playwright\\Device\\Tests\\": "tests/"
47+
}
48+
},
49+
"config": {
50+
"allow-plugins": {
51+
"phpstan/extension-installer": true
52+
},
53+
"sort-packages": true
54+
},
55+
"extra": {
56+
"thanks": {
57+
"name": "playwright-php/playwright",
58+
"url": "https://github.com/playwright-php/playwright"
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)