1. Go to this page and download the library: Download tobento/service-resolver 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/ */
tobento / service-resolver example snippets
use Tobento\Service\ResolverContainer\ResolverFactory;
use Tobento\Service\Resolver\ResolverFactoryInterface;
use Tobento\Service\Resolver\ResolverInterface;
$resolverFactory = new ResolverFactory();
var_dump($resolverFactory instanceof ResolverFactoryInterface);
// bool(true)
// create resolver
$resolver = $resolverFactory->createResolver();
var_dump($resolver instanceof ResolverInterface);
// bool(true)
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo {}
$resolver = (new ResolverFactory())->createResolver();
var_dump($resolver->has(Bar::class));
// bool(false)
var_dump($resolver->get(Foo::class));
// object(Foo)#5 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo
{
public function __construct(
protected string $name
) {}
}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(Foo::class, new Foo('value'));
var_dump($resolver->get(Foo::class));
// object(Foo)#3 (1) { ["name":protected]=> string(5) "value" }
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo
{
public function __construct(
protected string $name
) {}
}
$resolver = (new ResolverFactory())->createResolver();
// By the construct method:
$resolver->set(Foo::class)->construct('value');
// By the with method using parameter name:
$resolver->set(Foo::class)->with(['name' => 'value']);
// By the with method using parameter position:
$resolver->set(Foo::class)->with([0 => 'value']);
var_dump($resolver->get(Foo::class));
// object(Foo)#9 (1) { ["name":protected]=> string(5) "value" }
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo
{
public function __construct(
protected string $name
) {}
}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(Foo::class, function($container) {
return new Foo('value');
});
var_dump($resolver->get(Foo::class));
// object(Foo)#8 (1) { ["name":protected]=> string(5) "value" }
$resolver->set(BarInterface::class, Bar::class);
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo
{
protected array $called = [];
public function index(Bar $bar, string $name)
{
$this->called[] = [$bar, $name];
}
}
class Bar {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(Foo::class)->callMethod('index', ['name' => 'value']);
$resolver->set(Foo::class)->callMethod('index', [1 => 'value']);
$foo = $resolver->get(Foo::class);
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo {}
class Bar {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(Foo::class)->prototype();
$resolver->set(Bar::class, function() {
return new Bar();
})->prototype();
var_dump($resolver->get(Foo::class) === $resolver->get(Foo::class));
// bool(false)
var_dump($resolver->get(Bar::class) === $resolver->get(Bar::class));
// bool(false)
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo
{
public function __construct(
private Bar $bar,
private string $name
) {}
}
class Bar {}
$resolver = (new ResolverFactory())->createResolver();
$foo = $resolver->make(Foo::class, ['name' => 'value']);
use Tobento\Service\ResolverContainer\ResolverFactory;
class Foo
{
public function index(Bar $bar, string $name): string
{
return $name;
}
}
class Bar {}
$resolver = (new ResolverFactory())->createResolver();
$name = $resolver->call([Foo::class, 'index'], ['name' => 'value']);
var_dump($name);
// string(5) "value"
use Tobento\Service\ResolverContainer\ResolverFactory;
class AdminUser {}
class GuestUser {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->on(AdminUser::class, GuestUser::class);
$user = $resolver->get(AdminUser::class);
var_dump($user);
// object(GuestUser)#9 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
class AdminUser {}
class GuestUser {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->on(AdminUser::class, function($user) {
return new GuestUser();
});
// with typehint:
$resolver->on(AdminUser::class, function($user, GuestUser $guest) {
return $guest;
});
$user = $resolver->get(AdminUser::class);
var_dump($user);
// object(GuestUser)#17 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
class AdminUser {}
class GuestUser {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->on(AdminUser::class, function($user) {
// modify $user
});
$user = $resolver->get(AdminUser::class);
var_dump($user);
// object(AdminUser)#9 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
interface UserInterface {}
class GuestUser implements UserInterface {}
class AdminUser implements UserInterface {}
class ServiceFoo {
public function __construct(
protected UserInterface $user
) {}
}
class ServiceBar {
public function __construct(
protected UserInterface $user
) {}
}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(UserInterface::class, GuestUser::class);
$resolver->on(ServiceFoo::class, ['user' => AdminUser::class]);
var_dump($resolver->get(ServiceFoo::class));
// object(ServiceFoo)#14 (1) { ["user":protected]=> object(AdminUser)#11 (0) { } }
var_dump($resolver->get(ServiceBar::class));
// object(ServiceBar)#9 (1) { ["user":protected]=> object(GuestUser)#13 (0) { } }
use Tobento\Service\ResolverContainer\ResolverFactory;
interface UserInterface {}
class GuestUser implements UserInterface {}
class AdminUser implements UserInterface {}
class ServiceFoo {
public function __construct(
protected UserInterface $user
) {}
}
class ServiceBar {
public function __construct(
protected UserInterface $user
) {}
}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(UserInterface::class, GuestUser::class);
$resolver->on(ServiceFoo::class, function($service, AdminUser $user) {
return ['user' => $user];
});
var_dump($resolver->get(ServiceFoo::class));
// object(ServiceFoo)#12 (1) { ["user":protected]=> object(AdminUser)#17 (0) { } }
var_dump($resolver->get(ServiceBar::class));
// object(ServiceBar)#11 (1) { ["user":protected]=> object(GuestUser)#14 (0) { } }
use Tobento\Service\ResolverContainer\ResolverFactory;
class FooService {}
class AdminUser {
protected array $called = [];
public function set(FooService $service, string $value)
{
$this->called[] = [$service, $value];
}
public function getCalled(): array
{
return $this->called;
}
}
$resolver = (new ResolverFactory())->createResolver();
$resolver->on(AdminUser::class)
->callMethod('set', ['value' => 'foo']);
$user = $resolver->get(AdminUser::class);
var_dump($user->getCalled());
// array(1) { [0]=> array(2) { [0]=> object(FooService)#15 (0) { } [1]=> string(3) "foo" } }
use Tobento\Service\ResolverContainer\ResolverFactory;
trait SomeMethods {
protected array $called = [];
public function set(string $value)
{
$this->called[] = $value;
}
public function getCalled(): array
{
return $this->called;
}
}
class AdminUser {
use SomeMethods;
}
$resolver = (new ResolverFactory())->createResolver();
$resolver->on(SomeMethods::class)
->trait()
->once(false)
->callMethod('set', ['value' => 'foo']);
$user = $resolver->get(AdminUser::class);
var_dump($user->getCalled());
// array(1) { [0]=> string(3) "foo" }
class AdminUser {}
class GuestUser {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(AdminUser::class)->prototype();
$resolver->on(AdminUser::class, GuestUser::class);
->once(false);
var_dump($resolver->get(AdminUser::class));
// object(GuestUser)#10 (0) { }
var_dump($resolver->get(AdminUser::class));
// object(GuestUser)#10 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
class AdminUser {}
class GuestUser {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(AdminUser::class)->prototype();
$resolver->on(AdminUser::class, GuestUser::class)
->once(false)
->prototype();
var_dump(
$resolver->get(AdminUser::class)
=== $resolver->get(AdminUser::class)
);
// bool(false)
use Tobento\Service\ResolverContainer\ResolverFactory;
interface UserInterface {}
class User implements UserInterface {}
class GuestUser extends User {}
class AdminUser extends User {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->set(UserInterface::class, GuestUser::class);
$resolver->on(User::class, function($user) {
return new AdminUser();
})->instanceof();
var_dump($resolver->get(UserInterface::class));
// object(AdminUser)#12 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
class AdminUser {}
class GuestUser {}
class EditorUser {}
$resolver = (new ResolverFactory())->createResolver();
$resolver->on(AdminUser::class, EditorUser::class)
->priority(500);
$resolver->on(AdminUser::class, GuestUser::class)
->priority(1000);
var_dump($resolver->get(AdminUser::class));
// object(EditorUser)#8 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
use Tobento\Service\Resolver\RuleInterface;
use Tobento\Service\Resolver\OnRule;
class AdminUser {}
class GuestUser {}
$resolver = (new ResolverFactory())->createResolver();
$rule = new OnRule(AdminUser::class, GuestUser::class);
var_dump($rule instanceof RuleInterface);
// bool(true)
$resolver->rule($rule);
$user = $resolver->get(AdminUser::class);
var_dump($user);
// object(GuestUser)#9 (0) { }
use Tobento\Service\ResolverContainer\ResolverFactory;
use Psr\Container\ContainerInterface;
$resolver = (new ResolverFactory())->createResolver();
var_dump($resolver->container() instanceof ContainerInterface);
// bool(true)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.