lundi 4 avril 2016

Multiple services implementing same interface, switching at runtime

I'm trying to create multiple services that all implement the same interface/contract, and which service that is used depends on user input. So I can do that in controller to get service that is selected from user.

Service Provider:

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    $this->app->call([$this, 'registerProductionService']);
}

/**
 * Produce video depend on user selection.
 *
 * @param Request $request
 */
public function registerProductionService(Request $request)
{
    $service = studly_case($request->input('production')) . 'Repository';

    if (in_array($service, $this->availableServices))
    {
        $namespace = (new \ReflectionClass(ReportContract::class))->getNamespaceName();

        $this->app->bind(ReportContract::class, "{$namespace}\\{$service}");
    }
}

In controller:

public function create(ReportContract $report)
{
    $report->create();
}

But when I want to call create() method from queue, I can't get data from Request class. It means I can't get service that is selected from user? So how can I do that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire