1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moodle component manager. |
5
|
|
|
* |
6
|
|
|
* @author Luke Carrier <[email protected]> |
7
|
|
|
* @copyright 2016 Luke Carrier |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ComponentManager\Platform; |
12
|
|
|
|
13
|
|
|
use ComponentManager\Exception\PlatformException; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstract platform implementation. |
18
|
|
|
* |
19
|
|
|
* Utility methods for individual platforms. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractPlatform implements Platform { |
22
|
|
|
/** |
23
|
|
|
* Temporary file/directory prefix. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const TEMP_PREFIX = 'componentmgr-'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Temporary directories. |
31
|
|
|
* |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
protected $tempDirectories; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialiser. |
38
|
|
|
* |
39
|
|
|
* @param \Symfony\Component\Filesystem\Filesystem $filesystem |
40
|
|
|
*/ |
41
|
5 |
|
public function __construct(Filesystem $filesystem) { |
42
|
5 |
|
$this->filesystem = $filesystem; |
|
|
|
|
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @override \ComponentManager\Platform\Platform |
47
|
|
|
*/ |
48
|
|
|
public function createTempDirectory() { |
49
|
|
|
$root = sys_get_temp_dir(); |
50
|
|
|
$directory = tempnam($root, static::TEMP_PREFIX); |
51
|
|
|
|
52
|
|
|
unlink($directory); |
53
|
|
|
mkdir($directory); |
54
|
|
|
|
55
|
|
|
$this->tempDirectories[] = $directory; |
56
|
|
|
|
57
|
|
|
return $directory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @override \ComponentManager\Platform\Platform |
62
|
|
|
*/ |
63
|
4 |
|
public function getDirectorySeparator() { |
64
|
4 |
|
return DIRECTORY_SEPARATOR; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @override \ComponentManager\Platform\Platform |
69
|
|
|
*/ |
70
|
|
|
public function getWorkingDirectory() { |
71
|
|
|
return getcwd(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @override \ComponentManager\Platform\Platform |
76
|
|
|
*/ |
77
|
3 |
|
public function joinPaths($parts) { |
78
|
3 |
|
return implode($this->getDirectorySeparator(), $parts); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @override \ComponentManager\Platform\Platform |
83
|
|
|
*/ |
84
|
|
|
public function getPhpExecutable() { |
85
|
|
|
return PHP_BINARY; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @override \ComponentManager\Platform\Platform |
90
|
|
|
*/ |
91
|
|
|
public function getPhpScript() { |
92
|
|
|
return $_SERVER['argv'][0]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @override \ComponentManager\Platform\Platform |
97
|
|
|
*/ |
98
|
|
|
public function removeTempDirectories() { |
99
|
|
|
$this->filesystem->remove($this->tempDirectories); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @override \ComponentManager\Platform\Platform |
104
|
|
|
*/ |
105
|
|
|
public function removeTempDirectory($directory) { |
106
|
|
|
$index = array_search($directory, $this->tempDirectories, true); |
107
|
|
|
|
108
|
|
|
if ($index === false) { |
109
|
|
|
throw new PlatformException( |
110
|
|
|
sprintf('Directory "%s" is not a known temporary directory', $directory), |
|
|
|
|
111
|
|
|
PlatformException::CODE_UNKNOWN_TEMP_DIRECTORY); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->filesystem->remove($directory); |
115
|
|
|
unset($this->tempDirectories[$index]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: