Skip to content

Commit 3e3ab1c

Browse files
committed
- added following command:
- mkdir - added capability to remove dirs with "rm -rf [directory]"
1 parent 711123e commit 3e3ab1c

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

commandline/library/lSystem.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,35 @@ static function isFile($file){
5353
}
5454

5555
/*
56-
* returns the if a file exists
56+
* unlinks a file exists
57+
*/
58+
static function rmFile($file){
59+
unlink($file);
60+
}
61+
62+
/*
63+
* returns the if a directory exists
5764
*/
5865
static function dirExists($dir){
5966
return is_dir($dir);
6067
}
68+
69+
/*
70+
* removes a directory exists
71+
*/
72+
static function rmDir($dir){
73+
if (substr($dir, strlen($dir) - 1, 1) != '/') {
74+
$dir .= '/';
75+
}
76+
$files = glob($dir . '*', GLOB_MARK);
77+
foreach ($files as $file) {
78+
if (is_dir($file)) {
79+
self::rmDir($file);
80+
} else {
81+
unlink($file);
82+
}
83+
}
84+
rmdir($dir);
85+
}
6186
}
6287
?>

commandline/programs/mkdir/init.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
use PHPCommandLine\lCommand;
3+
use PHPCommandLine\lSystem;
4+
5+
function mkdirMain($args, $command){
6+
lCommand::write("> $command");
7+
if(count($args)==1){
8+
if(strlen($args[0])>0) {
9+
$result = mkdir(lSystem::getPWD()."/".$args[0]);
10+
}
11+
else lCommand::write("please specify a dirname");
12+
}else{
13+
lCommand::write("to ceate a new directory, call \"mkdir [dirname]\"");
14+
}
15+
16+
}
17+
?>

commandline/programs/rm/init.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@ function rmMain($args, $command){
88
$file = lSystem::getPWD()."/".$args[0];
99
if(lSystem::fileExists($file)){
1010
if(lSystem::isFile($file)){
11-
unlink($file);
11+
lSystem::rmFile($file);
1212
}else{
13-
lCommand::write("this is a directory and cannot be removed with this command");
13+
lCommand::write("this is a directory and can only be removed by \"rm -rf [directory]\"");
1414
}
1515
}else{
1616
lCommand::write("this file does not exists");
1717
}
18+
}else if(count($args)==1) {
19+
if($args[0]=="-rf"){
20+
$dir = lSystem::getPWD()."/".$args[1];
21+
if(lSystem::dirExists($dir)){
22+
lSystem::rmDir($dir);
23+
}else{
24+
lCommand::write("this directory does not exists");
25+
}
26+
}else{
27+
lCommand::write("to remove a file, call \"rm [file]\", to remove a directory, call \"rm -rf [directory]\"");
28+
}
1829
}else{
19-
lCommand::write("to remove a file, call \"rm [file]\"");
30+
lCommand::write("to remove a file, call \"rm [file]\", to remove a directory, call \"rm -rf [directory]\"");
2031
}
2132
}
2233
?>

0 commit comments

Comments
 (0)