In general, there are clients to connect to the API, for each of these clients there is an adapter:
<?php namespace App\Weather\Adapters; use App\Weather\Clients\ClientOpenWeather; use App\Weather\Translators\TranslatorOpenWeather; class ClientOpenWeatherAdapter implements WeatherClientAdapterInterface { private $client; public function __construct(ClientOpenWeather $client) { $this->client = $client; } public function getWeather(float $lat,float $lon) { //Получение данных о погоде от API $dataWeather = $this->client->getWeather($lat, $lon); if ($dataWeather === null) { return null; } //Трансляция данных из массива в WeatherDTO return TranslatorOpenWeather::translate($dataWeather); } } In the controller, I want to access the array (collection, any other structure that allows to iterate) of adapter objects: In approximately the following way:
foreach($apiAdapters as $apiAdapter){ $apiAdapter->getWeather($lat, $lon); } The problem is how to " register " such a data container in laravel. So that new adapters can be added to it by the way that provides , aliases in config/app.php :
Perhaps there is some solution to this problem since at the moment you have to use the following sad code:
... public function addWeather( Request $request, ClientOpenWeatherAdapter $clientOpenWeatherAdapter, ClientYandexWeatherAdapter $clientYandexAdapter ) { ... $clientOpenWeatherAdapter->getWeather($lat, $lon); $clientYandexAdapter->getWeather($lat, $lon); ... } Perhaps the question is too trivial, but attempts to implement it all or to find something in Google did not lead to success.