Page 1 of 1

Search/Replace (Edit File) does not work as expected

Posted: Tue Feb 09, 2016 9:09 am
by Kubik-Rubik
Hi guys,

I need to modify the configuration file of the application "Moodle", actually add a value to the file. The Search/Replace (Edit File) feature does not work as expected for me. Could you please check?

My code for the addition is:

Code: Select all

$o->sr('config.php', '#(\$CFG = new stdClass\(\);)#', "$1\n\n".'$CFG->slasharguments = false;');
Expected result:

Code: Select all

$CFG = new stdClass();

$CFG->slasharguments = false;
Actual result:

Code: Select all

$CFG = new stdClass();

$CFG->slasharguments = false;

$CFG->slasharguments = false;
As you see, the new value is added twice. Yes, the application still works like this but this is not a logical replacement. Why this is replaced like this and how do I have to use the sr function properly?

Thanks in advance!

Regards
Viktor

Re: Search/Replace (Edit File) does not work as expected

Posted: Tue Feb 09, 2016 10:27 am
by Kubik-Rubik
Update:

I saw that the rule is applied twice instead of only once (custom code is called twice)! I use the following call trigger:

Code: Select all

$this->registerCustomCode("moodle", "all", "install", "last", "process", "moodleInstall");
What should I change so that the replacement is only executed once?

Greetings

Re: Search/Replace (Edit File) does not work as expected

Posted: Tue Feb 09, 2016 12:40 pm
by Phil
Hello,

The custom code should only be called once. We're reviewing what's going on there.

To get this fixed use this expression instead to block the second match:

Code: Select all

$o->sr('config.php', '#\$CFG = new stdClass\(\);#', '$CFG = new stdClass( );'."\n\n".'$CFG->slasharguments = false;');
Thank you,
Phil

Re: Search/Replace (Edit File) does not work as expected

Posted: Wed Feb 10, 2016 4:35 am
by Kubik-Rubik
Hey Phil,

I can verify the bug, the custom code is called twice. If I do the following "trick", then the result is as expected:

Code: Select all

static $run_once = false;

if($run_once == false)
{
	$o->sr('config.php', '#(\$CFG = new stdClass\(\);)#', "$1\n\n".'$CFG->slasharguments = false;');

	$run_once = true;
}
I hope it helps you to fix it!

Greetings