1. Go to this page and download the library: Download slava-basko/container 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/ */
slava-basko / container example snippets
$container = new Container();
$container['cache-file-path'] = '/tmp/app.cache';
$container[CacheStorage::class] = fn (Container $c) => new CacheStorage($c['cache-file-path']);
$container[Cache::class] = fn(Container $c) => new Cache($c[CacheStorage::class]);
$cache = $container[Cache::class];
$cache->get('currency-rates');
$container = new Container();
$container['log-file-path'] = 'your.log';
$container->add('file-handler', fn (Container $c) => new StreamHandler($c['log-file-path']), ['handler']);
$container->add('stdout-handler', fn (Container $c) => new StreamHandler('php://output'), ['handler']);
$container[Logger::class] = function (Container $c) {
$logger = new Logger('app');
$logger->setHandlers($c->getByTag('handler'));
return $logger;
};
$logger = $container[Logger::class];
$logger->warning('Oops!');
class GameController extends Controller {
public function getPotion(string $potionType)
{
try {
$potionService = $this->container->getByTag($potionType)[0];
return new Response($potionService->getPotion());
} catch (NotFoundException) {
return new Response('Unknown potion type: ' . $potionType, 400);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
return new Response('Sorry, internal error', 500);
}
}
}
$container = new Container();
$container->addShared(MySQLConnection::class, fn (Container $c) => new MySQLConnection());
$conn1 = $container->get(MySQLConnection::class);
$conn2 = $container->get(MySQLConnection::class);
assert(spl_object_hash($conn1) === spl_object_hash($conn2));
$container = new Container();
$container[\Logger::class] = fn () => new \Logger();
$container[\SomeRepository::class] = fn () => new \SomeRepository();
$container->extend(\BaseRepository::class, function (\BaseRepository $repository, Container $c) {
$repository->setLogger($c[\Logger::class]);
return $repository;
});
$repository = $container[\SomeRepository::class];
// From now on, every repository returned by the container and which `extends` BaseRepository has a Logger inside.
$container = new Container();
$container[A::class] = fn (Container $c) => new \A($c[B::class]);
$container[B::class] = fn (Container $c) => new \B($c[A::class]);
$a = $container[\A::class];
// CircularDependencyException: Circular dependency detected: A -> B -> A
class FilesystemAdapter {
// some logic inside
}
class Filesystem {
/**
* @var \FilesystemAdapter
*/
private $adapter;
public function __construct(FilesystemAdapter $adapter)
{
$this->adapter = $adapter;
}
// use $this->adapter as you need
}
$container = new AutowireContainer();
$filesystem = $container[Filesystem::class]
// $filesystem will be an instance of Filesystem that contains FilesystemAdapter inside
$container = new Container();
$container['k'] = 'v1';
$container['k'] = 'v2'; // RewriteAttemptException: The resource 'k' already defined.
class LoggerServiceProvider {
public function __invoke(Container $container): void
{
// Define your handlers, formatters, processors, and logger itself.
// All Logger-related things are in one place.
}
}
$container = new Container();
$container->add(MasterMySqlClient::class, fn (Container $c) => new MasterMySqlClient($c['mysql-dsn']));
$container->add(SlaveMySqlClient::class, fn (Container $c) => new SlaveMySqlClient($c['mysql-dsn']));
$container->symlink(MasterMySqlClient::class, MySqlInterface::class);
$container->get(MySqlInterface::class); // MasterMySqlClient
$container->symlink(SlaveMySqlClient::class, MySqlInterface::class);
$container->get(MySqlInterface::class); // SlaveMySqlClient
$container = new Container();
// Add services and define dependencies
$graph = new Graphviz($container);
$graph->build();
// Define services, dependencies, etc.
$container = new Container();
// Wrap it.
$psrContainer = new PsrContainer($container);
$service = $psrContainer->get(Service::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.