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 1 commit
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
43 changes: 43 additions & 0 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ 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 +46,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 +326,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 +350,23 @@ public function parseLines(array $lines)
continue;
}

foreach (array_keys($this->extraConfigBuilders) as $prefix) {
if (strpos($command, $prefix . "_") === 0) {
$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;
}
}

// 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 +385,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;
}
}
20 changes: 20 additions & 0 deletions src/SiteConfig/ExtraConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

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
{

}