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 Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract platform implementation. |
17
|
|
|
* |
18
|
|
|
* Utility methods for individual platforms. |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractPlatform { |
21
|
|
|
/** |
22
|
|
|
* Temporary directories. |
23
|
|
|
* |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
protected $tempDirectories; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Temporary file/directory prefix. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
const TEMP_PREFIX = 'componentmgr-'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initialiser. |
37
|
|
|
* |
38
|
|
|
* @param \Symfony\Component\Filesystem\Filesystem $filesystem |
39
|
|
|
*/ |
40
|
5 |
|
public function __construct(Filesystem $filesystem) { |
41
|
5 |
|
$this->filesystem = $filesystem; |
|
|
|
|
42
|
5 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Destructor. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function __destruct() { |
50
|
|
|
$this->removeTempDirectories(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @override \ComponentManager\Platform\Platform |
55
|
|
|
*/ |
56
|
|
|
public function createTempDirectory() { |
57
|
|
|
$root = sys_get_temp_dir(); |
58
|
|
|
$directory = tempnam($root, static::TEMP_PREFIX); |
59
|
|
|
|
60
|
|
|
unlink($directory); |
61
|
|
|
mkdir($directory); |
62
|
|
|
|
63
|
|
|
$this->tempDirectories[] = $directory; |
64
|
|
|
|
65
|
|
|
return $directory; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @override \ComponentManager\Platform\Platform |
70
|
|
|
*/ |
71
|
4 |
|
public function getDirectorySeparator() { |
72
|
4 |
|
return DIRECTORY_SEPARATOR; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @override \ComponentManager\Platform\Platform |
77
|
|
|
*/ |
78
|
|
|
public function getWorkingDirectory() { |
79
|
|
|
return getcwd(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @override \ComponentManager\Platform\Platform |
84
|
|
|
*/ |
85
|
3 |
|
public function joinPaths($parts) { |
86
|
3 |
|
return implode($this->getDirectorySeparator(), $parts); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @override \ComponentManager\Platform\Platform |
91
|
|
|
*/ |
92
|
|
|
public function getPhpExecutable() { |
93
|
|
|
return PHP_BINARY; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @override \ComponentManager\Platform\Platform |
98
|
|
|
*/ |
99
|
|
|
public function getPhpScript() { |
|
|
|
|
100
|
|
|
return $_SERVER['argv'][0]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @override \ComponentManager\Platform\Platform |
105
|
|
|
*/ |
106
|
|
|
public function removeTempDirectories() { |
107
|
|
|
$this->filesystem->remove($this->tempDirectories); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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: