PHP code example of clessic-php / clessic

1. Go to this page and download the library: Download clessic-php/clessic 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/ */

    

clessic-php / clessic example snippets




$classLoader = 
use Clessic\Command;
use Clessic\Command\VirtualView;
use Clessic\Enum\ReturnCode;

// パスの設定
Clessic::addCommandPaths('Commands');
Clessic::addViewPaths('Views');
Clessic::addRequestBodyParserPaths('BodyParsers');
$classLoader->addPsr4('Modeles\\', 'Modeles');

// コマンドラインまたはクエリパラメータを入力として受け取る
$cmd = new Command(Clessic::$arguments);
$returnCode = $cmd->execute();
if(($returnCode instanceof ReturnCode) && $returnCode->isNotFound())
{
    $returnCode = $cmd->execute(VirtualView::class);
}



lessic\Clessic;
use Clessic\Enum\AnsiEscCode;

echo Clessic::AnsiEscape(AnsiEscCode::ResetAll) . Clessic::AnsiEscape(AnsiEscCode::TextRed, "Hello, World!");



namespace Clessic\Sample;

use Clessic\Base\CommandBase;
use Clessic\Enum\ReturnCode;
use Clessic\Enum\ArgumentType;
use Clessic\Enum\ArgumentMode;

if(!class_exists(SampleCommand::class)):
/**
 * サンプルコマンドクラス
 *
 * @package Clessic
 */
class SampleCommand extends CommandBase {
    /** @inheritdoc CommandBase */
    public static $package = 'sample';
    
    /** @inheritdoc CommandBase */
    public static $description = 'サンプルコマンドの説明文';
    
    /** @inheritdoc CommandBase */
    public static $version = 'v1.0.0-stable';
    
    /** @inheritdoc CommandBase */
    public static $options = [
        'name' => [
            ArgumentType::String,
            ArgumentMode::Single,
            '-n',
            '--name',
            '@名前を指定するオプション',
        ],
        'help' => [
            ArgumentType::Help,
            '-h',
            '--help',
            '@ヘルプを表示するためのフラグ',
        ]
    ];

    /** @inheritdoc CommandBase */
    public static function run(array $args, mixed $body, array $headers): int|ReturnCode {
        $name = $args['name'] ?? 'default';
        echo "Hello, " . $name . "!" . PHP_EOL;
        return ReturnCode::Success;
    }
}
endif;
return SampleCommand::class;

 return function(...$_){ 


return function($body, $params)
{
    return json_decode($body, true);
};
sh
php (パースとコマンドの実行を保存したファイル名) sample -n World