PHP Classes

File: example/cli

Recommend this page to a friend!
  Classes of nvb   CSV Component for PHP   example/cli   Download  
File: example/cli
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: CSV Component for PHP
Reader and writer for CSV files
Author: By
Last change: replaced deprecated array syntax >>array()<< with >>[]<<
Date: 7 years ago
Size: 5,224 bytes
 

Contents

Class file image Download
#!/usr/bin/env php <?php /** * @author stev leibelt <artodeto@bazzline.net> * @since 2015-06-30 * @see: * https://github.com/stevleibelt/examples/blob/master/php/cli/readline.php * https://github.com/yiisoft/yii2/issues/7974 * https://github.com/ErikDubbelboer/php-repl/blob/master/repl.php */ use Net\Bazzline\Component\Cli\Readline\ManagerFactory; use Net\Bazzline\Component\Csv\Reader\ReaderFactory; use Net\Bazzline\Component\Csv\Writer\WriterFactory; require_once __DIR__ . '/../vendor/autoload.php'; try { $path = ($argc > 1) ? $argv[1] : __DIR__ . '/file/example.csv'; if (!file_exists($path)) { throw new Exception('invalid file path provided: "' . $path . '"'); } $managerFactory = new ManagerFactory(); $readerFactory = new ReaderFactory(); $writerFactory = new WriterFactory(); $manager = $managerFactory->create(); $reader = $readerFactory->create(); $writer = $writerFactory->create(); $reader->setPath($path); $writer->setPath($path); $manager->setConfiguration( [ 'exit' => function () { exit(0); }, 'help' => function () { echo 'usage: ' . PHP_EOL . ' ' . basename(__FILE__) . ' [path to csv file]' . PHP_EOL; }, 'read' => [ 'all' => function () use ($reader) { foreach ($reader->readAll() as $line) { echo implode("\t", $line) . PHP_EOL; } }, 'many' => function($length = null, $start = null) use ($reader) { $showUsage = (is_null($length) || ((int) $length === 0)); if ($showUsage) { echo 'usage: ' . PHP_EOL . ' many <length> [<start>]' . PHP_EOL; } else { foreach ($reader->readMany($length, $start) as $line) { echo implode("\t", $line) . PHP_EOL; } } }, 'one' => function ($lineNumber = null) use ($reader) { $showUsage = (is_null($lineNumber)); if ($showUsage) { echo 'usage: ' . PHP_EOL . ' one <line number>' . PHP_EOL; } else { $line = $reader->readOne($lineNumber); if (is_array($line)) { echo implode("\t", $line) . PHP_EOL; } else if (is_scalar($line)) { echo $line . PHP_EOL; } } } ], 'write' => [ 'all' => function ($lines = null) use ($writer) { //@todo move into closure to reuse it $lines = func_get_args(); $numberOfLines = false; if (is_array($lines)) { $numberOfLines = $writer->writeMany($lines); } if ($numberOfLines === false) { echo 'no lines where written' . PHP_EOL; } else { echo count($lines) . ' lines written' . PHP_EOL; } }, 'many' => function () use ($writer) { $lines = func_get_args(); $numberOfLines = false; if (is_array($lines)) { $numberOfLines = $writer->writeMany($lines); } if ($numberOfLines === false) { echo 'no lines where written' . PHP_EOL; } else { echo count($lines) . ' lines written' . PHP_EOL; } }, 'one' => function () use ($writer) { $arguments = func_get_args(); $numberOfArguments = count($arguments); if ($numberOfArguments === 1) { $line = $arguments[0]; $numberOfLines = (is_scalar($line)) ? $writer->writeOne($line) : false; } else if ($numberOfArguments > 1) { $line = implode(',', $arguments); $numberOfLines = (is_scalar($line)) ? $writer->writeOne($line) : false; } else { $numberOfLines = false; } if ($numberOfLines === false) { echo 'no lines where written' . PHP_EOL; } else { echo '1 line written' . PHP_EOL; } } ] ] ); $manager->setPrompt('csv cli: '); $manager->run(); } catch (Exception $exception) { echo 'usage: ' . basename(__FILE__) . ' [<path/to/csv>]' . PHP_EOL; echo '----------------' . PHP_EOL; echo $exception->getMessage() . PHP_EOL; return 1; }