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) 2004-2009, Davey Shafik <davey@php.net>
4
 *                          Laurent Laville <pear@laurent-laville.org>
5
 *
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *     * Redistributions of source code must retain the above copyright
13
 *       notice, this list of conditions and the following disclaimer.
14
 *     * Redistributions in binary form must reproduce the above copyright
15
 *       notice, this list of conditions and the following disclaimer in the
16
 *       documentation and/or other materials provided with the distribution.
17
 *     * Neither the name of the authors nor the names of its contributors
18
 *       may be used to endorse or promote products derived from this software
19
 *       without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
 * POSSIBILITY OF SUCH DAMAGE.
32
 *
33
 * PHP versions 4 and 5
34
 *
35
 * @category PHP
36
 * @package  PHP_CompatInfo
37
 * @author   Davey Shafik <davey@php.net>
38
 * @author   Laurent Laville <pear@laurent-laville.org>
39
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
40
 * @version  CVS: $Id: Cli.php,v 1.75 2009/01/02 10:18:47 farell Exp $
41
 * @link     http://pear.php.net/package/PHP_CompatInfo
42
 * @since    File available since Release 0.8.0
43
 */
44
 
45
require_once 'PHP/CompatInfo.php';
46
require_once 'Console/Getargs.php';
47
 
48
/**
49
 * CLI Script to Check Compatibility of chunk of PHP code
50
 *
51
 * <code>
52
 * <?php
53
 *     require_once 'PHP/CompatInfo/Cli.php';
54
 *     $cli = new PHP_CompatInfo_Cli();
55
 *     $cli->run();
56
 * ?>
57
 * </code>
58
 *
59
 * @category  PHP
60
 * @package   PHP_CompatInfo
61
 * @author    Davey Shafik <davey@php.net>
62
 * @author    Laurent Laville <pear@laurent-laville.org>
63
 * @copyright 2003 Davey Shafik and Synaptic Media. All Rights Reserved.
64
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD
65
 * @version   Release: 1.9.0
66
 * @link      http://pear.php.net/package/PHP_CompatInfo
67
 * @since     Class available since Release 0.8.0
68
 */
69
 
70
class PHP_CompatInfo_Cli
71
{
72
    /**
73
     * @var    array    Current CLI Flags
74
     * @since  0.8.0
75
     */
76
    var $opts = array();
77
 
78
    /**
79
     * Unified data source reference
80
     *
81
     * @var    string   Directory, File or String to be processed
82
     * @since  1.8.0b3
83
     */
84
    var $dataSource;
85
 
86
    /**
87
     * @var    array    Current parser options
88
     * @since  1.4.0
89
     */
90
    var $options = array();
91
 
92
 
93
    /**
94
     * Command-Line Class constructor
95
     *
96
     * Command-Line Class constructor (ZE2) for PHP5+
97
     *
98
     * @since  version 0.8.0 (2004-04-22)
99
     */
100
    function __construct()
101
    {
102
        $this->opts = array(
103
            'dir' =>
104
                array('short' => 'd',
105
                      'desc'  => 'Parse DIR to get its compatibility info',
106
                      'default' => '',
107
                      'min'   => 0 , 'max' => 1),
108
            'file' =>
109
                array('short' => 'f',
110
                      'desc' => 'Parse FILE to get its compatibility info',
111
                      'default' => '',
112
                      'min'   => 0 , 'max' => 1),
113
            'string' =>
114
                array('short' => 's',
115
                      'desc' => 'Parse STRING to get its compatibility info',
116
                      'default' => '',
117
                      'min'   => 0 , 'max' => 1),
118
            'verbose' =>
119
                array('short'   => 'v',
120
                      'desc'    => 'Set the verbose level',
121
                      'default' => 1,
122
                      'min'     => 0 , 'max' => 1),
123
            'no-recurse' =>
124
                array('short' => 'n',
125
                      'desc'  => 'Do not recursively parse files when using --dir',
126
                      'max'   => 0),
127
            'ignore-files' =>
128
                array('short'   => 'if',
129
                      'desc'    => 'Data file name which contains a list of '
130
                                 . 'file to ignore',
131
                      'default' => 'files.txt',
132
                      'min'     => 0 , 'max' => 1),
133
            'ignore-dirs' =>
134
                array('short'   => 'id',
135
                      'desc'    => 'Data file name which contains a list of '
136
                                 . 'directory to ignore',
137
                      'default' => 'dirs.txt',
138
                      'min'     => 0 , 'max' => 1),
139
            'ignore-functions' =>
140
                array('short'   => 'in',
141
                      'desc'    => 'Data file name which contains a list of '
142
                                 . 'php function to ignore',
143
                      'default' => 'functions.txt',
144
                      'min'     => 0 , 'max' => 1),
145
            'ignore-constants' =>
146
                array('short'   => 'ic',
147
                      'desc'    => 'Data file name which contains a list of '
148
                                 . 'php constant to ignore',
149
                      'default' => 'constants.txt',
150
                      'min'     => 0 , 'max' => 1),
151
            'ignore-extensions' =>
152
                array('short'   => 'ie',
153
                      'desc'    => 'Data file name which contains a list of '
154
                                 . 'php extension to ignore',
155
                      'default' => 'extensions.txt',
156
                      'min'     => 0 , 'max' => 1),
157
            'ignore-versions' =>
158
                array('short'   => 'iv',
159
                      'desc'    => 'PHP versions - functions to exclude '
160
                                 . 'when parsing source code',
161
                      'default' => '5.0.0',
162
                      'min'     => 0 , 'max' => 2),
163
            'ignore-functions-match' =>
164
                array('short'   => 'inm',
165
                      'desc'    => 'Data file name which contains a list of '
166
                                 . 'php function pattern to ignore',
167
                      'default' => 'functions-match.txt',
168
                      'min'     => 0 , 'max' => 1),
169
            'ignore-extensions-match' =>
170
                array('short'   => 'iem',
171
                      'desc'    => 'Data file name which contains a list of '
172
                                 . 'php extension pattern to ignore',
173
                      'default' => 'extensions-match.txt',
174
                      'min'     => 0 , 'max' => 1),
175
            'ignore-constants-match' =>
176
                array('short'   => 'icm',
177
                      'desc'    => 'Data file name which contains a list of '
178
                                 . 'php constant pattern to ignore',
179
                      'default' => 'constants-match.txt',
180
                      'min'     => 0 , 'max' => 1),
181
            'file-ext' =>
182
                array('short'   => 'fe',
183
                      'desc'    => 'A comma separated list of file extensions '
184
                                 . 'to parse (only valid if parsing a directory)',
185
                      'default' => 'php, php4, inc, phtml',
186
                      'min'     => 0 , 'max' => 1),
187
            'report' =>
188
                array('short' => 'r',
189
                      'desc' => 'Print either "xml" or "csv" report',
190
                      'default' => 'text',
191
                      'min'   => 0 , 'max' => 1),
192
            'output-level' =>
193
                array('short' => 'o',
194
                      'desc' => 'Print Path/File + Version with additional data',
195
                      'default' => 31,
196
                      'min'   => 0 , 'max' => 1),
197
            'tab' =>
198
                array('short' => 't',
199
                      'desc'  => 'Columns width',
200
                      'default' => '29,12,20',
201
                      'min'   => 0 , 'max' => 1),
202
            'progress' =>
203
                array('short' => 'p',
204
                      'desc' => 'Show a wait message [text] or a progress bar [bar]',
205
                      'default' => 'bar',
206
                      'min'   => 0 , 'max' => 1),
207
            'summarize' =>
208
                array('short' => 'S',
209
                      'desc' => 'Print only summary when parsing directory',
210
                      'max'   => 0),
211
            'version' =>
212
                array('short' => 'V',
213
                      'desc'  => 'Print version information',
214
                      'max'   => 0),
215
            'help' =>
216
                array('short' => 'h',
217
                      'desc'  => 'Show this help',
218
                      'max'   => 0),
219
        );
220
    }
221
 
222
    /**
223
     * Command-Line Class constructor
224
     *
225
     * Command-Line Class constructor (ZE1) for PHP4
226
     *
227
     * @since  version 0.8.0 (2004-04-22)
228
     */
229
    function PHP_CompatInfo_Cli()
230
    {
231
        $this->__construct();
232
    }
233
 
234
    /**
235
     * Run the CLI version
236
     *
237
     * Run the CLI version of PHP_CompatInfo
238
     *
239
     * @return void
240
     * @access public
241
     * @since  version 0.8.0 (2004-04-22)
242
     */
243
    function run()
244
    {
245
        $args = & Console_Getargs::factory($this->opts);
246
        if (PEAR::isError($args)) {
247
            if ($args->getCode() === CONSOLE_GETARGS_HELP) {
248
                $error = '';
249
            } else {
250
                $error = $args->getMessage();
251
            }
252
            $this->_printUsage($error);
253
            return;
254
        }
255
 
256
        // default parser options
257
        $this->options = array(
258
            'file_ext' => array('php', 'php4', 'inc', 'phtml'),
259
            'recurse_dir' => true,
260
            'debug' => false,
261
            'is_string' => false,
262
            'ignore_files' => array(),
263
            'ignore_dirs' => array()
264
            );
265
 
266
        // version
267
        $V = $args->getValue('V');
268
        if (isset($V)) {
269
            $error = 'PHP_CompatInfo (cli) version 1.9.0'
270
                   . ' (http://pear.php.net/package/PHP_CompatInfo)';
271
            echo $error;
272
            return;
273
        }
274
 
275
        // debug
276
        if ($args->isDefined('v')) {
277
            $v = $args->getValue('v');
278
            if ($v > 3) {
279
                $this->options['debug'] = true;
280
            }
281
        }
282
 
283
        // no-recurse
284
        if ($args->isDefined('n')) {
285
            $this->options['recurse_dir'] = false;
286
        }
287
 
288
        // dir
289
        if ($args->isDefined('d')) {
290
            $d = $args->getValue('d');
291
            if (file_exists($d)) {
292
                if ($d{strlen($d)-1} == '/' || $d{strlen($d)-1} == '\\') {
293
                    $d = substr($d, 0, -1);
294
                }
295
                $this->dataSource = realpath($d);
296
            } else {
297
                $error = 'Failed opening directory "' . $d
298
                       . '". Please check your spelling and try again.';
299
                $this->_printUsage($error);
300
                return;
301
            }
302
        }
303
 
304
        // file
305
        if ($args->isDefined('f')) {
306
            $f = $args->getValue('f');
307
            if (file_exists($f)) {
308
                $this->dataSource = $f;
309
            } else {
310
                $error = 'Failed opening file "' . $f
311
                       . '". Please check your spelling and try again.';
312
                $this->_printUsage($error);
313
                return;
314
            }
315
        }
316
 
317
        // string
318
        if ($args->isDefined('s')) {
319
            $s = $args->getValue('s');
320
            if (!empty($s)) {
321
                $this->dataSource           = sprintf("<?php %s ?>", $s);
322
                $this->options['is_string'] = true;
323
            } else {
324
                $error = 'Failed opening string "' . $s
325
                       . '". Please check your spelling and try again.';
326
                $this->_printUsage($error);
327
                return;
328
            }
329
        }
330
 
331
        // ignore-files
332
        $if = $args->getValue('if');
333
        if (isset($if)) {
334
            if (file_exists($if)) {
335
                $options                       = $this->_parseParamFile($if);
336
                $this->options['ignore_files'] = $options['std'];
337
            } else {
338
                $error = 'Failed opening file "' . $if
339
                       . '" (ignore-files option). '
340
                       . 'Please check your spelling and try again.';
341
                $this->_printUsage($error);
342
                return;
343
            }
344
        }
345
 
346
        // ignore-dirs
347
        $id = $args->getValue('id');
348
        if (isset($id)) {
349
            if (file_exists($id)) {
350
                $options                      = $this->_parseParamFile($id);
351
                $this->options['ignore_dirs'] = $options['std'];
352
            } else {
353
                $error = 'Failed opening file "' . $id
354
                       . '" (ignore-dirs option). '
355
                       . 'Please check your spelling and try again.';
356
                $this->_printUsage($error);
357
                return;
358
            }
359
        }
360
 
361
        // ignore-functions
362
        $in = $args->getValue('in');
363
        if (isset($in)) {
364
            if (file_exists($in)) {
365
                $options                           = $this->_parseParamFile($in);
366
                $this->options['ignore_functions'] = $options['std'];
367
            } else {
368
                $error = 'Failed opening file "' . $in
369
                       . '" (ignore-functions option). '
370
                       . 'Please check your spelling and try again.';
371
                $this->_printUsage($error);
372
                return;
373
            }
374
        }
375
 
376
        // ignore-constants
377
        $ic = $args->getValue('ic');
378
        if (isset($ic)) {
379
            if (file_exists($ic)) {
380
                $options                           = $this->_parseParamFile($ic);
381
                $this->options['ignore_constants'] = $options['std'];
382
            } else {
383
                $error = 'Failed opening file "' . $ic
384
                       . '" (ignore-constants option). '
385
                       . 'Please check your spelling and try again.';
386
                $this->_printUsage($error);
387
                return;
388
            }
389
        }
390
 
391
        // ignore-extensions
392
        $ie = $args->getValue('ie');
393
        if (isset($ie)) {
394
            if (file_exists($ie)) {
395
                $options                            = $this->_parseParamFile($ie);
396
                $this->options['ignore_extensions'] = $options['std'];
397
            } else {
398
                $error = 'Failed opening file "' . $ie
399
                       . '" (ignore-extensions option). '
400
                       . 'Please check your spelling and try again.';
401
                $this->_printUsage($error);
402
                return;
403
            }
404
        }
405
 
406
        // ignore-versions
407
        $iv = $args->getValue('iv');
408
        if (isset($iv)) {
409
            if (!is_array($iv)) {
410
                $iv = array($iv);
411
            }
412
            $this->options['ignore_versions'] = $iv;
413
        }
414
 
415
        // ignore-functions-match
416
        $inm = $args->getValue('inm');
417
        if (isset($inm)) {
418
            if (file_exists($inm)) {
419
                $patterns = $this->_parseParamFile($inm, true);
420
                if (count($patterns['std']) > 0
421
                    && count($patterns['reg']) > 0) {
422
                    $error = 'Mixed "function_exists" and '
423
                           . '"preg_match" conditions are not allowed. '
424
                           . 'Please check your spelling and try again.';
425
                    $this->_printUsage($error);
426
                    return;
427
 
428
                } elseif (count($patterns['std']) > 0) {
429
                    $this->options['ignore_functions_match']
430
                        = array('function_exists', $patterns['std']);
431
                } elseif (count($patterns['reg']) > 0) {
432
                    $this->options['ignore_functions_match']
433
                        = array('preg_match', $patterns['reg']);
434
                }
435
            } else {
436
                $error = 'Failed opening file "' . $inm
437
                       . '" (ignore-functions-match option). '
438
                       . 'Please check your spelling and try again.';
439
                $this->_printUsage($error);
440
                return;
441
            }
442
        }
443
 
444
        // ignore-extensions-match
445
        $iem = $args->getValue('iem');
446
        if (isset($iem)) {
447
            if (file_exists($iem)) {
448
                $patterns = $this->_parseParamFile($iem, true);
449
                if (count($patterns['std']) > 0
450
                    && count($patterns['reg']) > 0) {
451
                    $error = 'Mixed "extension_loaded" and '
452
                           . '"preg_match" conditions are not allowed. '
453
                           . 'Please check your spelling and try again.';
454
                    $this->_printUsage($error);
455
                    return;
456
 
457
                } elseif (count($patterns['std']) > 0) {
458
                    $this->options['ignore_extensions_match']
459
                        = array('extension_loaded', $patterns['std']);
460
                } elseif (count($patterns['reg']) > 0) {
461
                    $this->options['ignore_extensions_match']
462
                        = array('preg_match', $patterns['reg']);
463
                }
464
            } else {
465
                $error = 'Failed opening file "' . $iem
466
                       . '" (ignore-extensions-match option). '
467
                       . 'Please check your spelling and try again.';
468
                $this->_printUsage($error);
469
                return;
470
            }
471
        }
472
 
473
        // ignore-constants-match
474
        $icm = $args->getValue('icm');
475
        if (isset($icm)) {
476
            if (file_exists($icm)) {
477
                $patterns = $this->_parseParamFile($icm, true);
478
                if (count($patterns['std']) > 0
479
                    && count($patterns['reg']) > 0) {
480
                    $error = 'Mixed "defined" and '
481
                           . '"preg_match" conditions are not allowed. '
482
                           . 'Please check your spelling and try again.';
483
                    $this->_printUsage($error);
484
                    return;
485
 
486
                } elseif (count($patterns['std']) > 0) {
487
                    $this->options['ignore_constants_match']
488
                        = array('defined', $patterns['std']);
489
                } elseif (count($patterns['reg']) > 0) {
490
                    $this->options['ignore_constants_match']
491
                        = array('preg_match', $patterns['reg']);
492
                }
493
            } else {
494
                $error = 'Failed opening file "' . $icm
495
                       . '" (ignore-constants-match option). '
496
                       . 'Please check your spelling and try again.';
497
                $this->_printUsage($error);
498
                return;
499
            }
500
        }
501
 
502
        // file-ext
503
        if ($args->isDefined('d') && $args->isDefined('fe')) {
504
            $fe = $args->getValue('fe');
505
            if (is_string($fe)) {
506
                $this->options['file_ext'] = explode(',', $fe);
507
            } else {
508
                $error = 'No valid file extensions provided "'
509
                       . '". Please check your spelling and try again.';
510
                $this->_printUsage($error);
511
                return;
512
            }
513
        }
514
 
515
        // file or directory options are minimum required to work
516
        if (!$args->isDefined('f')
517
            && !$args->isDefined('d')
518
            && !$args->isDefined('s')) {
519
            $error = 'ERROR: You must supply at least '
520
                   . 'one string, file or directory to process';
521
            $this->_printUsage($error);
522
            return;
523
        }
524
 
525
        if ($args->isDefined('r')) {
526
            $report = $args->getValue('r');
527
        } else {
528
            $report = 'text';
529
        }
530
 
531
        if ($args->isDefined('t')) {
532
            $defs = array('f' => 29, 'e' => 12, 'c' => 20);
533
            $tabs = $args->getValue('t');
534
            $tabs = explode(',', $tabs);
535
            for ($t = 0; $t < 3; $t++) {
536
                if (isset($tabs[$t])) {
537
                    if ($t == 0) {
538
                        $defs['f'] = (int)$tabs[$t];
539
                    } elseif ($t == 1) {
540
                        $defs['e'] = (int)$tabs[$t];
541
                    } else {
542
                        $defs['c'] = (int)$tabs[$t];
543
                    }
544
                }
545
            }
546
            $conf = array('colwidth' => $defs);
547
        } else {
548
            $conf = array();
549
        }
550
        $conf = array_merge($conf, array('args' => $args->getValues()));
551
 
552
        $compatInfo = new PHP_CompatInfo($report, $conf);
553
 
554
        // dir
555
        if ($args->isDefined('d')) {
556
            $d     = $args->getValue('d');
557
            $files = $compatInfo->parser->getFilelist($d, $this->options);
558
            if (count($files) == 0) {
559
                $error = 'No valid files into directory "'. $d
560
                       . '". Please check your spelling and try again.';
561
                $this->_printUsage($error);
562
                return;
563
            }
564
        }
565
        $compatInfo->parseData($this->dataSource, $this->options);
566
    }
567
 
568
    /**
569
     * Parse content of parameter files
570
     *
571
     * Parse content of parameter files used by switches
572
     * <ul>
573
     * <li>ignore-files
574
     * <li>ignore-dirs
575
     * <li>ignore-functions
576
     * <li>ignore-constants
577
     * <li>ignore-extensions
578
     * <li>ignore-functions-match
579
     * <li>ignore-extensions-match
580
     * <li>ignore-constants-match
581
     * </ul>
582
     *
583
     * @param string $fn          Parameter file name
584
     * @param bool   $withPattern TRUE if the file may contain regular expression
585
     *
586
     * @return array
587
     * @access private
588
     * @since  version 1.7.0b4 (2008-04-03)
589
     */
590
    function _parseParamFile($fn, $withPattern = false)
591
    {
592
        $lines    = file($fn);
593
        $patterns = array('std' => array(), 'reg' => array());
594
        foreach ($lines as $line) {
595
            $line = rtrim($line);  // remove line ending
596
            if (strlen($line) == 0) {
597
                continue;  // skip empty lines
598
            }
599
            if ($line{0} == ';') {
600
                continue;  // skip this pattern: consider as comment line
601
            }
602
            if ($line{0} == '=') {
603
                list($p, $s)       = explode('=', $line);
604
                $patterns['reg'][] = '/'.$s.'/';
605
            } else {
606
                if ($withPattern === true) {
607
                    $patterns['std'][] = '/'.$line.'/';
608
                } else {
609
                    $patterns['std'][] = $line;
610
                }
611
            }
612
        }
613
        return $patterns;
614
    }
615
 
616
    /**
617
     * Show full help information
618
     *
619
     * @param string $footer (optional) page footer content
620
     *
621
     * @return void
622
     * @access private
623
     * @since  version 0.8.0 (2004-04-22)
624
     */
625
    function _printUsage($footer = '')
626
    {
627
        $header = 'Usage: '
628
            . basename($_SERVER['SCRIPT_NAME']) . " [options]\n\n";
629
        echo Console_Getargs::getHelp($this->opts, $header,
630
            "\n$footer\n", 78, 2)."\n";
631
    }
632
}
633
?>