|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lincable\Http\File; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
|
6
|
|
|
|
|
7
|
|
|
class FileFactory |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Create a temporary file from specified original content. |
|
11
|
|
|
* |
|
12
|
|
|
* @param mixed $data |
|
13
|
|
|
* @param string $originalFileName |
|
14
|
|
|
* @param mixed|null $flags |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
4 |
|
public static function createTemporary( |
|
18
|
|
|
$data, |
|
19
|
|
|
string $originalFileName = null, |
|
20
|
|
|
$flags = null |
|
21
|
|
|
) { |
|
22
|
|
|
// Generate a temp file from configuration and file name. |
|
23
|
4 |
|
$filename = static::getTemporaryDirectory( |
|
|
|
|
|
|
24
|
4 |
|
str_random().'.'.static::extension($originalFileName) |
|
|
|
|
|
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
4 |
|
file_put_contents($filename, $data, $flags); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// Handle stream resource. |
|
30
|
4 |
|
if (is_resource($data)) { |
|
31
|
4 |
|
fclose($data); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
return new File($filename); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return the extension from filename. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $filename |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public static function extension(string $filename) |
|
44
|
|
|
{ |
|
45
|
5 |
|
return pathinfo($filename, PATHINFO_EXTENSION); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Return the file name from path. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $path |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
5 |
|
public static function fileName(string $path) |
|
55
|
|
|
{ |
|
56
|
5 |
|
return pathinfo($path, PATHINFO_BASENAME); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return the configured temporary directory. You may pass |
|
61
|
|
|
* a file name to build some custom path. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string|null $fileName |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
9 |
|
public static function getTemporaryDirectory(string $fileName = null) |
|
67
|
|
|
{ |
|
68
|
9 |
|
$configuredTemp = config('lincable.temp_directory'); |
|
69
|
|
|
|
|
70
|
9 |
|
$directory = str_finish( |
|
71
|
9 |
|
$configuredTemp === null |
|
72
|
9 |
|
? sys_get_temp_dir() |
|
73
|
9 |
|
: (string) $configuredTemp, |
|
74
|
9 |
|
DIRECTORY_SEPARATOR |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
9 |
|
if ($fileName) { |
|
78
|
4 |
|
return $directory.$fileName; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
5 |
|
return $directory; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.