Ubiquity  2.0.3
php rapid development framework
CodeUtils.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class CodeUtils {
8 
9  public static function cleanParameters($parameters) {
10  $optional = false;
11  $tmpResult = [ ];
12  $params = \explode ( ",", $parameters );
13  foreach ( $params as $param ) {
14  $param = \trim ( $param );
15  $list = \explode ( "=", $param );
16  if(isset($list[0])){
17  $var=$list[0];
18  }
19  if(isset($list[1])){
20  $value=$list[1];
21  }
22  if (isset ( $var ) && isset ( $value )) {
23  $value = \trim ( $value );
24  $var = self::checkVar ( $var );
25  $tmpResult [] = $var . '=' . $value;
26  $optional = true;
27  } elseif (isset ( $var )) {
28  $var = self::checkVar ( $var );
29  if ($optional)
30  $tmpResult [] = $var . "=''";
31  else
32  $tmpResult [] = $var;
33  }
34  }
35  return \implode ( ',', $tmpResult );
36  }
37 
38  public static function getParametersForRoute($parameters) {
39  $tmpResult = [ ];
40  $params = \explode ( ",", $parameters );
41  foreach ( $params as $param ) {
42  $param = \trim ( $param );
43  $list = \explode ( "=", $param );
44  if(isset($list[0])){
45  $var=$list[0];
46  }
47  if(isset($list[1])){
48  $value=$list[1];
49  }
50  if (isset ( $var ) && isset ( $value )) {
51  break;
52  } elseif (isset ( $var )) {
53  $var = self::unCheckVar ( $var );
54  $tmpResult [] = '{' . $var . '}';
55  }
56  }
57  return $tmpResult;
58  }
59 
60  public static function checkVar($var, $prefix = '$') {
61  if (UString::isNull ( $var ))
62  return "";
63  $var = \trim ( $var );
64  if (! UString::startswith ( $var, $prefix )) {
65  $var = $prefix . $var;
66  }
67  return $var;
68  }
69 
70  public static function unCheckVar($var, $prefix = '$') {
71  if (UString::isNull ( $var ))
72  return "";
73  $var = \trim ( $var );
74  if (UString::startswith ( $var, $prefix )) {
75  $var = \substr ( $var, \sizeof ( $prefix ) );
76  }
77  return $var;
78  }
79 
80  public static function indent($code, $count = 2) {
81  $tab = \str_repeat ( "\t", $count );
82  $lines = \explode ( "\n", $code );
83  return $tab . \implode ( $tab, $lines );
84  }
85 
86  public static function isValidCode($code) {
87  $output = [ ];
88  $result = 1;
89  $temp_file = tempnam ( sys_get_temp_dir (), 'Tux' );
90  $fp = fopen ( $temp_file, "w" );
91  fwrite ( $fp, $code );
92  fclose ( $fp );
93  if (file_exists ( $temp_file )) {
94  $phpExe = self::getPHPExecutable ();
95  if (isset ( $phpExe )) {
96  exec ( $phpExe . ' -l ' . $temp_file, $output, $result );
97  }
98  $output = implode ( "", $output );
99  \unlink ( $temp_file );
100  if (strpos ( $output, 'No syntax errors detected' ) === false && $result !== 1) {
101  return false;
102  }
103  }
104  return true;
105  }
106 
112  public static function getPHPExecutable() {
113  if (defined ( 'PHP_BINARY' ) && PHP_BINARY && in_array ( PHP_SAPI, array ('cli','cli-server' ) ) && is_file ( PHP_BINARY )) {
114  return PHP_BINARY;
115  } else if (strtoupper ( substr ( PHP_OS, 0, 3 ) ) === 'WIN') {
116  $paths = explode ( PATH_SEPARATOR, getenv ( 'PATH' ) );
117  foreach ( $paths as $path ) {
118  if (substr ( $path, strlen ( $path ) - 1 ) == DIRECTORY_SEPARATOR) {
119  $path = substr ( $path, 0, strlen ( $path ) - 1 );
120  }
121  if (substr ( $path, strlen ( $path ) - strlen ( 'php' ) ) == 'php') {
122  $response = $path . DIRECTORY_SEPARATOR . 'php.exe';
123  if (is_file ( $response )) {
124  return $response;
125  }
126  } else if (substr ( $path, strlen ( $path ) - strlen ( 'php.exe' ) ) == 'php.exe') {
127  if (is_file ( $response )) {
128  return $response;
129  }
130  }
131  }
132  } else {
133  $paths = explode ( PATH_SEPARATOR, getenv ( 'PATH' ) );
134  foreach ( $paths as $path ) {
135  if (substr ( $path, strlen ( $path ) - 1 ) == DIRECTORY_SEPARATOR) {
136  $path = substr ( $path, strlen ( $path ) - 1 );
137  }
138  if (substr ( $path, strlen ( $path ) - strlen ( 'php' ) ) == 'php') {
139  if (is_file ( $path )) {
140  return $path;
141  }
142  $response = $path . DIRECTORY_SEPARATOR . 'php';
143  if (is_file ( $response )) {
144  return $response;
145  }
146  }
147  }
148  }
149  return null;
150  }
151 }
static unCheckVar($var, $prefix='$')
Definition: CodeUtils.php:70
static checkVar($var, $prefix='$')
Definition: CodeUtils.php:60
static startswith($hay, $needle)
Definition: UString.php:12