stevegrunwell / php-cli-examples

Examples to accompany "Building for the PHP Command Line Interface"

Home Page:https://github.com/stevegrunwell/building-for-php-cli

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP CLI Examples

This repository contains several sample PHP CLI scripts to accompany my talk, Building for the PHP Command Line Interface.

Installation

To install these samples locally, please start by installing their dependencies via Composer:

$ composer install

Examples

Globals

Global variables defined within PHP.

$argc and $argv

The argument count and values, respectively, that have been passed to PHP.

The script name is treated as an argument, so $argc should never be < 1 nor should $argv ever be empty.

$ php examples/ArgExample.php abc 123

PHP Functions

Demonstrations of calling native PHP functions in CLI scripts.

exec()

Call an external program and returns the last line of its output.

$ php examples/ExecExample.php

shell_exec()

Call an external program and returns its full output as a string.

$ php examples/ShellExecExample.php

system()

Executes a given command and outputs the result, attempting to flush the output buffer with each line.

$ php examples/SystemExample.php

passthru()

Like exec() or system(), passthru() executes a given command and outputs the result, but the output is not altered in any way, which makes it better suited for things like binary file operations.

$ php examples/PassthruExample.php

getenv() and putenv()

Retrieve and set system environment variables.

# The script will read DEMO_NAME and recognize you.
$ export DEMO_NAME="Steve"
$ php examples/EnvExample.php

# Unset DEMO_NAME for a different behavior.
$ unset DEMO_NAME
$ php examples/EnvExample.php

exit

Demonstrates exit codes within PHP.

Note: Technically, exit is a language construct, not a function, though it operates much like one.

# Exit with a code of 0 (success) and continue to echo 'Success!';
$ php examples/ExitCodeExample.php && echo "Success"

# Exit with a non-zero status code to break the chain.
$ php examples/ExitCodeExample.php 123 && echo "You should never see this"

Libraries and Frameworks

Simple programs built using third-party tools.

Symfony Console Component

A simple example of a Symfony Console command that accepts user output and demonstrates output.

$ php examples/SymfonyExample.php symfony-example Steve

PHP-CLI Tools

Uses PHP-CLI Tools' cli\prompt() and cli\choose() helpers to collect user input, then displays output using cli\line().

$ php examples/PHPCliToolsExample.php

WP-CLI

A trivial WP-CLI command that accepts a user login and displays the latest posts from that user.

As WP-CLI requires a WordPress environment, you'll need to copy the example file into WordPress:

$ cp examples/WPCLIExample.php /path/to/wordpress/wp-content/wp-cli-example.php
$ cd /path/to/wordpress
$ wp --require=wp-content/example-command.php example-command latest-posts-by-user admin

License

The MIT License (MIT) Copyright (c) 2016 Steve Grunwell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Examples to accompany "Building for the PHP Command Line Interface"

https://github.com/stevegrunwell/building-for-php-cli


Languages

Language:PHP 100.0%