Skip to content

Commit

Permalink
Contract: Review message levels
Browse files Browse the repository at this point in the history
- Add `LEVEL_` prefix to constant names
- Rename `MessageLevel` to `HasMessageLevel`
- Extend or implement `HasMessageLevel` instead of using it directly
  • Loading branch information
lkrms committed Feb 20, 2025
1 parent cb8af13 commit 5b90c6e
Show file tree
Hide file tree
Showing 45 changed files with 333 additions and 351 deletions.
18 changes: 9 additions & 9 deletions docs/Console.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ called, preferably while bootstrapping your application.
### Output methods

<!-- prettier-ignore -->
| `Console` method | `ConsoleLevel` | Message prefix | Default output target |
| ---------------- | --------------- | -------------- | ------------------------- |
| `error[Once]()` | `ERROR` = `3` | ` ! ` | `STDERR` |
| `warn[Once]()` | `WARNING` = `4` | ` ^ ` | `STDERR` |
| `info[Once]()` | `NOTICE` = `5` | `` | `STDOUT` |
| `log[Once]()` | `INFO` = `6` | ` - ` | `STDOUT` |
| `debug[Once]()` | `DEBUG` = `7` | ` : ` | `STDOUT` (if `DEBUG` set) |
| `group()`[^2] | `NOTICE` = `5` | ` » ` | `STDOUT` |
| `logProgress()` | `INFO` = `6` | `` | `STDOUT` |
| Method | Message level | Default prefix | Default output target |
| --------------- | --------------------- | -------------- | ------------------------- |
| `error[Once]()` | `LEVEL_ERROR` = `3` | ` ! ` | `STDERR` |
| `warn[Once]()` | `LEVEL_WARNING` = `4` | ` ^ ` | `STDERR` |
| `info[Once]()` | `LEVEL_NOTICE` = `5` | `` | `STDOUT` |
| `log[Once]()` | `LEVEL_INFO` = `6` | ` - ` | `STDOUT` |
| `debug[Once]()` | `LEVEL_DEBUG` = `7` | ` : ` | `STDOUT` (if `DEBUG` set) |
| `group()`[^2] | `LEVEL_NOTICE` = `5` | ` » ` | `STDOUT` |
| `logProgress()` | `LEVEL_INFO` = `6` | `` | `STDOUT` |

[^1]:
Actually a facade for [ConsoleInterface][], which is backed by a shared
Expand Down
5 changes: 2 additions & 3 deletions scripts/delete-covers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<?php declare(strict_types=1);

use Salient\Cli\CliApplication;
use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Core\Facade\Console;
use Salient\Utility\Exception\FilesystemErrorException;
use Salient\Utility\File;
Expand Down Expand Up @@ -54,7 +53,7 @@
$replaced++;

if ($check) {
Console::count(Level::ERROR);
Console::count(Console::LEVEL_ERROR);
$status |= 1;
if (!class_exists(Differ::class)) {
Console::log('Install sebastian/diff to show changes');
Expand All @@ -76,7 +75,7 @@
}

if ($replaced) {
Console::printOut('', Level::INFO);
Console::printOut('', Console::LEVEL_INFO);
}

Console::summary(Inflect::format(
Expand Down
8 changes: 4 additions & 4 deletions scripts/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Salient\Console\Console as ConsoleService;
use Salient\Container\Container;
use Salient\Contract\Cache\CacheInterface;
use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Contract\Catalog\HasMessageLevel;
use Salient\Contract\Console\ConsoleInterface;
use Salient\Contract\Container\ContainerInterface;
use Salient\Contract\Core\Event\EventDispatcherInterface;
Expand Down Expand Up @@ -78,7 +78,7 @@
App::class => [ContainerInterface::class, [Container::class], '--desc', 'A facade for the global service container', '--api'],
Cache::class => [CacheInterface::class, [CacheStore::class], '--desc', 'A facade for the global cache', '--api'],
Config::class => [ConfigurationManager::class, '--api'],
Console::class => [ConsoleInterface::class, [ConsoleService::class], '--implement', Level::class, '--desc', 'A facade for the global console service', '--api'],
Console::class => [ConsoleInterface::class, [ConsoleService::class], '--implement', HasMessageLevel::class, '--desc', 'A facade for the global console service', '--api'],
Err::class => [ErrorHandler::class, '--skip', 'handleShutdown,handleError,handleException', '--api'],
Event::class => [EventDispatcherInterface::class, [EventDispatcher::class], '--desc', 'A facade for the global event dispatcher', '--api'],
Sync::class => [SyncStoreInterface::class, [SyncStore::class], '--desc', 'A facade for the global sync entity store'],
Expand Down Expand Up @@ -234,7 +234,7 @@ function generated($commandOrFile): void
Console::log('Skipping', $file);
} elseif (!$exists && in_array('--check', $args)) {
Console::info('Would create', $file);
Console::count(Level::ERROR);
Console::count(Console::LEVEL_ERROR);
$status |= 1;
continue;
} elseif (!in_array('--check', $args)) {
Expand Down Expand Up @@ -327,7 +327,7 @@ function generated($commandOrFile): void
if (File::getContents($file) !== $attributes) {
if (in_array('--check', $args)) {
Console::info('Would replace', $file);
Console::count(Level::ERROR);
Console::count(Console::LEVEL_ERROR);
$status |= 1;
} else {
Console::info('Replacing', $file);
Expand Down
5 changes: 2 additions & 3 deletions src/Toolkit/Cli/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Salient\Console\Support\ConsoleMarkdownFormat;
use Salient\Console\ConsoleFormatter as Formatter;
use Salient\Container\Application;
use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Contract\Cli\CliApplicationInterface;
use Salient\Contract\Cli\CliCommandInterface;
use Salient\Contract\Cli\CliHelpSectionName;
Expand Down Expand Up @@ -324,7 +323,7 @@ public function run()

// or version info if it's "--version"
if ($arg === '--version' && !$args) {
return $this->reportVersion(Level::INFO, true);
return $this->reportVersion(Console::LEVEL_INFO, true);
}

// - If $args was empty before this iteration, print terse usage
Expand Down Expand Up @@ -455,7 +454,7 @@ public function runAndExit()
/**
* @inheritDoc
*/
public function reportVersion(int $level = Level::INFO, bool $stdout = false)
public function reportVersion(int $level = Console::LEVEL_INFO, bool $stdout = false)
{
$version = $this->getVersionString();

Expand Down
3 changes: 1 addition & 2 deletions src/Toolkit/Cli/CliCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Salient\Cli\Exception\CliInvalidArgumentsException;
use Salient\Cli\Exception\CliUnknownValueException;
use Salient\Console\ConsoleFormatter as Formatter;
use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Contract\Cli\CliApplicationInterface;
use Salient\Contract\Cli\CliCommandInterface;
use Salient\Contract\Cli\CliHelpSectionName;
Expand Down Expand Up @@ -190,7 +189,7 @@ final public function __invoke(string ...$args): int
}

if ($this->HasVersionArgument) {
$this->App->reportVersion(Level::INFO, true);
$this->App->reportVersion(Console::LEVEL_INFO, true);
return 0;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Toolkit/Cli/CliOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Salient\Cli\Exception\CliInvalidArgumentsException;
use Salient\Cli\Exception\CliUnknownValueException;
use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Contract\Cli\CliOptionType;
use Salient\Contract\Cli\CliOptionValueType;
use Salient\Contract\Cli\CliOptionValueUnknownPolicy;
Expand Down Expand Up @@ -981,7 +980,7 @@ private function filterValue($value, ?string $source = null, ?int $policy = null
throw new CliUnknownValueException($message);
}

Console::message('__Warning:__', $message, Level::WARNING, MessageType::UNFORMATTED);
Console::message('__Warning:__', $message, Console::LEVEL_WARNING, MessageType::UNFORMATTED);
$value = array_intersect($value, $this->AllowedValues);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Toolkit/Console/Concept/ConsolePrefixTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Salient\Console\Concept;

use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Contract\Console\ConsoleInterface as Console;
use Salient\Contract\Console\ConsoleTag as Tag;
use Salient\Contract\Console\ConsoleTargetPrefixInterface;

Expand All @@ -16,15 +16,15 @@ abstract class ConsolePrefixTarget extends ConsoleTarget implements ConsoleTarge
private int $PrefixLength = 0;

/**
* @param Level::* $level
* @param Console::LEVEL_* $level
* @param array<string,mixed> $context
*/
abstract protected function writeToTarget($level, string $message, array $context): void;
abstract protected function writeToTarget(int $level, string $message, array $context): void;

/**
* @inheritDoc
*/
final public function write($level, string $message, array $context = []): void
final public function write(int $level, string $message, array $context = []): void
{
$this->assertIsValid();

Expand Down
10 changes: 5 additions & 5 deletions src/Toolkit/Console/Concept/ConsoleStreamTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Salient\Console\Support\ConsoleMessageFormats as MessageFormats;
use Salient\Console\Support\ConsoleTagFormats as TagFormats;
use Salient\Contract\Catalog\HasAnsiEscapeSequence;
use Salient\Contract\Catalog\MessageLevel as Level;
use Salient\Contract\Catalog\MessageLevelGroup as LevelGroup;
use Salient\Contract\Console\ConsoleInterface as Console;
use Salient\Contract\Console\ConsoleMessageType as MessageType;
use Salient\Contract\Console\ConsoleMessageTypeGroup as MessageTypeGroup;
use Salient\Contract\Console\ConsoleTag as Tag;
Expand Down Expand Up @@ -101,10 +101,10 @@ protected function createMessageFormats(): MessageFormats

return (new MessageFormats())
->set(LevelGroup::ERRORS, MessageTypeGroup::ALL, new MessageFormat($boldRed, $default, $boldRed))
->set(Level::WARNING, MessageTypeGroup::ALL, new MessageFormat($yellow, $default, $boldYellow))
->set(Level::NOTICE, MessageTypeGroup::ALL, new MessageFormat($bold, $cyan, $boldCyan))
->set(Level::INFO, MessageTypeGroup::ALL, new MessageFormat($default, $yellow, $yellow))
->set(Level::DEBUG, MessageTypeGroup::ALL, new MessageFormat($dim, $dim, $dim))
->set(Console::LEVEL_WARNING, MessageTypeGroup::ALL, new MessageFormat($yellow, $default, $boldYellow))
->set(Console::LEVEL_NOTICE, MessageTypeGroup::ALL, new MessageFormat($bold, $cyan, $boldCyan))
->set(Console::LEVEL_INFO, MessageTypeGroup::ALL, new MessageFormat($default, $yellow, $yellow))
->set(Console::LEVEL_DEBUG, MessageTypeGroup::ALL, new MessageFormat($dim, $dim, $dim))
->set(LevelGroup::INFO, MessageType::PROGRESS, new MessageFormat($default, $yellow, $yellow))
->set(LevelGroup::INFO, MessageTypeGroup::GROUP, new MessageFormat($boldMagenta, $default, $boldMagenta))
->set(LevelGroup::INFO, MessageType::SUMMARY, new MessageFormat($default, $default, $bold))
Expand Down
Loading

0 comments on commit 5b90c6e

Please sign in to comment.