|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
* @author Igor Sorokin <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Dspbee\Bundle\Common\File; |
|
7
|
|
|
|
|
8
|
|
|
use Dspbee\Bundle\Common\File\Exception\FileException; |
|
9
|
|
|
use Dspbee\Bundle\Common\File\Exception\FileNotFoundException; |
|
10
|
|
|
use Dspbee\Bundle\Common\File\Extension\Extension; |
|
11
|
|
|
use Dspbee\Bundle\Common\File\MimeType\MimeType; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class File |
|
15
|
|
|
* @package Dspbee\Bundle\Common\File |
|
16
|
|
|
*/ |
|
17
|
|
|
class File extends \SplFileInfo |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $path The path to the file |
|
21
|
|
|
* @param bool $checkPath Whether to check the path or not |
|
22
|
|
|
* |
|
23
|
|
|
* @throws FileNotFoundException If the given path is not a file |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($path, $checkPath = true) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($checkPath && !is_file($path)) { |
|
28
|
|
|
throw new FileNotFoundException($path); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
parent::__construct($path); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $directory |
|
36
|
|
|
* @param string|null $name |
|
37
|
|
|
* |
|
38
|
|
|
* @return File |
|
39
|
|
|
* |
|
40
|
|
|
* @throws FileException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function move($directory, $name = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$target = $this->getTargetFile($directory, $name); |
|
45
|
|
|
|
|
46
|
|
|
if (!@rename($this->getPathname(), $target)) { |
|
47
|
|
|
$error = error_get_last(); |
|
48
|
|
|
throw new FileException(sprintf('Could not rename the file "%s" (%s)', $this->getPathname(), strip_tags($error['message']))); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->customChmod($target); |
|
52
|
|
|
|
|
53
|
|
|
return $target; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function guessExtension() |
|
60
|
|
|
{ |
|
61
|
|
|
return Extension::getInstance()->guess($this->guessMimeType()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string|null |
|
66
|
|
|
*/ |
|
67
|
|
|
public function guessMimeType() |
|
68
|
|
|
{ |
|
69
|
|
|
return MimeType::getInstance()->guess($this->getPathname()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $directory |
|
74
|
|
|
* @param string|null $name |
|
75
|
|
|
* |
|
76
|
|
|
* @return File |
|
77
|
|
|
* |
|
78
|
|
|
* @throws FileException |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getTargetFile($directory, $name = null) |
|
81
|
|
|
{ |
|
82
|
|
|
if (!is_dir($directory)) { |
|
83
|
|
|
if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) { |
|
84
|
|
|
throw new FileException(sprintf('Unable to create the "%s" directory', $directory)); |
|
85
|
|
|
} |
|
86
|
|
|
} elseif (!is_writable($directory)) { |
|
87
|
|
|
throw new FileException(sprintf('Unable to write in the "%s" directory', $directory)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$target = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . (null === $name ? $this->getBasename() : $this->getName($name)); |
|
91
|
|
|
|
|
92
|
|
|
return new self($target, false); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns locale independent base name of the given path. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name The new file name |
|
99
|
|
|
* |
|
100
|
|
|
* @return string containing |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getName($name) |
|
103
|
|
|
{ |
|
104
|
|
|
$originalName = str_replace('\\', '/', $name); |
|
105
|
|
|
$pos = strrpos($originalName, '/'); |
|
106
|
|
|
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); |
|
107
|
|
|
|
|
108
|
|
|
return $originalName; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Chmod function with exception |
|
113
|
|
|
* |
|
114
|
|
|
* @param $target |
|
115
|
|
|
* @param $mode |
|
116
|
|
|
* |
|
117
|
|
|
* @throws FileException |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function customChmod($target, $mode = 0666) |
|
120
|
|
|
{ |
|
121
|
|
|
if (false === @chmod($target, $mode & ~umask())) { |
|
122
|
|
|
throw new FileException(sprintf('Unable to change mode of the "%s"', $target)); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |