{"id":28676482,"url":"https://github.com/code16/write-to-console","last_synced_at":"2025-10-25T20:08:12.539Z","repository":{"id":56954788,"uuid":"101763333","full_name":"code16/write-to-console","owner":"code16","description":"Simple Trait to interact with artisan console from any PHP class","archived":false,"fork":false,"pushed_at":"2020-06-26T12:30:49.000Z","size":6,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-02T11:18:45.870Z","etag":null,"topics":["artisan","console","console-tool","laravel","package"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/code16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-29T13:16:53.000Z","updated_at":"2020-06-26T12:29:50.000Z","dependencies_parsed_at":"2022-08-21T04:40:10.397Z","dependency_job_id":null,"html_url":"https://github.com/code16/write-to-console","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/code16/write-to-console","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code16%2Fwrite-to-console","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code16%2Fwrite-to-console/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code16%2Fwrite-to-console/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code16%2Fwrite-to-console/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code16","download_url":"https://codeload.github.com/code16/write-to-console/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code16%2Fwrite-to-console/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259732773,"owners_count":22903087,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artisan","console","console-tool","laravel","package"],"created_at":"2025-06-13T23:04:48.626Z","updated_at":"2025-10-25T20:08:07.519Z","avatar_url":"https://github.com/code16.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WriteToConsole trait\n\nThis is a simple trait to allow a third party class to output to an artisan console. \n\nUseful 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. \n\nIt 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. \n\n## Example\n\nUsing the trait in a custom script : \n\n```php\n\nnamespace App\\Tools;\n\nuse Code16\\WriteToConsole\\WriteToConsole;\n\nclass DataImporter\n{\n\tuse WriteToConsole;\n\n\tpublic function execute()\n\t{\n\t\t$this-\u003einfo(\"Import started\");\n\n\t\t$progress = $this-\u003eprogressBar(100);\n\n\t\tfor($x=0;$x\u003c100;$x++) {\n\t\t\t$progress-\u003eadvance();\n\t\t}\n\n\t\t$this-\u003einfo(\"Import finished\");\n\t}\n\n}\n\n```\n\nCalling the script from an artisan console command : \n\n```php\n\n\n\u003c?php\n\nnamespace App\\Console\\Commands;\n\nuse Illuminate\\Console\\Command;\nuse App\\Tools\\DataImporter;\n\nclass DataImportsCommand extends Command\n{\n    /**\n     * The name and signature of the console command.\n     *\n     * @var string\n     */\n    protected $signature = 'data:import';\n\n    /**\n     * The console command description.\n     *\n     * @var string\n     */\n    protected $description = 'Import DATA';\n\n    /**\n     * Create a new command instance.\n     *\n     * @return void\n     */\n    public function __construct()\n    {\n        parent::__construct();\n    }\n\n    /**\n     * Execute the console command.\n     *\n     * @return mixed\n     */\n    public function handle()\n    {   \n        $importer = app(DataImporter::class);\n        $importer-\u003esetConsole($this);\n        \n        $importer-\u003eexecute();\n\n    }\n}\n\n```\n\n## Output text to a logger\n\n`WriteToConsole` also accepts a Psr-3 compatible logger as additionnal argument. Therefore every methods, excepts table() and progressBar(), will be redirected to the logger. \n\n```\n    public function handle()\n    {   \n        $importer = app(DataImporter::class);\n        $importer-\u003esetConsole($this);\n\n        $logger = app(\\Psr\\Log\\LoggerInterface::class);\n        $importer-\u003esetLogger($logger);\n\n        $importer-\u003eexecute();\n\n    }\n```\n\n## License\n\nMIT\n\n(c) 2018 Code16.fr","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode16%2Fwrite-to-console","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode16%2Fwrite-to-console","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode16%2Fwrite-to-console/lists"}