/** * @param ContainerInterface $app * @throws \InvalidArgumentException */ public function register(ContainerInterface $app) { $app->singleton([Logger::class => 'Logger'], function () use($app) { $logger = new Logger($app['Config']['logger.name']); if (!$app['Config']->offsetExists('logger.handler')) { throw new \InvalidArgumentException('expected at least one logger handler in array(class, array config) format'); } /** * register logger handler */ foreach ($app['Config']['logger.handler'] as $class => $config) { $handler = (new \ReflectionClass($class))->newInstanceArgs($config['args']); if (!$handler instanceof HandlerInterface) { throw new \InvalidArgumentException(sprintf('logger handler must implement %s', HandlerInterface::class)); } $formatter = (new \ReflectionClass(key($config['formatter'])))->newInstance($config['formatter'][key($config['formatter'])]); if (!$formatter instanceof FormatterInterface) { throw new InvalidArgumentException(sprintf('logger formatter must implement %s', FormatterInterface::class)); } $handler->setFormatter($formatter); $logger->pushHandler($handler); } return $logger; }); $app->bind(LoggerInterface::class, 'Logger'); }
private function registerEventDispatcherProvider() { $this->app->singleton([EventDispatcher::class => 'EventDispatcher'], function () { return new EventDispatcher(); }); $this->app->bind(EventDispatcherInterface::class, 'EventDispatcher'); }
/** * @param ContainerInterface $app */ public function register(ContainerInterface $app) { $app->singleton([RouteParser::class => 'RouteParser']); $app->singleton([DataGenerator::class => 'DataGenerator']); $app->singleton([RouteCollector::class => 'RouteCollector']); /** * contextual binding */ $app->bind(RouteParserInterface::class, 'RouteParser'); $app->bind(DataGeneratorInterface::class, 'DataGenerator'); /** * register routing dispatcher */ $app->singleton([RouteDispatcher::class => 'RouteDispatcher'], function () use($app) { return new RouteDispatcher($app['RouteCollector']->getData()); }); }
/** * @param ContainerInterface $app */ public function register(ContainerInterface $app) { /** * swoole server */ $app->singleton('Server', function () use($app) { return new Server($app); }); /** * swoole server request handler */ $app->singleton('ServerRequestHandler', function () use($app) { return new ServerRequestHandler($app, $app['EventDispatcher']); }); /** * swoole server responder */ $app->singleton('ServerResponder', function () use($app) { return new ServerResponder($app); }); $app->bind(ServerInterface::class, 'Server'); $app->bind(ServerRequestHandlerInterface::class, 'ServerRequestHandler'); $app->bind(ServerResponderInterface::class, 'ServerResponder'); }
public function it_should_able_bind_object_to_container(ContainerInterface $container) { $container->bind('test', 'test')->shouldBeCalled(); $this->bind('test', 'test'); }
/** * {@inheritdoc} */ public function bind($abstract, $concrete = null) { $this->container->bind($abstract, $concrete); }