1. Go to this page and download the library: Download codeia/di-senpai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codeia / di-senpai example snippets
class Module {
function auth() {
return new biz\Auth();
}
function security(ContainerInterface $c) {
return new biz\Acl($c->get(SecurityPolicy::class));
}
function acl() {
return new stub\AllowEveryone();
}
function users(ContainerInterface $c) {
return new db\UserRepository($c->get(PDO::class));
}
function db() {
return new PDO('sqlite::memory:');
}
}
$provided = (new ObjectGraphBuilder(new Module))->withScoped([
// map the module method names to their return types
// you need to do this for interfaces; AutoResolve can handle the concrete
// dependencies
'auth' => [Authenticator::class],
'security' => [Authorization::class],
'acl' => [SecurityPolicy::class],
'users' => [UserRepository::class],
'db' => [PDO::class],
])->build();
$container = new AutoResolve($provided);
class LoginController {
// must use FQCNs in the annotations
/** @var site\service\Authenticator */
private $auth;
/** @var site\service\UserRepository */
private $users;
/** @var site\service\Authorization */
private $checker;
function __construct(Senpai $pls) {
$pls->inject($this, Senpai::NO);
}
function isFullyUsable() {
return !empty($this->auth)
&& !empty($this->users)
&& !empty($this->checker);
}
}