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.php
はvendor/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
要素を追加しておけば良い。