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\Command; |
12
|
|
|
|
13
|
|
|
use ComponentManager\Moodle; |
14
|
|
|
use ComponentManager\PackageFormat\PackageFormatFactory; |
15
|
|
|
use ComponentManager\PackageRepository\PackageRepository; |
16
|
|
|
use ComponentManager\PackageRepository\PackageRepositoryFactory; |
17
|
|
|
use ComponentManager\PackageSource\PackageSourceFactory; |
18
|
|
|
use ComponentManager\PlatformUtil; |
19
|
|
|
use ComponentManager\Project\Project; |
20
|
|
|
use ComponentManager\Project\ProjectFile; |
21
|
|
|
use ComponentManager\Project\ProjectLockFile; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
use Symfony\Component\Console\Command\Command; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Project-aware command trait. |
27
|
|
|
* |
28
|
|
|
* Provides helpful utility methods for accessing the project in the currrent |
29
|
|
|
* working directory. Import this into command implementations to reduce |
30
|
|
|
* duplication. |
31
|
|
|
*/ |
32
|
|
|
abstract class ProjectAwareCommand extends Command { |
33
|
|
|
/** |
34
|
|
|
* Logger. |
35
|
|
|
* |
36
|
|
|
* @var \Psr\Log\LoggerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Moodle bridge. |
42
|
|
|
* |
43
|
|
|
* @var \ComponentManager\Moodle |
44
|
|
|
*/ |
45
|
|
|
protected $moodle; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Package format factory. |
49
|
|
|
* |
50
|
|
|
* @var \ComponentManager\PackageFormat\PackageFormatFactory |
51
|
|
|
*/ |
52
|
|
|
protected $packageFormatFactory; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Package repository factory. |
56
|
|
|
* |
57
|
|
|
* @var \ComponentManager\PackageRepository\PackageRepositoryFactory |
58
|
|
|
*/ |
59
|
|
|
protected $packageRepositoryFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Package source factory. |
63
|
|
|
* |
64
|
|
|
* @var \ComponentManager\PackageSource\PackageSourceFactory |
65
|
|
|
*/ |
66
|
|
|
protected $packageSourceFactory; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Project. |
70
|
|
|
* |
71
|
|
|
* Lazily loaded -- be sure to call getProject() in order to ensure the |
72
|
|
|
* value is defined. |
73
|
|
|
* |
74
|
|
|
* @var \ComponentManager\Project\Project |
75
|
|
|
*/ |
76
|
|
|
protected $project; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initialiser. |
80
|
|
|
* |
81
|
|
|
* @param \ComponentManager\PackageFormat\PackageFormatFactory $packageFormatFactory |
|
|
|
|
82
|
|
|
* @param \ComponentManager\PackageRepository\PackageRepositoryFactory $packageRepositoryFactory |
|
|
|
|
83
|
|
|
* @param \ComponentManager\PackageSource\PackageSourceFactory $packageSourceFactory |
|
|
|
|
84
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
|
|
|
|
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function __construct(PackageRepositoryFactory $packageRepositoryFactory, |
|
|
|
|
87
|
|
|
PackageSourceFactory $packageSourceFactory, |
88
|
|
|
PackageFormatFactory $packageFormatFactory, |
89
|
|
|
LoggerInterface $logger) { |
90
|
|
|
$this->packageRepositoryFactory = $packageRepositoryFactory; |
91
|
|
|
$this->packageSourceFactory = $packageSourceFactory; |
92
|
|
|
$this->packageFormatFactory = $packageFormatFactory; |
93
|
|
|
|
94
|
|
|
$this->logger = $logger; |
95
|
|
|
|
96
|
|
|
parent::__construct(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the Moodle bridge. |
101
|
|
|
* |
102
|
|
|
* @param string|null $moodleDirectory |
103
|
|
|
* |
104
|
|
|
* @return \ComponentManager\Moodle |
105
|
|
|
*/ |
106
|
|
|
protected function getMoodle($moodleDirectory=null) { |
107
|
|
|
if ($this->moodle === null) { |
108
|
|
|
$moodleDirectory = ($moodleDirectory === null) |
109
|
|
|
? PlatformUtil::workingDirectory() : $moodleDirectory; |
110
|
|
|
|
111
|
|
|
$this->moodle = new Moodle($moodleDirectory); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->moodle; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get project. |
119
|
|
|
* |
120
|
|
|
* @param string|null $projectFilename |
121
|
|
|
* @param string|null $projectLockFilename |
122
|
|
|
* |
123
|
|
|
* @return \ComponentManager\Project\Project |
124
|
|
|
*/ |
125
|
|
|
protected function getProject($projectFilename=null, $projectLockFilename=null) { |
|
|
|
|
126
|
|
|
if ($this->project === null) { |
127
|
|
View Code Duplication |
if ($projectFilename === null) { |
|
|
|
|
128
|
|
|
$projectFilename = PlatformUtil::workingDirectory() |
129
|
|
|
. PlatformUtil::directorySeparator() |
130
|
|
|
. 'componentmgr.json'; |
131
|
|
|
} else { |
132
|
|
|
$projectFilename = PlatformUtil::expandPath($projectFilename); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
if ($projectLockFilename === null) { |
|
|
|
|
136
|
|
|
$projectLockFilename = PlatformUtil::workingDirectory() |
137
|
|
|
. PlatformUtil::directorySeparator() |
138
|
|
|
. 'componentmgr.lock.json'; |
139
|
|
|
} else { |
140
|
|
|
$projectLockFilename = PlatformUtil::expandPath( |
141
|
|
|
$projectLockFilename); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->logger->info('Parsing project file', [ |
145
|
|
|
'filename' => $projectFilename, |
146
|
|
|
]); |
147
|
|
|
$this->project = new Project( |
148
|
|
|
new ProjectFile($projectFilename), |
149
|
|
|
new ProjectLockFile($projectLockFilename), |
150
|
|
|
$this->packageRepositoryFactory, |
151
|
|
|
$this->packageSourceFactory, |
152
|
|
|
$this->packageFormatFactory); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->project; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.