承接 patinthehat/glacier 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

patinthehat/glacier

Composer 安装命令:

composer require patinthehat/glacier

包简介

Lightweight console application framework for PHP

README 文档

README

A lightweight framework for quickly putting together PHP CLI scripts and applications.

Install with composer

composer require patinthehat/glacier

Events

Glacier allows you to write event-driven applications quite easily using Events and EventListeners.

Custom events extend the Event class, and names are generated automatically. Event names are generated by inserting a period before every capital letter (except the first) and making everything lowercase. For example, "MyEvent123" would trigger the "my.event123" event.

An event can be defined as simply as:

class MyEvent1 extends Event { }

Properties, etc. can be added as required.

To trigger an event, call event():

event(new MyEvent1)

You may also send a payload along with the event by passing a second, optional parameter:

event(new MyEvent1, "my custom payload");

Event Listeners

Events are handled by event listeners, which are automatically registered by the application. No action needs to be taken other than defining an EventListener or MultipleEventListener class. To disable this behavior, set the property $autoRegister to false:

public static $autoRegister = false;

An event listener that handles events 'my.event1' and 'my.event2' can be defined as such:

class MyEventListener1 extends EventListener
{
    public static $events = [ 'my.event1', 'my.event2' ];

    public function handle($event, $payload = null)
    {
        app()->output()->write('[event fired] '.$event->name . PHP_EOL);
        return true;
    }

To listen for ALL events, the event name should be defined as '*':

    public static $events = [ '*' ];

Commands

Glacier allows support for multiple commands within the same application. To create a command, extend the Command class:

class MyCommand extends Command
{
    public static $autoRegister = true;
    public $name = 'mycmd';

    public function execute()
    {
	    $this->app->output()->writeln('my command!');
    }
}

By default, all commands are automatically registered. To disable this behavior, set the static property $autoRegister to false:

    public static $autoRegister = false;

If autoregistration is disabled, register it before calling $app->run():

$app->registerCommand(new MyCommand);

Default Commands

  • TBD

Command-Line Arguments

Glacier will accept all command-line flags by default. To accept a flag with a value, you must call the defineOption() method on the arguments() method. Once done defining options, you must call parse().

Once defined, even if the short flag is provided, its value can be accessed by calling setting($name), such as setting('test'), even if -t 55 was passed on the command line.

To define command-line options, use the following format:

defineOption(shortflag, long-flag, expects-value, default-value)

such as:

defineOption('t', 'test', true, 0)

In full, to define a command-line option:

$app->arguments()
    ->defineOption('t', 'test', true, 0)
    ->defineOption('a', 'all', false, false)
    ->defineOption('c', 'count', true, 10)
    ->parse();

This must be done before a call to $app->run().

Any of these settings defined here could later be referenced, for example, as setting('count') or setting('all').

If you simply wish to be able to handle flags with no values, you do not need to call defineOption(). To access, simply reference app()->arguments()->hasOption('myflag'):

  if (app()->arguments()->hasOption('quiet')) { ...

Configuration Files

  • TBD

A Basic Application

require(__DIR__.'/vendor/autoload.php');

use Glacier\Console\Application;
use Glacier\Console\DefaultCommand;
use Glacier\Events\Event;
use Glacier\Events\EventListener;
use Glacier\Events\MultipleEventListener;


class MyEvent1 extends Event { }
class MyEvent2 extends Event { }

//automatically registered with the application
class MyEventListener1 extends EventListener
{
    public static $events = [ 'my.event1', 'my.event2' ];

    public function handle($event, $payload = null)
    {
        app()->output()->write('[event fired] '.$event->name.'; listener: '.__CLASS__ . '; payload = '. (isset($payload->name) ? $payload->name : '') . PHP_EOL);
        return true;
    }
}

//automatically registered with the application
class MyEventListener3 extends MultipleEventListener
{
    public static $events = [ '*' ];
    protected $ignoreMissingHandlers = true;

    public function my_event1(Event $event, $payload = null)
    {
        echo '[1] hi from '.__CLASS__.': '.$event->name.PHP_EOL;
    }

    public function my_event2(Event $event, $payload = null)
    {
        echo '[2] hi from '.__CLASS__.': '.$event->name.PHP_EOL;
    }
}

class DemoCommand extends DefaultCommand
{
    public function initialize()
    {
       event(new MyEvent2);
    }

    public function execute()
    {
        event(new MyEvent1);
        app()->output()->writeln('hello from '.$this->getName());
    }

}

$app = new Application($argv,  __DIR__, true, false, [DemoCommand::class], false, false);

$app->arguments()
    ->defineOption('t', 'test', true, 0)
    ->parse();

$app->run();

Execution: php myapp.php -t 55 or php myapp.php --test=123

A Simple Glacier Application

require(__DIR__.'/vendor/autoload.php');

use Glacier\Console\Application;
use Glacier\Console\DefaultCommand;

class DemoCommand extends DefaultCommand
{
    public $name = 'demo2';

    public function initialize()
    {
        //
    }

    public function execute()
    {
        if (app()->arguments()->hasOption('goodbye')) {
            app()->output()->writeln('goodbye from '.$this->getName());
        } else {
            app()->output()->writeln('hello from '.$this->getName());
        }
    }

}

$app = new Application($argv,  __DIR__, true, true, [DemoCommand::class], false, false);
$app->run();

Execution: php myapp.php then php myapp.php --goodbye

patinthehat/glacier 适用场景与选型建议

patinthehat/glacier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 patinthehat/glacier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 patinthehat/glacier 我们能提供哪些服务?
定制开发 / 二次开发

基于 patinthehat/glacier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-08