A Composer package which installs the PhantomJS binary (Linux, Windows, Mac) into /bin
of your project.
To install PhantomJS as a local, per-project dependency to your project, simply add a dependency on jakoch/phantomjs-installer
to your project's composer.json
file.
{
"require": {
"jakoch/phantomjs-installer": "2.1.1"
},
"config": {
"bin-dir": "bin"
},
"scripts": {
"post-install-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
],
"post-update-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
]
}
}
How to require specific versions of PhantomJS?
- The version number of the package specifies the PhantomJS version. When you specify
2.1.1
.
- Composer fetches the 2.1.1 tag(!) of the installer. The installer fetches the 2.1.1 version of PhantomJS.
- If you specify
dev-master
, the latest version will be fetched.
- Composer fetches the latest version of the installer. The installer fetches the latest version of PhantomJS.
- You might also specify the PhantomJS version by using a version alias, e.g.
dev-master as <version>
.
- Composer fetches the latest version of the installer. The installer fetches
<version>
of PhantomJS!
- If you specify an explicit commit reference with a version alias, e.g.
dev-master#<commit-ref> as <version>
.
- Composer fetches a specific git commit of the installer. The installer fetches
<version>
of PhantomJS!
For a development dependency, change require
to require-dev
.
The download source used is: https://bitbucket.org/ariya/phantomjs/downloads/
By setting the Composer configuration directive bin-dir
, the vendor binaries will be installed into the defined folder.
Important! Composer will install the binaries into vendor\bin
by default.
The scripts
section is necessary, because currently Composer does not pass events to the handler scripts of dependencies. If you leave it away, you might execute the installer manually.
Now, assuming that the scripts section is set up as required, the PhantomJS binary
will be installed into the /bin
folder and updated alongside the project's Composer dependencies.
-
Fetching the PhantomJS Installer In your composer.json you require the package "phantomjs-installer". The package is fetched by composer and stored into
./vendor/jakoch/phantomjs-installer
. It contains only one file thePhantomInstaller\\Installer
. -
Platform-specific download of PhantomJS The
PhantomInstaller\\Installer
is run as a "post-install-cmd". That's why you need the "scripts" section in your "composer.json". The installer creates a new composer in-memory package "phantomjs", detects your OS and downloads the correct Phantom version to the folder./vendor/jakoch/phantomjs
. All PhantomJS files reside there, especially theexamples
. -
Installation into
/bin
folder The binary is then copied from./vendor/jakoch/phantomjs
to your composer configuredbin-dir
folder. -
Generation of PhantomBinary
The installer generates a PHP file PhantomInstaller\\PhantomBinary
and inserts the path to the binary.
To access the binary and its folder easily, the class PhantomBinary
is created automatically during installation.
The class defines the constants BIN
and DIR
:
BIN
is the full-path to the PhantomJS binary file, e.g./your_project/bin/phantomjs
DIR
is the folder of the binary, e.g./your_project/bin
Both constants are also accessible via their getter-methods getBin()
and getDir()
.
Usage:
use PhantomInstaller\PhantomBinary;
// get values with class constants
$bin = PhantomInstaller\PhantomBinary::BIN;
$dir = PhantomInstaller\PhantomBinary::DIR;
// get values with static functions
$bin = PhantomInstaller\PhantomBinary::getBin();
$dir = PhantomInstaller\PhantomBinary::getDir();
This feature is similar to location.js
of the phantomjs module for Node.
The environment variables PHANTOMJS_PLATFORM
and PHANTOMJS_BITSIZE
enable you to override the platform requirements at the time of packaging. This decouples the packaging system from the target system. It allows to package on Linux for MacOSX or on Windows for Linux.
Possible values for
PHANTOMJS_PLATFORM
are:macosx
,windows
,linux
.PHANTOMJS_BITSIZE
are:32
or64
.
In case downloading an archive fails with HttpStatusCode 404 (resource not found),
the downloader will automatically lower the version to the next available version
and retry. The number of retries is determined by the number of hardcoded PhantomJS
versions in getPhantomJSVersions()
. This feature was added, because of the problems
with v2.0.0 not being available for all platforms (see issue #25).