Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * Hoa
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2007-2017, Hoa community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
 
37
namespace Psy\Readline\Hoa;
38
 
39
/**
40
 * Class \Hoa\File\Generic.
41
 *
42
 * Describe a super-file.
43
 */
44
abstract class FileGeneric extends Stream implements StreamPathable, StreamStatable, StreamTouchable
45
{
46
    /**
47
     * Mode.
48
     */
49
    protected $_mode = null;
50
 
51
    /**
52
     * Get filename component of path.
53
     */
54
    public function getBasename(): string
55
    {
56
        return \basename($this->getStreamName());
57
    }
58
 
59
    /**
60
     * Get directory name component of path.
61
     */
62
    public function getDirname(): string
63
    {
64
        return \dirname($this->getStreamName());
65
    }
66
 
67
    /**
68
     * Get size.
69
     */
70
    public function getSize(): int
71
    {
72
        if (false === $this->getStatistic()) {
73
            return false;
74
        }
75
 
76
        return \filesize($this->getStreamName());
77
    }
78
 
79
    /**
80
     * Get informations about a file.
81
     */
82
    public function getStatistic(): array
83
    {
84
        return \fstat($this->getStream());
85
    }
86
 
87
    /**
88
     * Get last access time of file.
89
     */
90
    public function getATime(): int
91
    {
92
        return \fileatime($this->getStreamName());
93
    }
94
 
95
    /**
96
     * Get inode change time of file.
97
     */
98
    public function getCTime(): int
99
    {
100
        return \filectime($this->getStreamName());
101
    }
102
 
103
    /**
104
     * Get file modification time.
105
     */
106
    public function getMTime(): int
107
    {
108
        return \filemtime($this->getStreamName());
109
    }
110
 
111
    /**
112
     * Get file group.
113
     */
114
    public function getGroup(): int
115
    {
116
        return \filegroup($this->getStreamName());
117
    }
118
 
119
    /**
120
     * Get file owner.
121
     */
122
    public function getOwner(): int
123
    {
124
        return \fileowner($this->getStreamName());
125
    }
126
 
127
    /**
128
     * Get file permissions.
129
     */
130
    public function getPermissions(): int
131
    {
132
        return \fileperms($this->getStreamName());
133
    }
134
 
135
    /**
136
     * Get file permissions as a string.
137
     * Result sould be interpreted like this:
138
     *     * s: socket;
139
     *     * l: symbolic link;
140
     *     * -: regular;
141
     *     * b: block special;
142
     *     * d: directory;
143
     *     * c: character special;
144
     *     * p: FIFO pipe;
145
     *     * u: unknown.
146
     */
147
    public function getReadablePermissions(): string
148
    {
149
        $p = $this->getPermissions();
150
 
151
        if (($p & 0xC000) === 0xC000) {
152
            $out = 's';
153
        } elseif (($p & 0xA000) === 0xA000) {
154
            $out = 'l';
155
        } elseif (($p & 0x8000) === 0x8000) {
156
            $out = '-';
157
        } elseif (($p & 0x6000) === 0x6000) {
158
            $out = 'b';
159
        } elseif (($p & 0x4000) === 0x4000) {
160
            $out = 'd';
161
        } elseif (($p & 0x2000) === 0x2000) {
162
            $out = 'c';
163
        } elseif (($p & 0x1000) === 0x1000) {
164
            $out = 'p';
165
        } else {
166
            $out = 'u';
167
        }
168
 
169
        $out .=
170
            (($p & 0x0100) ? 'r' : '-').
171
            (($p & 0x0080) ? 'w' : '-').
172
            (($p & 0x0040) ?
173
            (($p & 0x0800) ? 's' : 'x') :
174
            (($p & 0x0800) ? 'S' : '-')).
175
            (($p & 0x0020) ? 'r' : '-').
176
            (($p & 0x0010) ? 'w' : '-').
177
            (($p & 0x0008) ?
178
            (($p & 0x0400) ? 's' : 'x') :
179
            (($p & 0x0400) ? 'S' : '-')).
180
            (($p & 0x0004) ? 'r' : '-').
181
            (($p & 0x0002) ? 'w' : '-').
182
            (($p & 0x0001) ?
183
            (($p & 0x0200) ? 't' : 'x') :
184
            (($p & 0x0200) ? 'T' : '-'));
185
 
186
        return $out;
187
    }
188
 
189
    /**
190
     * Check if the file is readable.
191
     */
192
    public function isReadable(): bool
193
    {
194
        return \is_readable($this->getStreamName());
195
    }
196
 
197
    /**
198
     * Check if the file is writable.
199
     */
200
    public function isWritable(): bool
201
    {
202
        return \is_writable($this->getStreamName());
203
    }
204
 
205
    /**
206
     * Check if the file is executable.
207
     */
208
    public function isExecutable(): bool
209
    {
210
        return \is_executable($this->getStreamName());
211
    }
212
 
213
    /**
214
     * Clear file status cache.
215
     */
216
    public function clearStatisticCache()
217
    {
218
        \clearstatcache(true, $this->getStreamName());
219
    }
220
 
221
    /**
222
     * Clear all files status cache.
223
     */
224
    public static function clearAllStatisticCaches()
225
    {
226
        \clearstatcache();
227
    }
228
 
229
    /**
230
     * Set access and modification time of file.
231
     */
232
    public function touch(int $time = null, int $atime = null): bool
233
    {
234
        if (null === $time) {
235
            $time = \time();
236
        }
237
 
238
        if (null === $atime) {
239
            $atime = $time;
240
        }
241
 
242
        return \touch($this->getStreamName(), $time, $atime);
243
    }
244
 
245
    /**
246
     * Copy file.
247
     * Return the destination file path if succeed, false otherwise.
248
     */
249
    public function copy(string $to, bool $force = StreamTouchable::DO_NOT_OVERWRITE): bool
250
    {
251
        $from = $this->getStreamName();
252
 
253
        if ($force === StreamTouchable::DO_NOT_OVERWRITE &&
254
            true === \file_exists($to)) {
255
            return true;
256
        }
257
 
258
        if (null === $this->getStreamContext()) {
259
            return @\copy($from, $to);
260
        }
261
 
262
        return @\copy($from, $to, $this->getStreamContext()->getContext());
263
    }
264
 
265
    /**
266
     * Move a file.
267
     */
268
    public function move(
269
        string $name,
270
        bool $force = StreamTouchable::DO_NOT_OVERWRITE,
271
        bool $mkdir = StreamTouchable::DO_NOT_MAKE_DIRECTORY
272
    ): bool {
273
        $from = $this->getStreamName();
274
 
275
        if ($force === StreamTouchable::DO_NOT_OVERWRITE &&
276
            true === \file_exists($name)) {
277
            return false;
278
        }
279
 
280
        if (StreamTouchable::MAKE_DIRECTORY === $mkdir) {
281
            FileDirectory::create(
282
                \dirname($name),
283
                FileDirectory::MODE_CREATE_RECURSIVE
284
            );
285
        }
286
 
287
        if (null === $this->getStreamContext()) {
288
            return @\rename($from, $name);
289
        }
290
 
291
        return @\rename($from, $name, $this->getStreamContext()->getContext());
292
    }
293
 
294
    /**
295
     * Delete a file.
296
     */
297
    public function delete(): bool
298
    {
299
        if (null === $this->getStreamContext()) {
300
            return @\unlink($this->getStreamName());
301
        }
302
 
303
        return @\unlink(
304
            $this->getStreamName(),
305
            $this->getStreamContext()->getContext()
306
        );
307
    }
308
 
309
    /**
310
     * Change file group.
311
     */
312
    public function changeGroup($group): bool
313
    {
314
        return \chgrp($this->getStreamName(), $group);
315
    }
316
 
317
    /**
318
     * Change file mode.
319
     */
320
    public function changeMode(int $mode): bool
321
    {
322
        return \chmod($this->getStreamName(), $mode);
323
    }
324
 
325
    /**
326
     * Change file owner.
327
     */
328
    public function changeOwner($user): bool
329
    {
330
        return \chown($this->getStreamName(), $user);
331
    }
332
 
333
    /**
334
     * Change the current umask.
335
     */
336
    public static function umask(int $umask = null): int
337
    {
338
        if (null === $umask) {
339
            return \umask();
340
        }
341
 
342
        return \umask($umask);
343
    }
344
 
345
    /**
346
     * Check if it is a file.
347
     */
348
    public function isFile(): bool
349
    {
350
        return \is_file($this->getStreamName());
351
    }
352
 
353
    /**
354
     * Check if it is a link.
355
     */
356
    public function isLink(): bool
357
    {
358
        return \is_link($this->getStreamName());
359
    }
360
 
361
    /**
362
     * Check if it is a directory.
363
     */
364
    public function isDirectory(): bool
365
    {
366
        return \is_dir($this->getStreamName());
367
    }
368
 
369
    /**
370
     * Check if it is a socket.
371
     */
372
    public function isSocket(): bool
373
    {
374
        return \filetype($this->getStreamName()) === 'socket';
375
    }
376
 
377
    /**
378
     * Check if it is a FIFO pipe.
379
     */
380
    public function isFIFOPipe(): bool
381
    {
382
        return \filetype($this->getStreamName()) === 'fifo';
383
    }
384
 
385
    /**
386
     * Check if it is character special file.
387
     */
388
    public function isCharacterSpecial(): bool
389
    {
390
        return \filetype($this->getStreamName()) === 'char';
391
    }
392
 
393
    /**
394
     * Check if it is block special.
395
     */
396
    public function isBlockSpecial(): bool
397
    {
398
        return \filetype($this->getStreamName()) === 'block';
399
    }
400
 
401
    /**
402
     * Check if it is an unknown type.
403
     */
404
    public function isUnknown(): bool
405
    {
406
        return \filetype($this->getStreamName()) === 'unknown';
407
    }
408
 
409
    /**
410
     * Set the open mode.
411
     */
412
    protected function setMode(string $mode)
413
    {
414
        $old = $this->_mode;
415
        $this->_mode = $mode;
416
 
417
        return $old;
418
    }
419
 
420
    /**
421
     * Get the open mode.
422
     */
423
    public function getMode()
424
    {
425
        return $this->_mode;
426
    }
427
 
428
    /**
429
     * Get inode.
430
     */
431
    public function getINode(): int
432
    {
433
        return \fileinode($this->getStreamName());
434
    }
435
 
436
    /**
437
     * Check if the system is case sensitive or not.
438
     */
439
    public static function isCaseSensitive(): bool
440
    {
441
        return !(
442
            \file_exists(\mb_strtolower(__FILE__)) &&
443
            \file_exists(\mb_strtoupper(__FILE__))
444
        );
445
    }
446
 
447
    /**
448
     * Get a canonicalized absolute pathname.
449
     */
450
    public function getRealPath(): string
451
    {
452
        if (false === $out = \realpath($this->getStreamName())) {
453
            return $this->getStreamName();
454
        }
455
 
456
        return $out;
457
    }
458
 
459
    /**
460
     * Get file extension (if exists).
461
     */
462
    public function getExtension(): string
463
    {
464
        return \pathinfo(
465
            $this->getStreamName(),
466
            \PATHINFO_EXTENSION
467
        );
468
    }
469
 
470
    /**
471
     * Get filename without extension.
472
     */
473
    public function getFilename(): string
474
    {
475
        $file = \basename($this->getStreamName());
476
 
477
        if (\defined('PATHINFO_FILENAME')) {
478
            return \pathinfo($file, \PATHINFO_FILENAME);
479
        }
480
 
481
        if (\strstr($file, '.')) {
482
            return \substr($file, 0, \strrpos($file, '.'));
483
        }
484
 
485
        return $file;
486
    }
487
}