Multiple services of type… pomenovanie zavislosti nefunguje

japlavaren
Člen | 404
+
0
-

ahoj,

mam 2 triedy, ktore vytvaraju formular:

<?php

class PersonFormFactory
{
	public function create()
    {
        $form = new Form();
		// ...
		return $form;
	}
}

class PersonRegistrationFormFactory extends PersonFormFactory
{
	public function create()
    {
        $form = parent::create();
		// ...
		return $form;
	}
}
?>

komponenty kde kazda chce dostat svoj formular:

<?php
class PersonFormComponent extends Control
{
	public function __construct(PersonFormFactory $personFormFactory)
	{
	}
}

class PersonRegistrationComponent extends Control
{
	public function __construct(PersonRegistrationFormFactory $personRegistrationFormFactory)
	{
	}
}
?>

interface na tovarnicku komponent:

<?php
interface IPersonFormComponentFactory
{
    public function create(): PersonFormComponent;
}

interface IPersonRegistrationComponentFactory
{
    public function create(): PersonRegistrationComponent;
}
?>

a vsetko zaregistrovane v neone:

services:
    personFormFactory: App\Form\PersonFormFactory
    personRegistrationFormFactory: App\Form\PersonRegistrationFormFactory
	- App\Component\PersonForm\IPersonFormComponentFactory()
	- App\Component\PersonRegistration\IPersonRegistrationComponentFactory

ked to spustim dostanem ocakavanu chybu:
Multiple services of type App\Form\PersonFormFactory found: personFormFactory, personRegistrationFormFactory
co je pochopitelne preco. tak som si do IPersonFormComponentFactory pridal pomenovanu zavislost ako:

services:
    personFormFactory: App\Form\PersonFormFactory
    personRegistrationFormFactory: App\Form\PersonRegistrationFormFactory
	- App\Component\PersonForm\IPersonFormComponentFactory(@personFormFactory)
	- App\Component\PersonRegistration\IPersonRegistrationComponentFactory

a teraz to zacina byt zaujimave. nette mi hlasi chybu:
Return value of class@anonymous::create() must be an instance of App\Component\PersonForm\PersonFormComponent, instance of App\Form\PersonFormFactory returned
a podivny kod DI kontajnera:

<?php
public function create(): App\Component\PersonForm\PersonFormComponent
{
	$service = $this->container->createServicePersonFormFactory();
	return $service;
}
?>

mam zly zapis, alebo je to bug? ako predam factory pomenovanu zavislost aby to fungovalo? skusal som este:

services:
    personFormFactory: App\Form\PersonFormFactory
    personRegistrationFormFactory: App\Form\PersonRegistrationFormFactory
	- App\Component\PersonForm\IPersonFormComponentFactory(..., @personFormFactory)
	- App\Component\PersonRegistration\IPersonRegistrationComponentFactory

ale zase dostavam chybu Multiple services of type App\Form\PersonFormFactory found…

Dakujem za pomoc

CZechBoY
Člen | 3608
+
+1
-

Musis vypnout autowiring pres autowired: no

japlavaren
Člen | 404
+
0
-

skusal som to ale mam problem ako to zapisat:

x:
	create: App\Component\PersonForm\IPersonFormComponentFactory(@personFormFactory)

hadze chybu Class IPersonFormComponentFactory used in service ‚x‘ not found.

David Matějka
Moderator | 6445
+
+1
-

misto factory pouzij implement

CZechBoY
Člen | 3608
+
0
-

btw nemusíš pojmenovávat tu službu, stačí použít pomlčku jako u služeb bez dalšího nastavení.

-
    implement: App\Component\PersonForm\IPersonFormComponentFactory(@personFormFactory)

Editoval CZechBoY (6. 9. 2018 22:35)

japlavaren
Člen | 404
+
0
-

takto to funguje:

-
         implement: App\Component\PersonForm\IPersonFormComponentFactory
         arguments: [..., ..., @personFormFactory]

Dakujem za pomoc