PHP Classes

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-03-27 (6 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 32 This week: 1All time: 10,580 This week: 156Up
Version License PHP version Categories
php_component_comman 1.0GNU Lesser Genera...5PHP 5, Console
Description Author

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.

Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 153798 in Germany Germany
Week rank: 71 Up3 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

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.

  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:32
This week:1
All time:10,580
This week:156Up
For more information send a message to info at phpclasses dot org.