One of the things that I was interested in is how some PHP frameworks can be used from command line interface to execute PHP code and commands. Some of the tools that I have used are composer and PHPUnit and both can be used from command line. For that reason, I started to think about adding this feature to WebFiori Framework.
After few attempts and failures, I managed to add basic support for running the framework from command line. You can see from the following picture that running the framework from CMD is working without problems.
There are few things that I had to do in order to add support for running the framework from the command line. The things are the following:
- Checking if main script file is loaded using command line using the method php_sapi_name().
- Initializing some $_SERVER and $_ENV variables as they are not initialized if PHP script is accessed through command line.
- Getting command line arguments using the constant $_SERVER['argv'].
For the last two steps, I created new class to handle the two tasks. The name of the class is CLI.php After doing that, I have only changed few lines of code in main class to redirect user to use CLI as shown in the following picture.