jakoch/phantomjs-installer
Laravel-friendly installer for PhantomJS. Downloads and installs the PhantomJS binary via Composer with cross-platform support, making it easy to bundle a headless browser in PHP apps and CI pipelines without manual setup.
Install via Composer:
composer require jakoch/phantomjs-installer
Add to composer.json under require-dev if PhantomJS is only needed for testing:
"require-dev": {
"jakoch/phantomjs-installer": "^2.0"
}
Autoload the Binary:
Add this to your composer.json under scripts:
"scripts": {
"post-install-cmd": [
"Jakoch\\PhantomJsInstaller\\Installer::checkAndInstall"
],
"post-update-cmd": [
"Jakoch\\PhantomJsInstaller\\Installer::checkAndInstall"
]
}
Run:
composer install
PhantomJS will now be available in your project’s /bin directory.
First Use Case: Use PhantomJS in a Laravel command or test:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process(['phantomjs', '--version']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
Testing with Laravel Dusk: PhantomJS is a dependency for Laravel Dusk. Install it via this package to ensure consistency across environments:
// In a DuskTestCase or custom test setup
public function setUp(): void
{
parent::setUp();
$this->artisan('dusk:chrome-driver');
}
Headless Screenshots: Generate screenshots in Laravel commands or jobs:
public function renderScreenshot($url, $outputPath)
{
$process = new Process([
'phantomjs',
'render.js',
$url,
$outputPath
]);
$process->run();
}
CI/CD Pipelines:
Ensure PhantomJS is installed in GitHub Actions, GitLab CI, or CircleCI by running composer install before tests.
Custom Binary Path:
Override the default /bin/phantomjs path in your composer.json:
"extra": {
"phantomjs-installer": {
"binary": "custom/path/phantomjs",
"binary-name": "my-phantom"
}
}
Version Pinning:
Specify a version in composer.json to avoid unexpected updates:
"jakoch/phantomjs-installer": "2.1.0"
Permission Issues:
/bin is writable:
chmod -R 777 /path/to/project/bin
bin directory exists and is accessible.Missing Dependencies:
g++ on Linux. Install via:
sudo apt-get install g++
brew install phantomjs
But disable auto-install in this package to avoid conflicts.CI/CD Failures:
RUN apt-get update && apt-get install -y g++ make
Verify Installation: Check if PhantomJS is installed:
./bin/phantomjs --version
If missing, run:
composer dump-autoload && composer install
Logs:
Enable verbose output in composer.json:
"scripts": {
"post-install-cmd": [
"Jakoch\\PhantomJsInstaller\\Installer::checkAndInstall --verbose"
]
}
Custom Installer Logic:
Extend the installer by subclassing Jakoch\PhantomJsInstaller\Installer:
use Jakoch\PhantomJsInstaller\Installer as BaseInstaller;
class CustomInstaller extends BaseInstaller
{
protected function getBinaryUrl()
{
return 'https://custom-cdn.com/phantomjs';
}
}
Post-Install Hooks:
Run additional commands after installation via post-install-cmd:
"scripts": {
"post-install-cmd": [
"Jakoch\\PhantomJsInstaller\\Installer::checkAndInstall",
"@php artisan cache:clear"
]
}
Windows-Specific Quirks:
$process = new Process([
'C:\\path\\to\\project\\bin\\phantomjs',
'script.js'
]);
str_replace('\\', '\\\\', $path).How can I help you explore Laravel packages today?