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

Local extended configuration #576

Open
wants to merge 2 commits into
base: develop
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
103 changes: 103 additions & 0 deletions common/oatbox/service/ExtendedConfigDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2019 (original work) Open Assessment Technologies SA;
*
* @author Oleksandr Zagovorychev <[email protected]>
*/

namespace oat\oatbox\service;


class ExtendedConfigDriver extends ServiceConfigDriver
{
const LOCAL_CONFIG_PREFIX = 'local';

/**
* Local configuration with high priority which overrides configuration for the service
* @var array
*/
private $localConfig;

public function get($id)
{
$entry = parent::get($id);
return $this->mergeLocalConfig($id, $entry);
}

private function mergeLocalConfig($id, $entry)
{
$opts = self::array_merge_recursive_ex($entry->getOptions(), $this->getLocalConfig($id));
$entry->setOptions($opts);
return $entry;
}

private static function array_merge_recursive_ex(array $array1, array $array2)
Copy link

Choose a reason for hiding this comment

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

Function array_merge_recursive_ex has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.

{
$merged = $array1;

foreach ($array2 as $key => & $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = static::array_merge_recursive_ex($merged[$key], $value);
} else if (is_numeric($key)) {
if (!in_array($value, $merged, 1)) {
$merged[] = $value;
}
} else {
$merged[$key] = $value;
}
}

return $merged;
}

private function getLocalConfig($id)
{
$localConfig = $this->loadLocalConfig();
$path = explode('/', $id);
$conf = $localConfig;
foreach ($path as $key) {
if (array_key_exists($key, $conf)) {
$conf = $conf[$key];
} else {
$conf = [];
break;
}
}
return $conf;
}

/**
*
*/
/**
* @return array|mixed
*/
private function loadLocalConfig()
{
if (!$this->localConfig) {
$this->localConfig = [];
$config = str_replace('.php', '.json', $this->getPath(self::LOCAL_CONFIG_PREFIX));
// extended configuration for the local service
if (is_readable($config)) {
$this->localConfig = @json_decode(@file_get_contents($config), 1);
}
}
return $this->localConfig;
}


}
1 change: 1 addition & 0 deletions common/oatbox/service/ServiceConfigDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
class ServiceConfigDriver extends \common_persistence_PhpFileDriver implements ConfigurationDriver
{

/**
* Get the config content associated to given $key
* $key has to be a configurable service
Expand Down
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'label' => 'Generis Core',
'description' => 'Core extension, provide the low level framework and an API to manage ontologies',
'license' => 'GPL-2.0',
'version' => '8.2.1',
'version' => '8.3.0',
'author' => 'Open Assessment Technologies, CRP Henri Tudor',
'requires' => array(),
'models' => array(
Expand Down
2 changes: 1 addition & 1 deletion scripts/update/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,6 @@ public function update($initialVersion)
$this->setVersion('8.0.0');
}

$this->skip('8.0.0', '8.2.1');
$this->skip('8.0.0', '8.3.0');
}
}