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: * File
36: * 13.02.2013 08:46
37: */
38: namespace MOC\Module\Drive;
39: use MOC\Api;
40: use MOC\Generic\Device\Module;
41:
42: /**
43: * Class which provides basic file access
44: */
45: class File implements Module {
46:
47: /** @var \MOC\Core\Drive\File $Resource */
48: private $Resource = null;
49:
50: /**
51: * Get Singleton/Instance
52: *
53: * @static
54: * @return File
55: */
56: public static function InterfaceInstance() {
57: $File = new File();
58: $File->Resource = Api::Core()->Drive()->File();
59: return $File;
60: }
61:
62: /**
63: * Get Changelog
64: *
65: * @static
66: * @return \MOC\Core\Changelog
67: */
68: public static function InterfaceChangelog() {
69: return Api::Core()->Changelog()->Create( __CLASS__ );
70: }
71:
72: /**
73: * Get Dependencies
74: *
75: * @static
76: * @return \MOC\Core\Depending
77: */
78: public static function InterfaceDepending() {
79: return Api::Core()->Depending();
80: }
81:
82: /**
83: * Opens file
84: *
85: * @param $Location
86: *
87: * @return File
88: */
89: public function Open( $Location ) {
90: $this->Resource()->Handle( $Location );
91: return $this;
92: }
93:
94: /**
95: * Reads file
96: *
97: * @return null|string
98: */
99: public function Read() {
100: if( $this->Resource()->Exists() ) {
101: return $this->Resource()->Content();
102: }
103: return null;
104: }
105:
106: /**
107: * Writes to file
108: *
109: * @param $Content
110: *
111: * @return File
112: */
113: public function Write( $Content ) {
114: $Mode = $this->Resource;
115: $this->Resource()->Content( $Content );
116: $this->Resource()->Save( $Mode::MODE_WRITE_BINARY );
117: return $this;
118: }
119:
120: /**
121: * Appends to file
122: *
123: * @param $Content
124: *
125: * @return File
126: */
127: public function Append( $Content ) {
128: $Mode = $this->Resource;
129: $this->Resource()->Content( $Content );
130: $this->Resource()->Save( $Mode::MODE_APPEND );
131: return $this;
132: }
133:
134: /**
135: * Deletes file
136: *
137: * @return File
138: */
139: public function Delete() {
140: $this->Resource()->Remove();
141: return $this;
142: }
143:
144: /**
145: * @return null|string
146: */
147: public function Exists() {
148: return $this->Resource()->Exists();
149: }
150:
151: /**
152: * Gets filename name
153: *
154: * @return null|string
155: */
156: public function GetName() {
157: return $this->Resource()->Name();
158: }
159:
160: /**
161: * Get filename extension
162: *
163: * @return null|string
164: */
165: public function GetExtension() {
166: return $this->Resource()->Extension();
167: }
168:
169: /**
170: * Gets file location
171: *
172: * @return null|string
173: */
174: public function GetLocation() {
175: return $this->Resource()->Location();
176: }
177:
178: /**
179: * Gets file path
180: *
181: * @return null|string
182: */
183: public function GetPath() {
184: return $this->Resource()->Path();
185: }
186:
187: /**
188: * Gets URL of the file location
189: *
190: * @return string
191: */
192: public function GetUrl() {
193: return Api::Core()->Proxy()->Url(
194: Api::Core()->Drive()->File()->Handle( $this->GetLocation() )
195: );
196: }
197:
198: /**
199: * Gets file size
200: *
201: * @return int|null
202: */
203: public function GetSize() {
204: return $this->Resource()->Size();
205: }
206:
207: /**
208: * Gets time
209: *
210: * @return int|null
211: */
212: public function GetTime() {
213: return $this->Resource()->Time();
214: }
215:
216: /**
217: * Gets hash
218: *
219: * @return null|string
220: */
221: public function GetHash() {
222: return $this->Resource()->Hash();
223: }
224:
225: /**
226: * Copies file to a location
227: *
228: * @param $Location
229: *
230: * @return File
231: */
232: public function CopyTo( $Location ) {
233: $this->Resource()->Copy( $Location );
234: return $this;
235: }
236:
237: /**
238: * Moves file to a location
239: *
240: * @param $Location
241: *
242: * @return File
243: */
244: public function MoveTo( $Location ) {
245: $this->Resource()->Move( $Location );
246: return $this;
247: }
248:
249: /**
250: * Gets resource
251: *
252: * @return \MOC\Core\Drive\File
253: */
254: private function Resource() {
255: return $this->Resource;
256: }
257: }
258: