/dev/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/trunk

« back to all changes in this revision

Viewing changes to classes/stream/iofilter.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class IOFilter extends FILTER implements STREAMObjectInterface {
 
4
 var $sink;
 
5
 var $sink_open;
 
6
 var $outlet;
 
7
 var $outlet_open;
 
8
 
 
9
 var $copymode;
 
10
 var $resource_close;
 
11
 var $extra_data = "";
 
12
 
 
13
 var $block_mode;
 
14
 
 
15
 function __construct(&$info = NULL, STREAMWriterInterface $output = NULL, $sink = NULL, $outlet = 0, $flags = 0) {
 
16
    parent::__construct($info, $output);
 
17
 
 
18
    $this->extra_data = false;
 
19
    
 
20
    $this->CreateFilter($sink, $outlet);
 
21
    
 
22
    if ($flags&STREAM::GIFT) $this->resource_close = true;
 
23
    else $this->resource_close = false;
 
24
 }
 
25
 
 
26
 /* 
 
27
 outlet:
 
28
    - NULL      : create standard
 
29
    - resourse  : create standard and initialize
 
30
    - WriterI   : use writer interface
 
31
    - #number   : dublicate sink
 
32
    - false     : no outlet
 
33
 */
 
34
     
 
35
 function CreateFilter($sink = NULL, $outlet = 0) {
 
36
    unset($this->sink);
 
37
    unset($this->outlet);
 
38
 
 
39
    if (is_resource($sink)) {
 
40
        $this->sink = new IO($sink, is_int($outlet));
 
41
        $this->sink_open = true;
 
42
    } elseif ($sink instanceof STREAMWriterInterface) {
 
43
        $this->sink = $sink;
 
44
        $this->sink_open = false;
 
45
    } elseif ($sink) {
 
46
        throw new ADEIException(translate("Unsupported sink is supplied for the FILTER"));
 
47
    } else {
 
48
        $this->sink = new IO($NULL=NULL, is_int($outlet));
 
49
        $this->sink_open = true;
 
50
    }    
 
51
        
 
52
    if ($outlet === false) {
 
53
        $this->outlet = false;
 
54
        $this->outlet_open = false;
 
55
        $this->filewriter = true;
 
56
        $this->copymode = false;
 
57
    } elseif (is_int($outlet)) {
 
58
        $this->outlet = $this->sink;
 
59
        $this->outlet_open = false;
 
60
        $this->filewriter = false;
 
61
        $this->copymode = true;
 
62
    } elseif (is_resource($args[0])) {
 
63
        $this->outlet = new IO($outlet, true);
 
64
        $this->outlet_open = true;
 
65
        $this->filewriter = false;
 
66
        $this->copymode = false;
 
67
    } elseif ($outlet instanceof STREAMWriterInterface) {
 
68
        $this->outlet = $outlet;
 
69
        $this->outlet_open = false;
 
70
        $this->filewriter = false;
 
71
        $this->copymode = false;
 
72
    } elseif ($outlet) {
 
73
        throw new ADEIException(translate("Unsupported outlet is supplied for the FILTER"));
 
74
    } else {
 
75
        $this->outlet = new IO($NULL=NULL, true);
 
76
        $this->outlet_open = true;
 
77
        $this->filewriter = false;
 
78
        $this->copymode = false;
 
79
    }
 
80
 }
 
81
 
 
82
 function OpenPipes(&$pipes) {
 
83
    if ($this->sink_open) {
 
84
        $this->sink->Open($pipes[0], $flags);
 
85
    }
 
86
    if ($this->outlet_open) {
 
87
        $this->outlet->Open($pipes[1], $flags);
 
88
    }
 
89
 }
 
90
 
 
91
 function ClosePipes(STREAMWriterInterface $h = NULL) {
 
92
    if ($this->output) $h = $this->output;
 
93
 
 
94
            // if outlet is copy of sink, we have outlet_open false
 
95
    if ($this->sink_open) {
 
96
        $this->sink->Close();
 
97
    }
 
98
 
 
99
    if ($this->outlet_open) {
 
100
        return $this->outlet->Close($h);
 
101
    }
 
102
 }
 
103
 
 
104
 function Open(&$args = NULL, &$pipes = NULL, $flags = 0) {
 
105
    parent::Open($args);
 
106
 
 
107
    if ($pipes) {
 
108
        $this->OpenPipes($pipes, $flags);
 
109
        $this->block_mode = false;
 
110
    } else $this->block_mode = true;
 
111
 }
 
112
 
 
113
 function BlockStart(&$args = NULL, &$pipes = NULL, $flags = 0) {
 
114
    if ($this->block_mode) {
 
115
        if ($pipes)
 
116
            $this->OpenPipes($pipes, $flags);
 
117
        else
 
118
            throw new ADEIException(translate("The actual pipes are not supplied IOFilter"));
 
119
    }
 
120
 }
 
121
 
 
122
 function BlockEnd(STREAMWriterInterface $h = NULL) {
 
123
    if ($this->block_mode) {
 
124
        return $this->ClosePipes($h);
 
125
    }
 
126
 }
 
127
 
 
128
 function Close(STREAMWriterInterface $h = NULL) {
 
129
    if (!$this->block_mode) {
 
130
        $res = $this->ClosePipes($h);
 
131
        if ($res) return $res . parent::Close($h);
 
132
    }
 
133
    return parent::Close($h);
 
134
 }
 
135
 
 
136
 function WriteData(&$data, $flags = 0) {
 
137
    $this->sink->Write($data);
 
138
 }
 
139
 
 
140
 function ReadData($limit = 0, $flags = 0) {
 
141
    if ($this->outlet) {
 
142
        if ($this->output)
 
143
            return new ADEIException(translate("The filter have STREAMWriter configured in. Therethore read requests are not permited"));
 
144
        
 
145
        if ($this->extra_data) {
 
146
            $res = $this->extra_data . $this->outlet->ReadData($limit, $flags);
 
147
            $this->extra_data = false;
 
148
            return $res;
 
149
        }
 
150
        return $this->outlet->ReadData($limit, $flags);
 
151
    } else
 
152
        throw new ADEIException(translate("The filter outlet is not configured"));
 
153
 }
 
154
 
 
155
 function StreamData(STREAMWriterInterface $h = NULL, $flags = 0) {
 
156
    if ($this->outlet) {
 
157
        if ($this->output) $h = $this->output;
 
158
        else if (!$h) throw new ADEIException(translate("The data routing is failed (there is no output specified for FILTER)"));
 
159
 
 
160
        if ($this->extra_data) {
 
161
            $h->WriteData($this->extra_data);
 
162
            $this->extra_data = false;
 
163
        }
 
164
        $this->outlet->StreamData($h, $flags);
 
165
    } else
 
166
        throw new ADEIException(translate("The filter outlet is not configured"));
 
167
 }
 
168
 
 
169
 function EOS() {
 
170
    if ($this->outlet) {
 
171
        if ($this->extra_data) return false;
 
172
        return $this->outlet->EOS();
 
173
    } else
 
174
        throw new ADEIException(translate("The filter outlet is not configured"));
 
175
 }
 
176
 
 
177
}
 
178
 
 
179
 
 
180
?>
 
 
b'\\ No newline at end of file'