1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2013, Gerd Christian Kunze
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions are
10: * met:
11: *
12: * * Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * * Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * * Neither the name of Gerd Christian Kunze nor the names of the
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * Directory
36: * 13.02.2013 09:07
37: */
38: namespace MOC\Module\Drive;
39: use MOC\Api;
40: use MOC\Generic\Device\Module;
41:
42: /**
43: * Class for tasks with directories
44: */
45: class Directory implements Module {
46:
47: /** @var null|\MOC\Core\Drive\Directory $Resource */
48: private $Resource = null;
49:
50: /**
51: * Gets Singleton/Instance
52: *
53: * @static
54: * @return Directory
55: */
56: public static function InterfaceInstance() {
57: $Directory = new Directory();
58: $Directory->Resource = Api::Core()->Drive()->Directory();
59: return $Directory;
60: }
61:
62: /**
63: * Gets Changelog
64: *
65: * @static
66: * @return \MOC\Core\Changelog
67: */
68: public static function InterfaceChangelog() {
69: return Api::Core()->Changelog()->Create( __CLASS__ )
70: ->Fix()->DocFix( '19.02.2013 11:05', 'Wrong Parameter-Types' )
71: ;
72: }
73:
74: /**
75: * Gets Dependencies
76: *
77: * @static
78: * @return \MOC\Core\Depending
79: */
80: public static function InterfaceDepending() {
81: return Api::Core()->Depending();
82: }
83:
84: /**
85: * Opens directory
86: *
87: * @param string $Location
88: *
89: * @return Directory
90: */
91: public function Open( $Location ) {
92: $this->Resource()->Handle( $Location );
93: return $this;
94: }
95:
96: /**
97: * Gets name
98: *
99: * @return null|string
100: */
101: public function GetName() {
102: return $this->Resource()->Name();
103: }
104:
105: /**
106: * Gets location
107: *
108: * @return null|string
109: */
110: public function GetLocation() {
111: return $this->Resource()->Location();
112: }
113:
114: /**
115: * Gets relative path of the directory
116: *
117: * @param Directory $Directory
118: *
119: * @return string
120: */
121: public function GetLocationRelative( Directory $Directory ) {
122: $Current = explode( DIRECTORY_SEPARATOR, $this->GetLocation() );
123: $Base = explode( DIRECTORY_SEPARATOR, $Directory->GetLocation() );
124: $Size = min( count( $Current ), count( $Base ) );
125:
126: for( $Run = 0; $Run < $Size; $Run++ ) {
127: if( empty( $Current[$Run] ) || empty( $Base[$Run] ) ) {
128: continue;
129: }
130: if( $Current[$Run] == $Base[$Run] ) {
131: $Current[$Run] = false;
132: } else {
133: $Current = array_filter( $Current );
134: array_unshift( $Current, '..' );
135: }
136: }
137:
138: return implode( '/', $Current );
139: }
140:
141: /**
142: * Gets path
143: *
144: * @return null|string
145: */
146: public function GetPath() {
147: return $this->Resource()->Path();
148: }
149:
150: /**
151: * Gets time
152: *
153: * @return int|null
154: */
155: public function GetTime() {
156: return $this->Resource()->Time();
157: }
158:
159: /**
160: * Gets hash
161: *
162: * @return null|string
163: */
164: public function GetHash() {
165: return $this->Resource()->Hash();
166: }
167:
168: /**
169: * Gets a list of all files (with recursive option)
170: *
171: * @param bool $doRecursive
172: *
173: * @return File[]
174: */
175: public function FileList( $doRecursive = false ) {
176: if( $doRecursive ) {
177: $List = $this->Resource()->FileListRecursive();
178: array_walk( $List,
179: function ( &$File ) {
180: /** @var \MOC\Core\Drive\File $File */
181: $File = File::InterfaceInstance()->Open( $File->Location() );
182: }
183: );
184: } else {
185: $List = $this->Resource()->FileList();
186: array_walk( $List,
187: function ( &$File ) {
188: /** @var \MOC\Core\Drive\File $File */
189: $File = File::InterfaceInstance()->Open( $File->Location() );
190: }
191: );
192: }
193: return $List;
194: }
195:
196: /**
197: * Gets a list of all directories (with recursive option)
198: *
199: * @param bool $doRecursive
200: *
201: * @return Directory[]
202: */
203: public function DirectoryList( $doRecursive = false ) {
204: if( $doRecursive ) {
205: $List = $this->Resource()->DirectoryListRecursive();
206: array_walk( $List,
207: function ( &$Directory ) {
208: /** @var \MOC\Core\Drive\Directory $Directory */
209: $Directory = Directory::InterfaceInstance()->Open( $Directory->Location() );
210: }
211: );
212: } else {
213: $List = $this->Resource()->DirectoryList();
214: array_walk( $List,
215: function ( &$Directory ) {
216: /** @var \MOC\Core\Drive\Directory $Directory */
217: $Directory = Directory::InterfaceInstance()->Open( $Directory->Location() );
218: }
219: );
220: }
221: return $List;
222: }
223:
224: /**
225: * Gets resource
226: *
227: * @return \MOC\Core\Drive\Directory|null
228: */
229: private function Resource() {
230: return $this->Resource;
231: }
232: }
233: