1. Go to this page and download the library: Download talesoft/tale-di 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/ */
talesoft / tale-di example snippets
$cachePool = new RedisCachePool();
$builder = new ContainerBuilder($cachePool);
$builder->add(ViewRenderer::class);
$builder->add(ControllerDispatcher::class);
$builder->addInstance(new PDO(...));
$container = $builder->build();
$pdo = $container->get(PDO::class);
$builder->addLocator(
new FileServiceLocator('src/Classes/MyClass.php')
);
$builder->addLocator(
new DirectoryServiceLocator('../src')
);
$builder->addLocator(
new GlobServiceLocator('../src/{Controller,Model}/**/*.php')
);
$container = $builder->build();
class OrderProvider
{
public function __construct(OrderRepository $repository)
}
$orderProvider = $container->get(OrderProvider::class);
interface ViewRendererInterface
{
}
class ViewRenderer implements ViewRendererInterface
{
}
//...
$renderer = $container->get(ViewRendererInterface::class);
// $renderer is instanceof ViewRenderer
class AvatarGenerator
{
/**
* @var CacheInterface
*/
private $cache;
public function __construct(CacheInterface $cache = null)
{
$this->cache = $cache ?? new RuntimeCache();
}
}
class EntityManager
{
public function __construct(DbalInterface $dbal)
}
class Importer
{
/**
* @param iterable<\App\Service\Importer\WorkerInterface>
*/
public function __construct(iterable $workers)
{
foreach ($workers as $worker) {
$this->initializeWorker($worker);
}
}
/**
* @param array<\App\Service\Importer\WorkerInterface>
*/
public function __construct(array $workers)
{
$this->workers = array_filter($workers, fn($worker) => $worker->canImport());
}
}
$builder->setParameter('someParameter', 'some value');
//Any class with a parameter 'someParameter' will get 'some value' injected as a string
class UserManager
{
public function __construct(PDO $pdo)
{
$this->pdo = $pdo; // Fully working PDO instance!
}
}
$builder->setParameters([
// These are the parameter names PDO internally uses in its constructor!
'dsn' => 'mysql:host=localhost',
'username' => 'root',
'passwd' => '',
'options' => [PDO::ATTR_ERRMODE => PDO_ERRMODE_EXCEPTION]
], PDO::class);
$builder->add(UserManager::class);
$container = $builder->build();
$userManager = $container->get(UserManager::class);
// PDO will be fully wired in UserManager
$dependencies = [
//Value Dependency is just a value. Can be any kind of value.
'test value' => new ValueDependency('some value'),
//A reference dependency references another value in the container
'reference' => new ReferenceDependency('test value'),
//A callback dependency only gets resolved when it's requested
'factory' => new CallbackDependency(function (ContainerInterface $container) {
return new Something($container->get(SomethingElse::class));
}),
//Same as CallbackDependency, but will cache the result between each ->get() call
'lazy factory' => new PersistentCallbackDependency(function () {
return (new SomeHeavyWorker())->getResult();
})
];
$container = new Container($dependencies);
$container->get('test value'); //"some value"
$container->get('reference'); //"some value"
// etc.
final class PdoDependency implements DependencyInterface
{
private $pdo;
public function __construct()
{
$this->pdo = new PDO(...);
}
public function get(ContainerInterface $container)
{
return $this->pdo;
}
}
$container = new Container(['pdo' => new PdoDependency()]);
$pdo = $container->get('pdo');
$stmt = $pdo->prepare(...);
$container = new ArrayContainer([
SomeClass::class => new SomeClass(),
'test key' => 15
]);
$container->get(SomeClass::class); //SomeClass instance
$container->get('test key'); //15
final class AdapterFactory
{
public function __construct(ContainerInterface $container = null)
{
$this->container = $container ?? new NullContainer();
}
public function createAdapter(): AdapterInterface
{
$adapter = null;
if ($this->container->has(SomeAdapter::class)) {
$adapter = $this->container->get(SomeAdapter::class);
} else if ($this->container->has(SomeOtherAdapter::class)) {
$adapter = $this->container->get(SomeOtherAdapter::class);
} else {
$adapter = new SomeDefaultAdapter();
}
return $adapter;
}
}