GVKun编程网logo

php – 如何将内存中的用户提供程序注入服务?(php内存管理机制)

13

对于想了解php–如何将内存中的用户提供程序注入服务?的读者,本文将提供新的信息,我们将详细介绍php内存管理机制,并且为您提供关于5.0.0迁移后,使用提供程序的多提供程序失败、Angular9-如

对于想了解php – 如何将内存中的用户提供程序注入服务?的读者,本文将提供新的信息,我们将详细介绍php内存管理机制,并且为您提供关于5.0.0 迁移后,使用提供程序的多提供程序失败、Angular 9-如何将动态参数注入服务构造函数、angular – 如何在导出的函数中注入服务?、Angularjs – 如何从不依赖的另一个模块更正注入服务?的有价值信息。

本文目录一览:

php – 如何将内存中的用户提供程序注入服务?(php内存管理机制)

php – 如何将内存中的用户提供程序注入服务?(php内存管理机制)

我正在关注 http://symfony.com/doc/current/cookbook/security/voters.html并尝试制作一个自定义投票人,拒绝访问不包含有效API密钥的请求和标题中的摘要(受 http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html影响 – 我没有建立身份验证提供程序,因为我需要使用FOSUserBundle提供者为同一请求).

我想将我的api密钥/秘密存储在内存中的用户提供程序中,并且可能会在以后将其迁移到自定义mongodb提供程序.所以我需要一种方法将用户提供者注入我的选民.我已经注入了服务容器,但可以从中访问用户提供程序吗?

我的服务定义:

services:
    security.access.api_client_Voter:
        class:     Acme\RestBundle\Security\Authorization\Voter\apiclientVoter
        arguments: [@service_container,%kernel.cache_dir%/security/nonces]
        public:    false
        tags:
            - { name: monolog.logger,channel: authentication }
            - { name: security.Voter }

所以我的问题是,我如何注入内存提供商?食谱中的WSSE示例似乎使用auth提供程序工厂来替换字符串’security.providers.in_memory’,但由于我只是使用选民,这是必要的吗?如果有必要,我的工厂会是什么样子?

内存中的用户提供程序首先在SecurityBundle的 security.xml中定义为抽象服务.然后,SecurityBundle的 SecurityExtension将根据您的用户提供程序在security.yml中的配置创建具体的内存中用户提供程序服务,每个用户都将其添加为一项服务.如您所见,此服务的名称为security.user.provider.concrete.[防火墙名称].因此,应该可以使用此服务或将此服务注入您的选民.您始终可以查看/ app / cache目录中的转储服务容器,以查找服务的名称以及是否已定义它们.

5.0.0 迁移后,使用提供程序的多提供程序失败

5.0.0 迁移后,使用提供程序的多提供程序失败

如何解决5.0.0 迁移后,使用提供程序的多提供程序失败?

我尝试迁移到 provider: ^5.0.0 并停止工作,对之前工作的内容进行采样。

  @override
  Widget build(BuildContext context) {
    WidgetsFlutterBinding.ensureInitialized();
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<ConfigurationProvider>(create: (context) => ConfigurationProvider(preferences: widget.preloadedFs)),ChangeNotifierProvider<ManagerProvider>(create: (context) => ManagerProvider()),],child: Builder(builder: (context) {
        ConfigurationProvider config = Provider.of<ConfigurationProvider>(context);
        ThemeData customTheme;
        ThemeData darkTheme;

        darkTheme = config.blackThemeEnabled ? AppTheme.black(config.accentColor) : AppTheme.dark(config.accentColor);
        customTheme = config.darkThemeEnabled ? darkTheme : AppTheme.white(config.accentColor);
        List<Locale> supportedLocales = [];
        supportedLanguages.forEach((element) => supportedLocales.add(Locale(element.languageCode,'''')));

        return MaterialApp(
          locale: _locale,supportedLocales: supportedLocales,localizationsDelegates: [
            FallbackLocalizationDelegate(),AppLocalizationsDelegate(),GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,GlobalCupertinoLocalizations.delegate,localeResolutionCallback: (locale,supportedLocales) {
            for (var supportedLocale in supportedLocales) {
              if (supportedLocale?.languageCode == locale?.languageCode && supportedLocale?.countryCode == locale?.countryCode) {
                return supportedLocale;
              }
            }
            return supportedLocales?.first;
          },builder: (context,child) {
            return ScrollConfiguration(
              behavior: CustomScrollBehavior(),child: child,);
          },);
      }),);
  }

错误日志

======== Exception caught by widgets library =======================================================
The following ArgumentError was thrown building Builder(dirty,dependencies: [_InheritedProviderScope<ConfigurationProvider>]):
Invalid argument(s) (value): Must not be null

The relevant error-causing widget was: 
  Builder file:///home/sony/Flutter-sdk/Flutter-app/demo-Flutter/lib/main.dart:109:14
When the exception was thrown,this was the stack: 
#0      ArgumentError.checkNotNull (dart:core/errors.dart:185:27)
#1      SharedPreferences._setValue (package:shared_preferences/shared_preferences.dart:146:19)
#2      SharedPreferences.setString (package:shared_preferences/shared_preferences.dart:132:7)
#3      LegacyPreferences.saveAudioDownloadpath (package:demo/internal/legacyPreferences.dart:127:11)
#4      ConfigurationProvider.audioDownloadpath= (package:demo/provider/configurationProvider.dart:165:17)
...
====================================================================================================

======== Exception caught by widgets library =======================================================
The following StateError was thrown building Builder(dirty,dependencies: [_InheritedProviderScope<ConfigurationProvider>]):
Bad state: Tried to read a provider that threw during the creation of its value.
The exception occurred during the creation of type ConfigurationProvider.

The relevant error-causing widget was: 
  Builder file:///home/sony/Flutter-sdk/Flutter-app/demo-Flutter/lib/main.dart:109:14
When the exception was thrown,this was the stack: 
#0      _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:661:7)
#1      _InheritedProviderScopeElement.value (package:provider/src/inherited_provider.dart:544:33)
#2      Provider.of (package:provider/src/provider.dart:289:29)
#3      _MainState.build.<anonymous closure> (package:demo/main.dart:110:49)
#4      Builder.build (package:Flutter/src/widgets/basic.dart:7798:48)
...
====================================================================================================

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Angular 9-如何将动态参数注入服务构造函数

Angular 9-如何将动态参数注入服务构造函数

我不知道您在哪里获得动态ID,但实际上您可以将它们放在provider数组中,并像注入令牌一样使用依赖项注入。如果可以为课程ID创建工厂方法

服务

export class Item3Service {

  constructor(
    @inject(LOCALE_ID) private locale: string) {}

}

app.moudle.ts

@NgModule({
  providers: [
    { provide: LOCALE_ID,useFactory: () => window.navigator.language}
  ]
})

编辑

由于ID是您路线的一部分,因此我会这样做

组件

import { Component,OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MyServiceService } from '../my-service.service';

@Component({
  selector: 'app-routed',templateUrl: './routed.component.html',styleUrls: ['./routed.component.scss']
})
export class RoutedComponent implements OnInit {

  constructor(private route: Router,private myService: MyServiceService) { }

  ngOnInit(): void {
    this.myService.setUrl(this.route.url)
  }

}

服务

import { Injectable } from '@angular/core';
import { ReplaySubject,Observable } from 'rxjs';
import { share,switchMap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class MyServiceService {
  private _url$: ReplaySubject<string> = new ReplaySubject<string>(1);

  private _mydata$: Observable<string>;
  get myData$() { return this._mydata$.pipe(share()); }


  constructor() {
    this._mydata$ = this._url$.pipe(
      switchMap(url => {
        const parsedUrl = this.parseUrl(url);
        return this.callBackend(parsedUrl)
      })
    )
  }

  setUrl(url: string) {
    this._url$.next(url);
  }

  private callBackend(parsedUrl): Observable<string> {
    // call backend 
  }

  private parseUrl(url: string): number[] {
    // parse ids
  }
}

,

让服务成为无状态的服务更好,它可以降低应用程序的复杂性并为您省去一些问题和调试工作,对于您而言,这是没有必要的,因为您始终可以获取 item1Id item2Id ,因此,让激活的路由保存应用程序的状态(在这种情况下,状态就是选择的Item1Id和Item2Id),并创建可从任何地方调用的无状态服务拥有Item API的逻辑。

这就是我设想您的服务的方式(请记住,这只是一个示例,因为我不完全了解您的语义和用例)

ItemService

export class ItemService {
  constructor(private http: HttpClient) {}

  getList(item1Id: string,item2Id: string) {
    /* Call to Get List endpoint with Item1Id and Item2Id */
  }

  getDetails(item1: string,item2: string,item3: string) {
    /* Call to Get Details endpoint with Item1Id and Item2Id and Item3Id */
  }
}

然后,只要您可以访问 ActivatedRouteSnapshot ActivatedRoute

,就可以在任何地方使用此服务

在解析器中用于路线item1 /:item1Id / item2 /:item2Id的示例

export class ItemResolver implements Resolve<any> {
  constructor(private itemService: ItemService) {}

  resolve(
    route: ActivatedRouteSnapshot,state: RouterStateSnapshot
  ): Observable<any> {
    return this.itemService.getList(route.params['item1Id'],route.params['item2Id']);
  }
}

示例在组件中用于路线item1 /:item1Id / item2 /:item2Id以获得第3项详细信息

export class HelloComponent  {

  constructor(private route: ActivatedRoute,private itemService: ItemService) {}

  getDetails(item3Id) {
    this.route.params.pipe(
      take(1),map(({ item1Id,item2Id }) => {
        console.log(this.itemService.getDetails(item1Id,item2Id,item3Id))
      })
    ).subscribe();
  }
}

以下是正在工作的StackBlitz,它演示了此内容:https://stackblitz.com/edit/angular-ivy-h4nszy

您应该很少使用有状态的服务(除非确实有必要,即使在这种情况下,我建议使用 ngrx 库之类的东西来管理您的状态),尽管如此,您确实不知道不必将参数传递给服务的构造函数,则应使其保持无状态并将参数传递给方法。

angular – 如何在导出的函数中注入服务?

angular – 如何在导出的函数中注入服务?

我写了一个’popping’消息( android toast like)组件.所有其他组件都将其作为兄弟,并通过共享服务访问它.现在我想从实用程序函数中使用它,就像这样:

export function handleError(errorResp: Response | any): Observable<string> {
    ....
    // here I would like to display the message
    return Observable.throw(errMsg);
}

我以为我可以将消息服务作为参数传递给handleError,但我觉得它不是DRY,因为我需要从每个组件事件中创建它,尽管组件不需要它用于其他目的.你能给我一些指导吗?

解决方法

由于此功能在Angular应用程序之外,因此没有太多选择.

>通过参数传递,或>直接导入服务(通过Javascript导入,因为Angular的服务只是一个普通的Javascript类),然后使用它.请注意,它可能不起作用,具体取决于实例化服务所需的要求(如果它使用Angular的某些功能,或使用DI注入其他服务),或者>您可以将handleError函数放在另一个可以通过依赖注入获取Toast内容的服务中.

Angularjs – 如何从不依赖的另一个模块更正注入服务?

Angularjs – 如何从不依赖的另一个模块更正注入服务?

我不明白如何工作模块化依赖.

我有4个模块,它们依赖于彼此,如图所示.

“App”模块包括“module1”和“module2”.
“module2”包含“核心”模块. There are source on plunker.

如果从模块核心到模块“module1”的注入服务工作正常.但“核心”模块不在模块“module1”中.为什么会发生

由于您的App模块依赖于Core模块(间接通过模块2),Core模块中的服务可以在App模块(包括模块1)的任何位置使用.

这是因为Angular将首先加载所有模块,然后开始实例化其组件并解决注入的依赖关系.

但是,如果您确实需要模块1中的Core服务,那么您应该使其依赖于Core模块.这样,您的应用程序不会破坏,如果模块2在以后修改(或完全删除),您的模块1将更加自包含和可重用(例如,您可以使用不同的应用程序不依赖于核心模块).

一般来说,你不应该依靠“间接”依赖.每个模块都应该显式地声明它的依赖关系.
角度足够聪明,只有加载模块,如果它还没有加载,所以没有开销.

引用开发者指南section on modules:

Modules can list other modules as their dependencies. Depending on a module implies that required module needs to be loaded before the requiring module is loaded. In other words the configuration blocks of the required modules execute before the configuration blocks of the requiring module. The same is true for the run blocks. Each module can only be loaded once,even if multiple other modules require it.

(强调我的)

我们今天的关于php – 如何将内存中的用户提供程序注入服务?php内存管理机制的分享就到这里,谢谢您的阅读,如果想了解更多关于5.0.0 迁移后,使用提供程序的多提供程序失败、Angular 9-如何将动态参数注入服务构造函数、angular – 如何在导出的函数中注入服务?、Angularjs – 如何从不依赖的另一个模块更正注入服务?的相关信息,可以在本站进行搜索。

本文标签: