/** * {@inheritdoc} */ public function register() { $dic = $this->app->getContainer(); $dic->define('Autarky\\Logging\\ChannelManager', function () { return new ChannelManager(); }); $dic->share('Autarky\\Logging\\ChannelManager'); $factory = new Definition(['Autarky\\Logging\\ChannelManager', 'getChannel']); $factory->addScalarArgument('$channel', 'string', false, null); $dic->define('Psr\\Log\\LoggerInterface', $factory); }
/** * {@inheritdoc} */ public function register() { $dic = $this->app->getContainer(); $dic->share('Autarky\\Database\\PDOInstantiator'); $dic->alias('Autarky\\Database\\PDOInstantiator', 'Autarky\\Database\\PDOInstantiatorInterface'); $dic->share('Autarky\\Database\\ConnectionFactory'); $dic->alias('Autarky\\Database\\ConnectionFactory', 'Autarky\\Database\\ConnectionFactoryInterface'); $dic->define('Autarky\\Database\\ConnectionManager', function (ContainerInterface $dic) { return new ConnectionManager($this->app->getConfig(), $dic->resolve('Autarky\\Database\\ConnectionFactoryInterface')); }); $dic->share('Autarky\\Database\\ConnectionManager'); $factory = new Definition(['Autarky\\Database\\ConnectionManager', 'getPdo']); $factory->addScalarArgument('$connection', 'string', false, null); $dic->define('PDO', $factory); }
/** * Add arguments to an existing factory via reflection. * * @param Definition $factory * @param ReflectionFunctionAbstract|null $reflectionFunction Optional */ protected static function addReflectionArguments(Definition $factory, ReflectionFunctionAbstract $reflectionFunction = null) { if (!$reflectionFunction) { $callable = $factory->getCallable(); if (is_array($callable)) { $reflectionFunction = new ReflectionMethod($callable[0], $callable[1]); } else { $reflectionFunction = new ReflectionFunction($callable); } } foreach ($reflectionFunction->getParameters() as $arg) { try { $name = $arg->getName(); $required = !$arg->isOptional(); if ($argClass = $arg->getClass()) { $factory->addClassArgument($name, $argClass->getName(), $required); } else { $default = $required ? null : $arg->getDefaultValue(); $factory->addScalarArgument($name, null, $required, $default); } } catch (ReflectionException $re) { throw UnresolvableArgumentException::fromReflectionParam($arg, $reflectionFunction, $re); } } }