Editing Code In a PHAR file
A PHAR file is a PHp ARchive file. You can create an archive of your PHP library and store it into a PHAR file for easy distribution.
If you want to change some code inside an existing PHAR file then you need to extract it, make your changes and re-pack all the changed files.
Let’s take the Guzzle.phar file for example.
You can find it here:
It’s a great PHP library for HTTP requests. I often use it for data mining and web scraping.
So, first of all we need a phar.exe file which comes with any of the latest PHP builds.
I use XAMPP and there is a phar.phar.bat file located in “C:\xampp\php”
which actually runs:
"php.exe" "pharcommand.phar" [command]
Just check your PHP installation directory for pharcommand.phar.
To see help, just type:
"php.exe" "pharcommand.phar" help
extract Extract a PHAR package to a directory. Required arguments: -f
Specifies the phar file to work on. Optional arguments: -i Specifies a regular expression for input files. -x Regular expression for input files to exclude. ... Directory to extract to (defaults to '.').
So, you need to extract the PHAR using a command like this:
"C:\xampp\php\php.exe" "C:\xampp\php\pharcommand.phar" extract -f "D:\DEV\guzzle.phar" "D:\DEV\Guzzle"
You can use relative paths if you are already in that directory.
But I find it best to give full directory paths to ensure the file gets extracted in the right folder: “D:\DEV\Guzzle”
Then suppose you change a file:
pack Pack files into a PHAR archive. When using -s
, then the stub file is being excluded from the list of input files/dirs.To create an archive that contains PEAR class PHP_Archive then point -p argument to PHP/Archive.php. Required arguments: -f Specifies the phar file to work on. ... Any number of input files and directories. If -i is in use then ONLY files and matching the given regular expression are being packed. If -x is given then files matching that regular expression are NOT being packed.
It also has a whole bunch of optional arguments that you might find useful.
After changes just re-pack the changed file using:
"C:\xampp\php\php.exe" "C:\xampp\php\pharcommand.phar" pack -f "D:\DEV\guzzle.phar" "GuzzleHttp\Cookie\CookieJar.php"
You can even open up a phar file in a text editor like Notepad++ just to see if the changes were done successfully.
Just don’t save the phar file using the text editor because then it will stop working.