🏗️ Allow access to Config from Collectors
This commit is contained in:
parent
c8bbc35886
commit
48cc6e9f28
@ -14,6 +14,14 @@ abstract class Collector
|
|||||||
$this->pool = $pool;
|
$this->pool = $pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Shikiryu\WebGobbler\Config
|
||||||
|
*/
|
||||||
|
public function getConfig(): Config
|
||||||
|
{
|
||||||
|
return $this->pool->getConfig();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
@ -22,13 +22,13 @@ class YahooImageCollector extends Collector
|
|||||||
*/
|
*/
|
||||||
public function getRandomImage()
|
public function getRandomImage()
|
||||||
{
|
{
|
||||||
$word_to_search = $this->generateRandomWord();
|
$word_to_search = $this->getConfig()->get('collector.keywords.keywords', $this->generateRandomWord());
|
||||||
|
|
||||||
$client = new Client();
|
$client = new Client();
|
||||||
$crawler = $client->request('GET', sprintf(self::SEARCH_URL, $word_to_search));
|
$crawler = $client->request('GET', sprintf(self::SEARCH_URL, $word_to_search));
|
||||||
$imgs = $crawler->filter('noscript img');
|
$imgs = $crawler->filter('noscript img');
|
||||||
if ($imgs->count() < 20) {
|
if ($imgs->count() < 20) {
|
||||||
return $this->getRandomImage();
|
return $this->getRandomImage(); // FIXME possible infinite loop if keywords is given
|
||||||
}
|
}
|
||||||
|
|
||||||
$img_url = $imgs->eq(random_int(0, $imgs->count() - 1))->attr('src');
|
$img_url = $imgs->eq(random_int(0, $imgs->count() - 1))->attr('src');
|
||||||
|
27
src/Pool.php
27
src/Pool.php
@ -21,16 +21,23 @@ class Pool
|
|||||||
*/
|
*/
|
||||||
protected $nb_images = 0;
|
protected $nb_images = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Shikiryu\WebGobbler\Config
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pool constructor.
|
* Pool constructor.
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config)
|
public function __construct(Config $config)
|
||||||
{
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$pool_config = $config->get('pool');
|
||||||
$collectors = [];
|
$collectors = [];
|
||||||
if (array_key_exists('collectors', $config)) {
|
if (array_key_exists('collectors', $pool_config)) {
|
||||||
$collectors = $config['collectors'];
|
$collectors = $pool_config['collectors'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$all_collectors = Collector::getAllCollectors($this);
|
$all_collectors = Collector::getAllCollectors($this);
|
||||||
@ -44,18 +51,25 @@ class Pool
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$pool_dir = $config['directory'];
|
$pool_dir = $pool_config['directory'];
|
||||||
if (!is_dir($pool_dir) && !mkdir($pool_dir) && !is_dir($pool_dir)) {
|
if (!is_dir($pool_dir) && !mkdir($pool_dir) && !is_dir($pool_dir)) {
|
||||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $pool_dir));
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $pool_dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->pool_directory = $pool_dir;
|
$this->pool_directory = $pool_dir;
|
||||||
|
|
||||||
$this->nb_images = $config['nb_images']; // FIXME
|
$this->nb_images = $pool_config['nb_images']; // FIXME
|
||||||
|
|
||||||
$this->prepareCollectors();
|
$this->prepareCollectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getConfig()
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getPoolDirectory()
|
public function getPoolDirectory()
|
||||||
{
|
{
|
||||||
return $this->pool_directory;
|
return $this->pool_directory;
|
||||||
@ -100,6 +114,9 @@ class Pool
|
|||||||
{
|
{
|
||||||
foreach ($this->collectors as $collector) {
|
foreach ($this->collectors as $collector) {
|
||||||
$directory = $collector->getPoolDirectory();
|
$directory = $collector->getPoolDirectory();
|
||||||
|
if (!is_dir($directory) && !mkdir($directory) && !is_dir($directory)) {
|
||||||
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
|
||||||
|
}
|
||||||
$images = glob($directory.'/*.{jpg,gif,png}', GLOB_BRACE);
|
$images = glob($directory.'/*.{jpg,gif,png}', GLOB_BRACE);
|
||||||
if (count($images) < $this->nb_images) {
|
if (count($images) < $this->nb_images) {
|
||||||
$collector->getRandomImages($this->nb_images - count($images));
|
$collector->getRandomImages($this->nb_images - count($images));
|
||||||
|
Loading…
Reference in New Issue
Block a user