Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Re-Introducing Symfony Console – CLI PHP for the Uninitiated!
May 25, 2017 @ 11:38:02

The SitePoint PHP blog has posted a tutorial from author Claudio Ribeiro that wants to re-introduce you to the Symfony Console package, a component of the larger Symfony framework that makes it easier to create and work with command-line PHP scripts.

As software developers, we often feel the need to resort to command line tools. These kinds of tools are helpful when we need to do a sort of recurring task like migrating data, performing imports, or creating cron jobs.

The Symfony Console component tool provides us with a simple framework to create our own command line tools. Unlike many components in Symfony, this is a standalone package and is used by the likes of Laravel‘s Artisan and many other famous PHP packages.

The tutorial then walks you through the installation process, via Composer, and the creation of a new command. With this simple base created, he then adds in actual functionality, building out a command to hash and verify a password string. They show how to use the command and an example of its output. Next up, he creates another command example, this time verifying the password hash provided as an argument. The tutorial wraps up with a look at testing your console comamnds with PHPUnit tests via the included CommandTester functionality.

tagged: symfony console commandline cli package component tutorial introduction

Link: https://www.sitepoint.com/re-introducing-symfony-console-cli-php-uninitiated/

Loïc Faugeron:
The Ultimate Developer Guide to Symfony - CLI Example
Apr 07, 2016 @ 10:43:51

Loïc Faugeron has posted another in his "ultimate guide" series of posts around components in the Symfony framework. In this latest post he gives an example of using the command line component with the Console component.

In this guide we've explored the main standalone libraries (also known as "Components") provided by Symfony to help us build applications: HTTP Kernel and HTTP Foundation, Event Dispatcher, Routing and YAML, Dependency Injection and Console. We've also seen how HttpKernel enabled reusable code with Bundles, and the different ways to organize our application tree directory.

Finally we've started to put all this knowledge in practice by creating a "fortune" project with: an endpoint that allows us to submit new fortunes, a page that lists all fortunes. In this article, we're going to continue the "fortune" project by creating a command that prints the last fortune.

He walks through the use of an example repository as a base and shows:

  • the creation of the command class
  • the matching tests to ensure it's working correctly
  • building out the logic to pull in the latest fortunes

They enter the fortunes via the web interface and use the command line to output them as as simple text.

tagged: ultimate developer guide symfony commandline cli example tutorial series

Link: https://gnugat.github.io/2016/04/06/ultimate-symfony-cli-example.html

Geert Eltink:
Zend-Expressive Console CLI Commands
Feb 12, 2016 @ 11:21:15

In a new post to his site Geert Eltink shares how he added console command support to Zend Expressive, a PSR-7 framework from Zend that recently hit it's v1.0 mark.

zend-expressive does not come out of the box with a console for handling cli commands. However it's easy to add this and make full use of the container and its dependencies.

He uses the Symfony console component to handle most of the "heavy lifting" with the command line interaction, pulled in via Composer. He shows the process for getting the component installed and how to create the "bootstrap" file needed to build an instance of the Application class. He follows this with a simple "greeting" command including the configuration to add a few arguments and output the simple "Hello" message. He then creates the functionality to wire it in to the Zend Expressive application and gives an example of it in use.

tagged: zend expressive framework console command commandline cli tutorial symfony component

Link: https://xtreamwayz.com/blog/2016-02-07-zend-expressive-console-cli-commands

SitePoint PHP Blog:
Console Wars – PHP CLI Libraries
Jul 27, 2015 @ 09:32:35

The SitePoint PHP blog has a post that compares some of the major PHP CLI libraries, three of them at least: the Symfony console component, Hoa console and the Webmozart solution.

I have always been a big fan of console commands and I try to provide a command line interface (CLI) as much as possible in most of my PHP projects. In this article, I’ll briefly compare three PHP console command libraries.

He starts with a brief history on each of the libraries, talking about their origins and age. He then talks about the necessary dependencies each requires and the overall complexity of the code they include. Next up is some practical examples putting each to use outputting a simple message back to the user using user input for both the message and output color.

tagged: console commandline library symfony hoa webmozart cli compare

Link: http://www.sitepoint.com/console-wars-php-cli-libraries/

Michelangelo van Dam:
popen for cli commands and pipes in php
May 05, 2015 @ 10:53:38

Michelangelo van Dam has a quick new post to his site talking about popen and pipes in command-line PHP as an alternative to the "exec" functions PHP provides to make command lines calls.

I got a question today about using commands that pipe output to other commands within PHP applications. There are two functions in PHP that are perfect for the task: popen and proc_open. But when you want to run it as a complete process, you can go about using exec, shell_exec, passthru or system and fiddle with escapeshellcmd. But often this looks messy and not reusable. A better approach would be to use "popen".

He includes a code example of how to use this method, showing a call to a command line tool and piping the results back into a PHP variable for later use. You can find out more about the use of popen in the PHP manual and accompanying examples.

tagged: popen procopen commandline cli pipe result example

Link: http://www.dragonbe.com/2015/05/popen-for-cli-commands-and-pipes-in-php.html

SitePoint PHP Blog:
Command line PHP using Symfony Console
Dec 12, 2013 @ 10:34:15

The SitePoint blog has a new post from Daniel Gafitescu covering the use of the Symfony Console component to create command line PHP scripts quickly and easily.

As a PHP developer, you will often find yourself working with PHP in the command line. The first time I had to use it was because I would get the "Maximum execution time of 30 seconds exceeded" error on a shared server where you could not change the max_execution_time PHP setting. Nowadays building command line scripts is much easier than it used to be. If you search on Packagist you will find a lot of packages to work with the command line but the one that stands out and is the most commonly used is Symfony/Console.

He starts with what you'll need to add to your Composer configuration to pull in a development version (2.4.x-dev) of the component. With that installed, he sets up a base directory ("/app") and a basic skeleton for your application. For his first command, he creates a script that will calculate the fibonacci numbers between two given numbers. He shows how to work with the input and Output objects inside the script and the code for the finished command - including some screenshots of the output.

tagged: command line cli symfony console tutorial

Link: http://www.sitepoint.com/command-line-php-using-symfony-console/

Cal Evans:
Signaling PHP
Oct 28, 2013 @ 09:21:42

Cal Evans has a new post to his site today about a book he's published covering a topic not really focused on in the PHP world - command-line usage. The book, "Signaling PHP" covers the use of the process control extension to handle command-line signals.

Most of the PHP I write these days is CLI scripts. I really wanted to be able to trap signals in some of my scripts. I struggled with this for a while; I even spent an entire weekend googling and reading only to find out that most of the information out there was either wrong, confusing, or incomplete. I decided that once I figured it out, I was going to put everything I learned together in one place to help others that were struggling with this topic as well.

The eBook is available for purchase and download now at a suggested price of only $5 USD. If you've been looking for a quick, concise guide to using process control in PHP, you should check it out.

tagged: signaling ebook calevans cli commandline

Link: http://blog.calevans.com/2013/10/27/signaling-php/

Smashing Magazine:
Powerful Command Line Tools For Developers
Oct 30, 2012 @ 10:18:56

On the Smashing Magazine site today they've posted a list of powerful CLI tools that every developer should at least know about to help make their lives easier - six of them ranging from SSH tunnels to HTTP testing.

Good tools are invaluable in figuring out where problems lie, and can also help to prevent problems from occurring in the first place, or just help you to be more efficient in general. Command line tools are particularly useful because they lend themselves well to automation and scripting, where they can be combined and reused in all sorts of different ways. Here we cover six particularly powerful and versatile tools which can help make your life a little bit easier.

The tools they mention are all things you'd install on a unix-based system:

  • Curl
  • Ngrep (network packet searching)
  • Netcat (to work with network connections)
  • Sshuttle (SSH tunneling)
  • Siege (HTTP benchmarking)
  • Mitmproxy (capturing proxy, both HTTP and HTTPS)
tagged: commandline cli tools developer list curl ngrep netcast sshuttle siege mitmproxy


Chris Jones:
Using the PHP CLI Webserver to Identify and Test Memory Issues in PHP
Aug 15, 2012 @ 08:35:07

Chris Jones has a new post today showing how you can use PHP 5.4's built-in web server to help test for memory issues in your application (and the language).

Rasmus mentioned on IRC how he ran the [command line] tests: simply renaming the ".phpt" test files as ".php", and invoking them through the CLI webserver. The SKIPIF sections get executed, but that doesn't affect the desired outcome of testing multiple HTTP requests with different input scripts. [Here] are some quick shell scripts I put together to automate testing the OCI8 extension with valgrind.

He uses the OCI8 extension as an example, showing how to set up these scripts to enable the execution of the tests, fire up the web server and execute Valgrind to help monitor the memory of the execution.

tagged: builtin webserver cli test unittest phpt tutorial shell


Odafe Ojenikoh's Blog:
Interactive PHP CLI Applications using Zend Form
Feb 16, 2012 @ 11:09:57

Odafe Ojenikoh has submitted a post he's written up showing how to create a command line PHP application with the help of the Zend Framework and it's Zend_Form elements.

Over the weekend, I was toying with the idea of interactive cli applications using readline() and Zend Form for validating input. My motivation for using Zend Form or rather Zend Form elements was to exploit the power of features such as labels, validators, filters and the prospect of reusing forms within web application code.

His example code defines a Filter for the form errors (returning them as a string) and a "Cli" class that handles the interaction with the command line. Next up is a class defining the form elements - a prompt for a color name and either of the words "thank" and "you". The Cli class transforms the form elements into prompts on the command line and lets you define validators to check their input.

tagged: zendform zendframework cli commandline application tutorial