2017 © Pedro Peláez
 

library yii-console

Yii Console

image

yiisoft/yii-console

Yii Console

  • Saturday, July 28, 2018
  • by cebe
  • Repository
  • 8 Watchers
  • 6 Stars
  • 20 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yii , (*1)

Yii Console


Latest Stable Version Total Downloads Build status Code Coverage Scrutinizer Quality Score Mutation testing badge static analysis type-coverage, (*2)

Yii Console package provides a console that could be added to an application. This console is based on Symfony Console. The following extra features are added:, (*3)

  • lazy command loader;
  • SymfonyEventDispatcher class that allows to use any PSR-14 compatible event dispatcher with Symfony console;
  • ErrorListener for logging console errors to any PSR-3 compatible logger;
  • console command serve that runs PHP built-in web server;
  • raises events ApplicationStartup and ApplicationShutdown in console application;
  • class ExitCode that contains constants for defining console command exit codes;
  • ConsoleBufferedOutput that wraps ConsoleOutput and buffers console output.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with Composer:, (*4)

composer require yiisoft/yii-console

General usage

In case you use one of Yii 3 standard application templates, console could be accessed as ./yii <command>., (*5)

If not, then in the simplest use case in your console entry script do the following:, (*6)

#!/usr/bin/env php
<?php

declare(strict_types=1);

use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Yii\Console\Application;
use Yiisoft\Yii\Console\CommandLoader;

require_once __DIR__ . '/vendor/autoload.php';

$app = new Application();

$app->setCommandLoader(new CommandLoader(
    // Any container implementing `Psr\Container\ContainerInterface` for example:
    new Container(ContainerConfig::create()),
    // An array with command names as keys and service IDs as values:
    ['my/custom' => MyCustomCommand::class],
));

$app->run();

Since \Yiisoft\Yii\Console\CommandLoader uses lazy loading of commands, it's necessary to specify the name and description in static properties when creating a command:, (*7)

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Yiisoft\Yii\Console\ExitCode;


#[AsCommand(
    name: 'my:custom',
    description: 'Description of my custom command.'
)]
final class MyCustomCommand extends Command
{    
    protected function configure(): void
    {
        // ...
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // ...
        return ExitCode::OK;
    }
}

Run the console entry script with your command:, (*8)

your-console-entry-script my/custom

When naming commands use : as a separator. For example: user:create, user:delete, etc., (*9)

Since the package is based on Symfony Console component, refer to its documentation for details on how to use the binary and create your own commands., (*10)

Aliases and hidden commands

To configure commands, set the names and aliases in \Yiisoft\Yii\Console\CommandLoader configuration. Names and aliases from the command class itself are always ignored., (*11)

The command can be marked as hidden by prefixing its name with |., (*12)

'yiisoft/yii-console' => [
    'commands' => [
        'hello' => Hello::class, // name: 'hello', aliases: [], hidden: false
        'start|run|s|r' => Run::class, // name: 'start', aliases: ['run', 's', 'r'], hidden: false
        '|hack|h' => Hack::class, // name: 'hack', aliases: ['h'], hidden: true
    ],
],

Runs PHP built-in web server

You can start local built-in web development server using the command:, (*13)

./yii serve

Your application will be accessible in your web browser at http://localhost:8080 by default. To configure default settings, set the options in \Yiisoft\Yii\Console\CommandLoader configuration., (*14)

'yiisoft/yii-console' => [
    'serve' => [
        'appRootPath' => null,
        'options' => [
            'address' => '127.0.0.1',
            'port' => '8080',
            'docroot' => 'public',
            'router' => 'public/index.php',
        ],
    ],
],

Alternatively, you can pass the settings through the console options., (*15)

Tip: To run a web server with XDebug enabled, pass --xdebug 1 to the command., (*16)

To see the available options, run ./yii serve --help., (*17)

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources., (*18)

License

The Yii Console is free software. It is released under the terms of the BSD License. Please see LICENSE for more information., (*19)

Maintained by Yii Software., (*20)

Support the project

Open Collective, (*21)

Follow updates

Official website Twitter Telegram Facebook Slack, (*22)

The Versions