0

我们公司的 CRM/ERP 有点老了,我们正在积极努力。但我们计划很快迁移到 Laravel。我有机会将一些照明包迁移到一个项目中来帮助这种迁移(所以我不需要全部重写)。我将https://github.com/mattstauffer/Torch/用于“操作方法”。现在我必须触发一些事件并通过队列发送一些侦听器。我能够使事件和队列分开工作。但我想让一些听众排队,就像在 Laravel 中一样。

代码:

<?php
// init.php
require './vendor/autoload.php';


use App\AppFactory;
use Illuminate\Events\EventServiceProvider;
use Illuminate\Redis\RedisManager;
use Illuminate\Queue\Capsule\Manager as Queue;
$container = AppFactory::getInstance();
$container->instance('app', $container);
(new EventServiceProvider($container))->register();

$container['events']->listen(\App\Events\PublishEvent::class, \App\Listeners\PublishListener::class);
$container['events']->listen(\App\Events\PublishEvent::class, 'App\Listeners\QueueListener@handle');


$container->bind('redis', function () use ($container) {
  return new RedisManager($container, 'predis', [
      'default' => [
          'host' => '127.0.0.1',
          'password' => null,
          'port' => 6379,
          'database' => 0,
      ],
  ]);
});

$container->bind('exception.handler', \App\ExceptionHandler::class);
$queue = new Queue($container);
$queue->addConnection([
    'driver' => 'redis',
    'connection' => 'default',
    'queue' => 'default',
]);

$container['queue'] = $queue->getQueueManager();
class DoThing
{
    public function fire($job, $data)
    {
        $handle = fopen('proof.txt', 'a');
        fwrite($handle, PHP_EOL . $data['string']);
        $job->delete();
    }
}

<?php
// trigger-event.php

require("init.php");
container['events']->dispatch(new \App\Events\PublishEvent('data'));
<?php
// worker.php
require "init.php";

$queue = $container['queue'];
$events = $container['events'];
$handler = $container['exception.handler'];
$isDownForMaintenance = function () use ($container) {
    return $container->isDownForMaintenance();
};

$worker = new \Illuminate\Queue\Worker($queue, $events, $handler, $isDownForMaintenance);
$options = new \Illuminate\Queue\WorkerOptions();

$worker->runNextJob('default', 'default', $options);
<?php
// PublishEvent

namespace App\Events;

class PublishEvent
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }
}

<?php
// Listener

namespace App\Listeners;

use App\Events\PublishEvent;
use Illuminate\Contracts\Queue\ShouldQueue;

class QueueListener implements ShouldQueue
{

    public function handle(PublishEvent $event)
    {
        $handle = fopen('proof.txt', 'a');
        fwrite($handle, PHP_EOL . "Queue");
    }

}


我收到以下错误:

 Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Queue\Factory] is not instantiable.
 

我尝试了很多不同的东西,但无法让它发挥作用。谢谢您的帮助!

4

0 回答 0