Код: Выделить всё
class WebhookHandlerFactory
{ // $config['client'] is hardcoded in the .env file in this case and then put into ConfigProvider
protected ContainerInterface $container;
public function __invoke(ContainerInterface $container): RequestHandlerInterface
{
$this->container = $container;
$config = $this->getConfig();
$client = (new SomeClient($config['client']))->getClient();
$controller = (new SomeControllerFactory)($container);
return new WebhookHandler($controller, $config, $client);
}
protected function getConfig(): ArrayObject
{
$config = $this->container->get('config');
// here I use my hardcoded data to find something in the DB and then put it into $config
return $config;
}
Единственный способ, который я нашел, — заставить эту фабрику возвращать другую фабрику, которая инициализируется. класс-обработчик в __construct, а затем вызывает метод handle следующим образом:
Код: Выделить всё
public function __construct(
ContainerInterface $container,
CreateController $controller,
ArrayObject $config
){
$this->container = $container;
$this->createController = $createController;
$this->config = $config;
$config = $this->getConfig();
$client = (new SomeClient($config['client']))->getClient();
$this->handler = new WebhookHandler($this->controller, $config, $client);
}
public function handle(ServerRequestInterface $request)
{
return $this->handler->handle($request);
}
Код: Выделить всё
return static function (Application $app): void
{
$app->post('/', App\Handler\WebhookHandlerFactory::class);
Подробнее здесь: https://stackoverflow.com/questions/791 ... nitialized
Мобильная версия