1. Go to this page and download the library: Download flyokai/amphp-injector 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/ */
flyokai / amphp-injector example snippets
use Amp\Injector\Application;
use Amp\Injector\Definitions;
use Amp\Injector\Injector;
use function Amp\Injector\{any, arguments, names, object, singleton, value};
class MyService
{
public function __construct(public array $config) {}
}
$definitions = (new Definitions())
->with(
singleton(object(MyService::class, arguments(names()
->with('config', value(['key' => 'val']))
))),
'my_service',
);
$application = new Application(new Injector(any()), $definitions, 'my-app');
$application->start();
/** @var MyService $svc */
$svc = $application->getContainer()->get('my_service');
$application->stop();
types()->with(ProviderContext::class, new ProviderDefinition(new ContextProvider()))
class FooImpl
{
public function __construct(
#[PrivateParameter] protected Bar $bar,
#[SharedParameter] protected Baz $baz,
#[ServiceParameter] protected Qux $qux,
#[FactoryParameter(Bar::class)] protected \Closure $barFactory,
) {}
public function makeBar(): Bar { return ($this->barFactory)(); }
}
$defs = definitions()
->with(object(Foo::class))
->with(object(Bar::class));
$injector = new Injector(automaticTypes($defs));
// Bar's constructor parameter of type Foo is auto-resolved.
$injector = new Injector(any(
automaticTypes($defs, $aliasResolver),
runtimeTypes(new Definitions(), $aliasResolver),
));
$aliasResolver = (new \Amp\Injector\AliasResolverImpl())
->with(Foo::class, FooImpl::class)
->with(Bar::class, BarImpl::class);
$injector = (new Injector(any(...)))
->withAlias($aliasResolver->alias(...));
$application = new Application($injector, $definitions, 'app', $aliasResolver);
$application->getContainer()->get(Foo::class); // → FooImpl
$definitions = (new Definitions())
->with(proxy(Car::class, object(Car::class)), 'car')
->with(proxy(V8::class, object(V8::class)), 'engine');
$car = $container->get('car'); // Car constructor NOT called yet
$car->turnRight(); // NOW Car is constructed
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.