Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

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