Skip to content

Commit

Permalink
Merge pull request pyrus#45 from fpoirotte/per_channel_settings
Browse files Browse the repository at this point in the history
Allow per-channel settings
  • Loading branch information
till committed Nov 15, 2011
2 parents b6a3c54 + ff5b271 commit 529ea8e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
12 changes: 12 additions & 0 deletions data/built-in-commands.xml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ based on the answers provided.</doc>
<type><bool/></type>
<doc>Manage plugin configuration only</doc>
</option>
<option>
<name>channel</name>
<shortopt>c</shortopt>
<type><string/></type>
<doc>Set the value only for that specific channel</doc>
</option>
</options>
<arguments>
<argument>
Expand Down Expand Up @@ -450,6 +456,12 @@ php pyrus.phar set download_dir /home/blah/downloads</doc>
<type><bool/></type>
<doc>Get plugin configuration only</doc>
</option>
<option>
<name>channel</name>
<shortopt>c</shortopt>
<type><string/></type>
<doc>Get the value for a specific channel</doc>
</option>
</options>
<arguments>
<argument>
Expand Down
24 changes: 23 additions & 1 deletion src/Pyrus/ScriptFrontend/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,11 @@ function get($args, $options)
$conf = \Pyrus\Config::singleton(\Pyrus\Config::current()->plugins_dir);
}

if ($options['channel']) {
$oldDefaultChannel = $conf->default_channel;
$channel = $conf->channelregistry->get($options['channel'], false);
$conf->default_channel = $channel->name;
}
if (
in_array($args['variable'], $conf->uservars)
|| in_array($args['variable'], $conf->systemvars)
Expand All @@ -907,6 +912,9 @@ function get($args, $options)
exit(1);
}

if ($options['channel']) {
$conf->default_channel = $oldDefaultChannel;
}
if ($options['plugin']) {
\Pyrus\Config::setCurrent($current->path);
}
Expand All @@ -920,10 +928,24 @@ function get($args, $options)
function set($args, $options)
{
$conf = $current = \Pyrus\Config::current();

if ($options['plugin']) {
$conf = \Pyrus\Config::singleton(\Pyrus\Config::current()->plugins_dir);
}
if (in_array($args['variable'], $conf->uservars)) {
if (in_array($args['variable'], $conf->channelvars)) {
if ($options['channel']) {
$oldDefaultChannel = $conf->default_channel;
$channel = $conf->channelregistry->get($options['channel'], false);
$conf->default_channel = $channel->name;
}

echo "Setting $args[variable] for " . $conf->default_channel . " in " . $conf->userfile . "\n";
$conf->{$args['variable']} = $args['value'];

if ($options['channel']) {
$conf->default_channel = $oldDefaultChannel;
}
} elseif (in_array($args['variable'], $conf->uservars)) {
echo "Setting $args[variable] in " . $conf->userfile . "\n";
$conf->{$args['variable']} = $args['value'];
} elseif (in_array($args['variable'], $conf->systemvars)) {
Expand Down
36 changes: 36 additions & 0 deletions tests/ScriptFrontend/Commands/set2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
\Pyrus\ScriptFrontend\Commands::set() on specific channel
--FILE--
<?php
require __DIR__ . '/setup.php.inc';
set_include_path(TESTDIR);
$a = \Pyrus\Config::singleton(TESTDIR, TESTDIR . '/plugins/pearconfig.xml');
$a->ext_dir = TESTDIR . '/ext';
$a->bin_dir = TESTDIR . '/bin';
file_put_contents(TESTDIR . '/plugins/pearconfig.xml', '<pearconfig version="1.0"></pearconfig>');

ob_start();
$cli = new test_scriptfrontend();
$cli->run($args = array (0 => 'set', '-c', 'pecl.php.net', 'handle', 'poo'));

$contents = ob_get_contents();
ob_end_clean();
$help1 = 'Using PEAR installation found at ' . TESTDIR . "\n";
$d = DIRECTORY_SEPARATOR;
$help2 = "Setting handle for pecl.php.net in " . TESTDIR . "/plugins/foo.xml\n";

$test->assertEquals($help1 . $help2,
$contents,
'set output');
\Pyrus\Config::current()->default_channel = 'pecl.php.net';
$test->assertEquals('poo', \Pyrus\Config::current()->handle, 'confirm value changed');
$a = simplexml_load_file(TESTDIR . '/plugins/foo.xml');
$test->assertEquals('poo', (string)$a->handle[0]->peclDOTphpDOTnet, 'confirm value saved');
?>
===DONE===
--CLEAN--
<?php
include __DIR__ . '/../../clean.php.inc';
?>
--EXPECT--
===DONE===

0 comments on commit 529ea8e

Please sign in to comment.