Skip to content

Commit

Permalink
Combine puphpeteer and rialto composer packages into one repository
Browse files Browse the repository at this point in the history
 - Update requirements to PHP 8.3, Node 18.0, puppeteer 22
 - Use relative paths for .mjs imports and no longer require node packages
 - Remove Frame->ExecutionContext() [Puppeteer v17]
 - Rename Page->createIncognitoBrowserContext() to Page->createBrowserContext() [Puppeteer v22]
 - Remove CDP (case-insensitve) from puppeteer classes in responses (CdpBrowser, etc) [Solves zoonru#8 and zoonru#11]
 - Add PHP strict_types declarations
 - Update phpunit to 11.3 fix tests to run
  • Loading branch information
mreiden committed Aug 21, 2024
1 parent d0cf573 commit d9e76eb
Show file tree
Hide file tree
Showing 91 changed files with 4,117 additions and 850 deletions.
23 changes: 11 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.3",
"ext-json": "*",
"ext-sockets": "*",
"ext-pcre": "*",
"clue/socket-raw": "^1.6",
"composer/semver": "^3.0",
"zoon/rialto": "dev-dmt",
"psr/log": "^3.0",
"thecodingmachine/safe": "^2.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.2",
"monolog/monolog": "^3.0",
"monolog/monolog": "^2.4 || ^3.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0",
"symfony/console": "^6.3",
"symfony/filesystem": "^6.3",
"symfony/process": "^6.3",
"symfony/var-dumper": "^6.3",
"phpunit/phpunit": "^10 || ^11",
"symfony/console": "^5.4 || ^6.4 || ^7.0",
"symfony/filesystem": "^5.4 || ^6.4 || ^7.0",
"symfony/process": "^5.4 || ^6.4 || ^7.0",
"symfony/var-dumper": "^5.4 || ^6.4 || ^7.0",
"thecodingmachine/phpstan-safe-rule": "^1.2"
},
"autoload": {
Expand All @@ -54,11 +55,9 @@
}
},
"scripts": {
"post-install-cmd": "npm install",
"test": "./vendor/bin/phpunit",
"update-docs": "php bin/console doc:generate",
"stan": "vendor/bin/phpstan analyze packages/*/src",
"format": "vendor/bin/php-cs-fixer fix"
"stan": "vendor/bin/phpstan analyze packages/*/src"
},
"config": {
"sort-packages": true
Expand Down
24 changes: 14 additions & 10 deletions examples/01_page_open.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;

$puppeteer = new Puppeteer;
$puppeteer = new Puppeteer();

$browser = $puppeteer->launch();
$page = $browser->newPage();
$page->goto('https://example.com');
$page->goto('https://example.com/');

// Get the "viewport" of the page, as reported by the page.
$dimensions = $page->evaluate(JsFunction::createWithBody(/** @lang JavaScript */"
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
"));
$javascript = <<<'JSFUNC'
/** @lang JavaScript */"
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
JSFUNC;
$dimensions = $page->evaluate(JsFunction::createWithBody($javascript));

printf('Dimensions: %s', print_r($dimensions, true));

$browser->close();
$browser->close();
8 changes: 5 additions & 3 deletions examples/02_page_screenshot.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use Nesk\Puphpeteer\Puppeteer;

$puppeteer = new Puppeteer;
$puppeteer = new Puppeteer();
$browser = $puppeteer->launch();

$page = $browser->newPage();
$page->goto('https://example.com');
$page->goto('https://example.com/');
$page->screenshot(['path' => 'example.png']);

$browser->close();
$browser->close();
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
"url": "https://johann.pardanaud.com/"
},
"license": "MIT",
"repository": "git+https://github.com/zoonru/puphpeteer.git",
"repository": {
"type": "git",
"url": "https://github.com/zoonru/puphpeteer.git"
},
"type": "module",
"main": "src/Rialto/node-process/index.mjs",
"engines": {
"node": ">=18.0.0"
},
"type": "module",
"dependencies": {
"@zoon/rialto": "git+https://github.com/mreiden/rialto.git#semver:^1.5.0",
"puppeteer": "^20"
"puppeteer-core": "^22"
},
"devDependencies": {
"@types/yargs": "^15.0.10",
"typescript": "^4.1.2",
"typescript": "^5.0",
"yargs": "^16.1.1"
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuite name="default">
Expand Down
42 changes: 23 additions & 19 deletions src/Command/GenerateDocumentationCommand.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<?php

declare(strict_types=1);

namespace Nesk\Puphpeteer\Command;

use Nesk\Puphpeteer\Puppeteer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\{
Command\Command,
Input\InputInterface,
Input\InputOption,
Output\OutputInterface,
Style\SymfonyStyle,
};
use Symfony\Component\Process\Process;

final class GenerateDocumentationCommand extends Command
{
private const DOC_FILE_NAME = 'doc-generator';

private const BUILD_DIR = __DIR__.'/../../.build';
private const BUILD_DIR = __DIR__ . '/../../.build';

private const NODE_MODULES_DIR = __DIR__.'/../../node_modules';
private const NODE_MODULES_DIR = __DIR__ . '/../../node_modules';

private const RESOURCES_DIR = __DIR__.'/../Resources';
private const RESOURCES_DIR = __DIR__ . '/../Resources';

private const RESOURCES_NAMESPACE = 'Nesk\\Puphpeteer\\Resources';

Expand All @@ -37,7 +41,7 @@ protected function configure(): void
null,
InputOption::VALUE_OPTIONAL,
'The path where Puppeteer is installed.',
self::NODE_MODULES_DIR.'/puppeteer'
self::NODE_MODULES_DIR . '/puppeteer',
);
}

Expand All @@ -48,10 +52,10 @@ private static function buildDocumentationGenerator(): void
{
self::rmdirRecursive(self::BUILD_DIR);
$process = new Process([
self::NODE_MODULES_DIR.'/.bin/tsc',
self::NODE_MODULES_DIR . '/.bin/tsc',
'--outDir',
self::BUILD_DIR,
__DIR__.'/../../src/'.self::DOC_FILE_NAME.'.ts',
__DIR__ . '/../../src/' . self::DOC_FILE_NAME . '.ts',
]);
$process->run();
}
Expand All @@ -70,12 +74,12 @@ private static function getDocumentation(string $puppeteerPath, array $resourceN
foreach (self::DOC_FORMATS as $format) {
$process = new Process(
array_merge(
['node', self::BUILD_DIR.'/'.self::DOC_FILE_NAME.'.js', $format],
['node', self::BUILD_DIR . '/' . self::DOC_FILE_NAME . '.js', $format],
$commonFiles,
$nodeFiles,
['--resources-namespace', self::RESOURCES_NAMESPACE, '--resources'],
$resourceNames
)
$resourceNames,
),
);
$process->mustRun();

Expand All @@ -96,7 +100,7 @@ private static function getResourceNames(): array
{
return array_map(static function (string $filePath): string {
return explode('.', basename($filePath))[0];
}, glob(self::RESOURCES_DIR.'/*'));
}, glob(self::RESOURCES_DIR . '/*'));
}

private static function generatePhpDocWithDocumentation(array $classDocumentation): ?string
Expand Down Expand Up @@ -135,7 +139,7 @@ private static function writePhpDoc(string $className, string $phpDoc): void
{
$reflectionClass = new \ReflectionClass($className);

if (! $reflectionClass) {
if (!$reflectionClass) {
return;
}

Expand Down Expand Up @@ -176,7 +180,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (null !== $classDocumentation) {
$phpDoc = self::generatePhpDocWithDocumentation($classDocumentation);
if (null !== $phpDoc) {
$resourceClass = self::RESOURCES_NAMESPACE.'\\'.$resourceName;
$resourceClass = self::RESOURCES_NAMESPACE . '\\' . $resourceName;
self::writePhpDoc($resourceClass, $phpDoc);
}
}
Expand Down Expand Up @@ -208,12 +212,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private static function rmdirRecursive(string $dir): bool
{
$files = scandir($dir);
if (! \is_array($files)) {
if (!\is_array($files)) {
return false;
}
$files = array_diff($files, ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? self::rmdirRecursive("$dir/$file") : unlink("$dir/$file");
is_dir("$dir/$file") ? self::rmdirRecursive("$dir/$file") : unlink("$dir/$file");
}

return rmdir($dir);
Expand Down
40 changes: 20 additions & 20 deletions src/Puppeteer.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace Nesk\Puphpeteer;

use Composer\Semver\Semver;
use Nesk\Puphpeteer\Resources\Browser;
use Nesk\Rialto\AbstractEntryPoint;
use Nesk\Puphpeteer\Resources\{Browser, BrowserFetcher};
use Nesk\Puphpeteer\Rialto\AbstractEntryPoint;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;

Expand All @@ -14,9 +16,9 @@
* @property mixed networkConditions
* @property string product
*
* @method \Nesk\Puphpeteer\Resources\Browser connect(array $options)
* @method Browser connect(array $options)
*
* @method-extended \Nesk\Puphpeteer\Resources\Browser connect(array<string, mixed> $options)
* @method-extended Browser connect(array<string, mixed> $options)
*
* @method void registerCustomQueryHandler(string $name, mixed $queryHandler)
*
Expand All @@ -34,9 +36,9 @@
*
* @method-extended void clearCustomQueryHandlers()
*
* @method \Nesk\Puphpeteer\Resources\Browser launch(array $options = [])
* @method Browser launch(array $options = [])
*
* @method-extended \Nesk\Puphpeteer\Resources\Browser launch(array<string, mixed> $options = null)
* @method-extended Browser launch(array<string, mixed> $options = null)
*
* @method string executablePath(string $channel = null)
*
Expand All @@ -46,18 +48,16 @@
*
* @method-extended string[] defaultArgs(array<string, mixed> $options = null)
*
* @method \Nesk\Puphpeteer\Resources\BrowserFetcher createBrowserFetcher(array $options)
* @method BrowserFetcher createBrowserFetcher(array $options)
*
* @method-extended \Nesk\Puphpeteer\Resources\BrowserFetcher createBrowserFetcher(array<string, mixed> $options)
* @method-extended BrowserFetcher createBrowserFetcher(array<string, mixed> $options)
*/
class Puppeteer extends AbstractEntryPoint
{
/**
* Default options.
*
* @var array
*/
protected $options = [
protected array $options = [
'read_timeout' => 30,

// Logs the output of Browser's console methods (console.log, console.debug, etc...) to the PHP logger
Expand All @@ -72,15 +72,15 @@ class Puppeteer extends AbstractEntryPoint
*/
public function __construct(array $userOptions = [])
{
if (! empty($userOptions['logger']) && $userOptions['logger'] instanceof LoggerInterface) {
if (!empty($userOptions['logger']) && $userOptions['logger'] instanceof LoggerInterface) {
$this->checkPuppeteerVersion($userOptions['executable_path'] ?? 'node', $userOptions['logger']);
}

parent::__construct(
__DIR__.'/PuppeteerConnectionDelegate.mjs',
__DIR__ . '/PuppeteerConnectionDelegate.mjs',
new PuppeteerProcessDelegate(),
$this->options,
$userOptions
$userOptions,
);
}

Expand All @@ -95,27 +95,27 @@ private function checkPuppeteerVersion(string $nodePath, LoggerInterface $logger
return;
}

if (! Semver::satisfies($currentVersion, $acceptedVersions)) {
if (!Semver::satisfies($currentVersion, $acceptedVersions)) {
$logger->warning(
"The installed version of Puppeteer (v$currentVersion) doesn't match the requirements"
." ($acceptedVersions), you may encounter issues."
"The installed version of Puppeteer (v$currentVersion) doesn't match the requirements" .
" ($acceptedVersions), you may encounter issues.",
);
}
}

private function currentPuppeteerVersion(string $nodePath): ?string
{
$process = new Process([$nodePath, __DIR__.'/get-puppeteer-version.mjs']);
$process = new Process([$nodePath, __DIR__ . '/get-puppeteer-version.mjs']);
$process->mustRun();

return json_decode($process->getOutput());
}

private function acceptedPuppeteerVersion(): string
{
$npmManifestPath = __DIR__.'/../package.json';
$npmManifestPath = __DIR__ . '/../package.json';
$npmManifest = json_decode(file_get_contents($npmManifestPath));

return $npmManifest->dependencies->puppeteer;
return $npmManifest->dependencies->{"puppeteer-core"};
}
}
Loading

0 comments on commit d9e76eb

Please sign in to comment.