In feed-io 2.2, I introduced a Factory class built to get a FeedIo instance in one single line of code :
$feedIo = \FeedIo\Factory::create()->getFeedIo();
Obviously this works assuming that you installed feed-io using Composer and you have included vendor/autoload.php before.
Factory’s main method : Factory::create()
The factory comes with the ability to configure feed-io before getting its main class. For that, you will provide one or two configuration arrays depending on which dependency you want to configure.
/**
* @param array $loggerConfig
* @param array $clientConfig
* @return \FeedIo\Factory
*/
static public function create(
array $loggerConfig = [
'builder' => 'NullLogger',
'config' => [],
],
array $clientConfig = [
'builder' => 'GuzzleClient',
'config' => [],
]
)
{
$factory = new static();
$factory
->setClientBuilder(
$factory->getBuilder(
$clientConfig['builder'],
$factory->extractConfig($clientConfig)
)
)
->setLoggerBuilder(
$factory->getBuilder(
$loggerConfig['builder'],
$factory->extractConfig($loggerConfig)
)
);
return $factory;
}
As you can see the first array is the logger’s configuration and the second one is the HTTP client’s. You can also notice that the default client builder relies on Guzzle, that’s why it is now a feed-io’s requirement and no longer a suggestion. Default logger’s configuration is to log nothing.
Factory::create() uses each array the same way :
- $config[‘builder’] is used to get the accurate Builder to use
- $config[‘config’] is the actual configuration passed to the Builder
The Builder can be a class provided with feed-io or one of your own. Native Builders are :
- GuzzleClient
- NullLogger
- Monolog
The first is the HTTP client and the two others are for logs.
Builders
Each Builder class is written to provide a properly configured dependency and must implement the \FeedIo\Factory \BuilderInterface. It also must implement LoggerBuilderInterface or ClientBuilderInterface whether the Builder builds a logger or a HTTP client.
If you want to use your own Builder you will have to specify its full name in the configuration, whereas built-in Builders can be called using their short name. Let’s say you want to use Monolog and your own HTTP Client :
$feedIo = \FeedIo\Factory::create(
// this builder is internal, no need to call it with its namespace
['builder' => 'Monolog'],
// Builder external to feed-io, use its namespace
['builder' => '\Acme\Builder\HttpClientBuilder']
)
->getFeedIo();
You can configure Monolog through the ‘handlers’ offset. MonologBuilder will create one handler for each entry in this array :
$feedIo = \FeedIo\Factory::create(
[
'builder' => 'Monolog',
'config' => [
'handlers' => [
'class' => 'Monolog\Handler\StreamHandler',
'params' => ['/var/app/feeds.log', Monolog\Logger::DEBUG],
]
]
])
->getFeedIo();
Of course, I highly encourage you to have a look at the Monolog’s documentation to see how to configure handlers.
If you have any questions on how to use feed-io, you’re welcome to submit an issue on github.