forrest icon indicating copy to clipboard operation
forrest copied to clipboard

Request: Support Facadeless Usage

Open SlyDave opened this issue 6 years ago • 1 comments

The service can't be injected into methods, because of how it's being registered. Please could we get it to so that the following behaviour is supported (because facades are evil):

function __construct(ForrestService $forrest) {
    $this->salesForceService = $forrest;
}

Thanks!

SlyDave avatar Apr 23 '18 10:04 SlyDave

A work-around to this is to write your own class that resolves the service based on the service key

<?php

declare(strict_types=1);

namespace App\Services\Forrest;

use Illuminate\Contracts\Container\Container;
use Omniphx\Forrest\Client as ForrestClient;

class ForrestResolver
{
    private const SERVICE_NAME = 'forrest';

    public function __construct(private Container $container)
    {
    }

    public function resolve(): ForrestClient
    {
        return $this->container->make(self::SERVICE_NAME);
    }
}

you can then inject and use it like

public function __construct(private ForrestResolver $forrestResolver) {}
...
$this->forrestResolver->resolve()->authenticate();

benrowe-chuffed avatar Apr 06 '22 02:04 benrowe-chuffed