Jan 9th, 2013
Often you want to publish your application with an command line interface. Composer lets you expose a binary while installing. Thats very handy, but a little bit tricky.
create a file without ending, which has this header:
(we refer to it as mybin, we assume that your bins in your directory are in root/bin)
#!/usr/bin/env php
<?php
do your normal bootstrapping, but when you try to include vendor/autoload, you have to check actually two paths:
__DIR__.vendor/autoload.php
AND __DIR__./../../autoload.php
the first is the case, when the bin file is called from your bin directory of the project and vendor is avaible, because it was created by composer
the second is for the case, when the bin file is called from a bin directory already inside vendor of ANOTHER project, which was installed by composer
first case:
/path/to/yourproject-root/bin/mybin
second case:
/path/to/an/other-project/vendor/yourvendor/yourproject-root/bin/mybin
in my humble opinion composer could help you choosing in which case you are in. Composer does create a small proxy file to call your bin inside the vendor directory of another project, but it could set a env var here. For example: COMPOSER_BIN_LOADED_AS_DEPENDENCY (…you name it). That would suffer from using a file_exists()
to find the right case. But most projects check if the vendor is avaible anyway, so that would not speed up things anyway.
DONT
reference a .bat file in the composer.json. just the path to mybin (with the script abouve) should suffer