Main class ConsoleOutput and its tests
parent
caf446844e
commit
3ae53ae648
@ -1,71 +1,21 @@
|
||||
# GroupedMessageProcessor
|
||||
# ConsoleOutput
|
||||
__PHP managers for console (CLI) outputting__
|
||||
|
||||
An processor for monolog managing "grouped" logs messages
|
||||
|
||||
|
||||
# PHPCommandLine
|
||||
__Command line management library in PHP (CLI)__
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
A command-line options parser with configurable expects and help auto generation.
|
||||
|
||||
There is two kind of arguments :
|
||||
- options (short/long tag) : _-h_ or _--version_
|
||||
- values : my/path/to/a/file
|
||||

|
||||

|
||||

|
||||
|
||||
## Installation
|
||||
```
|
||||
composer require darkelfe14728/commandline
|
||||
composer require darkelfe14728/console-output
|
||||
```
|
||||
|
||||
## Description
|
||||
At first, create a new CommandLine with program name and description. Add options and values by using respectively _addOption_ and _addValue_.
|
||||
Default options for help (-h / --help) and version (--version) can be add with _addDefaultArguments_ and use _treatDefaultArguments_ for launching associated treatments.
|
||||
|
||||
Treat script arguments with _parse_ : return an object of variables.
|
||||
|
||||
## Example
|
||||
```php
|
||||
<?php
|
||||
|
||||
use CommandLine\CommandLine;
|
||||
use CommandLine\Argument\Option\Flag;
|
||||
use CommandLine\Argument\Parser\StringParser;
|
||||
use CommandLine\Argument\Value\Value;
|
||||
|
||||
$cmdline = new CommandLine('Checker', 'File checker', 'php checker.php');
|
||||
$cmdline->addDefaultArguments();
|
||||
|
||||
$cmdline->addOption(new Flag('enhanced', false, 'Deep check ?'));
|
||||
|
||||
$cmdline->addValue(new Value('path', 'File path', new StringParser()));
|
||||
|
||||
var_dump($args = $cmdline->parse());
|
||||
$cmdline->treatDefaultArguments($args);
|
||||
```
|
||||
|
||||
Would display something like that :
|
||||
|
||||
class stdClass#13 (1) {
|
||||
public $enhanced => bool(false)
|
||||
public $path => string("my/path/to/a/file")
|
||||
}
|
||||
|
||||
|
||||
## Automatic help generation
|
||||
CommandLine provide a generator for command-line of programme. Example :
|
||||
|
||||
Checker
|
||||
File checker
|
||||
Classes to manage text output (to STDOUT by default, but customizable). Use ``Registry`` to manage global-level singletons.
|
||||
|
||||
php checker.php [OPTIONS] path
|
||||
Arguments :
|
||||
path string File path
|
||||
``ConsoleOutputHandler`` is a handler for [Monolog](https://packagist.org/packages/monolog/monolog)
|
||||
|
||||
Options :
|
||||
-h --help X Affiche cette aide
|
||||
--enhanced Deep check ?
|
||||
## Indentation
|
||||
``ConsoleOutput`` manage automatic text indentation through groups. A group represent one level of indentation.
|
||||
|
||||
If a group has a name, the all group lines will be surronded by its name.
|
@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
namespace Console\Output;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use function count;
|
||||
use function str_repeat;
|
||||
use function str_replace;
|
||||
use const PHP_EOL;
|
||||
|
||||
/**
|
||||
* A manager for console outputting
|
||||
*/
|
||||
class ConsoleOutput {
|
||||
/**
|
||||
* @var string Default indentation text
|
||||
*/
|
||||
public const INDENT_DEFAULT = ' ';
|
||||
|
||||
/**
|
||||
* @var bool Is silent ? (no output)
|
||||
*/
|
||||
protected $silent = false;
|
||||
/**
|
||||
* @var string Indentation text
|
||||
*/
|
||||
protected $indent;
|
||||
|
||||
/**
|
||||
* @var resource Output file handler
|
||||
*/
|
||||
protected $fileHandler;
|
||||
/**
|
||||
* @var array Liste of open groups : group name (string) or null if no name)
|
||||
*/
|
||||
protected $groups;
|
||||
/**
|
||||
* @var string The prefix for named group open
|
||||
*/
|
||||
protected $groupOpenPrefix = '';
|
||||
/**
|
||||
* @var string The prefix for named group close
|
||||
*/
|
||||
protected $groupClosePrefix = 'END ';
|
||||
/**
|
||||
* @var bool Must close file handler at destruction ?
|
||||
*/
|
||||
private $closeFileHandler = false;
|
||||
/**
|
||||
* New manager
|
||||
*
|
||||
* @param false|string|resource $handler The output file handler or file path
|
||||
* @param string $indent The indentation text
|
||||
*
|
||||
* @throws InvalidArgumentException The handler is not a resource or a string
|
||||
* @throws Exception Failed to open output file handler
|
||||
*/
|
||||
public function __construct ($handler = STDOUT, $indent = self::INDENT_DEFAULT) {
|
||||
$this->setIndent($indent);
|
||||
|
||||
if (is_resource($handler)) {
|
||||
$this->fileHandler = $handler;
|
||||
}
|
||||
elseif (is_string($handler)) {
|
||||
$this->fileHandler = fopen($handler, 'a');
|
||||
if ($this->fileHandler === false) {
|
||||
throw new Exception('Failed to open the output file handler: ' . $handler);
|
||||
}
|
||||
$this->closeFileHandler = true;
|
||||
}
|
||||
else {
|
||||
throw new InvalidArgumentException('The output file handler must be a ressource or a string');
|
||||
}
|
||||
|
||||
$this->groups = [];
|
||||
}
|
||||
/**
|
||||
* Manager destroyed
|
||||
*/
|
||||
public function __destruct () {
|
||||
if ($this->closeFileHandler) {
|
||||
fclose($this->fileHandler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a message
|
||||
*
|
||||
* @param string $message The message
|
||||
* @param bool $ignoreIndent Ignore group indentation
|
||||
*/
|
||||
public function output (string $message, $ignoreIndent = false) {
|
||||
if ($this->silent) {
|
||||
return;
|
||||
}
|
||||
|
||||
$indent = $ignoreIndent ? '' : $this->getCurrentIndent();
|
||||
fwrite($this->fileHandler, $indent . str_replace(PHP_EOL, PHP_EOL . $indent, $message) . PHP_EOL);
|
||||
}
|
||||
/**
|
||||
* Give the current indentation string
|
||||
*
|
||||
* @return string The indentation string
|
||||
*/
|
||||
public function getCurrentIndent () {
|
||||
if (count($this->groups) === 0) {
|
||||
return '';
|
||||
}
|
||||
return str_repeat($this->indent, count($this->groups));
|
||||
}
|
||||
|
||||
/**
|
||||
* The prefix for named group open
|
||||
*
|
||||
* @return string The prefix for named group open
|
||||
*/
|
||||
public function getGroupOpenPrefix (): string {
|
||||
return $this->groupOpenPrefix;
|
||||
}
|
||||
/**
|
||||
* Change the prefix for named group open
|
||||
*
|
||||
* @param string $groupOpenPrefix The new prefix for named group open
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setGroupOpenPrefix (string $groupOpenPrefix): self {
|
||||
$this->groupOpenPrefix = $groupOpenPrefix;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* The prefix for named group close
|
||||
*
|
||||
* @return string The prefix for named group close
|
||||
*/
|
||||
public function getGroupClosePrefix (): string {
|
||||
return $this->groupClosePrefix;
|
||||
}
|
||||
/**
|
||||
* Change the prefix for named group close
|
||||
*
|
||||
* @param string $groupClosePrefix The new prefix for named group close
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setGroupClosePrefix (string $groupClosePrefix): self {
|
||||
$this->groupClosePrefix = $groupClosePrefix;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a new group
|
||||
*
|
||||
* @param string|null $name The group name or Null if none
|
||||
*/
|
||||
public function openGroup (?string $name = null) {
|
||||
if (!is_null($name) && trim($name) !== '') {
|
||||
$this->output($this->getGroupOpenPrefix() . $name);
|
||||
}
|
||||
$this->groups[] = $name;
|
||||
}
|
||||
/**
|
||||
* Close the last group
|
||||
*/
|
||||
public function closeGroup () {
|
||||
$name = array_pop($this->groups);
|
||||
if (!is_null($name) && trim($name) !== '') {
|
||||
$this->output($this->getGroupClosePrefix() . $name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is silent ? (no output)
|
||||
*
|
||||
* @return bool Is silent ?
|
||||
*/
|
||||
public function isSilent (): bool {
|
||||
return $this->silent;
|
||||
}
|
||||
/**
|
||||
* Change if silent (no output)
|
||||
*
|
||||
* @param bool $silent Will be silent ?
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSilent (bool $silent): self {
|
||||
$this->silent = $silent;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The indentation text
|
||||
*
|
||||
* @return string The indentation text
|
||||
*/
|
||||
public function getIndent (): string {
|
||||
return $this->indent;
|
||||
}
|
||||
/**
|
||||
* Change the indentation text
|
||||
*
|
||||
* @param string $indent The new indentation text
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setIndent (string $indent): self {
|
||||
$this->indent = $indent;
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
*output.txt
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use Console\Output\ConsoleOutput;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$console = new ConsoleOutput(__DIR__ . '/file_output.txt', "\t");
|
||||
|
||||
$console->output('Message out of a group');
|
||||
$console->openGroup();
|
||||
$console->output('Message in a group');
|
||||
$console->closeGroup();
|
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use Console\Output\ConsoleOutput;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$console = new ConsoleOutput();
|
||||
$console->setGroupOpenPrefix('OPEN ');
|
||||
$console->setGroupOpenPrefix('CLOSE ');
|
||||
|
||||
$console->output('Before group');
|
||||
|
||||
$console->openGroup('Main');
|
||||
$console->output('If you see this message' . PHP_EOL . 'It\'s OK');
|
||||
$console->openGroup();
|
||||
$console->output('An unamed group');
|
||||
$console->output('A message ignoring indentation', true);
|
||||
$console->closeGroup();
|
||||
$console->closeGroup();
|
||||
|
||||
$console->output('After group');
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use Console\Output\ConsoleOutput;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$console = new ConsoleOutput();
|
||||
$console->setSilent(true);
|
||||
|
||||
$console->openGroup('Main');
|
||||
$console->output('If you see this message, there is a problem');
|
||||
$console->closeGroup();
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
use Console\Output\ConsoleOutput;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$console = new ConsoleOutput(STDERR);
|
||||
$console->openGroup('Main');
|
||||
$console->output('This is an error');
|
||||
$console->closeGroup();
|
Loading…
Reference in New Issue