PHP Classes

PHP Command Component: Base to create new components that call commands

Recommend this page to a friend!

  Author Author  
Picture of nvb
Name: nvb <contact>
Classes: 18 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 10x


  Detailed description   Download Download .zip .tar.gz  
This package provides a base to create new components that call commands.

It has an abstract class that can call external programs as if they run from the command line interface shell.

New command developers should extend this base class to provide specific functions to execute one or more external programs.

Details

PHP Command Component

This free as in freedom project aims to deliver a easy to use php command component.

The build status of the current master branch is tracked by Travis CI: Build Status Latest stable

The scrutinizer status are: code quality | build status

The versioneye status is: Dependency Status

Take a look on openhub.net.

The current change log can be found here.

Usage

You can find a lot of examples here.

class Zip extends Command
{
    / 
     * @param string $archiveName
     * @param array $items
     * @return array
     * @throws RuntimeException
     */
    public function __invoke($archiveName, array $items)
    { 
        return $this->zip($archiveName, $items);
    }

    / 
     * @param string $archiveName
     * @param array $items
     * @return array
     * @throws RuntimeException
     * @todo implement parameter validation
     */
    public function zip($archiveName, array $items)
    {   
        $command = '/usr/bin/zip -r ' . $archiveName . ' ' . implode(' ' , $items);

        return $this->execute($command);
    }

    / 
     * @param string $pathToArchive
     * @param null|string $outputPath
     * @return array
     * @throws RuntimeException
     * @todo implement parameter validation
     */
    public function unzip($pathToArchive, $outputPath = null)
    {   
        if (!is_null($outputPath)) {
            $command = '/usr/bin/unzip ' . $pathToArchive . ' -d ' . $outputPath;
        } else {
            $command = '/usr/bin/unzip ' . $pathToArchive;
        }

        return $this->execute($command);
    }

    / 
     * @param string $pathToArchive
     * @return array
     * @throws RuntimeException
     * @todo implement parameter validation
     */
    public function listContent($pathToArchive)
    {   
        $command = '/usr/bin/unzip -l ' . $pathToArchive;

        return $this->execute($command);
    }

    /
     * @throws InvalidSystemEnvironmentException
     */
    public function validateSystemEnvironment()
    {
        if (!is_executable('/usr/bin/zip')) {
            throw new InvalidSystemEnvironmentException(
                '/usr/bin/zip is mandatory'
            );
        }

        if (!is_executable('/usr/bin/unzip')) {
            throw new InvalidSystemEnvironmentException(
                '/usr/bin/unzip is mandatory'
            );
        }
    }
}

$zip = new Zip();

$pathToZipArchive = '/tmp/my.zip';

$zip->validateSystemEnvironment();

echo 'list archive content' . PHP_EOL;
$lines = $zip->listContent($pathToZipArchive);
foreach ($lines as $line) {
    echo $line . PHP_EOL;
}

echo 'unzip archive' . PHP_EOL;
$zip->unzip($pathToZipArchive, '/tmp/my_directory');

echo 'zip directory' . PHP_EOL;
//also valid call since we implemented __invoke
//$zip($pathToZipArchive, array('/tmp/my_directory'));
$zip->zip($pathToZipArchive, array('/tmp/my_directory'));

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_command
cd vendor/net_bazzline/php_component_command
git clone https://github.com/bazzline/php_component_command .

With Composer

composer require net_bazzline/php_component_command:dev-master

Benefits

  • easy and robust way of dealing with system commands
  • return value validation by using exceptions
  • hopefully the thinnest possible layer between system commands

API

API is available at bazzline.net.

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it :-D.


  Classes of nvb  >  PHP Command Component  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Command Component
Base name: php_component_comman
Description: Base to create new components that call commands
Version: -
PHP version: 5
License: GNU Lesser General Public License (LGPL)
 
  Groups   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Console Command line and console utilities View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imagesource (4 files)
Files folder imagetest (2 files, 1 directory)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  source  
File Role Description
  Plain text file AbstractCommand.php Class Class source
  Plain text file Command.php Class Class source
  Plain text file InvalidSystemEnvironmentException.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imageresources (2 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file CommandTest.php Class Class source

  Files folder image Files  /  test  /  resources  
File Role Description
  Accessible without login Plain text file failing_command.php Aux. Auxiliary script
  Accessible without login Plain text file running_command.php Aux. Auxiliary script

Download Download all files: php_component_comman.tar.gz php_component_comman.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
For more information send a message to info at phpclasses dot org.