box

We talked in a previous article about PHP Scoper, the PHP tool to help you prepare your code for packaging. Once your code is ready, you can send it to Box ! The Box application simplifies the PHAR building process. Out of the box (no pun intended), the application can do many great things:

  • Fast application bundling
  • PHAR isolation
  • Zero configuration by default
  • Requirements checker
  • Friendly error logging experience
  • Retrieve information about the PHAR extension or a PHAR file and its contents (box info or box diff)
  • Verify the signature of an existing PHAR (box verify)
  • Use Git tags and short commit hashes for versioning
  • Get recommendations and warnings about regarding your configuration (box validate)
  • Docker support (box docker)

Installation and usage

You can install box with composer :

composer global require humbug/box

Creating a PHAR should be as simple as running box compile, with no configuration required!. It will however assume some defaults that you might want to change. Box will by default be looking in order for the files box.json and box.json.dist in the current working directory. A basic configuration could be for example changing the PHAR permissions:

{
    "chmod": "0700"
}

You can then find more advanced configuration settings in the configuration documentation. For more information on which command or options is available, you can run:

box help

Docker support

One of the great features in Box is that you can also create a docker file of your application. To do so, you can either:

  • Directly generate the Dockerfile when generating the PHAR with the --with-docker option of the Box compile command
  • Generate the Dockerfile for a given PHAR with the Box docker command

The command will attempt to generate a Dockerfile for your PHAR, leveraging the requirement checker. Once the file generated, you have free hands on it: you can either use it right away (you just need to run $ docker build . to create the docker image) or you can tweak it however you want.

Additional features

Box is very complete in features, it provides a requirement checker tool to help checking the necessary requirements automatically. Box will retrieve all the (non-dev) requirements including the ones of the dependencies if there is any. When a composer.lock file is present, it will take the information from it. If not found, it will fallback on the composer.json but that it will not be able to retrieve any information regarding the dependencies requirements in this case.

You can also optimize your package since all the files in the PHAR are loaded in-memory when executing a PHAR. As a result, the more content there is to load, the bigger the overhead will be and unlike your regular application, a PHAR will not benefit from the opcache optimisations. The difference should however be minimal unless you have dozens of thousands of files in which case you might either accept it, consider an alternative or contribute to the PHAR extension in order to optimise it.

By default Box try to be smart about which files are required and will attempt to use only the necessary files. You can list the files of your PHAR with the box info --list command. It is however possible you want a finer control in which case you can adapt the included files thanks to the configuration options.

Overall, if you need to build and package your Phar, box should help you find your way quickly. Released under an MIT license. More information at https://github.com/box-project/box

LEAVE A REPLY

Please enter your comment!
Please enter your name here