/
PackageService.php
312 lines (278 loc) · 9.73 KB
/
PackageService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace Concrete\Core\Package;
use Concrete\Core\Application\Application;
use Concrete\Core\Database\EntityManager\Provider\PackageProviderFactory;
use Concrete\Core\Database\EntityManagerConfigUpdater;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Foundation\ClassLoader;
use Concrete\Core\Localization\Localization;
use Doctrine\ORM\EntityManagerInterface;
/**
* Service class for the package entities and controllers.
*/
class PackageService
{
/**
* The Localization service instance.
*
* @var Localization
*/
protected $localization;
/**
* The Application container instance.
*
* @var Application
*/
protected $application;
/**
* The EntityManagerInterface instance.
*
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* Initialize the instance.
*
* @param Localization $localization the Localization service instance
* @param Application $application the Application container instance
* @param EntityManagerInterface $entityManager the EntityManagerInterface instance
*/
public function __construct(Localization $localization, Application $application, EntityManagerInterface $entityManager)
{
$this->localization = $localization;
$this->application = $application;
$this->entityManager = $entityManager;
}
/**
* Get a package entity given its handle.
*
* @param string $pkgHandle
*
* @return \Concrete\Core\Entity\Package|null
*/
public function getByHandle($pkgHandle)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Package');
return $r->findOneBy(['pkgHandle' => $pkgHandle]);
}
/**
* Get a package entity given its ID.
*
* @param int $pkgID
*
* @return \Concrete\Core\Entity\Package|null
*/
public function getByID($pkgID)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Package');
return $r->findOneBy(['pkgID' => $pkgID]);
}
/**
* Get the package entities of installed packages.
*
* @return \Concrete\Core\Entity\Package[]
*/
public function getInstalledList()
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Package');
return $r->findBy(['pkgIsInstalled' => true], ['pkgDateInstalled' => 'asc']);
}
/**
* Get the package controllers of the available packages.
*
* @param bool $onlyNotInstalled true to get the controllers of not installed packages, false to get all the package controllers
*
* @return \Concrete\Core\Package\Package[]
*/
public function getAvailablePackages($onlyNotInstalled = true)
{
$dh = $this->application->make('helper/file');
$packages = $dh->getDirectoryContents(DIR_PACKAGES);
if ($onlyNotInstalled) {
$handles = $this->getInstalledHandles();
$packages = array_diff($packages, $handles);
}
if (count($packages) > 0) {
$packagesTemp = [];
// get package objects from the file system
foreach ($packages as $p) {
if (file_exists(DIR_PACKAGES . '/' . $p . '/' . FILENAME_CONTROLLER)) {
$pkg = $this->getClass($p);
if (!empty($pkg)) {
$packagesTemp[] = $pkg;
}
}
}
$packages = $packagesTemp;
}
return $packages;
}
/**
* Get the controllers of packages that have newer versions in the local packages directory than those which are in the Packages table.
* This means they're ready to be upgraded.
*
* @return \Concrete\Core\Package\Package[]
*/
public function getLocalUpgradeablePackages()
{
$packages = $this->getAvailablePackages(false);
$upgradeables = [];
foreach ($packages as $p) {
$entity = $this->getByHandle($p->getPackageHandle());
if ($entity) {
if (version_compare($p->getPackageVersion(), $entity->getPackageVersion(), '>')) {
$p->pkgCurrentVersion = $entity->getPackageVersion();
$upgradeables[] = $p;
}
}
}
return $upgradeables;
}
/**
* Get the handles of all the installed packages.
*
* @return string[]
*/
public function getInstalledHandles()
{
$query = 'select p.pkgHandle from \\Concrete\\Core\\Entity\\Package p';
$r = $this->entityManager->createQuery($query);
$result = $r->getArrayResult();
$handles = [];
foreach ($result as $r) {
$handles[] = $r['pkgHandle'];
}
return $handles;
}
/**
* Get the controllers of the packages that have an upgraded version available in the marketplace.
*
* @return \Concrete\Core\Package\Package[]
*/
public function getRemotelyUpgradeablePackages()
{
$packages = $this->getInstalledList();
$upgradeables = [];
foreach ($packages as $p) {
if (version_compare($p->getPackageVersion(), $p->getPackageVersionUpdateAvailable(), '<')) {
$upgradeables[] = $p;
}
}
return $upgradeables;
}
/**
* Initialize the entity manager for a package.
*
* @param \Concrete\Core\Package\Package $p The controller of package
* @param bool $clearCache Should the entity metadata cache be emptied?
*/
public function bootPackageEntityManager(Package $p, $clearCache = false)
{
$configUpdater = new EntityManagerConfigUpdater($this->entityManager);
$providerFactory = new PackageProviderFactory($this->application, $p);
$provider = $providerFactory->getEntityManagerProvider();
$configUpdater->addProvider($provider);
if ($clearCache) {
$cache = $this->entityManager->getConfiguration()->getMetadataCacheImpl();
if ($cache) {
$cache->flushAll();
}
}
}
/**
* Uninstall a package.
*
* @param \Concrete\Core\Package\Package $p the controller of the package to be uninstalled
*/
public function uninstall(Package $p)
{
$p->uninstall();
$config = $this->entityManager->getConfiguration();
$cache = $config->getMetadataCacheImpl();
if ($cache) {
$cache->flushAll();
}
}
/**
* Install a package.
*
* @param \Concrete\Core\Package\Package $p the controller of the package to be installed
* @param array $data data to be passed to the Package::validate_install(), Package::install(), ContentSwapper::swapContent(), ContentSwapper::on_after_swap_content() methods
*
* @return \Concrete\Core\Error\ErrorList\ErrorList|\Concrete\Core\Package\Package return an ErrorList instance in case of errors, the package controller otherwise
*/
public function install(Package $p, $data)
{
$this->localization->pushActiveContext(Localization::CONTEXT_SYSTEM);
ClassLoader::getInstance()->registerPackage($p);
if (method_exists($p, 'validate_install')) {
$response = $p->validate_install($data);
}
if (isset($response) && $response instanceof ErrorList && $response->has()) {
return $response;
}
$this->bootPackageEntityManager($p, true);
$p->install($data);
$u = new \User();
$swapper = $p->getContentSwapper();
if ($u->isSuperUser() && $swapper->allowsFullContentSwap($p) && $data['pkgDoFullContentSwap']) {
$swapper->swapContent($p, $data);
if (method_exists($p, 'on_after_swap_content')) {
$p->on_after_swap_content($data);
}
}
$this->localization->popActiveContext();
$pkg = $this->getByHandle($p->getPackageHandle());
return $p;
}
/**
* Get the controller of a package given its handle.
*
* @param string $pkgHandle Handle of package
*
* @return Package
*/
public function getClass($pkgHandle)
{
$cache = $this->application->make('cache/request');
$item = $cache->getItem('package/class/' . $pkgHandle);
$cl = $item->get();
if ($item->isMiss()) {
$item->lock();
// loads and instantiates the object
$cl = \Concrete\Core\Foundation\ClassLoader::getInstance();
$cl->registerPackageController($pkgHandle);
$class = '\\Concrete\\Package\\' . camelcase($pkgHandle) . '\\Controller';
try {
$cl = $this->application->make($class);
} catch (\Exception $ex) {
$cl = $this->application->make('Concrete\Core\Package\BrokenPackage', [$pkgHandle]);
}
$cache->save($item->set($cl));
}
return clone $cl;
}
/**
* @deprecated
*
* @param LocalizablePackageInterface $package
* @param string|null $locale
* @param \Zend\I18n\Translator\Translator|'current' $translator
*/
public function setupLocalization(LocalizablePackageInterface $package, $locale = null, $translator = 'current')
{
if ($translator === 'current') {
$translator = $this->localization->getActiveTranslateObject();
}
if (is_object($translator)) {
$locale = (string) $locale;
if ($locale === '') {
$locale = $this->localization->getLocale();
}
$languageFile = $package->getTranslationFile($locale);
if (is_file($languageFile)) {
$translator->addTranslationFile('gettext', $languageFile);
}
}
}
}