/** * {@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); }
protected function resolveScalarArg(ContainerInterface $container, ScalarArgument $arg, array $params) { $name = $arg->getName(); if ($params && array_key_exists($name, $params)) { return $params[$name]; } if (!$arg->isRequired()) { return $arg->getDefault(); } $pos = $arg->getPosition() + 1; $defName = $this->definition->getName(); $message = "Unresolvable argument: Argument #{$pos} ({$name}) of {$defName}" . ' - Argument is required and has no value'; throw new UnresolvableArgumentException($message); }
/** * Get the existing factory for a class. If a factory is not already defined * a default one will be created via reflection. * * @param string $class Name of the class * @param array $params Optional * * @return FactoryInterface */ public function getFactory($class, array $params = array()) { if (!isset($this->factories[$class]) && $this->autowire) { $this->factories[$class] = Definition::getDefaultForClass($class); } $factory = $this->factories[$class]; // if $params is defined, we need to either make a copy of the existing // Factory or make the Definition create a new factory with the params if ($params) { $factory = $factory->getFactory($params); } return $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); } } }