Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Copyright (c) 2008-2009, Laurent Laville <pear@laurent-laville.org>
4
 *
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *     * Redistributions of source code must retain the above copyright
12
 *       notice, this list of conditions and the following disclaimer.
13
 *     * Redistributions in binary form must reproduce the above copyright
14
 *       notice, this list of conditions and the following disclaimer in the
15
 *       documentation and/or other materials provided with the distribution.
16
 *     * Neither the name of the authors nor the names of its contributors
17
 *       may be used to endorse or promote products derived from this software
18
 *       without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGE.
31
 *
32
 * PHP versions 4 and 5
33
 *
34
 * @category PHP
35
 * @package  PHP_CompatInfo
36
 * @author   Laurent Laville <pear@laurent-laville.org>
37
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
38
 * @version  CVS: $Id: Renderer.php,v 1.9 2009/01/02 10:18:47 farell Exp $
39
 * @link     http://pear.php.net/package/PHP_CompatInfo
40
 * @since    File available since Release 1.8.0b2
41
 */
42
 
43
/**
44
 * Base class used by all renderers
45
 *
46
 * @category PHP
47
 * @package  PHP_CompatInfo
48
 * @author   Laurent Laville <pear@laurent-laville.org>
49
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
50
 * @version  Release: 1.9.0
51
 * @link     http://pear.php.net/package/PHP_CompatInfo
52
 * @since    Class available since Release 1.8.0b2
53
 * @abstract
54
 */
55
class PHP_CompatInfo_Renderer
56
{
57
    /**
58
     * PHP_CompatInfo_Parser instance
59
     *
60
     * @var    object
61
     * @access private
62
     */
63
    var $_parser;
64
 
65
    /**
66
     * @var    mixed    Progress bar render options (available only on CLI sapi)
67
     * @since  1.8.0b1
68
     * @access private
69
     */
70
    var $_pbar;
71
 
72
    /**
73
     * @var    string   End of line string (depending of server API)
74
     * @access public
75
     */
76
    var $eol;
77
 
78
    /**
79
     * Silent mode. Display or not extra info messages.
80
     *
81
     * @var    boolean
82
     * @access public
83
     */
84
    var $silent;
85
 
86
    /**
87
     * Data source parsed final results
88
     *
89
     * @var    array
90
     * @access public
91
     */
92
    var $parseData;
93
 
94
    /**
95
     * All console arguments that have been parsed and recognized
96
     *
97
     * @var   array
98
     * @since 1.8.0RC1
99
     * @access public
100
     */
101
    var $args;
102
 
103
    /**
104
     * A hash containing any additional configuration of specific driver
105
     *
106
     * @var    array
107
     * @since  1.8.0RC1
108
     * @access public
109
     */
110
    var $conf;
111
 
112
    /**
113
     * Base Renderer Class constructor
114
     *
115
     * Base Renderer Class constructor (ZE1) for PHP4
116
     *
117
     * @param object &$parser Instance of the parser (model of MVC pattern)
118
     * @param array  $conf    A hash containing any additional configuration
119
     *
120
     * @access public
121
     * @since  version 1.8.0b2 (2008-06-03)
122
     */
123
    function PHP_CompatInfo_Renderer(&$parser, $conf)
124
    {
125
        PHP_CompatInfo_Renderer::__construct($parser, $conf);
126
    }
127
 
128
    /**
129
     * Base Renderer Class constructor
130
     *
131
     * Base Renderer Class constructor (ZE2) for PHP5+
132
     *
133
     * @param object &$parser Instance of the parser (model of MVC pattern)
134
     * @param array  $conf    A hash containing any additional configuration
135
     *
136
     * @access public
137
     * @since  version 1.8.0b2 (2008-06-03)
138
     */
139
    function __construct(&$parser, $conf)
140
    {
141
        $this->_parser = $parser;
142
 
143
        $args = array(
144
            'summarize' => false,
145
            'output-level' => 31,
146
            'verbose' => 0
147
            );
148
        if (isset($conf['args']) && is_array($conf['args'])) {
149
            $this->args = array_merge($args, $conf['args']);
150
            unset($conf['args']);
151
        } else {
152
            $this->args = $args;
153
        }
154
        $this->conf = $conf;
155
 
156
        if (php_sapi_name() == 'cli') {
157
            // when running the CLI version, take arguments from console
158
            if (isset($this->args['progress'])) {
159
                $conf['progress'] = $this->args['progress'];
160
                $conf['silent']   = false;
161
            }
162
            $this->eol = PHP_EOL;
163
        } else {
164
            $this->eol = '<br/>'. PHP_EOL;
165
        }
166
 
167
        // activate (or not) the silent mode
168
        if (!isset($conf['silent'])) {
169
            $this->silent = true;  // default behavior
170
        } else {
171
            $this->silent = (bool) $conf['silent'];
172
        }
173
 
174
        if (isset($conf['progress']) && $conf['progress'] == 'bar') {
175
            // wait style = progress bar prefered (if available)
176
            $progressBar = 'Console/ProgressBar.php';
177
            if (php_sapi_name() == 'cli'
178
                && PHP_CompatInfo_Renderer::isIncludable($progressBar)) {
179
 
180
                include_once $progressBar;
181
 
182
                // default progress bar render options
183
                $default = array('formatString' => '- %fraction% files' .
184
                                                   ' [%bar%] %percent%' .
185
                                                   ' Elapsed Time: %elapsed%',
186
                                 'barfill' => '=>',
187
                                 'prefill' => '-',
188
                                 'options' => array());
189
 
190
                // apply custom render options if given
191
                if (isset($conf['progressbar'])) {
192
                    $pbar = $conf['progressbar'];
193
                } else {
194
                    $pbar = array();
195
                }
196
                $this->_pbar = array_merge($default, $pbar);
197
            } else {
198
                // no progress bar available
199
                $this->_pbar = false;
200
            }
201
        } else {
202
            // wait style = text prefered
203
            $this->_pbar = false;
204
        }
205
 
206
        // register the compatInfo view as observer
207
        $parser->addListener(array(&$this, 'update'));
208
    }
209
 
210
    /**
211
     * Create required instance of the Output 'driver'.
212
     *
213
     * Creates a concrete instance of the renderer depending of $type
214
     *
215
     * @param object &$parser A concrete instance of the parser
216
     * @param string $type    (optional) Type of instance required, case insensitive
217
     * @param array  $conf    (optional) A hash containing any additional
218
     *                        configuration information that a subclass might need.
219
     *
220
     * @return object PHP_CompatInfo_Renderer A concrete PHP_CompatInfo_Renderer
221
     *                                        instance, or null on error.
222
     * @access public
223
     * @since  version 1.8.0b2 (2008-06-03)
224
     */
225
    function &factory(&$parser, $type = 'array', $conf = array())
226
    {
227
        $class = 'PHP_CompatInfo_Renderer_' . ucfirst(strtolower($type));
228
        $file  = str_replace('_', '/', $class) . '.php';
229
 
230
        /**
231
         * Attempt to include our version of the named class, but don't treat
232
         * a failure as fatal.  The caller may have already included their own
233
         * version of the named class.
234
         */
235
        if (!PHP_CompatInfo_Renderer::_classExists($class)) {
236
            include_once $file;
237
        }
238
 
239
        // If the class exists, return a new instance of it.
240
        if (PHP_CompatInfo_Renderer::_classExists($class)) {
241
            $instance =& new $class($parser, $conf);
242
        } else {
243
            $instance = null;
244
        }
245
 
246
        return $instance;
247
    }
248
 
249
    /**
250
     * Update the current view
251
     *
252
     * Interface to update the view with current information.
253
     * Listen events produced by Event_Dispatcher and the PHP_CompatInfo_Parser
254
     *
255
     * @param object &$auditEvent Instance of Event_Dispatcher
256
     *
257
     * @return void
258
     * @access public
259
     * @since  version 1.8.0b2 (2008-06-03)
260
     */
261
    function update(&$auditEvent)
262
    {
263
        $notifyName = $auditEvent->getNotificationName();
264
        $notifyInfo = $auditEvent->getNotificationInfo();
265
 
266
        switch ($notifyName) {
267
        case PHP_COMPATINFO_EVENT_AUDITSTARTED :
268
            $this->startWaitProgress($notifyInfo['dataCount']);
269
            break;
270
        case PHP_COMPATINFO_EVENT_AUDITFINISHED :
271
            if (!isset($this->parseData)) {
272
                // invalid data source
273
                $this->parseData = false;
274
            }
275
            $this->endWaitProgress();
276
            $this->display();
277
            break;
278
        case PHP_COMPATINFO_EVENT_FILESTARTED :
279
            $this->stillWaitProgress($notifyInfo['filename'],
280
                                     $notifyInfo['fileindex']);
281
            break;
282
        case PHP_COMPATINFO_EVENT_CODESTARTED :
283
            $this->stillWaitProgress($notifyInfo['stringdata'],
284
                                     $notifyInfo['stringindex']);
285
            break;
286
        case PHP_COMPATINFO_EVENT_FILEFINISHED :
287
        case PHP_COMPATINFO_EVENT_CODEFINISHED :
288
            $this->parseData = $notifyInfo;
289
            break;
290
        }
291
    }
292
 
293
    /**
294
     * Initialize the wait process
295
     *
296
     * Initialize the wait process, with a simple message or a progress bar.
297
     *
298
     * @param integer $maxEntries Number of source to parse
299
     *
300
     * @return void
301
     * @access public
302
     * @since  version 1.8.0b2 (2008-06-03)
303
     */
304
    function startWaitProgress($maxEntries)
305
    {
306
        if ($this->silent == false) {
307
            // obey at silent mode protocol
308
            if ($maxEntries == 0) {
309
                // protect against invalid data source
310
                $this->_pbar = false;
311
            }
312
            if ($this->_pbar) {
313
                $this->_pbar = new Console_ProgressBar($this->_pbar['formatString'],
314
                                               $this->_pbar['barfill'],
315
                                               $this->_pbar['prefill'],
316
                                               78,
317
                                               $maxEntries,
318
                                               $this->_pbar['options']);
319
            } else {
320
                echo 'Wait while parsing data source ...'
321
                   . $this->eol;
322
            }
323
        }
324
    }
325
 
326
    /**
327
     * Update the wait message
328
     *
329
     * Update the wait message, or status of the progress bar
330
     *
331
     * @param string $source Source (file, string) currently parsing
332
     * @param string $index  Position of the $source in the data source list
333
     *                       to parse
334
     *
335
     * @return void
336
     * @access public
337
     * @since  version 1.8.0b2 (2008-06-03)
338
     */
339
    function stillWaitProgress($source, $index)
340
    {
341
        if ($this->silent == false) {
342
            // obey at silent mode protocol
343
            if ($this->_pbar) {
344
                // update the progress bar
345
                $this->_pbar->update($index);
346
            } else {
347
                if (is_file($source)) {
348
                    echo 'Wait while parsing file "' . $source . '"'
349
                       . $this->eol;
350
                } else {
351
                    echo 'Wait while parsing string "' . $index . '"'
352
                       . $this->eol;
353
                }
354
            }
355
        }
356
    }
357
 
358
    /**
359
     * Finish the wait process
360
     *
361
     * Finish the wait process, by erasing the progress bar
362
     *
363
     * @return void
364
     * @access public
365
     * @since  version 1.8.0b2 (2008-06-03)
366
     */
367
    function endWaitProgress()
368
    {
369
        if ($this->silent == false) {
370
            // obey at silent mode protocol
371
            if ($this->_pbar) {
372
                // remove the progress bar
373
                $this->_pbar->erase(true);
374
            }
375
        }
376
    }
377
 
378
    /**
379
     * Checks if in the include path
380
     *
381
     * Returns whether or not a file is in the include path
382
     *
383
     * @param string $file Path to filename to check if includable
384
     *
385
     * @static
386
     * @access public
387
     * @return boolean True if the file is in the include path, false otherwise
388
     * @since  version 1.7.0b4 (2008-04-03)
389
     */
390
    function isIncludable($file)
391
    {
392
        foreach (explode(PATH_SEPARATOR, get_include_path()) as $ip) {
393
            if (file_exists($ip . DIRECTORY_SEPARATOR . $file)
394
                && is_readable($ip . DIRECTORY_SEPARATOR . $file)
395
                ) {
396
                return true;
397
            }
398
        }
399
        return false;
400
    }
401
 
402
    /**
403
     * Utility function which wraps PHP's class_exists() function to ensure
404
     * consistent behavior between PHP versions 4 and 5. Autoloading behavior
405
     * is always disabled.
406
     *
407
     * @param string $class The name of the class whose existence should be tested.
408
     *
409
     * @return bool         True if the class exists, false otherwiser.
410
     *
411
     * @static
412
     * @access private
413
     * @since  version 1.8.0b2 (2008-06-03)
414
     */
415
    function _classExists($class)
416
    {
417
        if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
418
            return class_exists($class, false);
419
        }
420
 
421
        return class_exists($class);
422
    }
423
}
424
?>