sasin91 / wow-emulator-communication

为与 WoW 模拟器进行通信提供了一个流畅的 API。

dev-master 2017-11-17 22:03 UTC

This package is auto-updated.

Last update: 2024-09-12 04:18:38 UTC


README

Software License

为 WoW 私服提供远程 API 通信包

内容

安装

对于 Laravel ~5

composer require sasin91/wow-emulator-communication

在您的 config/app.php 中的 providers 数组中添加以下服务提供者

\Sasin91\WoWEmulatorCommunication\EmulatorServiceProvider::class,

配置

要发布 emulator.php 配置文件,请运行以下命令: vendor:publish

php artisan vendor:publish --provider="\Sasin91\WoWEmulatorCommunication\EmulatorServiceProvider"

您可以根据需要配置配置文件,但默认设置应该适用于大多数情况。

如果您想向多个驱动程序发送命令,请使用前缀 multiple,它将返回现有驱动程序的数组。

自定义驱动程序

除了配置文件之外,您还可以通过以下语法直接在 Manager 上注册自己的驱动程序

\Emulators::extend(string $class_name, \Closure $callback);

$callback 应返回 Sasin91\WoWEmulatorCommunication\Drivers\EmulatorCommunicationContract 的实现。

除了提供自己的实现之外,您也可以使用通用的驱动程序,如下所示

\Emulators::extend('SkyFire', \Emulators::genericDriverCallback());

注意:驱动程序构造函数应接受一个字符串 $name 和一个数组 $configurations 作为参数。

**尚未实现构造函数依赖注入支持**。

自定义驱动程序特质

对于您的实现,Sasin91\WoWEmulatorCommunication\Drivers\Concerns 命名空间中提供了一些方便的特质。

* DispatchesDynamicCommands [allows for commands such as Emulator::accountOnlineList() or Emulator::account_online_list()]
* ExecutesCommands [Enables execution of Command(s)]
* HasConfigurations

对于大部分情况,仅需要执行 ExecutesCommands。然而,如果没有 HasConfigurations 特质,则合约将要求您实现自己的 config($key = null, $default = null) 方法。

您还可以扩展默认的驱动程序 Sasin91\WoWEmulatorCommunication\Drivers\EmulatorCommunicationDriver

通信管道

将管道视为在将命令传递给处理器之前运行的中间件层。

管道接收的是实现公共处理方法的纯 PHP 类,该方法接收命令对象和一个表示堆栈下一个片段的闭包。

public function handle($command, $next) 
{
	// Your Logic

	return $next($command);
}

如果您需要灵感,请查看 Sasin91\WoWEmulatorCommunication\Communication\Pipes 命名空间中现有的管道。

对于自定义驱动程序,您应该在 emulator.php 配置文件中的 'drivers' 数组中像注册其他驱动程序一样注册管道。

命名命令

手动编写每个命令字符串可能会有些繁琐和容易出错。现在有命名命令了!

这些命令与常规命令不同,它们将包含命令字符串本身,并且只需要输入参数。除了通信管道外,还可以通过使用 Sasin91\WoWEmulatorCommunication\Commands\Concerns\Validatable 并设置您的规则([...]) 来利用 Laravel 验证器。

与驱动程序一样,也可以编写自己的命名命令。命名命令应该实现 Sasin91\WoWEmulatorCommunication\NamedEmulatorCommandContract 接口。

Http 中间件

待办事项

与任何包一样,刷新 composer 自动加载器是个好主意。

composer dump-autoload

然后您就可以开始了。

用法

模拟器外观

向默认驱动程序发送命令

\Emulators::command($command);

向特定驱动程序发送命令

\Emulators::driver('driver')->command($command);

向多个驱动程序发送命令

\Emulators::driver('multiple')->command($command);

除了常规驱动程序方法外,还可以调用

\Emulators::emulator('driver')

而不是。作为一个额外的便利,外观上也有 dispatchTo($driver, $command)

$command 可以是 \Sasin91\WoWEmulatorCommunication\EmulatorCommand 的实例或一个字符串。

外观提示

有关 Laravel 外观的有趣之处在于,您可以编写并注册自己的。

这使得您可以创建特定于驱动程序的外观,例如 App\Facades\TrinityCore

class TrinityCore extends \Illuminate\Support\Facades\Facade 
{
    /**
     * Get the root object behind the facade.
     *
     * @return mixed
     */
    public static function getFacadeRoot()
    {
        return parent::getFacadeRoot()->driver('TrinityCore');
    }

    /**
     * Get the registered name of the component.
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    protected static function getFacadeAccessor()
    {
        return 'Sasin91\WoWEmulatorCommunication\EmulatorManager';
    }
}

驱动程序管理器

关于这一点真的没有太多可以说的,如果您喜欢依赖注入而不是服务定位器(即外观),那么注入或解析 \Sasin91\WoWEmulatorCommunication\EmulatorManager 也是一个选项。

测试

在使用模拟器时,可以使用 assertDispatched & assertNotDispatched。

    \Emulators::fake();

    // perform command

    \Emulators::assertDispatched(EmulatorCommand::class, function ($event) use ($command) {
        return (string)$event === (string)$command;
    });

    \Emulators::assertNotDispatched(EmulatorCommand::class);

事件

在命令的生命周期中,

应该会触发 [CommandCreating, CommandCreated, CommandFiring, CommandFired] 事件。

但是,对于自定义命令,创建和创建事件是可选的,因为它们需要手动触发。

典型的地方是在构造函数中触发这些事件。

此外,可以通过调用 EmulatorCommand::unsetEventDispatcher() 来禁用事件的完整,同样地,也可以通过调用 EmulatorCommand::setEventDispatcher($dispatcher) 来替换调度程序。

问题

如果您发现任何漏洞,请通过电子邮件发送给我,我的邮箱是 jonas.kerwin.hansen@gmail.com

对于问题,请到 Github 上创建一个 issue。

我目前了解到 proxy-driver-commands 和测试中存在一些问题。

许可证

wow-emulator-communication 是一款自由软件,根据 MIT 许可证的条款进行分发。