Skip to content

Commit 4eecf7c

Browse files
committed
feat: absorb initphp/cli-table; document table rendering
The Console package's Utils/Table has been structurally identical to the standalone initphp/cli-table package for some time. Formalise the consolidation: - Declare composer 'replace' for initphp/cli-table so the deprecated package is not installed alongside this one. - Add aliases.php with a class_alias keeping \InitPHP\CLITable\Table resolvable for transitive consumers. - Document the table renderer in the README and add a migration section for users coming from initphp/cli-table.
1 parent cced885 commit 4eecf7c

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Console
2-
It is a simple helper library that will allow you to write your console/cli application with PHP.
2+
3+
A simple helper library for writing console/CLI applications in PHP.
4+
5+
Starting with **2.1**, this package also ships the ANSI-coloured table renderer that used to be distributed as the separate [`initphp/cli-table`](https://github.com/InitPHP/CLITable) package (now deprecated). See the [migration section](#migrating-from-initphpcli-table) below if you are coming from that package.
36

47
## Requirements
58

@@ -42,6 +45,52 @@ $console->run();
4245
php console.php list
4346
```
4447

48+
## Rendering tables
49+
50+
This package ships a styleable ASCII/ANSI table renderer under `\InitPHP\Console\Utils\Table`:
51+
52+
```php
53+
use InitPHP\Console\Utils\Table;
54+
55+
$table = Table::create()
56+
->setHeaderStyle(Table::COLOR_RED, Table::BOLD)
57+
->setBorderStyle(Table::COLOR_BLUE)
58+
->setCellStyle(Table::COLOR_GREEN);
59+
60+
$table->row(['id' => 1, 'name' => 'Matthew S.', 'email' => 'matthew@example.com', 'status' => true])
61+
->row(['id' => 2, 'name' => 'Millie J.', 'email' => 'millie@example.com', 'status' => false])
62+
->row(['id' => 3, 'name' => 'Regina G.', 'email' => 'regina@example.com', 'status' => true]);
63+
64+
echo $table; // or $table->getContent()
65+
```
66+
67+
The renderer is intentionally lightweight: it stringifies non-string cell values (`[NULL]`, `[TRUE]`, `[FALSE]`, `[CALLABLE]`, `[RESOURCE]`, class name for objects), auto-sizes columns, and emits standard SGR escape sequences. `mb_strlen()` is used when available so multibyte values align correctly.
68+
69+
## Migrating from `initphp/cli-table`
70+
71+
The standalone [`initphp/cli-table`](https://github.com/InitPHP/CLITable) package has been merged into this one starting with **2.1** and is now deprecated.
72+
73+
If your code currently uses `\InitPHP\CLITable\Table`, **no source changes are required** — this package ships a `class_alias` that keeps the old fully-qualified class name working. Just switch your dependency:
74+
75+
```diff
76+
- "initphp/cli-table": "^1.0",
77+
+ "initphp/console": "^2.1"
78+
```
79+
80+
(`initphp/console:^2.1` declares a Composer `replace` for `initphp/cli-table`, so Composer will not install both side-by-side.)
81+
82+
When you next touch the code, prefer the new canonical namespace:
83+
84+
```php
85+
// Before
86+
use InitPHP\CLITable\Table;
87+
88+
// After
89+
use InitPHP\Console\Utils\Table;
90+
```
91+
92+
The alias is intended as a transition aid and may be removed in a future major release.
93+
4594
## Credits
4695

4796
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<info@muhammetsafak.com.tr>>

‎composer.json‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"name": "initphp/console",
3-
"description": "It is a simple helper library that will allow you to write your console/cli application with PHP.",
3+
"description": "A simple helper library for writing console/CLI applications in PHP. As of 2.1 this package also ships the table renderer previously distributed as initphp/cli-table.",
44
"type": "library",
55
"license": "MIT",
66
"autoload": {
77
"psr-4": {
88
"InitPHP\\Console\\": "src/"
9-
}
9+
},
10+
"files": [
11+
"src/aliases.php"
12+
]
1013
},
1114
"authors": [
1215
{
@@ -19,5 +22,8 @@
1922
"minimum-stability": "stable",
2023
"require": {
2124
"php": ">=7.2"
25+
},
26+
"replace": {
27+
"initphp/cli-table": "*"
2228
}
2329
}

‎src/aliases.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* aliases.php
4+
*
5+
* Backwards-compatibility shim for users migrating from the deprecated
6+
* `initphp/cli-table` package. Aliases the old fully-qualified class
7+
* name to the canonical class that now lives in this package.
8+
*
9+
* Existing code using `use InitPHP\CLITable\Table;` continues to work
10+
* unchanged after switching to `initphp/console:^2.1`. The class_exists
11+
* guard with autoload disabled prevents fatal "cannot declare class"
12+
* errors if the legacy package is somehow still loaded alongside this
13+
* one — Composer's `replace` directive normally prevents that.
14+
*
15+
* @see https://github.com/InitPHP/Console#migrating-from-initphpcli-table
16+
*/
17+
18+
if (!class_exists(\InitPHP\CLITable\Table::class, false)) {
19+
class_alias(
20+
\InitPHP\Console\Utils\Table::class,
21+
'InitPHP\\CLITable\\Table'
22+
);
23+
}

0 commit comments

Comments
 (0)