Ubiquity  2.0.2
php rapid development framework
CodeUtils.php
Go to the documentation of this file.
1 <?php
4 
5 class CodeUtils {
6  public static function cleanParameters($parameters){
7  $optional=false;
8  $tmpResult=[];
9  $params=\explode(",", $parameters);
10  foreach ($params as $param){
11  $param=\trim($param);
12  @list($var,$value)=\explode("=", $param);
13  if(isset($var) && isset($value)){
14  $value=\trim($value);
15  $var=self::checkVar($var);
16  $tmpResult[]=$var.'='.$value;
17  $optional=true;
18  }elseif(isset($var)){
19  $var=self::checkVar($var);
20  if($optional)
21  $tmpResult[]=$var."=''";
22  else
23  $tmpResult[]=$var;
24  }
25  }
26  return \implode(',', $tmpResult);
27  }
28 
29  public static function getParametersForRoute($parameters){
30  $tmpResult=[];
31  $params=\explode(",", $parameters);
32  foreach ($params as $param){
33  $param=\trim($param);
34  @list($var,$value)=\explode("=", $param);
35  if(isset($var) && isset($value)){
36  break;
37  }elseif(isset($var)){
38  $var=self::unCheckVar($var);
39  $tmpResult[]='{'.$var.'}';
40  }
41  }
42  return $tmpResult;
43  }
44 
45  public static function checkVar($var,$prefix='$'){
46  if(UString::isNull($var))
47  return "";
48  $var=\trim($var);
49  if(!UString::startswith($var, $prefix)){
50  $var=$prefix.$var;
51  }
52  return $var;
53  }
54 
55  public static function unCheckVar($var,$prefix='$'){
56  if(UString::isNull($var))
57  return "";
58  $var=\trim($var);
59  if(UString::startswith($var, $prefix)){
60  $var=\substr($var, \sizeof($prefix));
61  }
62  return $var;
63  }
64 
65  public static function indent($code,$count=2){
66  $tab=\str_repeat("\t", $count);
67  $lines=\explode("\n",$code);
68  return $tab.\implode($tab, $lines);
69  }
70 
71  public static function isValidCode($code){
72  $temp_file = tempnam(sys_get_temp_dir(), 'Tux');
73  $fp = fopen($temp_file, "w");
74  fwrite($fp, $code);
75  fclose($fp);
76  $errors=exec('php -l '.$temp_file);
77  \unlink($temp_file);
78  if(strpos($errors, 'No syntax errors detected') === false){
79  return false;
80  }
81  return true;
82  }
83 
84 }
static unCheckVar($var, $prefix='$')
Definition: CodeUtils.php:55
static checkVar($var, $prefix='$')
Definition: CodeUtils.php:45
static startswith($hay, $needle)
Definition: UString.php:12