PHP code example of iviphp / console

1. Go to this page and download the library: Download iviphp/console library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iviphp / console example snippets




declare(strict_types=1);

use Ivi\Console\Console;
use Ivi\Console\ConsoleManager;

$manager = new ConsoleManager(
    applicationName: 'My Application',
    applicationVersion: '1.0.0'
);

$console = new Console($manager);



declare(strict_types=1);

use Ivi\Console\Contracts\InputInterface;
use Ivi\Console\Contracts\OutputInterface;

$console->command(
    name: 'hello',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $output->writeln('Hello from IviPHP.');

        return 0;
    },
    description: 'Display a greeting.'
);

use Ivi\Console\ConsoleManager;

ConsoleManager::SUCCESS;
ConsoleManager::FAILURE;
ConsoleManager::INVALID;

$console->command(
    name: 'project:check',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $output->success('Project configuration is valid.');

        return ConsoleManager::SUCCESS;
    }
);

$console->command(
    name: 'greet',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $name = $input->argument(
            0,
            'Developer'
        );

        $output->writeln(
            "Hello, {$name}."
        );

        return 0;
    },
    description: 'Greet a person.',
    usage: 'greet [name]'
);

$arguments = $input->arguments();

if ($input->hasArgument(0)) {
    $name = $input->argument(0);
}

$verbose = $input->option(
    'verbose',
    false
);

$environment = $input->option(
    'environment',
    'development'
);

$cacheEnabled = $input->booleanOption(
    'cache',
    true
);

$verbose = $input->hasOption('v');

[
    'a' => true,
    'b' => true,
    'c' => true,
]

$port = $input->integerOption(
    'p',
    8000
);

[
    '--example',
    '--verbose',
]

$groups = $input->option('group');

[
    'unit',
    'integration',
]

$verbose = $input->booleanOption(
    'verbose',
    false
);

$environment = $input->stringOption(
    'environment',
    'development'
);

$port = $input->integerOption(
    'port',
    8000
);

$console->command(
    name: 'cache:clear',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $output->success('Cache cleared.');

        return 0;
    },
    description: 'Remove all cached values.',
    aliases: [
        'cache:flush',
        'cc',
    ]
);

$console->command(
    name: 'user:create',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        return 0;
    },
    description: 'Create an application user.',
    usage: 'user:create <email> [--admin]'
);

$console->command(
    name: 'internal:sync',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $output->writeln(
            'Internal synchronization complete.'
        );

        return 0;
    },
    hidden: true
);



declare(strict_types=1);

use Ivi\Console\Command;
use Ivi\Console\Contracts\InputInterface;
use Ivi\Console\Contracts\OutputInterface;

$command = new Command(
    name: 'status',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $output->success(
            'Application is running.'
        );

        return 0;
    },
    description: 'Display application status.',
    aliases: [
        'health',
    ],
    usage: 'status',
    hidden: false
);

$console->register($command);

$command = Command::fromCallable(
    name: 'queue:work',
    handler: [$worker, 'run'],
    description: 'Process queued jobs.'
);

Ivi\Console\Contracts\CommandInterface



declare(strict_types=1);

use Ivi\Console\Contracts\CommandInterface;
use Ivi\Console\Contracts\InputInterface;
use Ivi\Console\Contracts\OutputInterface;

final class AboutCommand implements CommandInterface
{
    public function name(): string
    {
        return 'about';
    }

    public function description(): string
    {
        return 'Display application information.';
    }

    public function aliases(): array
    {
        return [];
    }

    public function usage(): string
    {
        return 'about';
    }

    public function isHidden(): bool
    {
        return false;
    }

    public function execute(
        InputInterface $input,
        OutputInterface $output
    ): int {
        $output->writeln(
            'Built with IviPHP.'
        );

        return 0;
    }
}

$console->register(
    new AboutCommand()
);

$console->registerMany([
    new AboutCommand(),
    new StatusCommand(),
    new CacheClearCommand(),
]);

$console->registerMany(
    $commands,
    replace: true
);

$registry = $console->registry();

if ($registry->has('cache:clear')) {
    $command = $registry->get(
        'cache:clear'
    );
}

$exists = $registry->hasPrimary(
    'cache:clear'
);

$isAlias = $registry->hasAlias('cc');

$name = $registry->resolveName('cc');

$commands = $console->commands();

$commands = $console->commands(
    

$names = $console
    ->registry()
    ->names();

$aliases = $console
    ->registry()
    ->aliases();

[
    'cache:flush' => 'cache:clear',
    'cc' => 'cache:clear',
]

$console->replace(
    $updatedCommand
);

$console->forget('cache:clear');

$console->forget('cc');

$console->renderVersion();

$output->write('Loading...');

$output->writeln('Complete.');

$output->info(
    'Reading configuration.'
);

$output->success(
    'Application installed.'
);

$output->warning(
    'Configuration file already exists.'
);

$output->error(
    'Unable to connect to the database.'
);



declare(strict_types=1);

use Ivi\Console\Output\ConsoleOutput;

$output = new ConsoleOutput();

$output = new ConsoleOutput(
    decorated: false
);

$output = new ConsoleOutput(
    decorated: true
);

$output->setDecorated(false);

if ($output->isDecorated()) {
    echo 'ANSI decoration enabled.';
}

$outputStream = fopen(
    'php://memory',
    'w+'
);

$errorStream = fopen(
    'php://memory',
    'w+'
);

$output = new ConsoleOutput(
    outputStream: $outputStream,
    errorStream: $errorStream,
    decorated: false
);

$output->newLine();

$output->newLine(2);

$output->writeError(
    'Raw error output.',
    newline: true
);



declare(strict_types=1);

use Ivi\Console\Input\ArgvInput;

$input = new ArgvInput([
    'console',
    'server:start',
    '--host=127.0.0.1',
    '--port=8080',
]);

$script = $input->script();

$command = $input->command();

$arguments = $input->arguments();

$options = $input->options();

$tokens = $input->tokens();

$input = ArgvInput::fromTokens(
    [
        'cache:clear',
        '--store=files',
    ],
    script: 'ivi'
);

$input = ArgvInput::fromGlobals();



declare(strict_types=1);

use Ivi\Console\Input\ArgvInput;
use Ivi\Console\Output\ConsoleOutput;

$input = ArgvInput::fromTokens([
    'hello',
    'Gaspard',
]);

$output = new ConsoleOutput();

$exitCode = $console->run(
    $input,
    $output
);

#!/usr/bin/env php


declare(strict_types=1);

ole;
use Ivi\Console\ConsoleManager;
use Ivi\Console\Contracts\InputInterface;
use Ivi\Console\Contracts\OutputInterface;

$console = new Console(
    new ConsoleManager(
        applicationName: 'Ivi Application',
        applicationVersion: '1.0.0'
    )
);

$console->command(
    name: 'hello',
    handler: static function (
        InputInterface $input,
        OutputInterface $output
    ): int {
        $name = $input->argument(
            0,
            'Developer'
        );

        $output->success(
            "Hello, {$name}."
        );

        return 0;
    },
    description: 'Display a greeting.',
    usage: 'hello [name]'
);

exit($console->run());

$manager = new ConsoleManager(
    catchExceptions: true
);

$console->setCatchExceptions(false);

if ($console->catchesExceptions()) {
    echo 'Exceptions are handled by the console.';
}

$console->setDebug(true);

if ($console->isDebug()) {
    echo 'Console debug mode enabled.';
}

$suggestions = $console->suggestCommands(
    'cach:clear'
);

$suggestions = $console->suggestCommands(
    'cach:clear',
    maximumSuggestions: 3
);

Ivi\Console\Contracts\InputInterface

public function script(): string;

public function command(): ?string;

public function arguments(): array;

public function hasArgument(int $index): bool;

public function argument(
    int $index,
    mixed $default = null
): mixed;

public function options(): array;

public function hasOption(string $name): bool;

public function option(
    string $name,
    mixed $default = null
): mixed;

public function tokens(): array;

Ivi\Console\Contracts\OutputInterface

public function write(
    string $message,
    bool $newline = false
): void;

public function writeln(
    string $message = ''
): void;

public function info(string $message): void;

public function success(string $message): void;

public function warning(string $message): void;

public function error(string $message): void;

public function isDecorated(): bool;

public function setDecorated(
    bool $decorated
): void;

Ivi\Console\Exceptions\ConsoleException



declare(strict_types=1);

use Ivi\Console\Exceptions\ConsoleException;

try {
    $command = $console->get(
        'missing:command'
    );
} catch (ConsoleException $exception) {
    echo $exception->getMessage();

    $context = $exception->context();
}

$console->clear();

$count = $console->count();

if ($console->registry()->isEmpty()) {
    echo 'No commands registered.';
}
bash
php console hello
bash
php console greet Gaspard
bash
php console build --verbose
bash
php console build --environment=production
bash
php console build --environment production
bash
php console build -abc
bash
php console server:start -p=8080
bash
php console inspect -- --example --verbose
bash
php console test --group=unit --group=integration
bash
php console help user:create
bash
php console
bash
php console list
bash
php console help
bash
php console --version
bash
php console cach:clear