Problém s str_replace v presenteru

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
Carson
Člen | 35
+
0
-

Ahoj,

Celý den si lámu hlavu nad tím, proč mi funkce která mi má nahradit při generování proměné %promena%, nenahrazuje vše, ale pouze 1 z nich.
Níže dávám kód.

Presenter

public function handleGenerateConfig()
 {
     $IpList = array(
         '10.250.20.20' => 'SW0010593',
         '10.250.20.30' => 'SW0010594',
     );

     foreach ($IpList as $SwitchIP => $SwitchSysname)
     {

         $Variables = array(
             '%sysname%' => $SwitchSysname,
             '%vlan%' => '801',
             '%mgmtvlan' => '800',
             '%domain%' => 'domain.cz'
         );

         $File = fopen('../www/switch_config/' . $SwitchSysname . '.txt','w');

         $GetBudle = $this->GenerateModel->GetBudle();

         $Commands = $this->GenerateModel->GenerateCommands();

         foreach ($GetBudle as $BudleOne) {

             $Config =  $this->replaceString($Variables, $BudleOne->budle);

             fwrite($File,$Config . "\n");

             foreach ($Commands as $CommandOne => $r_budle_id) {

                 if ($BudleOne->id == $r_budle_id) {

                     $Config =  $this->replaceString($Variables,$CommandOne );

                     fwrite($File, $Config . "\n");
                 }
             }
         }

         fclose($File);


         $this->flashMessage('Konfigurační soubor : ' . $SwitchSysname . '.txt Byl úspěšně vygenerován!','alert alert-success');
     }
 }

 public function replaceString($Variables,$String)
 {

     foreach($Variables as $Variable => $Replace)
     {
         return str_replace($Variable,$Replace,$String);
     }

 }

Model

public function GetBudle()
{
    $Sql = 'SELECT switch_generate_budle.budle,switch_generate_budle.id, switch_generate_cast_name.priority ';
    $Sql = $Sql.'FROM test.switch_generate_budle ';
    $Sql = $Sql.'INNER JOIN test.switch_generate_cast_name ';
    $Sql = $Sql.'ON switch_generate_budle.r_switch_generate_cast_name = switch_generate_cast_name.id ';
    $Sql = $Sql.'ORDER BY switch_generate_cast_name.priority ASC , switch_generate_budle.priority ASC';

    $Sql = $this->db->query($Sql);

    return $Sql;
}

public function GetCommand($BudleID)
{
    $Sql = 'SELECT * ';
    $Sql = $Sql.'FROM test.switch_generate_command ';
    $Sql = $Sql.'INNER JOIN test.switch_generate_budle ';
    $Sql = $Sql.'ON switch_generate_command.r_switch_generate_budle = switch_generate_budle.id ';
    $Sql = $Sql.'WHERE r_switch_generate_budle = %i ORDER BY switch_generate_command.priority DESC';

    $Sql = $this->db->query($Sql,$BudleID);

    return $Sql;
}

public function GenerateCommands()
{
    $GetBudle = $this->GetBudle();

    $Commands = array();

    foreach ($GetBudle as $Budle) {

        $GetCommand = $this->GetCommand($Budle->id);

        foreach ($GetCommand as $Command) {

            $Commands[$Command->command] = $Command->r_switch_generate_budle;

        }
    }

    return $Commands;
}

Aktuálně mi to vypisuje následující

sysname SW0010593
clock timezone 1 add 02:00:00
acl number 2010
rule 10 permit source 1.1.1.0 0.0.0.255
rule 20 permit source 1.1.1.0 0.0.0.255 %domain% %vlan%
acl number 2012
acl number 2013

\--

Editoval Carson (8. 11. 2015 14:28)

greeny
Člen | 405
+
0
-

Protože máš ve foreachi return, takže se provede jen jednou.

Změň ho na

foreach($Variables as $Variable => $Replace)
{
	$String = str_replace($Variable,$Replace,$String);
}
return $String;
David Grudl
Nette Core | 8228
+
+4
-

Protože v ní máš return. Ale tohle fakt nesouvisí s nette.