Skip to content

Commit 16a2de6

Browse files
committed
Look for "CHROME_PATH" env variable to find binaries [ci skip]
1 parent 611472f commit 16a2de6

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ to crawl websites... and almost everything that you can do with chrome as a huma
6969
$browser->close();
7070
```
7171

72+
### Using different chrome executable
73+
74+
When starting the factory will look for the environment variable ``"CHROME_PATH"`` to find the chrome executable.
75+
If the variable is not found then it will use ``"chrome"`` as the executable.
76+
77+
You can use an arbitrary executable of your choice. For instance ``"chromium-browser"``:
78+
79+
```php
80+
use HeadlessChromium\BrowserFactory;
81+
82+
// replace default 'chrome' with 'chromium-browser'
83+
$browserFactory = new BrowserFactory('chromium-browser');
84+
```
7285

7386
### Debugging
7487

@@ -89,16 +102,6 @@ The following example adds some development-oriented features to help debugging
89102
About ``debugLogger``: this can be any of a resource string, a resource or an object implementing ``LoggerInterface`` from Psr\Log (such as [monolog](https://github.com/Seldaek/monolog) or [apix/log](https://github.com/apix/log)).
90103

91104

92-
### Using different chrome executable
93-
94-
By default we assume that chrome will run with the command ``chrome`` but you can change the executable:
95-
96-
```php
97-
use HeadlessChromium\BrowserFactory;
98-
99-
// replace default 'chrome' with 'chromium-browser'
100-
$browserFactory = new BrowserFactory('chromium-browser');
101-
```
102105

103106

104107
------------------------------------------------------------------------------------------------------------------------

src/BrowserFactory.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,20 @@ class BrowserFactory
1414

1515
protected $chromeBinaries;
1616

17-
public function __construct(string $chromeBinaries = 'chrome')
17+
public function __construct(string $chromeBinaries = null)
1818
{
19+
// auto guess chrome binaries
20+
if (null === $chromeBinaries) {
21+
$envChromePath = getenv('CHROME_PATH');
22+
23+
if ($envChromePath) {
24+
$chromeBinaries = $envChromePath;
25+
} else {
26+
$chromeBinaries = 'chrome';
27+
}
28+
29+
}
30+
1931
$this->chromeBinaries = $chromeBinaries;
2032
}
2133

@@ -73,8 +85,13 @@ public function getChromeVersion()
7385

7486
if ($exitCode != 0) {
7587
$message = 'Cannot get chrome version, make sure you provided the correct chrome binaries';
76-
$message .= ' using (' . $this->chromeBinaries . '). ';
77-
$message .= trim($process->getErrorOutput());
88+
$message .= ' using: "' . $this->chromeBinaries . '". ';
89+
90+
$error = trim($process->getErrorOutput());
91+
92+
if (!empty($error)) {
93+
$message .= 'Additional info: ' . $error;
94+
}
7895
throw new \RuntimeException($message);
7996
}
8097

0 commit comments

Comments
 (0)