It's raining cats and dogs.

無駄なことなんてないはず

composerでSmarty2をインストールする

composerでsmartyを入れる時は

{
    "require": {
        "smarty/smarty": "*"
    }
}

とやるとインストールできるのだけど、これだとSmarty3が入ってしまう。でも今の環境ではSmarty2が使われているので2を入れたい(バージョンアップしたらいいがなという話は置いといて)。いろいろ調べてみたけどどうやらSmarty2はcomposerに対応していないようなので、packageを使って個別に入れることにする。

{
    "repositories":[
    {
        "type": "package",
        "package": {
            "name": "smarty/smarty2",
            "version": "2.6.28",
            "dist": {
                "url": "http://www.smarty.net/files/Smarty-2.6.28.tar.gz",
                "type": "tar"
            },
            "include-path": [
                "libs/"
            ]
        }
    }
    ],
    "require": {
        "smarty/smarty2": "*"
    }
}

nameは適当。あとは

$ php composer.phar install

でインストールし、

<?php
require 'vendor/autoload.php';
require 'Smarty.class.php';

$smarty = new Smarty();

という感じで使える。 composer.jsonの中でinclude-pathという項目を設定している。これが何かというと、composerではvendor/autoload.phpをrequireするとcomposerで使うためのinclude_pathを設定してくれる。これはvendor/composer/include_paths.phpの内容が設定されるようだ。composerに対応しているライブラリであれば、このinclude_paths.phpをよろしく編集してくれるので、特にこまらないのだけど、今回のSmarty2のようにpackageを使って入れたりするとinclude_pathを設定してくれないので、そのための設定。ちなみに上記のようにinclude-pathを設定すると

<?php

// include_paths.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    $vendorDir . '/smarty/smarty2/libs',
);

という感じでinclude_paths.phpを更新してくれる。
Smarty.class.phpvendor/smarty/smarty2/libs/Smarty.class.phpにいるので、"include-path": ["libs/"]を書いておくとvendor/smarty/smarty2/libs/までinclude_pathが通るので、

<?php
require 'Smarty.class.php';

で使えるわけだ。

ちなみに、require自体したくない場合は

{
    "repositories":[
    {
        "type": "package",
        "package": {
            "name": "smarty/smarty2",
            "version": "2.6.28",
            "dist": {
                "url": "http://www.smarty.net/files/Smarty-2.6.28.tar.gz",
                "type": "tar"
            },
            "autoload": {
                "files": ["libs/Smarty.class.php"]
            },
            "include-path": [
                "libs/"
            ]
        }
    }
    ],
    "require": {
        "smarty/smarty2": "*"
    }
}

という感じでautoload要素を追加しておけば良い。