20
20
*/
21
21
class KWUtils
22
22
{
23
-
24
- CONST DEFAULT_MKDIR_MODE = 0775 ;
25
-
23
+
24
+ CONST DEFAULT_MKDIR_MODE = 0775 ;
25
+
26
26
/**
27
27
* @method mkDir
28
28
* @TODO what to do with errors in a way that is consistent with error reporting
@@ -43,48 +43,48 @@ public static function mkDir($dir, $mode = self::DEFAULT_MKDIR_MODE)
43
43
{
44
44
return false ;
45
45
}
46
- return true ;
46
+ return true ;
47
47
}
48
-
49
- /**
48
+
49
+ /**
50
50
* @method createSubDirectories recursively create subdirectories starting at
51
51
* baseDirectory, sequentially creating each of the directories in the
52
52
* subDirectories array, according to the passed in mode.
53
53
* @param $baseDirectory the first directory to create
54
- * @param $subDirectories an array of directories that will be created in a
54
+ * @param $subDirectories an array of directories that will be created in a
55
55
* recursive fashion, each one appending to the last as a deeper subdirectory
56
56
* of baseDirectory
57
57
* @param the mode to create the new directories
58
- */
58
+ */
59
59
public static function createSubDirectories ($ baseDirectory , $ subDirectories , $ mode = self ::DEFAULT_MKDIR_MODE )
60
60
{
61
61
if (!file_exists ($ baseDirectory ) )
62
62
{
63
63
throw new Zend_Exception ($ baseDirectory . ' does not exist ' );
64
64
}
65
- $ relpath = '' ;
65
+ $ relpath = '' ;
66
66
foreach ($ subDirectories as $ directory )
67
67
{
68
- $ relpath .= $ directory . "/ " ;
69
-
68
+ $ relpath .= $ directory . "/ " ;
69
+
70
70
if (!KwUtils::mkDir ($ baseDirectory . $ relpath , $ mode ))
71
- {
71
+ {
72
72
throw new Zend_Exception ($ baseDirectory . $ relpath . ' could not be created ' );
73
73
}
74
74
}
75
75
return $ baseDirectory . $ relpath ;
76
76
}
77
77
78
- /**
78
+ /**
79
79
* @method isWindows()
80
80
* @return True if the current platform is windows
81
81
*/
82
82
public static function isWindows ()
83
83
{
84
- return (strtolower (substr (PHP_OS , 0 , 3 )) == "win " );
84
+ return (strtolower (substr (PHP_OS , 0 , 3 )) == "win " );
85
85
}
86
-
87
-
86
+
87
+
88
88
/**
89
89
* @method escapeCommand
90
90
* will escape a command respecting the format of the current platform
@@ -100,11 +100,11 @@ public static function escapeCommand($command )
100
100
{
101
101
$ command = '" ' .$ command .'" ' ;
102
102
}
103
-
104
- return $ command ;
105
- }
106
-
107
- /**
103
+
104
+ return $ command ;
105
+ }
106
+
107
+ /**
108
108
* @method appendStringIfNot will append the string $ext to
109
109
* $subject if it is not already a suffix of $subject
110
110
* @param $subject, the string to be appended to
@@ -115,9 +115,9 @@ public static function appendStringIfNot($subject, $ext)
115
115
{
116
116
if (!(substr ($ subject , strlen ($ subject ) - strlen ($ ext )) === $ ext ) )
117
117
{
118
- $ subject .= $ ext ;
118
+ $ subject .= $ ext ;
119
119
}
120
- return $ subject ;
120
+ return $ subject ;
121
121
}
122
122
123
123
/**
@@ -127,65 +127,65 @@ public static function appendStringIfNot($subject, $ext)
127
127
* @param $output, a reference to put the output of the command
128
128
* @param $chdir, the dir to change to for execution, if any
129
129
* @param $return_val, a reference to put the return value of the command
130
- * the temporary work dir
130
+ * the temporary work dir
131
131
*/
132
132
public static function exec ($ command , &$ output = null , $ chdir = "" , &$ return_val = null )
133
133
{
134
134
if (!empty ($ chdir ) && is_dir ($ chdir ))
135
- {
135
+ {
136
136
if (!chdir ($ chdir ))
137
137
{
138
138
throw new Zend_Exception ("Failed to change directory: [ " .$ chdir ."] " );
139
139
}
140
140
}
141
141
// on Linux need to add redirection to handle stderr
142
- $ redirect_error = KWUtils::isLinux () ? " 2>&1 " : "" ;
142
+ $ redirect_error = KWUtils::isLinux () ? " 2>&1 " : "" ;
143
143
exec (KWUtils::escapeCommand ($ command ) . $ redirect_error , $ output , $ return_val );
144
- }
145
-
146
-
147
- /**
144
+ }
145
+
146
+
147
+ /**
148
148
* @method isLinux()
149
149
* @return True if the current platform is Linux
150
150
*/
151
151
public static function isLinux ()
152
152
{
153
- return (strtolower (substr (PHP_OS , 0 , 5 )) == "linux " );
154
- }
155
-
156
-
157
-
153
+ return (strtolower (substr (PHP_OS , 0 , 5 )) == "linux " );
154
+ }
155
+
156
+
157
+
158
158
/**
159
159
* @method prepareExecCommand
160
- * will prepare an executable application and params for command line
160
+ * will prepare an executable application and params for command line
161
161
* execution, including escaping and quoting arguments.
162
162
* @param $app_name, the application to be executed
163
163
* @param $params, an array of arguments to the application
164
- * @return the full command line command, escaped and quoted, will throw a
164
+ * @return the full command line command, escaped and quoted, will throw a
165
165
* Zend_Exception if the app is not in the path and not executable
166
166
*/
167
167
public static function prepareExecCommand ($ app_name , $ params = array ())
168
168
{
169
169
// Check if application is executable, if not, see if you can find it
170
170
// in the path
171
- if (!KWUtils::isExecutable ($ app_name , false ))
171
+ if (!KWUtils::isExecutable ($ app_name , false ))
172
172
{
173
173
$ app_name = KWUtils::findApp ($ app_name , true );
174
174
}
175
-
175
+
176
176
// escape parameters
177
177
$ escapedParams = array ();
178
178
foreach ($ params as $ param )
179
179
{
180
180
$ escapedParams [] = escapeshellarg ($ param );
181
181
}
182
-
182
+
183
183
// glue together app_name and params using spaces
184
184
return escapeshellarg ($ app_name )." " .implode (" " , $ escapedParams );
185
- }
186
-
187
-
188
- /**
185
+ }
186
+
187
+
188
+ /**
189
189
* @method isExecutable will return true if the app can be found and is
190
190
* executable, can optionally look in the path.
191
191
* @param string $app_name, the app to check
@@ -198,7 +198,7 @@ public static function isExecutable($app_name, $check_in_path = false)
198
198
{
199
199
if ($ check_in_path )
200
200
{
201
- try
201
+ try
202
202
{
203
203
if (KWUtils::findApp ($ app_name , true ))
204
204
{
@@ -212,11 +212,11 @@ public static function isExecutable($app_name, $check_in_path = false)
212
212
}
213
213
return false ;
214
214
}
215
- return true ;
216
- }
217
-
215
+ return true ;
216
+ }
217
+
218
218
/**
219
- * @method findApp will return the absolute path of an application
219
+ * @method findApp will return the absolute path of an application
220
220
* @param $app_name, the name of the application
221
221
* @param $check_execution_flag, whether to include in the check that the
222
222
* application is executable
@@ -226,11 +226,11 @@ public static function isExecutable($app_name, $check_in_path = false)
226
226
*/
227
227
public static function findApp ($ app_name , $ check_execution_flag )
228
228
{
229
- $ PHP_PATH_SEPARATOR = ": " ;
229
+ $ PHP_PATH_SEPARATOR = ": " ;
230
230
// split path
231
- $ path_list = explode ($ PHP_PATH_SEPARATOR , getenv ("PATH " ));
232
-
233
- // loop through paths
231
+ $ path_list = explode ($ PHP_PATH_SEPARATOR , getenv ("PATH " ));
232
+
233
+ // loop through paths
234
234
foreach ($ path_list as $ path )
235
235
{
236
236
$ status = false ;
@@ -239,41 +239,41 @@ public static function findApp($app_name, $check_execution_flag )
239
239
{
240
240
if (is_executable ($ path_to_app ))
241
241
{
242
- $ status = true ;
243
- break ;
244
- }
242
+ $ status = true ;
243
+ break ;
244
+ }
245
245
}
246
246
else
247
247
{
248
248
if (file_exists ($ path_to_app ))
249
249
{
250
250
$ status = true ;
251
- break ;
251
+ break ;
252
252
}
253
253
}
254
254
}
255
255
if (!$ status )
256
256
{
257
257
throw new Zend_Exception ("Failed to locate the application: [ " .$ app_name ."] [check_execution_flag: " .$ check_execution_flag ."] " );
258
258
}
259
- return $ path_to_app ;
260
- }
261
-
262
-
259
+ return $ path_to_app ;
260
+ }
261
+
262
+
263
263
264
264
265
- /**
265
+ /**
266
266
* @method formatAppName
267
267
* Format the application name according to the platform.
268
268
*/
269
269
public static function formatAppName ($ app_name )
270
270
{
271
271
if (substr (PHP_OS , 0 , 3 ) == "WIN " )
272
- {
273
- $ app_name = KWUtils::appendStringIfNot ($ app_name , ".exe " );
272
+ {
273
+ $ app_name = KWUtils::appendStringIfNot ($ app_name , ".exe " );
274
274
}
275
- return $ app_name ;
276
- }
277
-
278
-
275
+ return $ app_name ;
276
+ }
277
+
278
+
279
279
}
0 commit comments