diff --git a/README.md b/README.md index 0e88a12..83b9e32 100644 --- a/README.md +++ b/README.md @@ -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)__ - -![last release](https://badgen.net/packagist/v/darkelfe14728/commandline?label=Last%20release) -![licence](https://badgen.net/packagist/license/darkelfe14728/commandline?color=red) -![nb releases](https://badgen.net/packagist/dt/darkelfe14728/commandline?color=green) - -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 +![last release](https://badgen.net/packagist/v/darkelfe14728/console-output?label=Last%20release) +![licence](https://badgen.net/packagist/license/darkelfe14728/console-output?color=red) +![nb releases](https://badgen.net/packagist/dt/darkelfe14728/console-output?color=green) ## 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 -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") - } - +Classes to manage text output (to STDOUT by default, but customizable). Use ``Registry`` to manage global-level singletons. -## Automatic help generation -CommandLine provide a generator for command-line of programme. Example : +``ConsoleOutputHandler`` is a handler for [Monolog](https://packagist.org/packages/monolog/monolog) - Checker - File checker - - php checker.php [OPTIONS] path - Arguments : - path string File path - - 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. \ No newline at end of file diff --git a/composer.json b/composer.json index 3fcb160..92faeea 100644 --- a/composer.json +++ b/composer.json @@ -4,10 +4,9 @@ "keywords": [ "cli" ], "minimum-stability": "dev", - "require": { - "php": "^7.2", - "monolog/monolog": "^2.0", - "petrknap/php-singleton": "^1.0" + "require": { + "php": "^7.2", + "monolog/monolog": "^2.0" }, "autoload": { "psr-4": { @@ -32,4 +31,4 @@ "docs": "https://git.jrosset.ovh/darkelfe14728/ConsoleOutput/wiki", "source": "https://git.jrosset.ovh/darkelfe14728/ConsoleOutput" } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index 485b172..29402ab 100644 --- a/composer.lock +++ b/composer.lock @@ -1,20 +1,20 @@ { - "_readme": [ + "_readme": [ "This file locks the dependencies of your project to a known state", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c163ee35d8eab5a63e0509ceaaf7dfaf", - "packages": [ + "content-hash": "c1ee5b23c510d784897304a57f555c9a", + "packages": [ { - "name": "monolog/monolog", + "name": "monolog/monolog", "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/Seldaek/monolog.git", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", "reference": "39637a5d0e98068d98189ce48a87f3dd61455429" }, - "dist": { + "dist": { "type": "zip", "url": "https://api.github.com/repos/Seldaek/monolog/zipball/39637a5d0e98068d98189ce48a87f3dd61455429", "reference": "39637a5d0e98068d98189ce48a87f3dd61455429", @@ -97,47 +97,6 @@ ], "time": "2020-10-29T15:43:01+00:00" }, - { - "name": "petrknap/php-singleton", - "version": "v1.0.0", - "source": { - "type": "git", - "url": "https://github.com/petrknap/php-singleton.git", - "reference": "dec131d0d6c72dd76b5a5c7940f04f14f5df7a20" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/petrknap/php-singleton/zipball/dec131d0d6c72dd76b5a5c7940f04f14f5df7a20", - "reference": "dec131d0d6c72dd76b5a5c7940f04f14f5df7a20", - "shasum": "" - }, - "require": { - "php": ">=5.4" - }, - "require-dev": { - "phpunit/phpunit": "4.*" - }, - "type": "library", - "autoload": { - "psr-4": { - "PetrKnap\\Php\\Singleton\\": "src/Singleton" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Petr Knap", - "email": "dev@petrknap.cz", - "homepage": "http://petrknap.cz" - } - ], - "description": "Singleton pattern for PHP", - "homepage": "https://github.com/petrknap/php-singleton", - "time": "2016-09-22T07:59:18+00:00" - }, { "name": "psr/log", "version": "dev-master", diff --git a/src/Console/Output/ConsoleOutput.php b/src/Console/Output/ConsoleOutput.php new file mode 100644 index 0000000..88dab87 --- /dev/null +++ b/src/Console/Output/ConsoleOutput.php @@ -0,0 +1,212 @@ +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; + } +} \ No newline at end of file diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..3bd074d --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +*output.txt \ No newline at end of file diff --git a/tests/file.php b/tests/file.php new file mode 100644 index 0000000..bf02ca0 --- /dev/null +++ b/tests/file.php @@ -0,0 +1,12 @@ +output('Message out of a group'); +$console->openGroup(); +$console->output('Message in a group'); +$console->closeGroup(); \ No newline at end of file diff --git a/tests/normal.php b/tests/normal.php new file mode 100644 index 0000000..8b3533f --- /dev/null +++ b/tests/normal.php @@ -0,0 +1,21 @@ +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'); \ No newline at end of file diff --git a/tests/silent.php b/tests/silent.php new file mode 100644 index 0000000..3e9a45f --- /dev/null +++ b/tests/silent.php @@ -0,0 +1,12 @@ +setSilent(true); + +$console->openGroup('Main'); +$console->output('If you see this message, there is a problem'); +$console->closeGroup(); \ No newline at end of file diff --git a/tests/stderr.php b/tests/stderr.php new file mode 100644 index 0000000..525d696 --- /dev/null +++ b/tests/stderr.php @@ -0,0 +1,10 @@ +openGroup('Main'); +$console->output('This is an error'); +$console->closeGroup(); \ No newline at end of file