Creating PHAR files with Box

PHAR files are the “executable”‘s of the PHP world. It allows you to create a package of your application for easy delivery. The most popular projects providing PHAR files are PHPUnit and Composer, but there are many more, and if you have a project that contains command line tools, you should too.

The process boils down to a few basic steps:

  • create a PHAR compiler
  • collect all files that need to be packaged
  • produce PHAR file and sign it

The Box project provides an easy way of creating a PHAR file for your project. Installation is simple:

curl -LSs | php

I like putting all phar and other custom commands that I use regularly in my ~/bin directory. If you do so, make sure you add the directory to your PATH environment variable:

# edit .bash_profile or .profile
export PATH=~/bin:$PATH

Then, create a box.json file which will be used to configure box and create your PHAR file. The content could be something like this:

    "directories": ["src"],
    "finder": [
            "name": "*.php",
            "exclude": ["Tests", "tests"],
            "in": "vendor"
    "main": "bin/command",
    "output": "your.phar",
    "stub": true

This configuration will create a file named your.phar, and will run bin/command when it is executed. It will include in it all files inside the directories src and vendor (except Tests).

Once you are done with this, run the build command:

box build -v

Compiling a PHAR with box

Posted in PHP Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *