Ingo_Script_Procmail_Recipe::generate PHP Method

generate() public method

Generates procmail code to represent the recipe.
public generate ( ) : string
return string Procmail code to represent the recipe.
    public function generate()
    {
        $nest = 0;
        $prefix = '';
        $text = array();
        if (!$this->_valid) {
            return '';
        }
        // Set the global flags for the whole rule, each condition
        // will add its own (such as Body or Case Sensitive)
        $global = $this->_flags;
        if (isset($this->_conditions[0])) {
            $global .= $this->_conditions[0]['flags'];
        }
        $text[] = ':0 ' . $global . (isset($this->_params['delivery_agent']) ? 'w' : '');
        foreach ($this->_conditions as $condition) {
            if ($nest > 0) {
                $text[] = str_repeat('  ', $nest - 1) . '{';
                $text[] = str_repeat('  ', $nest) . ':0 ' . $condition['flags'];
                $text[] = str_repeat('  ', $nest) . $condition['condition'];
            } else {
                $text[] = $condition['condition'];
            }
            $nest++;
        }
        if (--$nest > 0) {
            $prefix = str_repeat('  ', $nest);
        }
        foreach ($this->_action as $val) {
            $text[] = $prefix . $val;
        }
        for ($i = $nest; $i > 0; $i--) {
            $text[] = str_repeat('  ', $i - 1) . '}';
        }
        if ($this->_disable) {
            $code = '';
            foreach ($text as $val) {
                $comment = new Ingo_Script_Procmail_Comment($val);
                $code .= $comment->generate() . "\n";
            }
            return $code;
        }
        return implode("\n", $text);
    }