Skip to content

yannoff/phpcc

Repository files navigation

yannoff/phpcc

PHP Code compiler - Phar executable compiling utility

Help Contents

Usage

Synopsis

phpcc --help
phpcc --version
phpcc \
    -e <main> \
    -o <output> \
    [-d <dir> [-d <dir> ...]] \
    [-f <file> [-f <file> ...]] \
    [-b <banner>] \
    [-m <metadata> [-m <metadata> ...]]
    [--debug]

Options/Arguments

The output and entrypoint scripts are mandatory.

Name / Shorthand Type Description Required
--output, -o value The Phar archive output file y
--main, -e value The main application entrypoint script y
--banner, -b value Specify the filepath to the legal notice banner
Will be included in the human-readable part of the stub.
n
--file, -f multi Adds a single file to the archive n
--dir, -d multi Adds a sources directory to the archive
Possible dir spec formats:
- $dir => include all files in directory
- $dir:$extension => filter files on a specific extension
n
--meta, -m multi Adds a metadata to the archive
Metadata must be specified in the $key:$value format
n
--no-minify, -n flag Don't minify PHP source files
Useful for debugging the compiled executable in case of runtime errors
n
--shebang-less flag Produce a stub deprived of the shebang directive
Useful when the phar is meant to be included instead of being executed directly
n
--debug flag Turn on debug mode
Set php error reporting level to E_ALL at compilation time
n
--quiet, -q flag Reduce output messages amount: set verbosity level to INFO instead of default DEBUG n

Examples

A concrete use-case: the phpcc self-compiling command

phpcc -d src:php -d vendor:php -e bin/compile.php -o bin/phpcc -b .banner

Example 1: PHP sources located in several directories

  • Add all *.php files from src/ and vendor/ dirs
  • Define main.php as the stub main entrypoint script
  • Save compiled phar executable to bin/foobar
phpcc -d src:php -d vendor:php -e main.php -o bin/foobar

Example 2: Multiple extensions in the same directory

  • Add all *.php and *.phtml files from src/ dir
  • Define main.php as the stub main entrypoint script
  • Save compiled phar executable to bin/foobar
phpcc -d src:php -d src:phtml -e main.php -o bin/foobar

Example 3: Standalone php script

  • Define app.php as the stub main entrypoint script
  • Save compiled phar executable to foobar.phar
  • Use LICENSE file contents as legal notice banner
phpcc -e app.php -o foobar.phar -b LICENSE

Example 4: Add sparse single PHP files

  • Define app.php as the stub main entrypoint script
  • Save compiled phar executable to foobar.phar
  • Add foo.php and bar.php files to the archive
phpcc -e app.php -o foobar.phar -f foo.php -f bar.php

Example 5: Adding metadata to the archive

  • Define app.php as the stub main entrypoint script
  • Save compiled phar executable to bin/acme
  • Add the license & author metadata to the archive
phpcc -e app.php -o bin/acme -m license:MIT -m author:yannoff

Install

Requirements

  • php or paw 7.1+
  • phar.readonly php config directive must be set to Off

Quick install

Get the latest release from Github

💡 ${BINDIR} may be /usr/bin, /usr/local/bin or $HOME/bin

curl -Lo ${BINDIR}/phpcc https://github.com/yannoff/phpcc/releases/latest/download/phpcc

Add execution permissions to the binary

chmod +x ${BINDIR}/phpcc

Github Action

A github action is available for integration in CI scripts.

The action will install PHP (in the desired version), and the phpcc binary.

Synopsis: use yannoff/phpcc/actions/install@<release>.

Integration example

Installing phpcc version 1.2.4 / PHP 8.0

# ...
jobs:
    compile:
        name: Compile source files
        runs-on: ubuntu-latest
        steps:
            - name: Checkout repository
              uses: actions/checkout@v4

            - name: Install PHP & PHPCodeCompiler
              uses: yannoff/phpcc/actions/install@1.2.4
              with:
                  php-version: 8.0

            - name: Install dependencies
              run: composer install --no-dev --optimize-autoloader

            - name: Create virtual version
              run: echo $(date +"%Y-%m-%d %H:%M:%S") > ./version

            - name: Compile sources
              run: php -d phar.readonly=0 /usr/local/bin/phpcc -e bin/acme.php -d src -d vendor -f version -o bin/acme --quiet

            - name: Smoke test (show version)
              run: bin/acme --version

License

Licensed under the MIT License.

About

PHP Code compiler - Phar executable compiling utility

Topics

Resources

License

Stars

Watchers

Forks