本文整理汇总了PHP中Interop\Container\ContainerInterface::setService方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerInterface::setService方法的具体用法?PHP ContainerInterface::setService怎么用?PHP ContainerInterface::setService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Interop\Container\ContainerInterface
的用法示例。
在下文中一共展示了ContainerInterface::setService方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
/**
* @internal
* @codeCoverageIgnore
*/
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null)
{
// Retreive database configuration from config file.
$config = $container->get('Library\\UserConfig')['database'];
$config['options']['buffer_results'] = true;
// Set charset to utf8mb4 for MySQL, utf8 for everything else.
if (stripos($config['driver'], 'mysql') === false) {
$config['charset'] = 'utf8';
} else {
$config['charset'] = 'utf8mb4';
}
$adapter = new \Zend\Db\Adapter\Adapter($config);
$database = \Nada\Factory::getDatabase($adapter);
if ($database->isSqlite()) {
$database->emulatedDatatypes = array('bool', 'date', 'decimal', 'timestamp');
} elseif ($database->isMySql()) {
$database->emulatedDatatypes = array('bool');
}
$database->setTimezone();
// Return requested service, store instance of other service first
if ($requestedName == 'Db') {
$container->setService('Database\\Nada', $database);
return $adapter;
} else {
$container->setService('Db', $adapter);
return $database;
}
}
示例2: marshalTranslatorFromConfig
/**
* Attempt to marshal a translator from configuration.
*
* Returns:
* - an MvcTranslator seeded with a DummyTranslator if "translator"
* configuration is available, and evaluates to boolean false.
* - an MvcTranslator seed with an I18nTranslator if "translator"
* configuration is available, and is a non-empty array or a Traversable
* instance.
* - null in all other cases, including absence of a configuration service.
*
* @param ContainerInterface $container
* @return null|MvcTranslator
*/
private function marshalTranslatorFromConfig(ContainerInterface $container)
{
if (!$container->has('config')) {
return;
}
$config = $container->get('config');
if (!array_key_exists('translator', $config)) {
return;
}
// 'translator' => false
if ($config['translator'] === false) {
return new MvcTranslator(new DummyTranslator());
}
// Empty translator configuration
if (is_array($config['translator']) && empty($config['translator'])) {
return;
}
// Unusable translator configuration
if (!is_array($config['translator']) && !$config['translator'] instanceof Traversable) {
return;
}
// Create translator from configuration
$i18nTranslator = I18nTranslator::factory($config['translator']);
// Inject plugins, if present
if ($container->has('TranslatorPluginManager')) {
$i18nTranslator->setPluginManager($container->get('TranslatorPluginManager'));
}
// Inject into service manager instances
if ($container instanceof ServiceManager) {
$container->setService(TranslatorInterface::class, $i18nTranslator);
}
return new MvcTranslator($i18nTranslator);
}