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