# Symfony PharBuilder Bundle

The PharBuilder Bundle for Symfony (opens new window) can help you develop and maintain Phars (opens new window) built with the Symfony Framework. It internally uses Box (opens new window) to handle the actual Phar stuff and is in effect not much more than a fancy Symfony integration for this wonderful tool.

This Bundle supports Symfony ≥ 5.1 and PHP ≥ 7.3

WARNING

Currently only CLI applications are supported. It is however planned to support both CLI and Web-facing applications once this bundle reaches v1.0.

# Getting Started

First install the package with:

$ composer require efrane/phar-builder-bundle

Then activate it in your bundles configuration (typically config/bundles.php):

return [
    // ...
    EFrane\PharBuilder\Bundle\PharBuilderBundle::class => ['all'],
    // ...
];

Required configuration

The PharBuilder has several configuration options, most of which can be skipped for typical setups. To get started, you only need to set a name for the generated phar and an output directory:

# config/packages/phar_builder.yaml
phar_builder:
    build:
        output_path: '%kernel.project_dir%'
        output_filename: 'your-phar-name'

TIP

Add the concatenation of output_path and output_filename to your .gitignore.

Build your Phar

$ php bin/console phar:build 

# Adding Commands

The main idea behind this bundle is to enable developing a Phar that is separate from it's containing Symfony application. To that extent, no commands registered to the Symfony console (bin/console) are registered inside the Phar. Instead, you have to implement the EFrane\PharBuilder\Command\PharCommandInterface on commands that shall be accessible in the Phar.

The EFrane\PharBuilder\Command\PharCommand is a Symfony Command base class implementing that interface to safe you some typing:

<?php

namespace App\Command;

use EFrane\PharBuilder\Command\PharCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FooCommand extends PharCommand {
    public static $defaultName = 'foo';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('Foo');

        return PharCommand::SUCCESS;
    }
} 

Alternatively, if you need access to commands registered by Symfony itself or other Bundles, you need to tag the command classes with the phar.command tag.

TIP

The EFrane\PharBuilder\Command\PharCommandInterface interface will never require the implementation of any methods. It is purely used for command discovery.

# Development Workflow

Phars, unlike normal PHP Applications, need a more traditional development mindset as there always is a build step and a run step. This bundle aims to make the transition from the instant run workflow we're used to as PHP Developers to this other style of working as smooth as possible. To that end the commands phar:build and phar:watch are provided.

# Developing with phar:build

To build the Phar with the current configuration, simply run

$ php bin/console phar:build

To save some time and unnecessary network requests, you can skip downloading the dependencies after the first run by using

$ php bin/console phar:build --no-update-dependencies

Remember that you need to rebuild the Phar after every change in the code if you want to test that change.

# Learn More