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 http://box-project.org/installer.php | 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: ,

Copy SSH keys for easy and quick access to servers

SSH keys are great. It allows you to login to servers and SSH-based serveices without using passwords.

Before using SSH keys, you need to create an key:

ssh-keygen -t dsa

Answer a few questions, and then you will have a new pair of keys. Now, copy the key to the remote server:

ssh-copy-id user@remote-host

If you are on OSX, you can install the utility with homebrew:

brew install ssh-copy-id

Or download it using the following command from this github repo:

curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh

If you are on Windows, you can use the following command (credit to Andres Gonzalez):

cat ~/.ssh/id_rsa.pub | ssh user@remote-host “mkdir ~/.ssh; cat <<; ~/.ssh/authorized_keys”

Posted in devops Tagged with: