Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
json_concat.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 /*
4  * @author Anakeen
5  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
6  * @package FDL
7 */
8 /**
9  * Concatenate '*-mo.js' JSON localization files
10  *
11  * @author Anakeen 2010
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  */
14 
15 array_shift($argv);
16 
17 $js_out = array();
18 foreach ($argv as $js_file) {
19  $js = file_get_contents($js_file);
20  if ($js === false) {
21  error_log(sprintf("Error reading content from '%s'.", $js_file));
22  exit(1);
23  }
24  $js_dec = json_decode($js);
25  if ($js_dec === null) {
26  error_log(sprintf("Error decoding json from '%s': %s.", $js_file, json_last_errmsg()));
27  exit(2);
28  }
29  foreach ($js_dec as $k => $v) {
30  $js_out[$k] = $v;
31  }
32 }
33 
34 if (count($js_out) <= 0) {
35  exit(0);
36 }
37 
38 $js_enc = json_encode($js_out);
39 if ($js_enc == "") {
40  error_log(sprintf("Error encoding json: %s.", json_last_errmsg()));
41  exit(3);
42 }
43 
44 print $js_enc;
45 
46 exit(0);
47 
48 function json_last_errmsg($errcode = null)
49 {
50  if ($errcode === null) {
51  $errcode = json_last_error();
52  }
53  switch ($errcode) {
54  case JSON_ERROR_NONE:
55  return 'JSON_ERROR_NONE';
56  break;
57 
58  case JSON_ERROR_DEPTH:
59  return 'JSON_ERROR_DEPTH';
60  break;
61 
62  case JSON_ERROR_STATE_MISMATCH:
63  return 'JSON_ERROR_STATE_MISMATCH';
64  break;
65 
66  case JSON_ERROR_CTRL_CHAR:
67  return 'JSON_ERROR_CTRL_CHAR';
68  break;
69 
70  case JSON_ERROR_SYNTAX:
71  return 'JSON_ERROR_SYNTAX';
72  break;
73 
74  case JSON_ERROR_UTF8:
75  return 'JSON_ERROR_UTF8';
76  break;
77  }
78  return sprintf("error code '%s'", $errcode);
79 }
80 ?>
← centre documentaire © anakeen - published under CC License - Dynacase