Multiple services of type… pomenovanie zavislosti nefunguje
- japlavaren
- Člen | 404
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
- japlavaren
- Člen | 404
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.
- japlavaren
- Člen | 404
takto to funguje:
-
implement: App\Component\PersonForm\IPersonFormComponentFactory
arguments: [..., ..., @personFormFactory]
Dakujem za pomoc