-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHPPushPop.class.php
73 lines (68 loc) · 1.97 KB
/
PHPPushPop.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* A class that reproduces bash's pushd and popd functionality in PHP
* @author [email protected]
* @license Apache 2.0
* @version 0.1
*/
class PushPop {
/**
* Internal directory stack
* @var array
*/
protected $stack = array();
/**
* Changes to $newDir and adds previous current directory to the stack
* Returns FALSE when an error exists
* @param string $newDir Change directory to $newDir
* @param boolean $beginning Add the directory to the start of the stack
* instead of the end.
* @return string Resulting directory location
*/
public function pushd($newDir, $beginning = FALSE) {
$currentDir=getcwd();
if (chdir($newDir)) {
if ($beginning) array_unshift($this->stack, $currentDir);
else $this->stack[] = $currentDir;
return $newDir;
} else {
trigger_error('Could not chdir() to directory '.$newDir,E_USER_WARNING);
return FALSE;
}
}
/**
* Returns current working directory to a previous directory in the
* directory stack.
* Returns FALSE when an error exists
* @param int $layers Iterate over $layers layers in the directory stack. DEFAULT: 1
* @param boolean $beginning Pull from the begininng of the directory stack
* instead of the end. DEFAULT: FALSE
* @return string Resulting directory location
*/
public function popd($layers = 1, $beginning = FALSE) {
$testStack = $this->stack;
for ($h=0; $h<$layers;$h++) {
if ($beginning) $testDir = array_shift($testStack);
else $testDir = array_pop($testStack);
if ($testDir === NULL) {
trigger_error('Requested layers exceeds number of directory stack layers.',E_USER_NOTICE);
return FALSE;
}
}
if (@chdir($testDir)) {
$this->stack = $testStack;
} else {
trigger_error('Could not chdir() to '.$testDir,E_USER_WARNING);
return FALSE;
}
return $testDir;
}
/**
* Returns the directory stack as an array
* @return array Array of directory locations
*/
public function getStack() {
return $this->stack;
}
}
?>