1. Go to this page and download the library: Download psx/dependency 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/ */
psx / dependency example snippets
use PSX\Dependency\Attribute\Tag;
use PSX\Dependency\Container;
use PSX\Dependency\Tests\Playground\FooService;
use PSX\Dependency\Tests\Playground\BarService;
class MyContainer extends Container
{
public function getFooService(): FooService
{
return new FooService();
}
#[Tag('my_tag')]
public function getBarService(): BarService
{
return new BarService($this->get('foo_service'));
}
}
use PSX\Dependency\Inspector\ContainerInspector;
use PSX\Dependency\TypeResolver;
use PSX\Dependency\AutowireResolver;
use PSX\Dependency\Tests\Playground\MyContainer;
use PSX\Dependency\Tests\Playground\AutowireService;
$container = new MyContainer();
$inspector = new ContainerInspector($container);
$typeResolver = new TypeResolver($container, $inspector);
$autowireResolver = new AutowireResolver($typeResolver);
$service = $autowireResolver->getObject(AutowireService::class);
use Psr\Container\ContainerInterface;
use PSX\Dependency\TypeResolver;
use PSX\Dependency\AutowireResolver;
use PSX\Dependency\Tests\Playground\RepositoryInterface;
$typeResolver = new TypeResolver(...);
$autowireResolver = new AutowireResolver($typeResolver);
$typeResolver->addFactoryResolver(RepositoryInterface::class, function (string $class, ContainerInterface $container): RepositoryInterface {
return $container->get('table_manager')->getRepository($class);
});
// this now allows to use the MyRepository class as a type-hint at a service and
// the autowire resolver injects the fitting service through the defined resolver
$repository = $autowireResolver->getObject(MyService::class);
use PSX\Dependency\Inspector\ContainerInspector;
use PSX\Dependency\TagResolver;
use PSX\Dependency\Tests\Playground\MyContainer;
$container = new MyContainer();
$inspector = new ContainerInspector($container);
$tagResolver = new TagResolver($container, $inspector);
$services = $tagResolver->getServicesByTag('my_tag');
use PSX\Dependency\Compiler\PhpCompiler;
use PSX\Dependency\Tests\Playground\MyContainer;
$compiler = new PhpCompiler('Container', __NAMESPACE__);
$container = new MyContainer();
// contains the compiled DI container
$code = $compiler->compile($container);
class MyController
{
#[Inject]
protected FooService $fooService;
#[Inject('bar_service')]
protected BarService $baz;
public function doSomething()
{
$this->fooService->power();
}
}
use PSX\Dependency\ObjectBuilder;
use PSX\Dependency\Tests\Playground\MyContainer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
$container = new MyContainer();
$cache = new Pool(new ArrayCache());
$debug = false;
$builder = new ObjectBuilder(
$container,
$reader,
$cache,
$debug
);
$controller = $builder->getObject(MyController::class);
use Psr\Container\ContainerInterface;
$container = new \PSX\Dependency\Container();
$container->set('foo_service', function(ContainerInterface $c){
return new FooService();
});
$container->set('bar_service', function(ContainerInterface $c){
return new BarService($c->get('foo_service'));
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.