modulate/artisan-interceptor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

modulate/artisan-interceptor

最新稳定版本:v1.0.0

Composer 安装命令:

composer require modulate/artisan-interceptor

包简介

Allows you to add options, listeners and handlers to artisan commands

README 文档

README

An easy but elegant way to change global behaviours of Artisan commands

Latest Version on Packagist Scrutinizer Code Quality Code Coverage Build Status Total Downloads

Features

  • Add new global options to artisan e.g. --tenant=2 or --module=my-module
  • Add handlers to be executed before and/or after an artisan command is run
  • Add conditional handlers than only run when a specified option is given to a command
  • Fluent builder for adding input options to artisan

Adding Global Options

Generally speaking there is currently no easy way to add new global options to the artisan command. Options like --env or --version come built in but artisan doesn't expose a way for you to add new ones out of the box. This is where artisan interceptor comes in.

The interceptor allows you to add new global options to artisan and add your own custom handlers detect and process those options. This is all done using the built in artisan events but gives you a clean and elegant way of adding and interacting with new options

Installation

You can install the package via composer:

composer require modulate/artisan-interceptor

Usage

Adding Global Options

// Add a new optional option to artisan
ArtisanInterceptor::addOption(
    ArtisanInterceptor::optionBuilder()
        ->name('tenant')
        ->optional()
        ->get()
);

// Adding required options to the shell to handle things like authentication
ArtisanInterceptor::addOptions(
    ArtisanInterceptor::optionBuilder()
        ->name('user')
        ->required()
        ->get(),
    ArtisanInterceptor::optionBuilder()
        ->name('password')
        ->required()
        ->get()
);

Adding Listeners

<?php
use Modulate\Artisan\Interceptor\InterceptedCommand;

ArtisanInterceptor::before(function(InterceptedCommand $intercepted) {
    // Add a callback that runs before the command is run
    // but will only run if the given option is set
    $intercepted->getOutput()->writeln(sprintf(
        'Hello from %s tenantId: %d', 
        $intercepted->getCommand(),
        $intercepted->getInput()->getOption('tenant')
    ));
}, 'tenant')
->after(function(InterceptedCommand $intercepted) {
    // Add a callback that runs after the command is run
    // but will only run if the given option is set
    $intercepted->getOutput()->writeln(sprintf(
        'exitCode %d',
        $intercepted->getExitCode(),
    ));
}, 'tenant')
->after(function(InterceptedCommand $intercepted) {
    // You can also omit the option parameter to a before or after
    // callback to always run the callback
    $intercepted->getOutput()->writeln('This callback will always run after a command');
});

Extending Interceptor

You have full control of what the interceptor for all callback types. You can even add your own custom handlers by implementing the handler contracts directly

Testing

vendor/bin/testbench package:test

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-14