6

I have a php command line tool that I would like to share with the world. Well, actually I plan to write it, and it is a tool for loading design documents into couchdb, but that's not really the point.

Anyway, is there a best practice for packaging php scripts so that they can be distributed for easy installation? I'm thinking something along the lines of the good old "./configure & make & make install", but what to do for a php script? I have read about phar, but it seems that it is intended mainly for libraries. So any ideas? Or examples of how other projects have done this?

Mikael Lindqvist
  • 775
  • 5
  • 21

3 Answers3

1

Composer is a good example of how to package up the tool, both as a stand-alone set of source code, and code that can be run. I installed it as a phar and now can just run it anywhere as 'composer --options args'

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • ah yep I just read up on composer a bit more, and it seems like a .phar script can actually be made to behave like an executable command. I read about it here: http://getcomposer.org/doc/00-intro.md where they suggest doing "sudo mv composer.phar /usr/local/bin/composer". I think that's pretty much what I'm after. Will check the details... – Mikael Lindqvist Feb 18 '13 at 11:19
  • PHAR is exactly what I was looking for, it just seems a bit non-straightforward and not particularly well documented how to actually create a PHAR archive. At least at the moment... – Mikael Lindqvist Feb 18 '13 at 11:39
  • Take a look at the build process for Composer, and maybe also phpunit. There's also various blog posts, or the original info, at http://www.php.net/manual/en/intro.phar.php – Alister Bulman Feb 18 '13 at 13:22
  • 2
    I finally managed to create it... This was helpful: http://stackoverflow.com/questions/6336144/please-explain-how-to-create-phps-phar-stubs – Mikael Lindqvist Feb 18 '13 at 13:30
0

There's composer or the good old PEAR that's still widely used, especially for CLI tools. To create your own PEAR channel, Pirum by SensioLabs (the Symfony guys) comes in handy.

For further reading, Stuart Herbert has a great series on component based development.

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • hm... I know about PEAR and composer, but still it seems like those tools are mainly used for installing _dependencies_ into a project. I would like to be able to create something that behaves more like a command, so that it will sit in the /bin directory and just be invokable from the command line by typing it's name. – Mikael Lindqvist Feb 18 '13 at 11:14
  • if your component does not have dependencies, composer and PEAR are still useful. And especially PEAR is exactly what you are looking for. I.e. the installation of PHPUnit via PEAR goes like: `pear install pear.phpunit.de/PHPUnit` – Fabian Schmengler Feb 18 '13 at 11:20
  • hm.. but.. after installing PHPUnit, do I get a command line tool that I can run? which one? – Mikael Lindqvist Feb 18 '13 at 11:37
  • well, you get PHPUnit. It's a ready to use command line tool. – Fabian Schmengler Feb 18 '13 at 12:03
  • Mikaels-iMac:~ mikael$ PHPunit -bash: PHPunit: command not found – Mikael Lindqvist Feb 18 '13 at 12:08
  • actually that doesn't work for me either... where is that file located on your system? – Mikael Lindqvist Feb 18 '13 at 12:49
0

Check out CLIFramework it handles subcommands, argument validation, option values (multiple, flag, optional, default) and a powerful zsh/bash completion generator.

A compile command is also out-of-box to help you compile your console application into a phar file for distributing. (phpbrew uses this way to distribute the application to the world)

Here is a screencast of using zsh completion generator (it completes arguments and options):

zsh completion generator

There are also many popular applications using CLIFramework and GetOptionKit. e.g., PHPBrew, LazyRecord

c9s
  • 1,888
  • 19
  • 15