本文整理匯總了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);
}