90% of 532OPs |
88% of 82Lines |
73% of 49Branches |
87% of 31Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\mock\streams\fs; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum, |
| 7 |
mageekguy\atoum\exceptions, |
| 8 |
mageekguy\atoum\mock\stream |
| 9 |
; |
| 10 |
|
| 11 |
class controller extends stream\controller |
| 12 |
{
|
| 13 |
protected $adapter = null; |
| 14 |
protected $exists = true; |
| 15 |
protected $stat = array(); |
| 16 |
|
| 17 |
public function __construct($path, atoum\adapter $adapter = null)100% |
| 18 |
{
|
| 19 |
parent::__construct($path); |
| 20 |
|
| 21 |
$this->setAdapter($adapter)->stat = array( |
| 22 |
'dev' => 0, |
| 23 |
'ino' => 0, |
| 24 |
'mode' => 0, |
| 25 |
'nlink' => 0, |
| 26 |
'uid' => getmyuid(), |
| 27 |
'gid' => getmygid(), |
| 28 |
'rdev' => 0, |
| 29 |
'size' => 0, |
| 30 |
'atime' => 507769200, |
| 31 |
'mtime' => 507769200, |
| 32 |
'ctime' => 507769200, |
| 33 |
'blksize' => 0, |
| 34 |
'blocks' => 0 |
| 35 |
); |
| 36 |
|
| 37 |
$this->stat[0] = & $this->stat['dev']; |
| 38 |
$this->stat[1] = & $this->stat['ino']; |
| 39 |
$this->stat[2] = & $this->stat['mode']; |
| 40 |
$this->stat[3] = & $this->stat['nlink']; |
| 41 |
$this->stat[4] = & $this->stat['uid']; |
| 42 |
$this->stat[5] = & $this->stat['gid']; |
| 43 |
$this->stat[6] = & $this->stat['rdev']; |
| 44 |
$this->stat[7] = & $this->stat['size']; |
| 45 |
$this->stat[8] = & $this->stat['atime']; |
| 46 |
$this->stat[9] = & $this->stat['mtime']; |
| 47 |
$this->stat[10] = & $this->stat['ctime']; |
| 48 |
$this->stat[11] = & $this->stat['blksize']; |
| 49 |
$this->stat[12] = & $this->stat['blocks']; |
| 50 |
} |
| 51 |
|
| 52 |
public function getAdapter()100% |
| 53 |
{
|
| 54 |
return $this->adapter; |
| 55 |
} |
| 56 |
|
| 57 |
public function setAdapter(atoum\adapter $adapter = null)100% |
| 58 |
{
|
| 59 |
$this->adapter = $adapter ?: new atoum\adapter(); |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function exists()100% |
| 65 |
{
|
| 66 |
$this->exists = true; |
| 67 |
|
| 68 |
return $this->clearStatCache(); |
| 69 |
} |
| 70 |
|
| 71 |
public function notExists()100% |
| 72 |
{
|
| 73 |
$this->exists = false; |
| 74 |
|
| 75 |
return $this->clearStatCache(); |
| 76 |
} |
| 77 |
|
| 78 |
public function isNotReadable()100% |
| 79 |
{
|
| 80 |
return $this->removePermissions(0444); |
| 81 |
} |
| 82 |
|
| 83 |
public function isReadable()100% |
| 84 |
{
|
| 85 |
return $this->addPermission(0444); |
| 86 |
} |
| 87 |
|
| 88 |
public function isNotWritable()100% |
| 89 |
{
|
| 90 |
return $this->removePermissions(0222); |
| 91 |
} |
| 92 |
|
| 93 |
public function isWritable()100% |
| 94 |
{
|
| 95 |
return $this->addPermission(0222); |
| 96 |
} |
| 97 |
|
| 98 |
public function isNotExecutable()100% |
| 99 |
{
|
| 100 |
return $this->removePermissions(0111); |
| 101 |
} |
| 102 |
|
| 103 |
public function isExecutable()100% |
| 104 |
{
|
| 105 |
return $this->addPermission(0111); |
| 106 |
} |
| 107 |
|
| 108 |
public function setPermissions($permissions)100% |
| 109 |
{
|
| 110 |
return $this->setStat('mode', $permissions);
|
| 111 |
} |
| 112 |
|
| 113 |
public function getPermissions()100% |
| 114 |
{
|
| 115 |
return ($this->exists === false ? null : (int) sprintf('%03o', $this->stat['mode'] & 07777));
|
| 116 |
} |
| 117 |
|
| 118 |
public function duplicate()0% |
| 119 |
{
|
| 120 |
$controller = parent::duplicate(); |
| 121 |
|
| 122 |
$controller->adapter = & $this->adapter; |
| 123 |
$controller->exists = & $this->exists; |
| 124 |
$controller->stat = & $this->stat; |
| 125 |
|
| 126 |
return $controller; |
| 127 |
} |
| 128 |
|
| 129 |
public function getStat()100% |
| 130 |
{
|
| 131 |
return ($this->exists === false ? false : $this->stat); |
| 132 |
} |
| 133 |
|
| 134 |
public function stream_stat()100% |
| 135 |
{
|
| 136 |
if ($this->nextCallIsOverloaded(__FUNCTION__) === true) |
| 137 |
{
|
| 138 |
return $this->invoke(__FUNCTION__, array()); |
| 139 |
} |
| 140 |
else |
| 141 |
{
|
| 142 |
$this->addCall(__FUNCTION__, array()); |
| 143 |
|
| 144 |
return $this->getStat(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public function url_stat($path, $flags)100% |
| 149 |
{
|
| 150 |
if ($this->nextCallIsOverloaded(__FUNCTION__) === true) |
| 151 |
{
|
| 152 |
return $this->invoke(__FUNCTION__, func_get_args()); |
| 153 |
} |
| 154 |
else |
| 155 |
{
|
| 156 |
$this->addCall(__FUNCTION__, func_get_args()); |
| 157 |
|
| 158 |
return $this->getStat(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
protected function setStat($name, $value)100% |
| 163 |
{
|
| 164 |
if (isset($this->stat[$name]) === true) |
| 165 |
{
|
| 166 |
$this->stat[$name] = $value; |
| 167 |
|
| 168 |
$this->clearStatCache(); |
| 169 |
} |
| 170 |
|
| 171 |
return $this; |
| 172 |
} |
| 173 |
|
| 174 |
protected function clearStatCache()100% |
| 175 |
{
|
| 176 |
$this->adapter->clearstatcache(false, $this->getPath()); |
| 177 |
|
| 178 |
return $this; |
| 179 |
} |
| 180 |
|
| 181 |
protected function addPermission($permissions)100% |
| 182 |
{
|
| 183 |
return $this->setStat('mode', $this->stat['mode'] | $permissions);
|
| 184 |
} |
| 185 |
|
| 186 |
protected function removePermissions($permissions)100% |
| 187 |
{
|
| 188 |
return $this->setStat('mode', $this->stat['mode'] & ~ $permissions);
|
| 189 |
} |
| 190 |
|
| 191 |
protected function checkIfReadable()100% |
| 192 |
{
|
| 193 |
return $this->checkPermission(0400, 0040, 0004); |
| 194 |
} |
| 195 |
|
| 196 |
protected function checkIfWritable()100% |
| 197 |
{
|
| 198 |
return $this->checkPermission(0200, 0020, 0002); |
| 199 |
} |
| 200 |
|
| 201 |
protected function checkPermission($user, $group, $other)44% |
| 202 |
{
|
| 203 |
$permissions = $this->stat['mode'] & 07777; |
| 204 |
|
| 205 |
switch (true) |
| 206 |
{
|
| 207 |
case getmyuid() === $this->stat['uid']: |
| 208 |
return ($permissions & $user) > 0; |
| 209 |
|
| 210 |
case getmygid() === $this->stat['gid']: |
| 211 |
return ($permissions & $group) > 0; |
| 212 |
|
| 213 |
default: |
| 214 |
return ($permissions & $other) > 0; |
| 215 |
} |
| 216 |
} |
| 217 |
} |