Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented extra config builder extension point #102

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class ConfigBuilder
private $configFiles = array();
private $cache = array();

/**
* Third party config builders, indexed by their prefix.
*
* @var \Graby\SiteConfig\ExtraConfigBuilder[]
*/
private $extraConfigBuilders = [];

/**
* @param array $config
* @param LoggerInterface|null $logger
Expand All @@ -40,6 +47,17 @@ public function __construct($config = array(), LoggerInterface $logger = null)
$this->configFiles = Files::getFiles($this->config['site_config']);
}

/**
* Adds a ConfigBuilder for an extra, custom configuration.
*
* @param $prefix
* @param \Graby\SiteConfig\ExtraConfigBuilder $extraConfigBuilder
*/
public function addExtraConfigBuilder($prefix, ExtraConfigBuilder $extraConfigBuilder)
{
$this->extraConfigBuilders[$prefix] = $extraConfigBuilder;
}

public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
Expand Down Expand Up @@ -309,6 +327,7 @@ public function mergeConfig(SiteConfig $currentConfig, SiteConfig $newConfig)
*/
public function parseLines(array $lines)
{
$extraConfigItems = [];
$config = new SiteConfig();

foreach ($lines as $line) {
Expand All @@ -332,6 +351,24 @@ public function parseLines(array $lines)
continue;
}

foreach (array_keys($this->extraConfigBuilders) as $prefix) {
if (strpos($command, $prefix.'_') === false) {
continue;
}
$command = substr($command, strlen($prefix) + 1);
if (!isset($extraConfigItems[$prefix][$command])) {
$extraConfigItems[$prefix][$command] = $val;
} elseif (!is_array($extraConfigItems[$prefix][$command])) {
$extraConfigItems[$prefix][$command] = [
$extraConfigItems[$prefix][$command],
$val,
];
} else {
$extraConfigItems[$prefix][$command][] = $val;
}
continue 2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be continue since you remove one level with the first if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. A single continue would continue from foreach (array_keys($this->extraConfigBuilders) as $prefix). As we have found an extra config matching the prefix, we want to continue from the next line (foreach ($lines as $line)).

}

// check for commands where we accept multiple statements
if (in_array($command, array('title', 'body', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'next_page_link', 'test_url', 'find_string', 'replace_string', 'login_extra_fields'))) {
array_push($config->$command, $val);
Expand All @@ -350,6 +387,14 @@ public function parseLines(array $lines)
}
}

foreach ($extraConfigItems as $extraConfigBuilderKey => $commands) {
if (!isset($this->extraConfigBuilders[$extraConfigBuilderKey])) {
continue;
}
$config->extraConfigs[$extraConfigBuilderKey] =
$this->extraConfigBuilders[$extraConfigBuilderKey]->parseCommands($commands);
}

return $config;
}
}
15 changes: 15 additions & 0 deletions src/SiteConfig/ExtraConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Graby\SiteConfig;

interface ExtraConfigBuilder
{
/**
* Parses an array of commands => values into a SiteExtraConfig object.
*
* @param array $commands
*
* @return \Graby\SiteConfig\SiteExtraConfig
*/
public function parseCommands(array $commands);
}
5 changes: 5 additions & 0 deletions src/SiteConfig/SiteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class SiteConfig
*/
public $login_extra_fields = array();

/**
* @var \Graby\SiteConfig\SiteExtraConfig[]
*/
public $extraConfigs = [];

/**
* Process HTML with tidy before creating DOM (bool or null if undeclared).
*
Expand Down
7 changes: 7 additions & 0 deletions src/SiteConfig/SiteExtraConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Graby\SiteConfig;

interface SiteExtraConfig
{
}
4 changes: 2 additions & 2 deletions tests/GrabyFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ public function testMultipage()
'extractor' => array(
'config_builder' => array(
'site_config' => array(dirname(__FILE__).'/fixtures/site_config'),
)
)
),
),
));
$res = $graby->fetchContent('http://www.journaldugamer.com/tests/rencontre-ils-bossaient-sur-une-exclu-kinect-qui-ne-sortira-jamais/');

Expand Down