| 1: | <?php | 
| 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: | namespace Kint\Object\Representation; | 
| 27: |  | 
| 28: | use Kint\Utils; | 
| 29: | use SplFileInfo; | 
| 30: |  | 
| 31: | class SplFileInfoRepresentation extends Representation | 
| 32: | { | 
| 33: | public $perms; | 
| 34: | public $flags; | 
| 35: | public $path; | 
| 36: | public $realpath; | 
| 37: | public $linktarget; | 
| 38: | public $size; | 
| 39: | public $is_dir = false; | 
| 40: | public $is_file = false; | 
| 41: | public $is_link = false; | 
| 42: | public $owner; | 
| 43: | public $group; | 
| 44: | public $ctime; | 
| 45: | public $mtime; | 
| 46: | public $typename = 'Unknown file'; | 
| 47: | public $typeflag = '-'; | 
| 48: | public $hints = array('fspath'); | 
| 49: |  | 
| 50: | public function __construct(SplFileInfo $fileInfo) | 
| 51: | { | 
| 52: | parent::__construct('SplFileInfo'); | 
| 53: |  | 
| 54: | if ($fileInfo->getRealPath()) { | 
| 55: | $this->realpath = $fileInfo->getRealPath(); | 
| 56: | $this->perms = $fileInfo->getPerms(); | 
| 57: | $this->size = $fileInfo->getSize(); | 
| 58: | $this->owner = $fileInfo->getOwner(); | 
| 59: | $this->group = $fileInfo->getGroup(); | 
| 60: | $this->ctime = $fileInfo->getCTime(); | 
| 61: | $this->mtime = $fileInfo->getMTime(); | 
| 62: | } | 
| 63: |  | 
| 64: | $this->path = $fileInfo->getPathname(); | 
| 65: |  | 
| 66: | $this->is_dir = $fileInfo->isDir(); | 
| 67: | $this->is_file = $fileInfo->isFile(); | 
| 68: | $this->is_link = $fileInfo->isLink(); | 
| 69: |  | 
| 70: | if ($this->is_link) { | 
| 71: | $this->linktarget = $fileInfo->getLinkTarget(); | 
| 72: | } | 
| 73: |  | 
| 74: | switch ($this->perms & 0xF000) { | 
| 75: | case 0xC000: | 
| 76: | $this->typename = 'Socket'; | 
| 77: | $this->typeflag = 's'; | 
| 78: | break; | 
| 79: | case 0x6000: | 
| 80: | $this->typename = 'Block device'; | 
| 81: | $this->typeflag = 'b'; | 
| 82: | break; | 
| 83: | case 0x2000: | 
| 84: | $this->typename = 'Character device'; | 
| 85: | $this->typeflag = 'c'; | 
| 86: | break; | 
| 87: | case 0x1000: | 
| 88: | $this->typename = 'Named pipe'; | 
| 89: | $this->typeflag = 'p'; | 
| 90: | break; | 
| 91: | default: | 
| 92: | if ($this->is_file) { | 
| 93: | if ($this->is_link) { | 
| 94: | $this->typename = 'File symlink'; | 
| 95: | $this->typeflag = 'l'; | 
| 96: | } else { | 
| 97: | $this->typename = 'File'; | 
| 98: | $this->typeflag = '-'; | 
| 99: | } | 
| 100: | } elseif ($this->is_dir) { | 
| 101: | if ($this->is_link) { | 
| 102: | $this->typename = 'Directory symlink'; | 
| 103: | $this->typeflag = 'l'; | 
| 104: | } else { | 
| 105: | $this->typename = 'Directory'; | 
| 106: | $this->typeflag = 'd'; | 
| 107: | } | 
| 108: | } | 
| 109: | break; | 
| 110: | } | 
| 111: |  | 
| 112: | $this->flags = array($this->typeflag); | 
| 113: |  | 
| 114: |  | 
| 115: | $this->flags[] = (($this->perms & 0400) ? 'r' : '-'); | 
| 116: | $this->flags[] = (($this->perms & 0200) ? 'w' : '-'); | 
| 117: | if ($this->perms & 0100) { | 
| 118: | $this->flags[] = ($this->perms & 04000) ? 's' : 'x'; | 
| 119: | } else { | 
| 120: | $this->flags[] = ($this->perms & 04000) ? 'S' : '-'; | 
| 121: | } | 
| 122: |  | 
| 123: |  | 
| 124: | $this->flags[] = (($this->perms & 0040) ? 'r' : '-'); | 
| 125: | $this->flags[] = (($this->perms & 0020) ? 'w' : '-'); | 
| 126: | if ($this->perms & 0010) { | 
| 127: | $this->flags[] = ($this->perms & 02000) ? 's' : 'x'; | 
| 128: | } else { | 
| 129: | $this->flags[] = ($this->perms & 02000) ? 'S' : '-'; | 
| 130: | } | 
| 131: |  | 
| 132: |  | 
| 133: | $this->flags[] = (($this->perms & 0004) ? 'r' : '-'); | 
| 134: | $this->flags[] = (($this->perms & 0002) ? 'w' : '-'); | 
| 135: | if ($this->perms & 0001) { | 
| 136: | $this->flags[] = ($this->perms & 01000) ? 's' : 'x'; | 
| 137: | } else { | 
| 138: | $this->flags[] = ($this->perms & 01000) ? 'S' : '-'; | 
| 139: | } | 
| 140: |  | 
| 141: | $this->contents = \implode($this->flags).' '.$this->owner.' '.$this->group; | 
| 142: | $this->contents .= ' '.$this->getSize().' '.$this->getMTime().' '; | 
| 143: |  | 
| 144: | if ($this->is_link && $this->linktarget) { | 
| 145: | $this->contents .= $this->path.' -> '.$this->linktarget; | 
| 146: | } elseif (null !== $this->realpath && \strlen($this->realpath) < \strlen($this->path)) { | 
| 147: | $this->contents .= $this->realpath; | 
| 148: | } else { | 
| 149: | $this->contents .= $this->path; | 
| 150: | } | 
| 151: | } | 
| 152: |  | 
| 153: | public function getLabel() | 
| 154: | { | 
| 155: | return $this->typename.' ('.$this->getSize().')'; | 
| 156: | } | 
| 157: |  | 
| 158: | public function getSize() | 
| 159: | { | 
| 160: | if ($this->size) { | 
| 161: | $size = Utils::getHumanReadableBytes($this->size); | 
| 162: |  | 
| 163: | return \round($size['value'], 2).$size['unit']; | 
| 164: | } | 
| 165: | } | 
| 166: |  | 
| 167: | public function getMTime() | 
| 168: | { | 
| 169: | $year = \date('Y', $this->mtime); | 
| 170: |  | 
| 171: | if ($year !== \date('Y')) { | 
| 172: | return \date('M d Y', $this->mtime); | 
| 173: | } | 
| 174: |  | 
| 175: | return \date('M d H:i', $this->mtime); | 
| 176: | } | 
| 177: | } | 
| 178: |  |