Usage¶
This is a quite simple library but it tries to be as flexible as possible, so there are multiple ways to use it.
Simple example¶
The simplest way to use it is with SphinxInventoryParser::parseFromDoc()
that creates a SphinxInventory
object directly from an online documentation,
based on its URL (and an optional inventory path).
use Club1\SphinxInventoryParser\SphinxInventoryParser;
$inventory = SphinxInventoryParser::parseFromDoc('https://club1.fr/docs/fr/');
Using an existing stream¶
Another way is to create a new SphinxInventoryParser
object
with a readable resource, typically obtained with fopen()
with r
mode,
then call its parse()
method
that will return a SphinxInventory
object.
use Club1\SphinxInventoryParser\SphinxInventoryParser;
$stream = fopen('file:///tmp/objects.inv', 'r');
$parser = new SphinxInventoryParser($stream);
$inventory = $parser->parse('https://club1.fr/docs/fr/');
fclose($stream);
Parse objects one by one¶
When dealing with a large amount of data, it could be interesting to parse
the objects one by one, instead of storing them all in memory.
This can be achieved by calling parseHeader()
and parseObjects()
directly:
use Club1\SphinxInventoryParser\SphinxInventoryParser;
$stream = fopen('https://club1.fr/docs/fr/objects.inv', 'r');
$parser = new SphinxInventoryParser($stream);
$header = $parser->parseHeader();
$objects = $parser->parseObjects($header, 'https://club1.fr/docs/fr/');
foreach($objects as $object) {
// do something with $object
}
// close only after iterating
fclose($stream);
Warning
SphinxInventoryParser::parseObjects()
is a generator,
so its execution is postponed until iteration over its result begins,
you must thus not close the stream before the actual iteration.