|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Util |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2010-2013 Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Represents a file in the file system and uses SplFileInfo a high-level object oriented interface to |
|
13
|
|
|
* information for an individual file. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Util |
|
16
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class File extends \SplFileInfo |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Constructs a new file from the given path. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $path The path to the file |
|
24
|
|
|
* @param boolean $check Whether to check the path or not |
|
25
|
|
|
* |
|
26
|
|
|
* @throws \OutOfRangeException If the given path is not a file |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($path, $check = true) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($check && !is_file($path)) { |
|
31
|
|
|
throw new \OutOfRangeException("path $path is not a file"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
parent::__construct($path); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Returns the extension of the file. |
|
39
|
|
|
* SplFileInfo::getExtension() is not available before PHP 5.3.6 |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getExtension() |
|
44
|
|
|
{ |
|
45
|
|
|
return pathinfo($this->getBasename(), PATHINFO_EXTENSION); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Moves the file to a new location. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $dir The destination folder |
|
52
|
|
|
* @param string $name The new file name |
|
53
|
|
|
* |
|
54
|
|
|
* @return File A File object representing the new file |
|
55
|
|
|
* @throws \RuntimeException if the target file could not be created |
|
56
|
|
|
*/ |
|
57
|
|
|
public function move($dir, $name = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$target = $this->getTargetFile($dir, $name); |
|
60
|
|
|
|
|
61
|
|
|
rename($this->getPathname(), $target); |
|
62
|
|
|
|
|
63
|
|
|
chmod($target, 0666 & ~umask()); |
|
64
|
|
|
|
|
65
|
|
|
return $target; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $dir The destination folder |
|
70
|
|
|
* @param null|string $name |
|
71
|
|
|
* |
|
72
|
|
|
* @return File |
|
73
|
|
|
* @throws \RuntimeException |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getTargetFile($dir, $name = null) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!is_dir($dir)) { |
|
78
|
|
|
throw new \RuntimeException("The destination folder $dir"); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (!is_writable($dir)) { |
|
82
|
|
|
throw new \RuntimeException("Unable to write in the $dir directory"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return new self($dir . DS . (null === $name ? $this->getBasename() : $this->getName($name)), false); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns locale independent base name of the given path. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $name The new file name |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getName($name) |
|
96
|
|
|
{ |
|
97
|
|
|
$originalName = str_replace('\\', '/', $name); |
|
98
|
|
|
$pos = strrpos($originalName, '/'); |
|
99
|
|
|
|
|
100
|
|
|
return (false === $pos) ? $originalName : substr($originalName, $pos + 1); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|