简体   繁体   English

如何重写Dispatch.php中的方法

[英]How to override methods in Dispatch.php

I'm hard coding the $connection and $queue in over 10 files so I'm trying to clean that up. 我正在用超过10个文件对$ connection和$ queue进行硬编码,因此我试图进行清理。 My first thought is to create some helpers that I can access in all of these files. 我的第一个想法是创建一些可以在所有这些文件中访问的助手。 However, I don't need those methods/variable available throughout my entire app. 但是,我不需要整个应用程序中都可用的那些方法/变量。 Instead, it would make most sense to place them in the ShouldQueue class. 相反,将它们放置在ShouldQueue类中是最有意义的。 Any thoughts on the proper way to do this? 对执行此操作的正确方法有何想法?

namespace App\Listeners\User;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;

class CreateJenzUser implements ShouldQueue
{

 use InteractsWithQueue;

 public $connection = 'sqs_high';

 public $queue = 'portal_high.fifo';

 //Would rather use
 public $connection = $highConnection;

 public function handle(UserBeingCreated $event)
 {
 }
}

EDIT 编辑

It turns out Laravel is creating a new instance of CreateJenzUser without the constructor. 事实证明Laravel在没有构造函数的情况下创建了CreateJenzUser的新实例。

vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php - line 479 供应商/laravel/framework/src/Illuminate/Events/Dispatcher.php-第479行

/**
 * Create the listener and job for a queued listener.
 *
 * @param  string  $class
 * @param  string  $method
 * @param  array  $arguments
 * @return array
 */
protected function createListenerAndJob($class, $method, $arguments)
{
    $listener = (new ReflectionClass($class))->newInstanceWithoutConstructor();

    return [$listener, $this->propagateListenerOptions(
        $listener, new CallQueuedListener($class, $method, $arguments)
    )];
}

Just need to figure out how to override this method. 只需要弄清楚如何重写此方法即可。

I would make a parent abstract class and extend it from all the classes that needs to use the queue. 我将创建一个父抽象类 ,并从需要使用队列的所有类中扩展它。 You can specify all the method that you must implement as abstract methods in the parent class so you do not need to implement the ShouldQueue interface. 您可以在父类中指定必须作为抽象方法实现的所有方法,因此您无需实现ShouldQueue接口。

So, the parent class will look like 因此,父类看起来像

abstract class Queue
{
     use InteractsWithQueue;

     protected $connection = 'sqs_high';

     protected $queue = 'portal_high.fifo';

     // list here all the methods in your ShouldQueue interface
     abstract protected function handle(UserBeingCreated $event);
}

Then the child class: 然后是子类:

class CreateJenzUser extends Queue
{

    protected function handle(UserBeingCreated $event)
    {
        // your code here
    }
}

Update 更新资料

If you need to keep the interface (for type hint check for example) you can extend and implement in the same time. 如果需要保留接口(例如,用于类型提示检查),则可以同时扩展和实现。 So, the child class in this case will look like: 因此,在这种情况下,子类如下所示:

class CreateJenzUser extends Queue implements ShouldQueue
{

    protected function handle(UserBeingCreated $event)
    {
        // your code here
    }
}

The Queue class may not need to be abstract in this case. 在这种情况下, Queue类可能不需要是抽象的。 However, that depends on how you want to design. 但是,这取决于您要如何设计。 If you have some methods that you want to call from the parent but define in the child you can still keep it as an abstract class. 如果您有一些要从父级调用但在子级中定义的方法,则仍可以将其保留为抽象类。

It sounds like that all those classes share code, that should be refactored. 听起来所有这些类都共享代码,应该对其进行重构。 Probably the best option would be to extract class and then pass it as a dependency in the constructors of classes, which need that behavior. 最好的选择可能是提取类 ,然后将其作为依赖项传递给需要该行为的类的构造函数。

You should also consider the fact, that trait is basically interpretator assisted copy-paste. 您还应该考虑以下事实,即trait基本上是解释器辅助的复制粘贴。 You are just using a language structure to hide that code duplication. 您只是使用一种语言结构来隐藏该代码重复。

PS 聚苯乙烯
Don't use extends to fix this. 不要使用extends来解决此问题。 The "extends" keyword should be read as "is a special subtype of". “ extends”关键字应读作“是...的特殊子类型”。 And user is not a special type of queue ... frankly, some could see it as an insult. 而且用户不是特殊的队列类型……坦率地说,有些人会认为这是一种侮辱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM