annotate vendor/sabre/vobject/lib/Cli.php @ 7:430dbd5346f7

vendor sabre as distributed
author Charlie Root
date Sat, 13 Jan 2018 09:06:10 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
1 <?php
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
2
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
3 namespace Sabre\VObject;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
4
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
5 use
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
6 InvalidArgumentException;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
7
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
8 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
9 * This is the CLI interface for sabre-vobject.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
10 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
11 * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
12 * @author Evert Pot (http://evertpot.com/)
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
13 * @license http://sabre.io/license/ Modified BSD License
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
14 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
15 class Cli {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
16
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
17 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
18 * No output
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
19 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
20 * @var bool
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
21 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
22 protected $quiet = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
23
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
24 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
25 * Help display
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
26 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
27 * @var bool
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
28 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
29 protected $showHelp = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
30
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
31 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
32 * Wether to spit out 'mimedir' or 'json' format.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
33 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
34 * @var string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
35 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
36 protected $format;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
37
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
38 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
39 * JSON pretty print
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
40 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
41 * @var bool
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
42 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
43 protected $pretty;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
44
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
45 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
46 * Source file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
47 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
48 * @var string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
49 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
50 protected $inputPath;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
51
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
52 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
53 * Destination file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
54 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
55 * @var string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
56 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
57 protected $outputPath;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
58
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
59 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
60 * output stream
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
61 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
62 * @var resource
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
63 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
64 protected $stdout;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
65
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
66 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
67 * stdin
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
68 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
69 * @var resource
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
70 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
71 protected $stdin;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
72
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
73 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
74 * stderr
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
75 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
76 * @var resource
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
77 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
78 protected $stderr;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
79
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
80 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
81 * Input format (one of json or mimedir)
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
82 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
83 * @var string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
84 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
85 protected $inputFormat;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
86
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
87 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
88 * Makes the parser less strict.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
89 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
90 * @var bool
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
91 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
92 protected $forgiving = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
93
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
94 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
95 * Main function
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
96 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
97 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
98 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
99 public function main(array $argv) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
100
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
101 // @codeCoverageIgnoreStart
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
102 // We cannot easily test this, so we'll skip it. Pretty basic anyway.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
103
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
104 if (!$this->stderr) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
105 $this->stderr = fopen('php://stderr', 'w');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
106 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
107 if (!$this->stdout) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
108 $this->stdout = fopen('php://stdout', 'w');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
109 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
110 if (!$this->stdin) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
111 $this->stdin = fopen('php://stdin', 'r');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
112 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
113
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
114 // @codeCoverageIgnoreEnd
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
115
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
116
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
117 try {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
118
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
119 list($options, $positional) = $this->parseArguments($argv);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
120
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
121 if (isset($options['q'])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
122 $this->quiet = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
123 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
124 $this->log($this->colorize('green', "sabre/vobject ") . $this->colorize('yellow', Version::VERSION));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
125
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
126 foreach($options as $name=>$value) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
127
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
128 switch($name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
129
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
130 case 'q' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
131 // Already handled earlier.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
132 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
133 case 'h' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
134 case 'help' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
135 $this->showHelp();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
136 return 0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
137 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
138 case 'format' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
139 switch($value) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
140
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
141 // jcard/jcal documents
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
142 case 'jcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
143 case 'jcal' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
144
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
145 // specific document versions
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
146 case 'vcard21' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
147 case 'vcard30' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
148 case 'vcard40' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
149 case 'icalendar20' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
150
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
151 // specific formats
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
152 case 'json' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
153 case 'mimedir' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
154
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
155 // icalendar/vcad
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
156 case 'icalendar' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
157 case 'vcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
158 $this->format = $value;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
159 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
160
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
161 default :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
162 throw new InvalidArgumentException('Unknown format: ' . $value);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
163
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
164 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
165 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
166 case 'pretty' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
167 if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
168 $this->pretty = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
169 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
170 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
171 case 'forgiving' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
172 $this->forgiving = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
173 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
174 case 'inputformat' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
175 switch($value) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
176 // json formats
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
177 case 'jcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
178 case 'jcal' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
179 case 'json' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
180 $this->inputFormat = 'json';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
181 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
182
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
183 // mimedir formats
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
184 case 'mimedir' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
185 case 'icalendar' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
186 case 'vcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
187 case 'vcard21' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
188 case 'vcard30' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
189 case 'vcard40' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
190 case 'icalendar20' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
191
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
192 $this->inputFormat = 'mimedir';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
193 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
194
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
195 default :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
196 throw new InvalidArgumentException('Unknown format: ' . $value);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
197
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
198 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
199 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
200 default :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
201 throw new InvalidArgumentException('Unknown option: ' . $name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
202
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
203 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
204
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
205 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
206
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
207 if (count($positional) === 0) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
208 $this->showHelp();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
209 return 1;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
210 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
211
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
212 if (count($positional) === 1) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
213 throw new InvalidArgumentException('Inputfile is a required argument');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
214 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
215
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
216 if (count($positional) > 3) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
217 throw new InvalidArgumentException('Too many arguments');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
218 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
219
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
220 if (!in_array($positional[0], array('validate','repair','convert','color'))) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
221 throw new InvalidArgumentException('Uknown command: ' . $positional[0]);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
222 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
223
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
224 } catch (InvalidArgumentException $e) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
225 $this->showHelp();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
226 $this->log('Error: ' . $e->getMessage(), 'red');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
227 return 1;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
228 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
229
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
230 $command = $positional[0];
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
231
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
232 $this->inputPath = $positional[1];
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
233 $this->outputPath = isset($positional[2])?$positional[2]:'-';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
234
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
235 if ($this->outputPath !== '-') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
236 $this->stdout = fopen($this->outputPath, 'w');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
237 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
238
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
239 if (!$this->inputFormat) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
240 if (substr($this->inputPath, -5)==='.json') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
241 $this->inputFormat = 'json';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
242 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
243 $this->inputFormat = 'mimedir';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
244 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
245 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
246 if (!$this->format) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
247 if (substr($this->outputPath,-5)==='.json') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
248 $this->format = 'json';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
249 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
250 $this->format = 'mimedir';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
251 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
252 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
253
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
254
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
255 $realCode = 0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
256
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
257 try {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
258
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
259 while($input = $this->readInput()) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
260
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
261 $returnCode = $this->$command($input);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
262 if ($returnCode!==0) $realCode = $returnCode;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
263
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
264 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
265
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
266 } catch (EofException $e) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
267 // end of file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
268 } catch (\Exception $e) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
269 $this->log('Error: ' . $e->getMessage(),'red');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
270 return 2;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
271 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
272
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
273 return $realCode;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
274
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
275 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
276
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
277 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
278 * Shows the help message.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
279 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
280 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
281 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
282 protected function showHelp() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
283
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
284 $this->log('Usage:', 'yellow');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
285 $this->log(" vobject [options] command [arguments]");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
286 $this->log('');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
287 $this->log('Options:', 'yellow');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
288 $this->log($this->colorize('green', ' -q ') . "Don't output anything.");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
289 $this->log($this->colorize('green', ' -help -h ') . "Display this help message.");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
290 $this->log($this->colorize('green', ' --format ') . "Convert to a specific format. Must be one of: vcard, vcard21,");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
291 $this->log($this->colorize('green', ' --forgiving ') . "Makes the parser less strict.");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
292 $this->log(" vcard30, vcard40, icalendar20, jcal, jcard, json, mimedir.");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
293 $this->log($this->colorize('green', ' --inputformat ') . "If the input format cannot be guessed from the extension, it");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
294 $this->log(" must be specified here.");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
295 // Only PHP 5.4 and up
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
296 if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
297 $this->log($this->colorize('green', ' --pretty ') . "json pretty-print.");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
298 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
299 $this->log('');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
300 $this->log('Commands:', 'yellow');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
301 $this->log($this->colorize('green', ' validate') . ' source_file Validates a file for correctness.');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
302 $this->log($this->colorize('green', ' repair') . ' source_file [output_file] Repairs a file.');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
303 $this->log($this->colorize('green', ' convert') . ' source_file [output_file] Converts a file.');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
304 $this->log($this->colorize('green', ' color') . ' source_file Colorize a file, useful for debbugging.');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
305 $this->log(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
306 <<<HELP
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
307
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
308 If source_file is set as '-', STDIN will be used.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
309 If output_file is omitted, STDOUT will be used.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
310 All other output is sent to STDERR.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
311
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
312 HELP
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
313 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
314
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
315 $this->log('Examples:', 'yellow');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
316 $this->log(' vobject convert contact.vcf contact.json');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
317 $this->log(' vobject convert --format=vcard40 old.vcf new.vcf');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
318 $this->log(' vobject convert --inputformat=json --format=mimedir - -');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
319 $this->log(' vobject color calendar.ics');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
320 $this->log('');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
321 $this->log('https://github.com/fruux/sabre-vobject','purple');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
322
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
323 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
324
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
325 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
326 * Validates a VObject file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
327 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
328 * @param Component $vObj
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
329 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
330 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
331 protected function validate($vObj) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
332
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
333 $returnCode = 0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
334
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
335 switch($vObj->name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
336 case 'VCALENDAR' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
337 $this->log("iCalendar: " . (string)$vObj->VERSION);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
338 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
339 case 'VCARD' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
340 $this->log("vCard: " . (string)$vObj->VERSION);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
341 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
342 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
343
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
344 $warnings = $vObj->validate();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
345 if (!count($warnings)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
346 $this->log(" No warnings!");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
347 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
348
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
349 $levels = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
350 1 => 'REPAIRED',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
351 2 => 'WARNING',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
352 3 => 'ERROR',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
353 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
354 $returnCode = 2;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
355 foreach($warnings as $warn) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
356
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
357 $extra = '';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
358 if ($warn['node'] instanceof Property) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
359 $extra = ' (property: "' . $warn['node']->name . '")';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
360 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
361 $this->log(" [" . $levels[$warn['level']] . '] ' . $warn['message'] . $extra);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
362
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
363 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
364
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
365 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
366
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
367 return $returnCode;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
368
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
369 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
370
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
371 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
372 * Repairs a VObject file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
373 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
374 * @param Component $vObj
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
375 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
376 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
377 protected function repair($vObj) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
378
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
379 $returnCode = 0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
380
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
381 switch($vObj->name) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
382 case 'VCALENDAR' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
383 $this->log("iCalendar: " . (string)$vObj->VERSION);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
384 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
385 case 'VCARD' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
386 $this->log("vCard: " . (string)$vObj->VERSION);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
387 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
388 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
389
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
390 $warnings = $vObj->validate(Node::REPAIR);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
391 if (!count($warnings)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
392 $this->log(" No warnings!");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
393 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
394
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
395 $levels = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
396 1 => 'REPAIRED',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
397 2 => 'WARNING',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
398 3 => 'ERROR',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
399 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
400 $returnCode = 2;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
401 foreach($warnings as $warn) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
402
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
403 $extra = '';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
404 if ($warn['node'] instanceof Property) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
405 $extra = ' (property: "' . $warn['node']->name . '")';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
406 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
407 $this->log(" [" . $levels[$warn['level']] . '] ' . $warn['message'] . $extra);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
408
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
409 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
410
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
411 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
412 fwrite($this->stdout, $vObj->serialize());
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
413
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
414 return $returnCode;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
415
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
416 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
417
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
418 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
419 * Converts a vObject file to a new format.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
420 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
421 * @param Component $vObj
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
422 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
423 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
424 protected function convert($vObj) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
425
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
426 $json = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
427 $convertVersion = null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
428 $forceInput = null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
429
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
430 switch($this->format) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
431 case 'json' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
432 $json = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
433 if ($vObj->name === 'VCARD') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
434 $convertVersion = Document::VCARD40;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
435 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
436 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
437 case 'jcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
438 $json = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
439 $forceInput = 'VCARD';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
440 $convertVersion = Document::VCARD40;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
441 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
442 case 'jcal' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
443 $json = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
444 $forceInput = 'VCALENDAR';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
445 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
446 case 'mimedir' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
447 case 'icalendar' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
448 case 'icalendar20' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
449 case 'vcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
450 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
451 case 'vcard21' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
452 $convertVersion = Document::VCARD21;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
453 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
454 case 'vcard30' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
455 $convertVersion = Document::VCARD30;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
456 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
457 case 'vcard40' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
458 $convertVersion = Document::VCARD40;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
459 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
460
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
461 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
462
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
463 if ($forceInput && $vObj->name !== $forceInput) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
464 throw new \Exception('You cannot convert a ' . strtolower($vObj->name) . ' to ' . $this->format);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
465 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
466 if ($convertVersion) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
467 $vObj = $vObj->convert($convertVersion);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
468 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
469 if ($json) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
470 $jsonOptions = 0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
471 if ($this->pretty) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
472 $jsonOptions = JSON_PRETTY_PRINT;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
473 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
474 fwrite($this->stdout, json_encode($vObj->jsonSerialize(), $jsonOptions));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
475 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
476 fwrite($this->stdout, $vObj->serialize());
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
477 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
478
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
479 return 0;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
480
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
481 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
482
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
483 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
484 * Colorizes a file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
485 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
486 * @param Component $vObj
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
487 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
488 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
489 protected function color($vObj) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
490
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
491 fwrite($this->stdout, $this->serializeComponent($vObj));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
492
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
493 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
494
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
495 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
496 * Returns an ansi color string for a color name.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
497 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
498 * @param string $color
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
499 * @return string
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
500 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
501 protected function colorize($color, $str, $resetTo = 'default') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
502
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
503 $colors = array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
504 'cyan' => '1;36',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
505 'red' => '1;31',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
506 'yellow' => '1;33',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
507 'blue' => '0;34',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
508 'green' => '0;32',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
509 'default' => '0',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
510 'purple' => '0;35',
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
511 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
512 return "\033[" . $colors[$color] . 'm' . $str . "\033[".$colors[$resetTo]."m";
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
513
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
514 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
515
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
516 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
517 * Writes out a string in specific color.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
518 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
519 * @param string $color
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
520 * @param string $str
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
521 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
522 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
523 protected function cWrite($color, $str) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
524
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
525 fwrite($this->stdout, $this->colorize($color, $str));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
526
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
527 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
528
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
529 protected function serializeComponent(Component $vObj) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
530
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
531 $this->cWrite('cyan', 'BEGIN');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
532 $this->cWrite('red', ':');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
533 $this->cWrite('yellow', $vObj->name . "\n");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
534
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
535 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
536 * Gives a component a 'score' for sorting purposes.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
537 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
538 * This is solely used by the childrenSort method.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
539 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
540 * A higher score means the item will be lower in the list.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
541 * To avoid score collisions, each "score category" has a reasonable
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
542 * space to accomodate elements. The $key is added to the $score to
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
543 * preserve the original relative order of elements.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
544 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
545 * @param int $key
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
546 * @param array $array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
547 * @return int
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
548 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
549 $sortScore = function($key, $array) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
550
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
551 if ($array[$key] instanceof Component) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
552
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
553 // We want to encode VTIMEZONE first, this is a personal
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
554 // preference.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
555 if ($array[$key]->name === 'VTIMEZONE') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
556 $score=300000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
557 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
558 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
559 $score=400000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
560 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
561 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
562 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
563 // Properties get encoded first
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
564 // VCARD version 4.0 wants the VERSION property to appear first
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
565 if ($array[$key] instanceof Property) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
566 if ($array[$key]->name === 'VERSION') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
567 $score=100000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
568 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
569 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
570 // All other properties
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
571 $score=200000000;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
572 return $score+$key;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
573 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
574 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
575 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
576
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
577 };
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
578
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
579 $tmp = $vObj->children;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
580 uksort(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
581 $vObj->children,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
582 function($a, $b) use ($sortScore, $tmp) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
583
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
584 $sA = $sortScore($a, $tmp);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
585 $sB = $sortScore($b, $tmp);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
586
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
587 return $sA - $sB;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
588
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
589 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
590 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
591
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
592 foreach($vObj->children as $child) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
593 if ($child instanceof Component) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
594 $this->serializeComponent($child);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
595 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
596 $this->serializeProperty($child);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
597 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
598 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
599
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
600 $this->cWrite('cyan', 'END');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
601 $this->cWrite('red', ':');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
602 $this->cWrite('yellow', $vObj->name . "\n");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
603
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
604 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
605
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
606 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
607 * Colorizes a property.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
608 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
609 * @param Property $property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
610 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
611 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
612 protected function serializeProperty(Property $property) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
613
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
614 if ($property->group) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
615 $this->cWrite('default', $property->group);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
616 $this->cWrite('red', '.');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
617 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
618
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
619 $str = '';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
620 $this->cWrite('yellow', $property->name);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
621
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
622 foreach($property->parameters as $param) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
623
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
624 $this->cWrite('red',';');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
625 $this->cWrite('blue', $param->serialize());
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
626
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
627 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
628 $this->cWrite('red',':');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
629
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
630 if ($property instanceof Property\Binary) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
631
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
632 $this->cWrite('default', 'embedded binary stripped. (' . strlen($property->getValue()) . ' bytes)');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
633
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
634 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
635
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
636 $parts = $property->getParts();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
637 $first1 = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
638 // Looping through property values
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
639 foreach($parts as $part) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
640 if ($first1) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
641 $first1 = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
642 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
643 $this->cWrite('red', $property->delimiter);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
644 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
645 $first2 = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
646 // Looping through property sub-values
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
647 foreach((array)$part as $subPart) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
648 if ($first2) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
649 $first2 = false;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
650 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
651 // The sub-value delimiter is always comma
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
652 $this->cWrite('red', ',');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
653 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
654
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
655 $subPart = strtr(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
656 $subPart,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
657 array(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
658 '\\' => $this->colorize('purple', '\\\\', 'green'),
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
659 ';' => $this->colorize('purple', '\;', 'green'),
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
660 ',' => $this->colorize('purple', '\,', 'green'),
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
661 "\n" => $this->colorize('purple', "\\n\n\t", 'green'),
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
662 "\r" => "",
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
663 )
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
664 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
665
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
666 $this->cWrite('green', $subPart);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
667 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
668 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
669
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
670 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
671 $this->cWrite("default", "\n");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
672
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
673 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
674
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
675 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
676 * Parses the list of arguments.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
677 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
678 * @param array $argv
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
679 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
680 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
681 protected function parseArguments(array $argv) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
682
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
683 $positional = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
684 $options = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
685
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
686 for($ii=0; $ii < count($argv); $ii++) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
687
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
688 // Skipping the first argument.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
689 if ($ii===0) continue;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
690
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
691 $v = $argv[$ii];
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
692
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
693 if (substr($v,0,2)==='--') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
694 // This is a long-form option.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
695 $optionName = substr($v,2);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
696 $optionValue = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
697 if (strpos($optionName,'=')) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
698 list($optionName, $optionValue) = explode('=', $optionName);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
699 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
700 $options[$optionName] = $optionValue;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
701 } elseif (substr($v,0,1) === '-' && strlen($v)>1) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
702 // This is a short-form option.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
703 foreach(str_split(substr($v,1)) as $option) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
704 $options[$option] = true;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
705 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
706
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
707 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
708
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
709 $positional[] = $v;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
710
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
711 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
712
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
713 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
714
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
715 return array($options, $positional);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
716
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
717 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
718
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
719 protected $parser;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
720
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
721 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
722 * Reads the input file
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
723 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
724 * @return Component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
725 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
726 protected function readInput() {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
727
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
728 if (!$this->parser) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
729 if ($this->inputPath!=='-') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
730 $this->stdin = fopen($this->inputPath,'r');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
731 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
732
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
733 if ($this->inputFormat === 'mimedir') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
734 $this->parser = new Parser\MimeDir($this->stdin, ($this->forgiving?Reader::OPTION_FORGIVING:0));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
735 } else {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
736 $this->parser = new Parser\Json($this->stdin, ($this->forgiving?Reader::OPTION_FORGIVING:0));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
737 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
738 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
739
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
740 return $this->parser->parse();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
741
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
742 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
743
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
744 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
745 * Sends a message to STDERR.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
746 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
747 * @param string $msg
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
748 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
749 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
750 protected function log($msg, $color = 'default') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
751
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
752 if (!$this->quiet) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
753 if ($color!=='default') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
754 $msg = $this->colorize($color, $msg);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
755 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
756 fwrite($this->stderr, $msg . "\n");
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
757 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
758
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
759 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
760
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
761 }