xp-forge / inject

Dependency injection for the XP Framework
0 stars 0 forks source link

Binding DSL #22

Closed thekid closed 4 years ago

thekid commented 5 years ago

The following usage patterns exist:

// Bind interface to implementation, the "default" usage
$inject->bind(Repository::class, InDatabase::class);

// Named
$inject->bind(Path::class, new Path($webroot, 'src/main/handlebars'), 'templates');

// Instances
$inject->bind(DBConnection::class, $conn);

// Singleton
$inject->bind(TemplateEngine::class, $inject->get(TemplateEngine::class));

Here's the DSL form of the above:

$inject= new Injector(Bindings::using()
  ->typed(Repository::class, InDatabase::class)
  ->named('templates', new Path($webroot, 'src/main/handlebars'))
  ->instance(DriverManager::getConnection($dsn))
  ->singleton(TemplateEngine::class)
);