30/08
2016
Pack all composer dependencies into a single phar file
Composer + Phar, (*1)
Pack all composer dependencies into a single phar file., (*2)
Install via Packagist., (*3)
example@localhost:~$ composer global require mpyw/comphar:^1.0 Changed current directory to /Users/mpyw/.composer ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing mpyw/comphar (dev-master XXXXXXX) Cloning XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Writing lock file Generating autoload files example@localhost:~$
$PATH
If not yet, you must add ~/.composer/vendor/bin
to $PATH
.
Append the following statement to ~/.bashrc
, ~/.zshrc
and so on., (*4)
export PATH="~/.composer/vendor/bin:$PATH"
example@localhost:~$ comphar -h Usage: ./comphar [options] Options: -h, --help Show help. -v, --verbose Verbose output. -o, --out <value> Output archive name. Default to "vendor.phar". -d, --dir <value> Project root directory. Default to getcwd(). --yes Without confirmation. example@localhost:~$
Prepare composer.json
., (*5)
{ "name": "mpyw/my-new-package", "description": "This is a stupid example", "require": { "mpyw/my-dependency-1": "@dev", "mpyw/my-dependency-2": "1.0.0", }, "autoload": { "psr-4": { "mpyw\\MyNewPackage\\": "src/" } } }
vendor.phar
Let's generate vendor.phar
in that directory., (*6)
example@localhost:~/my-new-package$ composer update Loading composer repositories with package information Updating dependencies (including require-dev) Nothing to install or update Writing lock file Generating autoload files example@localhost:~/my-new-package$ comphar -v Project directory: ~/my-new-package Output filename: ~/my-new-package/vendor.phar Continue? [y/n]: y Added: ... Added: ... Added: ... Added: ... Added: ... example@localhost:~/my-new-package$
Even if you have no dependencies, you have to call composer install
or composer update
to adjust your own library to autoloading., (*7)
You can require vendor.phar
as well as usual vendor/autoload.php
., (*8)
<?php use mpyw\MyNewPackage\Foo; require __DIR__ . '/vendor.phar'; $foo = new Foo();
<?php use mpyw\MyNewPackage\Foo; use mpyw\MyOtherPackage\Bar; $loader = require __DIR__ . '/vendor.phar'; $loader->addPsr4('mpyw\\MyOtherPackage\\', '~/my-other-package/src'); $foo = new Foo(); $bar = new Bar();
It's possible you run into the error "Permission denied":, (*9)
example@localhost:~/my-new-package$ comphar -v Error: Permission denied: Disable "phar.readonly" and "phar.require_hash"
In this case, you need to adjust your configuration at the "php.ini":, (*10)
[Phar] ; http://php.net/phar.readonly phar.readonly = Off ; http://php.net/phar.require-hash phar.require_hash = Off