public function process(Config\Template $tmpl) { /* process template if condition is not set or condition is valid */ if ($tmpl->getCondition() == null || $this->conditionValidator->isValid($tmpl->getCondition(), $this->vars)) { /* load source file */ if (is_file($tmpl->getSource())) { $content = file_get_contents($tmpl->getSource()); /* replace all vars by values */ if (is_array($this->vars)) { foreach ($this->vars as $key => $value) { $content = str_replace('${' . $key . '}', $value, $content); } } /* save destination file */ if (is_file($tmpl->getDestination()) && !$tmpl->isCanRewrite()) { $this->io->write(__CLASS__ . ": <comment>Destination file '{$tmpl->getDestination()}' is already exist and cannot be rewrote (rewrite = false).</comment>"); } else { $this->fileSaver->save($tmpl->getDestination(), $content); $this->io->write(__CLASS__ . ": <info>Destination file '{$tmpl->getDestination()}' is created from source template '{$tmpl->getSource()}'.</info>"); } } else { $this->io->writeError(__CLASS__ . ": <error>Cannot open source template ({$tmpl->getSource()}).</error>"); } } else { /* there is wrong condition for template */ $outSrc = $tmpl->getSource(); $cond = $tmpl->getCondition(); $outCond = '${' . $cond->getVar() . '}' . $cond->getOperation() . $cond->getValue(); $this->io->write(__CLASS__ . ": <comment>Skip processing of the template ({$outSrc}) because condition ({$outCond}) is 'false'.</comment>"); } }
public function test_conditionNotEqual_varEqual() { /* Prepare test mocks and data */ $cond = new Condition(); $cond->setVar('VAR'); $cond->setOperation(Condition::OPER_NEQ); $cond->setValue('value'); /* Create object and perform testing action. */ $obj = new ConditionValidator(); $res = $obj->isValid($cond, ['VAR' => 'value']); $this->assertFalse($res); }