LoginSignup
3
3

More than 5 years have passed since last update.

boxコマンドでpharファイルをビルドする

Posted at

box (https://box-project.github.io/box2/) を使ってpharファイルを作ってみます。

クイックスタート

以下のmain.phpを格納するpharファイルを作ります。

main.php
<?php
echo "Hello Phar!\n",
    __FILE__,"\n",
    Phar::running(),"\n",
    Phar::running(false),"\n";

boxのインストール

$ composer global require kherge/box

box.jsonファイルの作成

box.json
{
    "main": "main.php",
    "stub": true
}

ビルド

$ box build

default.pharというファイルが作成されました。実行結果の違いを見てみましょう。

$ php main.php
Hello Phar!
C:\Users\ishida\src\box-test\main.php


$ php default.phar
Hello Phar!
phar://C:/Users/ishida/src/box-test/default.phar/main.php
phar://C:/Users/ishida/src/box-test/default.phar
C:/Users/ishida/src/box-test/default.phar

Phar::running()を使って、現在pharで実行されているかを調べることができます。

また、pharファイル内では、__FILE____DIR__はpharファイル内を指します。pharファイルの外側にアクセスしたい場合は、__FILE____DIR__の代わりにPhar::running(false)dirname(Phar::running(false))を使います。

box.jsonの他のさまざまなオプションについては、box help buildを見ましょう。気が向いたら追記します。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3