/
Package.php
1243 lines (1097 loc) · 38.2 KB
/
Package.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Concrete\Core\Package;
use Concrete\Core\Application\Application;
use Concrete\Core\Application\UserInterface\Dashboard\Navigation\NavigationCache;
use Concrete\Core\Backup\ContentImporter;
use Concrete\Core\Config\Repository\Liaison;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Database\DatabaseStructureManager;
use Concrete\Core\Database\EntityManager\Driver\CoreDriver;
use Concrete\Core\Database\EntityManager\Provider\PackageProviderFactory;
use Concrete\Core\Database\Schema\Schema;
use Concrete\Core\Entity\Package as PackageEntity;
use Concrete\Core\Package\Dependency\DependencyChecker;
use Concrete\Core\Package\ItemCategory\Manager;
use Concrete\Core\Page\Theme\Theme;
use Concrete\Core\Support\Facade\Application as ApplicationFacade;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Common\Proxy\ProxyGenerator;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Schema\Comparator as SchemaComparator;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup;
use Gettext\Translations;
use Localization;
use stdClass;
abstract class Package implements LocalizablePackageInterface
{
/**
* Error code: Invalid Package.
*
* @var int
*/
const E_PACKAGE_NOT_FOUND = 1;
/**
* Error code: You've already installed that package.
*
* @var int
*/
const E_PACKAGE_INSTALLED = 2;
/**
* Error code: This package requires Concrete version %s or greater.
*
* @var int
*/
const E_PACKAGE_VERSION = 3;
/**
* Error code: An error occurred while downloading the package.
*
* @var int
*/
const E_PACKAGE_DOWNLOAD = 4;
/**
* Error code: Concrete was not able to save the package after download.
*
* @var int
*/
const E_PACKAGE_SAVE = 5;
/**
* Error code: An error occurred while trying to unzip the package.
*
* @var int
*/
const E_PACKAGE_UNZIP = 6;
/**
* Error code: An error occurred while trying to install the package.
*
* @var int
*/
const E_PACKAGE_INSTALL = 7;
/**
* Error code: Unable to backup old package directory to %s.
*
* @var int
*/
const E_PACKAGE_MIGRATE_BACKUP = 8;
/**
* Error code: This package isn't currently available for this version of Concrete.
*
* @var int
*/
const E_PACKAGE_INVALID_APP_VERSION = 20;
/**
* Error code: This package contains the active site theme, please change the theme before uninstalling.
*
* @var int
*/
const E_PACKAGE_THEME_ACTIVE = 21;
/**
* Absolute path to the /concrete/packages directory.
*
* @var string
*/
protected $DIR_PACKAGES_CORE = DIR_PACKAGES_CORE;
/**
* Absolute path to the /packages directory.
*
* @var string
*/
protected $DIR_PACKAGES = DIR_PACKAGES;
/**
* Path to the /concrete/packages directory relative to the web root.
*
* @var string
*/
protected $REL_DIR_PACKAGES_CORE = REL_DIR_PACKAGES_CORE;
/**
* Path to the /concrete/packages directory relative to the web root.
*
* @var string
*/
protected $REL_DIR_PACKAGES = REL_DIR_PACKAGES;
/**
* Associated package entity.
*
* @var PackageEntity|null
*/
protected $entity;
/**
* The Application instance.
*
* @var Application
*/
protected $app;
/**
* The database configuration liaison.
*
* @var Liaison|null
*/
protected $config;
/**
* The file configuration liaison.
*
* @var Liaison|null
*/
protected $fileConfig;
/**
* @deprecated
* Whether to automatically map core extensions into the packages src/Concrete directory (and map them to Concrete\Package\MyPackage),
* or map the entire src/ directory to Concrete\Package\MyPackage\Src
* (and automatically map core extensions to Concrete\Package\MyPackage\Src).
* This will be ALWAYS considered as FALSE if your package requires 8.0 or greater or if your package defines the pkgAutoloaderMapCoreExtensions property.
*
* @var bool
*/
protected $pkgEnableLegacyNamespace = true;
/**
* The custom autoloader prefixes to be automatically added to the class loader.
* Array keys are the locations (relative to the package directory).
* Array values are the paths (not relative to the package namespace).
*
* @var array
*
* @example ['src/PortlandLabs' => \PortlandLabs']
*/
protected $pkgAutoloaderRegistries = [];
/**
* The minimum Concrete version compatible with the package.
* Override this value according to the minimum required version for your package.
*
* @var string
*/
protected $appVersionRequired = '5.7.0';
/**
* Override this value and set it to true if your package clears all existing website content when it's being installed.
*
* @var bool
*/
protected $pkgAllowsFullContentSwap = false;
/**
* Override this value to add additional content swap templates.
*
* @var array
*/
protected $pkgContentSwapFiles = [
"content.xml" => "Default"
];
/**
* Override this value and set it to true if your package provides the file thumbnails.
* If it's false, the file thumbnails are generated during the install process.
*
* @var bool
*/
protected $pkgContentProvidesFileThumbnails = false;
/**
* The full path of the package directory moved to the trash folder.
*
* @var string|null
*/
protected $backedUpFname;
/**
* An array describing the package dependencies.
* Keys are package handles.
* Values may be:
* - false: this package can't be installed if the other package is already installed.
* - true: this package can't be installed of the other package is not installed
* - a string: this package can't be installed of the other package is not installed or it's installed with an older version
* - an array with two strings, representing the minimum and the maximum version of the other package to be installed.
*
* @var array
*
* @example [
* // This package can't be installed if a package with handle other_package_1 is already installed.
* 'other_package_1' => false,
* // This package can't be installed if a package with handle other_package_2 is not installed.
* 'other_package_2' => true,
* // This package can't be installed if a package with handle other_package_3 is not installed, or it has a version prior to 1.0
* 'other_package_3' => '1.0',
* // This package can't be installed if a package with handle other_package_4 is not installed, or it has a version prior to 2.0, or it has a version after 2.9
* 'other_package_4' => ['2.0', '2.9'],
* ]
*/
protected $packageDependencies = [];
/**
* Initialize the instance.
*
* @param Application $app the application instance
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Get the associated package entity (if available).
*
* @return PackageEntity|null May return NULL if the package is invalid and/or if it's not installed
*/
public function getPackageEntity()
{
if ($this->entity === null) {
$this->entity = $this->app->make(PackageService::class)->getByHandle($this->getPackageHandle());
}
return $this->entity;
}
/**
* Set the associated package entity.
*
* @param PackageEntity $entity
*/
public function setPackageEntity(PackageEntity $entity)
{
$this->entity = $entity;
}
/**
* @return array
*/
public function getContentSwapFiles(): array
{
return $this->pkgContentSwapFiles;
}
/**
* @param array $pkgContentSwapFiles
* @return Package
*/
public function setContentSwapFiles(array $pkgContentSwapFiles): Package
{
$this->pkgContentSwapFiles = $pkgContentSwapFiles;
return $this;
}
/**
* Get the Application instance.
*
* @return Application
*/
public function getApplication()
{
return $this->app;
}
/**
* Get the content swapper.
*
* @return \Concrete\Core\Package\ContentSwapperInterface
*/
public function getContentSwapper()
{
return new ContentSwapper();
}
/**
* Import a concrete5-cif XML file.
*
* @param string $file the path to the file, relative to the package directory
*/
public function installContentFile($file)
{
$ci = new ContentImporter();
$cache = $this->app->make('cache/request');
$cacheEnabled = $cache->isEnabled();
if ($cacheEnabled) {
$cache->disable();
}
try {
$ci->importContentFile($this->getPackagePath() . '/' . $file);
} finally {
if ($cacheEnabled) {
$cache->enable();
}
}
}
/**
* Should this package enable legacy namespaces?
*
* This returns true IF:
* 1. $this->pkgAutoloaderMapCoreExtensions is false or unset
* 2. The required package version > 7.9.9 meaning version 8 or newer
* 3. $this->pkgEnableLegacyNamespace is true
*
* @return bool
*/
public function shouldEnableLegacyNamespace()
{
if (isset($this->pkgAutoloaderMapCoreExtensions) && $this->pkgAutoloaderMapCoreExtensions) {
// We're no longer using this to denote non-legacy applications, but if a package uses this
// that means it's NOT using the legacy namespace.
return false;
}
$concrete = '7.9.9';
$package = $this->getApplicationVersionRequired();
if (version_compare($package, $concrete, '>')) {
return false;
}
return $this->pkgEnableLegacyNamespace;
}
/**
* Get the default configuration liaison.
*
* @return Liaison
*/
public function getConfig()
{
return $this->getDatabaseConfig();
}
/**
* Get the database configuration liaison.
*
* @return Liaison
*/
public function getDatabaseConfig()
{
if (!$this->config) {
$this->config = new Liaison($this->app->make('config/database'), $this->getPackageHandle());
}
return $this->config;
}
/**
* Get the filesystem configuration liaison.
*
* @return Liaison
*/
public function getFileConfig()
{
if (!$this->fileConfig) {
$this->fileConfig = new Liaison($this->app->make('config'), $this->getPackageHandle());
}
return $this->fileConfig;
}
/**
* Get the custom autoloader prefixes to be automatically added to the class loader.
* Array keys are the locations (relative to the package directory).
* Array values are the paths (not relative to the package namespace).
*
* @return array
*
* @example ['src/PortlandLabs' => \PortlandLabs']
*/
public function getPackageAutoloaderRegistries()
{
return $this->pkgAutoloaderRegistries;
}
/**
* Get the package handle.
*
* @return string
*/
public function getPackageHandle()
{
return isset($this->pkgHandle) ? $this->pkgHandle : '';
}
/**
* Get the translated name of the package.
*
* @return string
*/
public function getPackageName()
{
return isset($this->pkgName) ? t($this->pkgName) : '';
}
/**
* Get the translated package description.
*
* @return string
*/
public function getPackageDescription()
{
return isset($this->pkgDescription) ? t($this->pkgDescription) : '';
}
/**
* Get the installed package version.
*
* @return string
*/
public function getPackageVersion()
{
return isset($this->pkgVersion) ? $this->pkgVersion : '';
}
/**
* Get the minimum Concrete version compatible with the package.
*
* @return string
*/
public function getApplicationVersionRequired()
{
return $this->appVersionRequired;
}
/**
* Should the install options page be shown?
* The install options page may be for install notes and/or full contents swap confirmation.
*
* @return bool
*/
public function showInstallOptionsScreen()
{
return $this->hasInstallNotes() || $this->allowsFullContentSwap();
}
/**
* Does this package have install notes?
*
* @return bool
*/
public function hasInstallNotes()
{
return file_exists($this->getPackagePath() . '/' . DIRNAME_ELEMENTS . '/' . DIRNAME_DASHBOARD . '/install.php');
}
/**
* Does this package have uninstall notes?
*
* @return bool
*/
public function hasUninstallNotes()
{
return file_exists($this->getPackagePath() . '/' . DIRNAME_ELEMENTS . '/' . DIRNAME_DASHBOARD . '/uninstall.php');
}
/**
* Does this package have a post-install page?
*
* @return bool
*/
public function hasInstallPostScreen()
{
return file_exists($this->getPackagePath() . '/' . DIRNAME_ELEMENTS . '/' . DIRNAME_DASHBOARD . '/install_post.php');
}
/**
* Does this package clear all existing website content when it's being installed?
*
* @return bool
*/
public function allowsFullContentSwap()
{
return $this->pkgAllowsFullContentSwap;
}
/**
* Get the absolute path to the package.
*
* @return string
*/
public function getPackagePath()
{
$packageHandle = $this->getPackageHandle();
$result = $this->DIR_PACKAGES . '/' . $packageHandle;
if (!is_dir($result)) {
$result = $this->DIR_PACKAGES_CORE . '/' . $packageHandle;
}
return $result;
}
/**
* Get the path to the package relative to the web root.
*
* @return string
*/
public function getRelativePath()
{
$packageHandle = $this->getPackageHandle();
if (is_dir($this->DIR_PACKAGES . '/' . $packageHandle)) {
$result = $this->REL_DIR_PACKAGES . '/' . $packageHandle;
} else {
$result = $this->REL_DIR_PACKAGES_CORE . '/' . $packageHandle;
}
return $result;
}
/**
* Get the path to the package relative to the Concrete installation folder.
*
* @return string
*/
public function getRelativePathFromInstallFolder()
{
return '/' . DIRNAME_PACKAGES . '/' . $this->getPackageHandle();
}
/**
* {@inheritdoc}
*
* @see LocalizablePackageInterface::getTranslationFile()
*/
public function getTranslationFile($locale)
{
return $this->getPackagePath() . '/' . DIRNAME_LANGUAGES . "/{$locale}/LC_MESSAGES/messages.mo";
}
/**
* Does this package provide the file thumbnails?
* If false, the file thumbnails are generated during the install process.
*
* @return bool
*/
public function contentProvidesFileThumbnails()
{
return $this->pkgContentProvidesFileThumbnails;
}
/**
* Install the package info row and the database (doctrine entities and db.xml).
* Packages installing additional content should override this method, call the parent method (`parent::install()`).
*
* @return PackageEntity
*/
public function install()
{
PackageList::refreshCache();
$em = $this->app->make(EntityManagerInterface::class);
$package = new PackageEntity();
$package->setPackageName($this->getPackageName());
$package->setPackageDescription($this->getPackageDescription());
$package->setPackageVersion($this->getPackageVersion());
$package->setPackageHandle($this->getPackageHandle());
$em->persist($package);
$em->flush();
$this->app->make('cache/overrides')->flush();
$this->installDatabase();
Localization::clearCache();
$navigationCache = $this->app->make(NavigationCache::class);
$navigationCache->clear();
return $package;
}
/**
* Uninstall the package:
* - delete the installed items associated to the package
* - destroy the package proxy classes of entities
* - remove the package info row.
*/
public function uninstall()
{
/** @var Manager $manager */
$manager = $this->app->make(Manager::class, ['application' => $this->app]);
$categories = $manager->getPackageItemCategories();
$package = $this->getPackageEntity();
foreach ($categories as $category) {
if ($category->hasItems($package)) {
$category->removeItems($package);
}
}
$this->app->make('config')->clearNamespace($this->getPackageHandle());
$this->app->make('config/database')->clearNamespace($this->getPackageHandle());
$em = $this->getPackageEntityManager();
$dbm = null;
if ($em !== null) {
$dbm = new DatabaseStructureManager($em);
$dbm->destroyProxyClasses();
}
$em = $this->app->make(EntityManagerInterface::class);
$em->remove($package);
$em->flush();
Localization::clearCache();
if ($dbm) {
$dbm->generateProxyClasses();
}
$navigationCache = $this->app->make(NavigationCache::class);
$navigationCache->clear();
}
/**
* Get the contents of the package's CHANGELOG file.
*
* @return string if no changelog is available an empty string is returned
*/
public function getChangelogContents()
{
$prefix = $this->getPackagePath() . '/';
foreach (['CHANGELOG', 'CHANGELOG.txt', 'CHANGELOG.md'] as $name) {
$file = $prefix . $name;
if (is_file($file)) {
$contents = $this->app->make('helper/file')->getContents($file);
return nl2br(h($contents));
}
}
return '';
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getInstalledList()
*
* @return PackageEntity[]
*/
public static function getInstalledList()
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getInstalledList();
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getInstalledHandles()
*
* @return string[]
*/
public static function getInstalledHandles()
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getInstalledHandles();
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getByHandle($pkgHandle)
*
* @param string $pkgHandle
*
* @return PackageEntity|null
*/
public static function getByHandle($pkgHandle)
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getByHandle($pkgHandle);
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getLocalUpgradeablePackages()
*
* @return PackageEntity[]
*/
public static function getLocalUpgradeablePackages()
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getLocalUpgradeablePackages();
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getRemotelyUpgradeablePackages()
*
* @return PackageEntity[]
*/
public static function getRemotelyUpgradeablePackages()
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getRemotelyUpgradeablePackages();
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getAvailablePackages($filterInstalled)
*
* @param bool $filterInstalled
*
* @return Package[]
*/
public static function getAvailablePackages($filterInstalled = true)
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getAvailablePackages($filterInstalled);
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getByID($pkgID)
*
* @param int $pkgID
*
* @return PackageEntity|null
*/
public static function getByID($pkgID)
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getByID($pkgID);
}
/**
* @deprecated
* Use $app->make('Concrete\Core\Package\PackageService')->getClass($pkgHandle)
*
* @param string $pkgHandle
*
* @return Package
*/
public static function getClass($pkgHandle)
{
$app = ApplicationFacade::getFacadeApplication();
return $app->make(PackageService::class)->getClass($pkgHandle);
}
/**
* Perform tests before this package is installed.
*
* @param bool $testForAlreadyInstalled Set to false to skip checking if this package is already installed
*
* @return \Concrete\Core\Error\ErrorList\ErrorList|true return true if the package can be installed, an ErrorList instance otherwise
*/
public function testForInstall($testForAlreadyInstalled = true)
{
$errors = $this->app->make('error');
// Step 1 does that package exist ?
if ($this instanceof BrokenPackage) {
$errors->add($this->getErrorText(self::E_PACKAGE_NOT_FOUND));
} elseif ($this->getPackageHandle() === '' || !is_dir($this->getPackagePath())) {
$errors->add($this->getErrorText(self::E_PACKAGE_NOT_FOUND));
}
// Step 2 - check to see if the user has already installed a package w/this handle
if ($testForAlreadyInstalled) {
$entity = $this->getPackageEntity();
if ($entity !== null && $entity->isPackageInstalled()) {
$errors->add($this->getErrorText(self::E_PACKAGE_INSTALLED));
}
}
if (!$errors->has()) {
// Step 3 - test minimum application version requirement
$applicationVersionRequired = $this->getApplicationVersionRequired();
if (version_compare(APP_VERSION, $applicationVersionRequired, '<')) {
$errors->add($this->getErrorText([self::E_PACKAGE_VERSION, $applicationVersionRequired]));
}
// Step 4 - Check for package dependencies
$dependencyChecker = $this->app->build(DependencyChecker::class);
$errors->add($dependencyChecker->testForInstall($this));
}
return $errors->has() ? $errors : true;
}
/**
* Perform tests before this package is upgraded.
*
* @return \Concrete\Core\Error\ErrorList\ErrorList|true return null if the package can be upgraded, an ErrorList instance otherwise
*/
public function testForUpgrade()
{
$result = $this->testForInstall(false);
return $result;
}
/**
* Perform tests before this package is uninstalled.
*
* @return \Concrete\Core\Error\ErrorList\ErrorList|true return true if the package can be uninstalled, an ErrorList instance otherwise
*/
public function testForUninstall()
{
$errors = $this->app->make('error');
/** @var Manager $manager */
$manager = $this->app->make(Manager::class, ['application' => $this->app]);
$driver = $manager->driver('theme');
$themes = $driver->getItems($this->getPackageEntity());
// Step 1, check for active themes
$active_theme = Theme::getSiteTheme();
foreach ($themes as $theme) {
if ($active_theme->getThemeID() == $theme->getThemeID()) {
$errors->add($this->getErrorText(self::E_PACKAGE_THEME_ACTIVE));
break;
}
}
// Step 2, check for package dependencies
$dependencyChecker = $this->app->build(DependencyChecker::class);
$errors->add($dependencyChecker->testForUninstall($this));
return $errors->has() ? $errors : true;
}
/**
* Move the current package directory to the trash directory, and rename it with the package handle and a date code.
*
* @return \Concrete\Core\Error\ErrorList\ErrorList|static return the Package instance if the package has been moved, an ErrorList instance otherwise
*/
public function backup()
{
$packageHandle = $this->getPackageHandle();
$errors = $this->app->make('error');
if ($packageHandle === '' || !is_dir(DIR_PACKAGES . '/' . $packageHandle)) {
$errors->add($this->getErrorText(self::E_PACKAGE_NOT_FOUND));
} else {
$config = $this->app->make('config');
$trash = $config->get('concrete.misc.package_backup_directory');
if (!is_dir($trash)) {
@mkdir($trash, $config->get('concrete.filesystem.permissions.directory'));
}
if (!is_dir($trash)) {
$errors->add($this->getErrorText(self::E_PACKAGE_MIGRATE_BACKUP));
} else {
$trashName = $trash . '/' . $packageHandle . '_' . date('YmdHis');
if (!@rename(DIR_PACKAGES . '/' . $this->getPackageHandle(), $trashName)) {
$errors->add($this->getErrorText(self::E_PACKAGE_MIGRATE_BACKUP));
} else {
$this->backedUpFname = $trashName;
}
}
}
return $errors->has() ? $errors : $this;
}
/**
* If a package was just backed up by this instance of the package object and the packages/package handle directory doesn't exist,
* this will restore the package from the trash.
*
* @return bool
*/
public function restore()
{
$result = false;
if ($this->backedUpFname !== null && is_dir($this->backedUpFname)) {
$newPath = DIR_PACKAGES . '/' . $this->getPackageHandle();
if (!is_dir($newPath)) {
if (@rename($this->backedUpFname, $newPath)) {
$result = true;
}
}
}
return $result;
}
/**
* @deprecated
* This method was limited. It let you specify a location but in V8 with the Doctrine Entity Manager driver chain
* we also need to specify namespaces. Instead of specifying entity paths this way, update your
* package controller to implement the Concrete\Core\Database\EntityManager\Provider\ProviderAggregateInterface,
* and create a method named getEntityManagerProvider that returns an instance of the
* Concrete\Core\Database\EntityManager\Provider\ProviderInterface.
*
* For example, if I want to specify that my package has entities found at src/PortlandLabs\FooBar\Entity, with the
* namespace PortlandLabs\FooBar\Entity, my method is simply
* public function getEntityManagerProvider() {
* return new StandardPackageProvider($this->app, $this, ['src/MSM/Entity' => 'PortlandLabs\MSM\Entity']);
* }
*/
public function getPackageEntityPaths()
{
// Support for the legacy method for backwards compatibility
if (method_exists($this, 'getPackageEntityPath')) {
return [$this->getPackageEntityPath()];
}
// If we're using a legacy package, we scan the entire src directory
return [$this->getPackagePath() . '/' . DIRNAME_CLASSES];
}
/**
* Installs the packages database through doctrine entities and db.xml database definitions.
*/
public function installDatabase()
{
$this->installEntitiesDatabase();
static::installDB($this->getPackagePath() . '/' . FILENAME_PACKAGE_DB);
}
public function installEntitiesDatabase()
{
$em = $this->getPackageEntityManager();
if ($em !== null) {
$structure = new DatabaseStructureManager($em);
$structure->installDatabase();
// Create or update entity proxies
$metadata = $em->getMetadataFactory()->getAllMetadata();
$em->getProxyFactory()->generateProxyClasses($metadata, $em->getConfiguration()->getProxyDir());
}
}
/**
* Installs a package database from an XML file.
*
* @param string $xmlFile Path to the database XML file
*
* @throws \Doctrine\DBAL\ConnectionException
*
* @return bool|stdClass Returns false if the XML file could not be found
*/
public static function installDB($xmlFile)
{
if (file_exists($xmlFile)) {
$app = ApplicationFacade::getFacadeApplication();
$db = $app->make(Connection::class);
/* @var Connection $db */
$parser = Schema::getSchemaParser(simplexml_load_file($xmlFile));
$parser->setIgnoreExistingTables(false);
$toSchema = $parser->parse($db);
$fromSchema = $db->getSchemaManager()->createSchema();
$comparator = new SchemaComparator();
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
$saveQueries = $schemaDiff->toSaveSql($db->getDatabasePlatform());
if (count($saveQueries)) {
$db->beginTransaction();
foreach ($saveQueries as $query) {
$db->query($query);
}
if ($db->isTransactionActive() && !$db->isAutoCommit()) {
$db->commit();
}
}