26

I'm experimenting in building CLI tools using the Symfony2 console library. I've got something basic working and now I want to package it as a phar. I've read a few examples but the ones I've seen are very simple (3 files, no namespaces, etc).

In my src/ directory I have the following:

enter image description here

Above src/ I have a console.php that I execute to run the app. I also have a vendors/ dir as I'm using composer to install dependencies. console.php is very simple:

#!/usr/bin/env php
<?php

set_time_limit(0);
$loader = require 'vendor/autoload.php';

use Symfony\Component\Console\Application;
use Bendihossan\Pinfo\Command\EnvironmentCommand;
use Bendihossan\Pinfo\Command\ExtensionsCommand;
use Bendihossan\Pinfo\Command\RunAllCommand;

$console = new Application();

$console->add(new RunAllCommand());
$console->add(new EnvironmentCommand);
$console->add(new ExtensionsCommand);

$console->run();

From what (little) I understand about build a phar I think I need to include console.php as the stub and everything else in src/ plus all my dependencies in vendors/.

Looking at the example code on phpmaster.com they specify every file manually to be included in the phar using file_get_contents, but I need to maintain my directory structure in order to use the composer's autoloader and keep to PSR-0 directory structure.

Is there an easy way to create a .phar and maintain my directory structure within it so I can still use composer's autoloader?

1 Answer 1

25

I suggest you to take a look at Composer's Compiler (it's originally created by Fabien Potencier in Silex). In that class, you can see how a big console application like Composer creates the .phar file.


Some interesting parts:

// line 49
$phar = new \Phar($pharFile, 0, 'composer.phar');
$phar->setSignatureAlgorithm(\Phar::SHA1);

$phar->startBuffering();

Phar#startBuffering starts the creation of the phar file.

// Line 54
$finder = new Finder();
$finder->files()
    ->ignoreVCS(true)
    ->name('*.php')
    ->notName('Compiler.php')
    ->notName('ClassLoader.php')
    ->in(__DIR__.'/..')

Here, Composer uses the Symfony2 Finder Component to find every file in the src directory (except this file and the autoloader).

// Line 63
foreach ($finder as $file) {
    $this->addFile($phar, $file);
}

Here, Composer iterates over every found file and adds it to the Phar archive. (you can see the Compiler#addFile method on line 116).

This is repeated some times. And then at line 93, the Composer autoloader is used:

$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_namespaces.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_classmap.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_real.php'));

Because Phar is a stream, the directory structure is kept in the phar file and the Composer autoloader is still able to load the classes.

Then, at the end, the stubs are added and the buffering stopped:

$phar->setStub($this->getStub());

$phar->stopBuffering();

(see the Compiler#getStub method at line 173). The Phar#stopBuffering method stops the creation of the phar and saves it to the phar file.

To make this story complete, Composer creates a very simple CLI compile file which runs this command.

3
  • 3
    Thanks! Looks exactly like what I need :) Apr 1, 2013 at 21:27
  • 10
    @Bendihossan I would also advise you to look at Box - a little utility dedicated to building phars. It supports much of the same stuff we have in the Composer compiler and then some.
    – Seldaek
    Apr 2, 2013 at 7:19
  • The URL for the box project is now github.com/box-project/box May 7, 2023 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.