1: <?php
2:
3: namespace webfilesframework\core\datasystem\file\system;
4:
5:
6: 7: 8: 9: 10: 11:
12: class MDirectory extends MFile
13: {
14:
15: protected $m_sPath;
16:
17: public function __construct($p_sPath)
18: {
19: parent::__construct($p_sPath);
20: $this->m_sPath = $p_sPath;
21: }
22:
23:
24: 25: 26: 27: 28:
29: public function getFiles()
30: {
31:
32: $filenames = array();
33:
34: if ( ! $this->exists() ) {
35: throw new \Exception("file '" . $this->m_sPath . "' does not exist.");
36: }
37:
38: $filewebfiles = array();
39:
40: if ($oDirectoryHandle = opendir($this->m_sPath)) {
41: while (false !== ($filename = readdir($oDirectoryHandle))) {
42: if ($filename != "." && $filename != ".." && (!is_dir($this->m_sPath . "/" . $filename))) {
43: array_push($filenames, $filename);
44: }
45: }
46: asort($filenames);
47:
48: foreach ($filenames as $filename) {
49: $filewebfile = new MFile($this->getPath() . "/" . $filename);
50: array_push($filewebfiles, $filewebfile);
51: }
52: }
53: return $filewebfiles;
54: }
55:
56: 57: 58: 59: 60: 61:
62: public function getLatestFiles($count)
63: {
64:
65: $filesArray = $this->getFiles();
66: $latestFilesArray = array_slice($filesArray, $count * -1);
67: return $latestFilesArray;
68: }
69:
70: 71: 72: 73: 74:
75: public function getFileNames()
76: {
77:
78: $filesArray = $this->getFiles();
79: $filenamesArray = array();
80:
81: foreach ($filesArray as $file) {
82: $sFileName = $file->getName();
83: array_push($filenamesArray, $sFileName);
84: }
85: sort($filenamesArray);
86: return $filenamesArray;
87: }
88:
89: 90: 91: 92: 93:
94: public function getSubdirectories()
95: {
96: $directories = array();
97: if ($directoryHandle = opendir($this->m_sPath)) {
98: while (false !== ($sFileName = readdir($directoryHandle))) {
99: if (
100: $sFileName != "."
101: && $sFileName != ".."
102: && (is_dir($this->m_sPath . "/" . $sFileName))) {
103:
104: array_push($directories, $sFileName);
105: }
106: }
107: }
108: sort($directories);
109: return $directories;
110: }
111:
112: 113: 114:
115: public function create()
116: {
117: mkdir($this->m_sPath, 0700, TRUE);
118: }
119:
120: 121: 122: 123: 124:
125: public function createSubDirectoryIfNotExists($p_sName)
126: {
127:
128: $subdirectoryPath = $this->m_sPath . "/" . $p_sName;
129:
130: $subdirectory = new MDirectory($subdirectoryPath);
131:
132: if (!$subdirectory->exists()) {
133: mkdir($subdirectoryPath);
134: }
135:
136: return $subdirectory;
137: }
138:
139: 140: 141: 142:
143: public function getPath()
144: {
145: return $this->m_sPath;
146: }
147:
148: 149: 150: 151: 152:
153: public function exists()
154: {
155: return file_exists($this->m_sPath);
156: }
157:
158: public function isWritable()
159: {
160: return is_writable($this->m_sPath);
161: }
162:
163: }
164: