]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHP/Token/Stream/TextUI/Command.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHP / Token / Stream / TextUI / Command.php
1 <?php
2 /**
3  * php-token-stream
4  *
5  * Copyright (c) 2009-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
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  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @package   PHP_TokenStream
38  * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
39  * @copyright 2009-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41  * @since     File available since Release 1.0.0
42  */
43
44 require_once 'PHP/Token/Stream.php';
45 require_once 'ezc/Base/base.php';
46
47 function __autoload($className)
48 {
49     ezcBase::autoload($className);
50 }
51
52 /**
53  * TextUI frontend for PHP_TokenStream.
54  *
55  * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
56  * @copyright 2009-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
58  * @version   Release: 1.0.1
59  * @link      http://github.com/sebastianbergmann/php-token-stream/tree
60  * @since     Class available since Release 1.0.0
61  */
62 class PHP_Token_Stream_TextUI_Command
63 {
64     /**
65      * Main method.
66      */
67     public static function main()
68     {
69         $input = new ezcConsoleInput;
70
71         $input->registerOption(
72           new ezcConsoleOption(
73             'h',
74             'help',
75             ezcConsoleInput::TYPE_NONE,
76             NULL,
77             FALSE,
78             '',
79             '',
80             array(),
81             array(),
82             FALSE,
83             FALSE,
84             TRUE
85            )
86         );
87
88         $input->registerOption(
89           new ezcConsoleOption(
90             'v',
91             'version',
92             ezcConsoleInput::TYPE_NONE,
93             NULL,
94             FALSE,
95             '',
96             '',
97             array(),
98             array(),
99             FALSE,
100             FALSE,
101             TRUE
102            )
103         );
104
105         try {
106             $input->process();
107         }
108
109         catch (ezcConsoleOptionException $e) {
110             print $e->getMessage() . "\n";
111             exit(1);
112         }
113
114         if ($input->getOption('help')->value) {
115             self::showHelp();
116             exit(0);
117         }
118
119         else if ($input->getOption('version')->value) {
120             self::printVersionString();
121             exit(0);
122         }
123
124         $arguments = $input->getArguments();
125
126         if (empty($arguments)) {
127             self::showHelp();
128             exit(1);
129         }
130
131         self::printVersionString();
132
133         print "Line   Token                          Text\n" .
134               str_repeat('-', 79) . "\n";
135
136         foreach (new PHP_Token_Stream($arguments[0]) as $token) {
137             if ($token instanceof PHP_Token_WHITESPACE) {
138                 $text = '';
139             } else {
140                 $text = str_replace(array("\r", "\n"), '', (string)$token);
141
142                 if (strlen($text) > 40) {
143                     $text = explode("\n", wordwrap($text, 40));
144                     $text = $text[0];
145                 }
146             }
147
148             printf(
149               "%5d  %-30s %s\n",
150               $token->getLine(),
151               str_replace('PHP_Token_', '', get_class($token)),
152               $text
153             );
154         }
155     }
156
157     /**
158      * Shows the help.
159      */
160     protected static function showHelp()
161     {
162         self::printVersionString();
163
164         print <<<EOT
165 Usage: phptok [switches] <file>
166
167   --help                   Prints this usage information.
168   --version                Prints the version and exits.
169
170 EOT;
171     }
172
173     /**
174      * Prints the version string.
175      */
176     protected static function printVersionString()
177     {
178         print "phptok 1.0.1 by Sebastian Bergmann.\n\n";
179     }
180 }
181 ?>