https://github.com/code16/write-to-console
Simple Trait to interact with artisan console from any PHP class
https://github.com/code16/write-to-console
artisan console console-tool laravel package
Last synced: 3 months ago
JSON representation
Simple Trait to interact with artisan console from any PHP class
- Host: GitHub
- URL: https://github.com/code16/write-to-console
- Owner: code16
- License: mit
- Created: 2017-08-29T13:16:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-26T12:30:49.000Z (over 5 years ago)
- Last Synced: 2025-05-02T11:18:45.870Z (9 months ago)
- Topics: artisan, console, console-tool, laravel, package
- Language: PHP
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WriteToConsole trait
This is a simple trait to allow a third party class to output to an artisan console.
Useful when implementing heavy procedural scripts which code is shared with other part of the application, or just written in its own class for easier testability.
It simply redirects the `info()` , `error()` , `comment()`, `table()` and `progressBar()` methods to the command object set by calling the public `setConsole()` method of the trait, or just pass-through if no command object is set.
## Example
Using the trait in a custom script :
```php
namespace App\Tools;
use Code16\WriteToConsole\WriteToConsole;
class DataImporter
{
use WriteToConsole;
public function execute()
{
$this->info("Import started");
$progress = $this->progressBar(100);
for($x=0;$x<100;$x++) {
$progress->advance();
}
$this->info("Import finished");
}
}
```
Calling the script from an artisan console command :
```php
setConsole($this);
$importer->execute();
}
}
```
## Output text to a logger
`WriteToConsole` also accepts a Psr-3 compatible logger as additionnal argument. Therefore every methods, excepts table() and progressBar(), will be redirected to the logger.
```
public function handle()
{
$importer = app(DataImporter::class);
$importer->setConsole($this);
$logger = app(\Psr\Log\LoggerInterface::class);
$importer->setLogger($logger);
$importer->execute();
}
```
## License
MIT
(c) 2018 Code16.fr