VOOZH about

URL: https://deepwiki.com/hypervel/permission/6.3-console-commands

⇱ Console Commands | hypervel/permission | DeepWiki


Loading...
Menu

Console Commands

The hypervel/permission package provides the permission:show console command for displaying permission and role relationships in a tabular format. The command is registered in ConfigProvider and implemented in the ShowCommand class.

Command Registration

The ShowCommand class is registered in the ConfigProvider::__invoke() method under the commands array. This makes the command available through the Hyperf console application.

Command Registration:

'commands' => [
 ShowCommand::class,
],

Sources: src/ConfigProvider.php18-20

Available Commands

CommandDescriptionArguments
permission:showDisplay permissions and roles in table formatguard (optional), style (optional)

Sources: src/Console/ShowCommand.php13-17

ShowCommand Class Structure

The ShowCommand class extends Hypervel\Console\Command and implements the permission:show command.

Class Definition:

PropertyTypeValue
$signature?string'permission:show {guard?} {style?}'
$descriptionstring'Show a table of all permissions and roles'
handle()methodMain execution method

Command Arguments:

  • guard (optional): Filters output to a specific guard name
  • style (optional): Table display style - default, borderless, compact, or box

Sources: src/Console/ShowCommand.php11-17

ShowCommand Component Dependencies:


Sources: src/ConfigProvider.php18-20 src/Console/ShowCommand.php7-9 src/Console/ShowCommand.php21-22

Command Execution Logic

handle() Method Execution Flow:


Sources: src/Console/ShowCommand.php19-67

Model Resolution

The handle() method instantiates model classes from configuration:


This allows the command to work with custom model implementations as configured in config/autoload/permission.php.

Sources: src/Console/ShowCommand.php21-22

Guard Discovery and Filtering

Guard Resolution Logic:

If the guard argument is provided, the command uses only that guard. Otherwise, it discovers all guards by:

  1. Querying all distinct guard_name values from the permissions table
  2. Querying all distinct guard_name values from the roles table
  3. Merging both collections and extracting unique values

Sources: src/Console/ShowCommand.php25-30

Permission-Role Matrix Construction

For each guard, the command builds a matrix showing which permissions are assigned to which roles:

Data Retrieval:

  1. Query roles with eager-loaded permissions: where('guard_name', $guard)->with('permissions')->orderBy('name')->get()
  2. Extract permission IDs for each role using mapWithKeys() and pluck()
  3. Query all permissions for the guard: whereGuardName($guard)->orderBy('name')->pluck('name', id)

Matrix Generation: The matrix is built by mapping each permission to a row where each cell indicates role assignment:

  • ' ✔' if the role has the permission (ID exists in role's permissions collection)
  • ' ·' if the role does not have the permission

Sources: src/Console/ShowCommand.php34-51

Table Header Generation

Role names in the table header are processed to remove the guard suffix:

  1. Split role name by underscore
  2. Remove last element (guard name)
  3. Rejoin with underscore

The first column header is empty (using TableCell('')).

Sources: src/Console/ShowCommand.php54-62

Usage

Basic Usage:


Displays all permissions and roles across all guards with default table style.

With Guard Filter:


Displays only permissions and roles for the "web" guard.

With Custom Table Style:


Displays "web" guard permissions with compact table style.

Available Table Styles:

StyleDescription
defaultStandard bordered table
borderlessTable without borders
compactMinimal row spacing
boxBox-drawing characters for borders

The style argument is passed directly to Symfony Console's table() method.

Sources: src/Console/ShowCommand.php13-15 src/Console/ShowCommand.php24 src/Console/ShowCommand.php64

Output Format

Console Output Structure:

For each guard, the command outputs:

  1. Guard name: info("Guard: {$guard}")
  2. Table with:
    • Header row: Empty cell + role names (with guard suffix removed)
    • Body rows: Permission name + checkmarks/dots indicating assignment
    • Checkmark (✔): Role has permission
    • Dot (·): Role does not have permission

Example Output:

Guard: web
+------------------------+----------+----------+
| | admin | editor |
+------------------------+----------+----------+
| create-posts | ✔ | ✔ |
| delete-posts | ✔ | · |
| edit-posts | ✔ | ✔ |
| view-posts | ✔ | ✔ |
+------------------------+----------+----------+

Sources: src/Console/ShowCommand.php32 src/Console/ShowCommand.php47-51 src/Console/ShowCommand.php53-65

Console Command Dependencies


Sources: src/Console/ShowCommand.php7-9

The console commands integrate with the broader permission system through the configuration layer and model abstractions, providing a consistent interface for administrative operations while maintaining flexibility for different framework implementations.