Skip to content

Add Injector::implementations() to select implementations for a given type#32

Open
thekid wants to merge 6 commits intomasterfrom
feature/implementations
Open

Add Injector::implementations() to select implementations for a given type#32
thekid wants to merge 6 commits intomasterfrom
feature/implementations

Conversation

@thekid
Copy link
Member

@thekid thekid commented Jul 16, 2024

Previously:

use inject\{Injector, ProvisionException};

class Prompts {
  public function __construct(private Injector $inject) { }

  public function send($prompt, $model) {
    if ($instance= $this->injector->get(Model::class, $model)) {
      return $instance->send($prompt);
    }
    throw new ProvisionException('No such model '.$model);
  }
}

$prompts= (new Injector(/* ... */))->get(Prompts::class);

New alternative:

use inject\Injector;

class Prompts {

  /** @param inject.Implementations<Model> $implementations */
  public function __construct(private $implementations) { }

  public function send($prompt, $model) {
    return $this->implementations->named($model)->send($prompt);
  }
}

$prompts= (new Injector(/* ... */))->get(Prompts::class);

@thekid
Copy link
Member Author

thekid commented Jul 20, 2024

This will supersede providers.

Before

$provider= $injector->get('inject.Provider<com.example.writers.ReportWriter>');

// ...later on
$instance= $provider->get();

After

$implementations= $injector->implementations(ReportWriter::class);

// ...later on
$instance= $implementations->default();

Rollout

  • Merge this PR
  • Deprecate providers
  • Feature release
  • Remove providers
  • Major release

@thekid
Copy link
Member Author

thekid commented Jul 21, 2024

Alternative idea: Create inject.Injection superseding inject.Injector; only implementing new features there but already removing other APIs such as add(), args() and newInstance(). This would open a much faster path towards a new, leaner API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant