1 | <?php |
||
2 | |||
3 | /** |
||
4 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
5 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
6 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
7 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
8 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
9 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
10 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
11 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
12 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
13 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
14 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
15 | * |
||
16 | * This software consists of voluntary contributions made by many individuals |
||
17 | * and is licensed under the LGPL. For more information please see |
||
18 | * <http://phing.info>. |
||
19 | */ |
||
20 | |||
21 | namespace Phing\Filter; |
||
22 | |||
23 | use Exception; |
||
24 | use Phing\Exception\BuildException; |
||
25 | use Phing\Io\FilterReader as IoFilterReader; |
||
26 | use Phing\Io\Reader; |
||
27 | use Phing\Phing; |
||
28 | use Phing\Project; |
||
29 | use Phing\Type\FilterReader; |
||
30 | use Phing\Type\Parameterizable; |
||
31 | |||
32 | /** |
||
33 | * Process a FilterReader chain. |
||
34 | * |
||
35 | * Here, the interesting method is 'getAssembledReader'. |
||
36 | * The purpose of this one is to create a simple Reader object which |
||
37 | * apply all filters on another primary Reader object. |
||
38 | * |
||
39 | * For example : In copyFile (phing.util.FileUtils) the primary Reader |
||
40 | * is a FileReader object (more accuratly, a BufferedReader) previously |
||
41 | * setted for the source file to copy. So, consider this filterchain : |
||
42 | * |
||
43 | * <filterchain> |
||
44 | * <stripphpcomments /> |
||
45 | * <linecontains> |
||
46 | * <contains value="foo"> |
||
47 | * </linecontains> |
||
48 | * <tabtospaces tablength="8" /> |
||
49 | * </filterchain> |
||
50 | * |
||
51 | * getAssembledReader will return a Reader object wich read on each |
||
52 | * of these filters. Something like this : ('->' = 'which read data from') : |
||
53 | * |
||
54 | * [TABTOSPACES] -> [LINECONTAINS] -> [STRIPPHPCOMMENTS] -> [FILEREADER] |
||
55 | * (primary reader) |
||
56 | * |
||
57 | * So, getAssembledReader will return the TABTOSPACES Reader object. Then |
||
58 | * each read done with this Reader object will follow this path. |
||
59 | * |
||
60 | * Hope this explanation is clear :) |
||
61 | * |
||
62 | * TODO: Implement the classPath feature. |
||
63 | * |
||
64 | * @author <a href="mailto:[email protected]">Yannick Lecaillez</a> |
||
65 | */ |
||
66 | class ChainReaderHelper |
||
67 | { |
||
68 | /** |
||
69 | * Primary reader to wich the reader chain is to be attached. |
||
70 | */ |
||
71 | private $primaryReader; |
||
72 | |||
73 | /** |
||
74 | * The site of the buffer to be used. |
||
75 | */ |
||
76 | private $bufferSize = 8192; |
||
77 | |||
78 | /** |
||
79 | * Chain of filters. |
||
80 | */ |
||
81 | private $filterChains = []; |
||
82 | |||
83 | /** |
||
84 | * The Phing project. |
||
85 | */ |
||
86 | private $project; |
||
87 | |||
88 | // Sets the primary reader |
||
89 | |||
90 | 28 | public function setPrimaryReader(Reader $reader) |
|
91 | { |
||
92 | 28 | $this->primaryReader = $reader; |
|
93 | 28 | } |
|
94 | |||
95 | // Set the project to work with |
||
96 | |||
97 | 28 | public function setProject(Project $project) |
|
98 | { |
||
99 | 28 | $this->project = $project; |
|
100 | 28 | } |
|
101 | |||
102 | // Get the project |
||
103 | 28 | public function getProject() |
|
104 | { |
||
105 | 28 | return $this->project; |
|
106 | } |
||
107 | |||
108 | /* |
||
109 | * Sets the buffer size to be used. Defaults to 8192, |
||
110 | * if this method is not invoked. |
||
111 | */ |
||
112 | |||
113 | /** |
||
114 | * @param $size |
||
115 | */ |
||
116 | 28 | public function setBufferSize($size) |
|
117 | { |
||
118 | 28 | $this->bufferSize = $size; |
|
119 | 28 | } |
|
120 | |||
121 | // Sets the collection of filter reader sets |
||
122 | |||
123 | /** |
||
124 | * @param $fchain |
||
125 | */ |
||
126 | 28 | public function setFilterChains(&$fchain) |
|
127 | { |
||
128 | 28 | $this->filterChains = &$fchain; |
|
129 | 28 | } |
|
130 | |||
131 | // Assemble the reader |
||
132 | |||
133 | /** |
||
134 | * @throws Exception |
||
135 | * |
||
136 | * @return null|FilterReader|Parameterizable|Reader |
||
137 | */ |
||
138 | 28 | public function getAssembledReader() |
|
139 | { |
||
140 | 28 | $instream = $this->primaryReader; |
|
141 | 28 | $filterReadersCount = count($this->filterChains); |
|
142 | 28 | $finalFilters = []; |
|
143 | |||
144 | // Collect all filter readers of all filter chains used ... |
||
145 | 28 | for ($i = 0; $i < $filterReadersCount; ++$i) { |
|
146 | 28 | $filterchain = &$this->filterChains[$i]; |
|
147 | 28 | $filterReaders = $filterchain->getFilterReaders(); |
|
148 | 28 | $readerCount = count($filterReaders); |
|
149 | 28 | for ($j = 0; $j < $readerCount; ++$j) { |
|
150 | 28 | $finalFilters[] = $filterReaders[$j]; |
|
151 | } |
||
152 | } |
||
153 | |||
154 | // ... then chain the filter readers. |
||
155 | 28 | $filtersCount = count($finalFilters); |
|
156 | 28 | if ($filtersCount > 0) { |
|
157 | 28 | for ($i = 0; $i < $filtersCount; ++$i) { |
|
158 | 28 | $filter = $finalFilters[$i]; |
|
159 | |||
160 | 28 | if ($filter instanceof FilterReader) { |
|
161 | // This filter reader is an external class. |
||
162 | 8 | $className = $filter->getClassName(); |
|
163 | 8 | $classpath = $filter->getClasspath(); |
|
164 | |||
165 | 8 | if (null === $className) { |
|
166 | throw new BuildException('No class name set on filter', $filter->getLocation()); |
||
167 | } |
||
168 | |||
169 | 8 | $cls = Phing::import($className, $classpath); |
|
170 | 8 | $impl = new $cls(); |
|
171 | |||
172 | 8 | if (!($impl instanceof IoFilterReader)) { |
|
173 | throw new Exception($className . ' does not extend ' . IoFilterReader::class); |
||
174 | } |
||
175 | |||
176 | 8 | $impl->setReader($instream); // chain |
|
177 | 8 | $impl->setProject($this->getProject()); // what about $project above ? |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
178 | |||
179 | 8 | if ($impl instanceof Parameterizable) { |
|
180 | 7 | $impl->setParameters($filter->getParams()); |
|
181 | } |
||
182 | |||
183 | 8 | $instream = $impl; // now that it's been chained |
|
184 | 22 | } elseif (($filter instanceof ChainableReader) && ($filter instanceof Reader)) { |
|
185 | 22 | if (null !== $this->getProject() && ($filter instanceof BaseFilterReader)) { |
|
186 | 22 | $filter->setProject($this->getProject()); |
|
187 | } |
||
188 | 22 | $instream = $filter->chain($instream); |
|
189 | } else { |
||
190 | throw new Exception('Cannot chain invalid filter: ' . get_class($filter)); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | 28 | return $instream; |
|
196 | } |
||
197 | } |
||
198 |