Skip to content

Repacking your application in a new phar

You have created a Castor application, with many tasks, and you want to distribute it as a single phar file? Castor can help you with that.

Pre-requisites

In your project, install Castor as a dependency:

composer require jolicode/castor

You'll also need box to create the phar. The box binary must be available in your path.

You'll also need to ensure the phar creation is allowed by your PHP configuration. See the PHP documentation to disabled phar.readonly.

Running the Repack Command

Then, run the repack command to create the new phar:

vendor/bin/castor repack

Note

Castor will automatically import all files in the current directly. So ensure to have the less files possible in the directory where you run the repack task to avoid including useless files in the phar.

Note

If a box.json file exists in your application directory, it will be merged with the config file used by Castor. None of theses keys base-path, main, alias or output keys can be defined in your application box config.

Caution

If some classes are missing in your phar, it might be because they are excluded by castor's box.json file. In this case, you should override the default configuration with a local box.json file

Usage

castor:repack [--app-name APP-NAME] [--app-version APP-VERSION] [--os OS] [--no-logo] [--logo-file [LOGO-FILE]] [--output-directory OUTPUT-DIRECTORY]

Options

--app-name

The name of the phar application

  • Accept value: yes
  • Is value required: yes
  • Is multiple: no
  • Is negatable: no
  • Default: 'my-app'

--app-version

The version of the phar application

  • Accept value: yes
  • Is value required: yes
  • Is multiple: no
  • Is negatable: no
  • Default: '1.0.0'

--os

The targeted OS

  • Accept value: yes
  • Is value required: yes
  • Is multiple: no
  • Is negatable: no
  • Default: 'linux'

Hide Castor logo

  • Accept value: no
  • Is value required: no
  • Is multiple: no
  • Is negatable: no
  • Default: false

--logo-file

Path to a PHP file that returns a logo as a string, or a closure that returns a logo as a string

  • Accept value: yes
  • Is value required: no
  • Is multiple: no
  • Is negatable: no
  • Default: NULL

--output-directory

Path to the directory where the phar will be generated

  • Accept value: yes
  • Is value required: yes
  • Is multiple: no
  • Is negatable: no
  • Default: ''

When you repack, you can use the --no-logo option to hide the Castor logo.

Alternatively, you can replace the Castor logo with your own.
Use the --logo-file option and provide the absolute path (or a path relative to the working directory) of a .php file.

This file must return a string or a closure that returns a string.
The closure will receive the application name and version as string parameters.

Example

// repack/my-logo.php

<?php

return '❤️ My LOGO ❤️';
vendor/bin/castor repack --logo-file repack/my-logo.php

or

// repack/my-complex-logo.php

<?php

return function (string $appName, string $appVersion) {
    return <<<LOGO

    ❤️ My LOGO for {$appName} in Version {$appVersion} ❤️

    LOGO;
};
vendor/bin/castor repack --logo-file repack/my-complex-logo.php

img.png

Going further

Packaging your Castor app as a phar simplifies distribution but requires PHP setup on target systems.

Castor's compile command streamlines this by embedding the phar in a PHP binary, creating a static executable for diverse environments.