[BUGFIX] Always throw exception if ext_emconf.php is missing
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Package / PackageManager.php
1 <?php
2 namespace TYPO3\CMS\Core\Package;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Symfony\Component\Finder\Finder;
18 use Symfony\Component\Finder\SplFileInfo;
19 use TYPO3\CMS\Core\Compatibility\LoadedExtensionArrayElement;
20 use TYPO3\CMS\Core\Core\Bootstrap;
21 use TYPO3\CMS\Core\Core\ClassLoadingInformation;
22 use TYPO3\CMS\Core\Service\OpcodeCacheService;
23 use TYPO3\CMS\Core\Utility\ArrayUtility;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\PathUtility;
26 use TYPO3\CMS\Core\Utility\StringUtility;
27
28 /**
29 * The default TYPO3 Package Manager
30 * Adapted from FLOW for TYPO3 CMS
31 */
32 class PackageManager implements \TYPO3\CMS\Core\SingletonInterface
33 {
34 /**
35 * @var \TYPO3\CMS\Core\Package\DependencyResolver
36 */
37 protected $dependencyResolver;
38
39 /**
40 * @var \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
41 */
42 protected $coreCache;
43
44 /**
45 * @var string
46 */
47 protected $cacheIdentifier;
48
49 /**
50 * @var array
51 */
52 protected $packagesBasePaths = [];
53
54 /**
55 * @var array
56 */
57 protected $packageAliasMap = [];
58
59 /**
60 * @var array
61 */
62 protected $runtimeActivatedPackages = [];
63
64 /**
65 * Absolute path leading to the various package directories
66 * @var string
67 */
68 protected $packagesBasePath = PATH_site;
69
70 /**
71 * Array of available packages, indexed by package key
72 * @var PackageInterface[]
73 */
74 protected $packages = [];
75
76 /**
77 * A map between ComposerName and PackageKey, only available when scanAvailablePackages is run
78 * @var array
79 */
80 protected $composerNameToPackageKeyMap = [];
81
82 /**
83 * List of active packages as package key => package object
84 * @var array
85 */
86 protected $activePackages = [];
87
88 /**
89 * @var string
90 */
91 protected $packageStatesPathAndFilename;
92
93 /**
94 * Package states configuration as stored in the PackageStates.php file
95 * @var array
96 */
97 protected $packageStatesConfiguration = [];
98
99 /**
100 * Constructor
101 */
102 public function __construct()
103 {
104 $this->packageStatesPathAndFilename = PATH_typo3conf . 'PackageStates.php';
105 }
106
107 /**
108 * @param \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend $coreCache
109 */
110 public function injectCoreCache(\TYPO3\CMS\Core\Cache\Frontend\PhpFrontend $coreCache)
111 {
112 $this->coreCache = $coreCache;
113 }
114
115 /**
116 * @param DependencyResolver
117 */
118 public function injectDependencyResolver(DependencyResolver $dependencyResolver)
119 {
120 $this->dependencyResolver = $dependencyResolver;
121 }
122
123 /**
124 * Initializes the package manager
125 *
126 * @return void
127 */
128 public function initialize()
129 {
130 try {
131 $this->loadPackageManagerStatesFromCache();
132 } catch (Exception\PackageManagerCacheUnavailableException $exception) {
133 $this->loadPackageStates();
134 $this->initializePackageObjects();
135 $this->initializeCompatibilityLoadedExtArray();
136 $this->saveToPackageCache();
137 }
138 }
139
140 /**
141 * @return string
142 */
143 protected function getCacheIdentifier()
144 {
145 if ($this->cacheIdentifier === null) {
146 $mTime = @filemtime($this->packageStatesPathAndFilename);
147 if ($mTime !== false) {
148 $this->cacheIdentifier = md5($this->packageStatesPathAndFilename . $mTime);
149 } else {
150 $this->cacheIdentifier = null;
151 }
152 }
153 return $this->cacheIdentifier;
154 }
155
156 /**
157 * @return string
158 */
159 protected function getCacheEntryIdentifier()
160 {
161 $cacheIdentifier = $this->getCacheIdentifier();
162 return $cacheIdentifier !== null ? 'PackageManager_' . $cacheIdentifier : null;
163 }
164
165 /**
166 * Saves the current state of all relevant information to the TYPO3 Core Cache
167 */
168 protected function saveToPackageCache()
169 {
170 $cacheEntryIdentifier = $this->getCacheEntryIdentifier();
171 if ($cacheEntryIdentifier !== null && !$this->coreCache->has($cacheEntryIdentifier)) {
172 // Package objects get their own cache entry, so PHP does not have to parse the serialized string
173 $packageObjectsCacheEntryIdentifier = StringUtility::getUniqueId('PackageObjects_');
174 // Build cache file
175 $packageCache = [
176 'packageStatesConfiguration' => $this->packageStatesConfiguration,
177 'packageAliasMap' => $this->packageAliasMap,
178 'loadedExtArray' => $GLOBALS['TYPO3_LOADED_EXT'],
179 'composerNameToPackageKeyMap' => $this->composerNameToPackageKeyMap,
180 'packageObjectsCacheEntryIdentifier' => $packageObjectsCacheEntryIdentifier
181 ];
182 $this->coreCache->set($packageObjectsCacheEntryIdentifier, serialize($this->packages));
183 $this->coreCache->set(
184 $cacheEntryIdentifier,
185 'return ' . PHP_EOL . var_export($packageCache, true) . ';'
186 );
187 }
188 }
189
190 /**
191 * Attempts to load the package manager states from cache
192 *
193 * @throws Exception\PackageManagerCacheUnavailableException
194 */
195 protected function loadPackageManagerStatesFromCache()
196 {
197 $cacheEntryIdentifier = $this->getCacheEntryIdentifier();
198 if ($cacheEntryIdentifier === null || !$this->coreCache->has($cacheEntryIdentifier) || !($packageCache = $this->coreCache->requireOnce($cacheEntryIdentifier))) {
199 throw new Exception\PackageManagerCacheUnavailableException('The package state cache could not be loaded.', 1393883342);
200 }
201 $this->packageStatesConfiguration = $packageCache['packageStatesConfiguration'];
202 if ($this->packageStatesConfiguration['version'] < 5) {
203 throw new Exception\PackageManagerCacheUnavailableException('The package state cache could not be loaded.', 1393883341);
204 }
205 $this->packageAliasMap = $packageCache['packageAliasMap'];
206 $this->composerNameToPackageKeyMap = $packageCache['composerNameToPackageKeyMap'];
207 $GLOBALS['TYPO3_LOADED_EXT'] = $packageCache['loadedExtArray'];
208 $GLOBALS['TYPO3_currentPackageManager'] = $this;
209 // Strip off PHP Tags from Php Cache Frontend
210 $packageObjects = substr(substr($this->coreCache->get($packageCache['packageObjectsCacheEntryIdentifier']), 6), 0, -2);
211 $this->packages = unserialize($packageObjects);
212 unset($GLOBALS['TYPO3_currentPackageManager']);
213 }
214
215 /**
216 * Loads the states of available packages from the PackageStates.php file.
217 * The result is stored in $this->packageStatesConfiguration.
218 *
219 * @throws Exception\PackageStatesUnavailableException
220 * @return void
221 */
222 protected function loadPackageStates()
223 {
224 $forcePackageStatesRewrite = false;
225 $this->packageStatesConfiguration = @include($this->packageStatesPathAndFilename) ?: [];
226 if (!isset($this->packageStatesConfiguration['version']) || $this->packageStatesConfiguration['version'] < 4) {
227 $this->packageStatesConfiguration = [];
228 } elseif ($this->packageStatesConfiguration['version'] === 4) {
229 // Convert to v5 format which only includes a list of active packages.
230 // Deprecated since version 8, will be removed in version 9.
231 $activePackages = [];
232 foreach ($this->packageStatesConfiguration['packages'] as $packageKey => $packageConfiguration) {
233 if ($packageConfiguration['state'] !== 'active') {
234 continue;
235 }
236 $activePackages[$packageKey] = ['packagePath' => $packageConfiguration['packagePath']];
237 }
238 $this->packageStatesConfiguration['packages'] = $activePackages;
239 $this->packageStatesConfiguration['version'] = 5;
240 $forcePackageStatesRewrite = true;
241 }
242 if ($this->packageStatesConfiguration !== []) {
243 $this->registerPackagesFromConfiguration($this->packageStatesConfiguration['packages'], false, $forcePackageStatesRewrite);
244 } else {
245 throw new Exception\PackageStatesUnavailableException('The PackageStates.php file is either corrupt or unavailable.', 1381507733);
246 }
247 }
248
249 /**
250 * Initializes activePackages property
251 *
252 * Saves PackageStates.php if list of required extensions has changed.
253 *
254 * @return void
255 */
256 protected function initializePackageObjects()
257 {
258 $requiredPackages = [];
259 $activePackages = [];
260 foreach ($this->packages as $packageKey => $package) {
261 if ($package->isProtected()) {
262 $requiredPackages[$packageKey] = $package;
263 }
264 if (isset($this->packageStatesConfiguration['packages'][$packageKey])) {
265 $activePackages[$packageKey] = $package;
266 }
267 }
268 $previousActivePackages = $activePackages;
269 $activePackages = array_merge($requiredPackages, $activePackages);
270
271 if ($activePackages != $previousActivePackages) {
272 foreach ($requiredPackages as $requiredPackageKey => $package) {
273 $this->registerActivePackage($package);
274 }
275 $this->sortAndSavePackageStates();
276 }
277 }
278
279 /**
280 * @param PackageInterface $package
281 */
282 protected function registerActivePackage(PackageInterface $package)
283 {
284 // reset the active packages so they are rebuilt.
285 $this->activePackages = [];
286 $this->packageStatesConfiguration['packages'][$package->getPackageKey()] = ['packagePath' => str_replace($this->packagesBasePath, '', $package->getPackagePath())];
287 }
288
289 /**
290 * Initializes a backwards compatibility $GLOBALS['TYPO3_LOADED_EXT'] array
291 *
292 * @return void
293 */
294 protected function initializeCompatibilityLoadedExtArray()
295 {
296 $loadedExtObj = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this);
297 $GLOBALS['TYPO3_LOADED_EXT'] = $loadedExtObj->toArray();
298 }
299
300 /**
301 * Scans all directories in the packages directories for available packages.
302 * For each package a Package object is created and stored in $this->packages.
303 *
304 * @return void
305 */
306 public function scanAvailablePackages()
307 {
308 $packagePaths = $this->scanPackagePathsForExtensions();
309 $packages = [];
310 foreach ($packagePaths as $packageKey => $packagePath) {
311 try {
312 $composerManifest = $this->getComposerManifest($packagePath);
313 $packageKey = $this->getPackageKeyFromManifest($composerManifest, $packagePath);
314 $this->composerNameToPackageKeyMap[strtolower($composerManifest->name)] = $packageKey;
315 $packages[$packageKey] = ['packagePath' => str_replace($this->packagesBasePath, '', $packagePath)];
316 } catch (Exception\MissingPackageManifestException $exception) {
317 if (!$this->isPackageKeyValid($packageKey)) {
318 continue;
319 }
320 } catch (Exception\InvalidPackageKeyException $exception) {
321 continue;
322 }
323 }
324
325 $registerOnlyNewPackages = !empty($this->packages);
326 $this->registerPackagesFromConfiguration($packages, $registerOnlyNewPackages);
327 }
328
329 /**
330 * Scans all directories for a certain package.
331 *
332 * @param string $packageKey
333 * @return PackageInterface
334 */
335 protected function registerPackageDuringRuntime($packageKey)
336 {
337 $packagePaths = $this->scanPackagePathsForExtensions();
338 $packagePath = $packagePaths[$packageKey];
339 $composerManifest = $this->getComposerManifest($packagePath);
340 $packageKey = $this->getPackageKeyFromManifest($composerManifest, $packagePath);
341 $this->composerNameToPackageKeyMap[strtolower($composerManifest->name)] = $packageKey;
342 $packagePath = PathUtility::sanitizeTrailingSeparator($packagePath);
343 $package = new Package($this, $packageKey, $packagePath);
344 $this->registerPackage($package);
345 return $package;
346 }
347
348 /**
349 * Fetches all directories from sysext/global/local locations and checks if the extension contains an ext_emconf.php
350 *
351 * @return array
352 */
353 protected function scanPackagePathsForExtensions()
354 {
355 $collectedExtensionPaths = [];
356 foreach ($this->getPackageBasePaths() as $packageBasePath) {
357 // Only add the extension if we have an EMCONF and the extension is not yet registered.
358 // This is crucial in order to allow overriding of system extension by local extensions
359 // and strongly depends on the order of paths defined in $this->packagesBasePaths.
360 $finder = new Finder();
361 $finder
362 ->name('ext_emconf.php')
363 ->followLinks()
364 ->depth(0)
365 ->ignoreUnreadableDirs()
366 ->in($packageBasePath);
367
368 /** @var SplFileInfo $fileInfo */
369 foreach ($finder as $fileInfo) {
370 $path = dirname($fileInfo->getPathname());
371 $extensionName = basename($path);
372 // Fix Windows backslashes
373 // we can't use GeneralUtility::fixWindowsFilePath as we have to keep double slashes for Unit Tests (vfs://)
374 $currentPath = str_replace('\\', '/', $path) . '/';
375 if (!isset($collectedExtensionPaths[$extensionName])) {
376 $collectedExtensionPaths[$extensionName] = $currentPath;
377 }
378 }
379 }
380 return $collectedExtensionPaths;
381 }
382
383 /**
384 * Requires and registers all packages which were defined in packageStatesConfiguration
385 *
386 * @param array $packages
387 * @param bool $registerOnlyNewPackages
388 * @param bool $packageStatesHasChanged
389 * @throws Exception\InvalidPackageStateException
390 * @throws Exception\PackageStatesFileNotWritableException
391 */
392 protected function registerPackagesFromConfiguration(array $packages, $registerOnlyNewPackages = false, $packageStatesHasChanged = false)
393 {
394 foreach ($packages as $packageKey => $stateConfiguration) {
395 if ($registerOnlyNewPackages && $this->isPackageAvailable($packageKey)) {
396 continue;
397 }
398
399 if (!isset($stateConfiguration['packagePath'])) {
400 $this->unregisterPackageByPackageKey($packageKey);
401 $packageStatesHasChanged = true;
402 continue;
403 }
404
405 try {
406 $packagePath = PathUtility::sanitizeTrailingSeparator($this->packagesBasePath . $stateConfiguration['packagePath']);
407 $package = new Package($this, $packageKey, $packagePath);
408 } catch (Exception\InvalidPackagePathException $exception) {
409 $this->unregisterPackageByPackageKey($packageKey);
410 $packageStatesHasChanged = true;
411 continue;
412 } catch (Exception\InvalidPackageKeyException $exception) {
413 $this->unregisterPackageByPackageKey($packageKey);
414 $packageStatesHasChanged = true;
415 continue;
416 } catch (Exception\InvalidPackageManifestException $exception) {
417 $this->unregisterPackageByPackageKey($packageKey);
418 $packageStatesHasChanged = true;
419 continue;
420 }
421
422 $this->registerPackage($package);
423 }
424 if ($packageStatesHasChanged) {
425 $this->sortAndSavePackageStates();
426 }
427 }
428
429 /**
430 * Register a native TYPO3 package
431 *
432 * @param PackageInterface $package The Package to be registered
433 * @return PackageInterface
434 * @throws Exception\InvalidPackageStateException
435 * @throws Exception\PackageStatesFileNotWritableException
436 */
437 public function registerPackage(PackageInterface $package)
438 {
439 $packageKey = $package->getPackageKey();
440 if ($this->isPackageAvailable($packageKey)) {
441 throw new Exception\InvalidPackageStateException('Package "' . $packageKey . '" is already registered.', 1338996122);
442 }
443
444 $this->packages[$packageKey] = $package;
445
446 if ($package instanceof PackageInterface) {
447 foreach ($package->getPackageReplacementKeys() as $packageToReplace => $versionConstraint) {
448 $this->packageAliasMap[strtolower($packageToReplace)] = $package->getPackageKey();
449 }
450 }
451 return $package;
452 }
453
454 /**
455 * Unregisters a package from the list of available packages
456 *
457 * @param string $packageKey Package Key of the package to be unregistered
458 * @return void
459 */
460 protected function unregisterPackageByPackageKey($packageKey)
461 {
462 try {
463 $package = $this->getPackage($packageKey);
464 if ($package instanceof PackageInterface) {
465 foreach ($package->getPackageReplacementKeys() as $packageToReplace => $versionConstraint) {
466 unset($this->packageAliasMap[strtolower($packageToReplace)]);
467 }
468 }
469 } catch (Exception\UnknownPackageException $e) {
470 }
471 unset($this->packages[$packageKey]);
472 unset($this->packageStatesConfiguration['packages'][$packageKey]);
473 }
474
475 /**
476 * Resolves a TYPO3 package key from a composer package name.
477 *
478 * @param string $composerName
479 * @return string
480 */
481 public function getPackageKeyFromComposerName($composerName)
482 {
483 if (isset($this->packageAliasMap[$composerName])) {
484 return $this->packageAliasMap[$composerName];
485 }
486 $lowercasedComposerName = strtolower($composerName);
487 if (isset($this->composerNameToPackageKeyMap[$lowercasedComposerName])) {
488 return $this->composerNameToPackageKeyMap[$lowercasedComposerName];
489 } else {
490 return $composerName;
491 }
492 }
493
494 /**
495 * Returns a PackageInterface object for the specified package.
496 * A package is available, if the package directory contains valid MetaData information.
497 *
498 * @param string $packageKey
499 * @return PackageInterface The requested package object
500 * @throws Exception\UnknownPackageException if the specified package is not known
501 * @api
502 */
503 public function getPackage($packageKey)
504 {
505 if (isset($this->packageAliasMap[$lowercasedPackageKey = strtolower($packageKey)])) {
506 $packageKey = $this->packageAliasMap[$lowercasedPackageKey];
507 }
508 if (!$this->isPackageAvailable($packageKey)) {
509 throw new Exception\UnknownPackageException('Package "' . $packageKey . '" is not available. Please check if the package exists and that the package key is correct (package keys are case sensitive).', 1166546734);
510 }
511 return $this->packages[$packageKey];
512 }
513
514 /**
515 * Returns TRUE if a package is available (the package's files exist in the packages directory)
516 * or FALSE if it's not. If a package is available it doesn't mean necessarily that it's active!
517 *
518 * @param string $packageKey The key of the package to check
519 * @return bool TRUE if the package is available, otherwise FALSE
520 * @api
521 */
522 public function isPackageAvailable($packageKey)
523 {
524 if (isset($this->packageAliasMap[$lowercasedPackageKey = strtolower($packageKey)])) {
525 $packageKey = $this->packageAliasMap[$lowercasedPackageKey];
526 }
527 return isset($this->packages[$packageKey]);
528 }
529
530 /**
531 * Returns TRUE if a package is activated or FALSE if it's not.
532 *
533 * @param string $packageKey The key of the package to check
534 * @return bool TRUE if package is active, otherwise FALSE
535 * @api
536 */
537 public function isPackageActive($packageKey)
538 {
539 return isset($this->runtimeActivatedPackages[$packageKey]) || isset($this->packageStatesConfiguration['packages'][$packageKey]);
540 }
541
542 /**
543 * Deactivates a package and updates the packagestates configuration
544 *
545 * @param string $packageKey
546 * @throws Exception\PackageStatesFileNotWritableException
547 * @throws Exception\ProtectedPackageKeyException
548 * @throws Exception\UnknownPackageException
549 */
550 public function deactivatePackage($packageKey)
551 {
552 $packagesWithDependencies = $this->sortActivePackagesByDependencies();
553
554 foreach ($packagesWithDependencies as $packageStateKey => $packageStateConfiguration) {
555 if ($packageKey === $packageStateKey || empty($packageStateConfiguration['dependencies'])) {
556 continue;
557 }
558 if (in_array($packageKey, $packageStateConfiguration['dependencies'], true)) {
559 $this->deactivatePackage($packageStateKey);
560 }
561 }
562
563 if (!$this->isPackageActive($packageKey)) {
564 return;
565 }
566
567 $package = $this->getPackage($packageKey);
568 if ($package->isProtected()) {
569 throw new Exception\ProtectedPackageKeyException('The package "' . $packageKey . '" is protected and cannot be deactivated.', 1308662891);
570 }
571
572 $this->activePackages = [];
573 unset($this->packageStatesConfiguration['packages'][$packageKey]);
574 $this->sortAndSavePackageStates();
575 }
576
577 /**
578 * @param string $packageKey
579 */
580 public function activatePackage($packageKey)
581 {
582 $package = $this->getPackage($packageKey);
583 $this->registerTransientClassLoadingInformationForPackage($package);
584
585 if ($this->isPackageActive($packageKey)) {
586 return;
587 }
588
589 $this->registerActivePackage($package);
590 $this->sortAndSavePackageStates();
591 }
592
593 /**
594 * Enables packages during runtime, but no class aliases will be available
595 *
596 * @param string $packageKey
597 * @api
598 */
599 public function activatePackageDuringRuntime($packageKey)
600 {
601 $package = $this->registerPackageDuringRuntime($packageKey);
602 $this->runtimeActivatedPackages[$package->getPackageKey()] = $package;
603 if (!isset($GLOBALS['TYPO3_LOADED_EXT'][$package->getPackageKey()])) {
604 $loadedExtArrayElement = new LoadedExtensionArrayElement($package);
605 $GLOBALS['TYPO3_LOADED_EXT'][$package->getPackageKey()] = $loadedExtArrayElement->toArray();
606 }
607 $this->registerTransientClassLoadingInformationForPackage($package);
608 }
609
610 /**
611 * @param PackageInterface $package
612 * @throws \TYPO3\CMS\Core\Exception
613 */
614 protected function registerTransientClassLoadingInformationForPackage(PackageInterface $package)
615 {
616 if (Bootstrap::usesComposerClassLoading()) {
617 return;
618 }
619 ClassLoadingInformation::registerTransientClassLoadingInformationForPackage($package);
620 }
621
622 /**
623 * Removes a package from the file system.
624 *
625 * @param string $packageKey
626 * @throws Exception
627 * @throws Exception\InvalidPackageStateException
628 * @throws Exception\ProtectedPackageKeyException
629 * @throws Exception\UnknownPackageException
630 */
631 public function deletePackage($packageKey)
632 {
633 if (!$this->isPackageAvailable($packageKey)) {
634 throw new Exception\UnknownPackageException('Package "' . $packageKey . '" is not available and cannot be removed.', 1166543253);
635 }
636
637 $package = $this->getPackage($packageKey);
638 if ($package->isProtected()) {
639 throw new Exception\ProtectedPackageKeyException('The package "' . $packageKey . '" is protected and cannot be removed.', 1220722120);
640 }
641
642 if ($this->isPackageActive($packageKey)) {
643 $this->deactivatePackage($packageKey);
644 }
645
646 $this->unregisterPackage($package);
647 $this->sortAndSavePackageStates();
648
649 $packagePath = $package->getPackagePath();
650 $deletion = GeneralUtility::rmdir($packagePath, true);
651 if ($deletion === false) {
652 throw new Exception('Please check file permissions. The directory "' . $packagePath . '" for package "' . $packageKey . '" could not be removed.', 1301491089);
653 }
654 }
655
656 /**
657 * Returns an array of \TYPO3\CMS\Core\Package objects of all active packages.
658 * A package is active, if it is available and has been activated in the package
659 * manager settings. This method returns runtime activated packages too
660 *
661 * @return PackageInterface[]
662 * @api
663 */
664 public function getActivePackages()
665 {
666 if (empty($this->activePackages)) {
667 if (!empty($this->packageStatesConfiguration['packages'])) {
668 foreach ($this->packageStatesConfiguration['packages'] as $packageKey => $packageConfig) {
669 $this->activePackages[$packageKey] = $this->getPackage($packageKey);
670 }
671 }
672 }
673 return array_merge($this->activePackages, $this->runtimeActivatedPackages);
674 }
675
676 /**
677 * Orders all active packages by comparing their dependencies. By this, the packages
678 * and package configurations arrays holds all packages in the correct
679 * initialization order.
680 *
681 * @return array
682 */
683 protected function sortActivePackagesByDependencies()
684 {
685 $packagesWithDependencies = $this->resolvePackageDependencies($this->packageStatesConfiguration['packages']);
686
687 // sort the packages by key at first, so we get a stable sorting of "equivalent" packages afterwards
688 ksort($packagesWithDependencies);
689 $sortedPackageKeys = $this->dependencyResolver->sortPackageStatesConfigurationByDependency($packagesWithDependencies);
690
691 // Reorder the packages according to the loading order
692 $this->packageStatesConfiguration['packages'] = [];
693 foreach ($sortedPackageKeys as $packageKey) {
694 $this->registerActivePackage($this->packages[$packageKey]);
695 }
696 return $packagesWithDependencies;
697 }
698
699 /**
700 * Resolves the dependent packages from the meta data of all packages recursively. The
701 * resolved direct or indirect dependencies of each package will put into the package
702 * states configuration array.
703 *
704 * @param $packageConfig
705 * @return array
706 */
707 protected function resolvePackageDependencies($packageConfig)
708 {
709 $packagesWithDependencies = [];
710 foreach ($packageConfig as $packageKey => $_) {
711 $packagesWithDependencies[$packageKey]['dependencies'] = $this->getDependencyArrayForPackage($packageKey);
712 $packagesWithDependencies[$packageKey]['suggestions'] = $this->getSuggestionArrayForPackage($packageKey);
713 }
714 return $packagesWithDependencies;
715 }
716
717 /**
718 * Returns an array of suggested package keys for the given package.
719 *
720 * @param string $packageKey The package key to fetch the suggestions for
721 * @return array|NULL An array of directly suggested packages
722 */
723 protected function getSuggestionArrayForPackage($packageKey)
724 {
725 if (!isset($this->packages[$packageKey])) {
726 return null;
727 }
728 $suggestedPackageKeys = [];
729 $suggestedPackageConstraints = $this->packages[$packageKey]->getPackageMetaData()->getConstraintsByType(MetaData::CONSTRAINT_TYPE_SUGGESTS);
730 foreach ($suggestedPackageConstraints as $constraint) {
731 if ($constraint instanceof MetaData\PackageConstraint) {
732 $suggestedPackageKey = $constraint->getValue();
733 if (isset($this->packages[$suggestedPackageKey])) {
734 $suggestedPackageKeys[] = $suggestedPackageKey;
735 }
736 }
737 }
738 return array_reverse($suggestedPackageKeys);
739 }
740
741 /**
742 * Saves the current content of $this->packageStatesConfiguration to the
743 * PackageStates.php file.
744 *
745 * @throws Exception\PackageStatesFileNotWritableException
746 */
747 protected function sortAndSavePackageStates()
748 {
749 $this->sortActivePackagesByDependencies();
750
751 $this->packageStatesConfiguration['version'] = 5;
752
753 $fileDescription = "# PackageStates.php\n\n";
754 $fileDescription .= "# This file is maintained by TYPO3's package management. Although you can edit it\n";
755 $fileDescription .= "# manually, you should rather use the extension manager for maintaining packages.\n";
756 $fileDescription .= "# This file will be regenerated automatically if it doesn't exist. Deleting this file\n";
757 $fileDescription .= "# should, however, never become necessary if you use the package commands.\n";
758
759 if (!@is_writable($this->packageStatesPathAndFilename)) {
760 // If file does not exists try to create it
761 $fileHandle = @fopen($this->packageStatesPathAndFilename, 'x');
762 if (!$fileHandle) {
763 throw new Exception\PackageStatesFileNotWritableException(
764 sprintf('We could not update the list of installed packages because the file %s is not writable. Please, check the file system permissions for this file and make sure that the web server can update it.', $this->packageStatesPathAndFilename),
765 1382449759
766 );
767 }
768 fclose($fileHandle);
769 }
770 $packageStatesCode = "<?php\n$fileDescription\nreturn " . ArrayUtility::arrayExport($this->packageStatesConfiguration) . ";\n";
771 GeneralUtility::writeFile($this->packageStatesPathAndFilename, $packageStatesCode, true);
772
773 $this->initializeCompatibilityLoadedExtArray();
774
775 GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($this->packageStatesPathAndFilename);
776 }
777
778 /**
779 * Check the conformance of the given package key
780 *
781 * @param string $packageKey The package key to validate
782 * @return bool If the package key is valid, returns TRUE otherwise FALSE
783 * @api
784 */
785 public function isPackageKeyValid($packageKey)
786 {
787 return preg_match(PackageInterface::PATTERN_MATCH_PACKAGEKEY, $packageKey) === 1 || preg_match(PackageInterface::PATTERN_MATCH_EXTENSIONKEY, $packageKey) === 1;
788 }
789
790 /**
791 * Returns an array of \TYPO3\CMS\Core\Package objects of all available packages.
792 * A package is available, if the package directory contains valid meta information.
793 *
794 * @return PackageInterface[] Array of PackageInterface
795 * @api
796 */
797 public function getAvailablePackages()
798 {
799 return $this->packages;
800 }
801
802 /**
803 * Unregisters a package from the list of available packages
804 *
805 * @param PackageInterface $package The package to be unregistered
806 * @return void
807 * @throws Exception\InvalidPackageStateException
808 */
809 public function unregisterPackage(PackageInterface $package)
810 {
811 $packageKey = $package->getPackageKey();
812 if (!$this->isPackageAvailable($packageKey)) {
813 throw new Exception\InvalidPackageStateException('Package "' . $packageKey . '" is not registered.', 1338996142);
814 }
815 $this->unregisterPackageByPackageKey($packageKey);
816 }
817
818 /**
819 * Reloads a package and its information
820 *
821 * @param string $packageKey
822 * @throws Exception\InvalidPackageStateException if the package isn't available
823 * @throws Exception\InvalidPackageKeyException if an invalid package key was passed
824 * @throws Exception\InvalidPackagePathException if an invalid package path was passed
825 * @throws Exception\InvalidPackageManifestException if no extension configuration file could be found
826 */
827 public function reloadPackageInformation($packageKey)
828 {
829 if (!$this->isPackageAvailable($packageKey)) {
830 throw new Exception\InvalidPackageStateException('Package "' . $packageKey . '" is not registered.', 1436201329);
831 }
832
833 /** @var PackageInterface $package */
834 $package = $this->packages[$packageKey];
835 $packagePath = $package->getPackagePath();
836 $newPackage = new Package($this, $packageKey, $packagePath);
837 $this->packages[$packageKey] = $newPackage;
838 unset($package);
839 }
840
841 /**
842 * Returns contents of Composer manifest as a stdObject
843 *
844 * @param string $manifestPath
845 * @return \stdClass
846 * @throws Exception\InvalidPackageManifestException
847 */
848 public function getComposerManifest($manifestPath)
849 {
850 $composerManifest = null;
851 if (file_exists($manifestPath . 'composer.json')) {
852 $json = file_get_contents($manifestPath . 'composer.json');
853 $composerManifest = json_decode($json);
854 if (!$composerManifest instanceof \stdClass) {
855 throw new Exception\InvalidPackageManifestException('The composer.json found for extension "' . basename($manifestPath) . '" is invalid!', 1439555561);
856 }
857 }
858
859 $extensionManagerConfiguration = $this->getExtensionEmConf($manifestPath);
860 $composerManifest = $this->mapExtensionManagerConfigurationToComposerManifest(
861 basename($manifestPath),
862 $extensionManagerConfiguration,
863 $composerManifest ?: new \stdClass()
864 );
865
866 return $composerManifest;
867 }
868
869 /**
870 * Fetches MetaData information from ext_emconf.php, used for
871 * resolving dependencies as well.
872 *
873 * @param string $packagePath
874 * @return array
875 * @throws Exception\InvalidPackageManifestException
876 */
877 protected function getExtensionEmConf($packagePath)
878 {
879 $packageKey = basename($packagePath);
880 $_EXTKEY = $packageKey;
881 $path = $packagePath . 'ext_emconf.php';
882 $EM_CONF = null;
883 if (@file_exists($path)) {
884 include $path;
885 if (is_array($EM_CONF[$_EXTKEY])) {
886 return $EM_CONF[$_EXTKEY];
887 }
888 }
889 throw new Exception\InvalidPackageManifestException('No valid ext_emconf.php file found for package "' . $packageKey . '".', 1360403545);
890 }
891
892 /**
893 * Fetches information from ext_emconf.php and maps it so it is treated as it would come from composer.json
894 *
895 * @param string $packageKey
896 * @param array $extensionManagerConfiguration
897 * @param \stdClass $composerManifest
898 * @return \stdClass
899 * @throws Exception\InvalidPackageManifestException
900 */
901 protected function mapExtensionManagerConfigurationToComposerManifest($packageKey, array $extensionManagerConfiguration, \stdClass $composerManifest)
902 {
903 $this->setComposerManifestValueIfEmpty($composerManifest, 'name', $packageKey);
904 $this->setComposerManifestValueIfEmpty($composerManifest, 'type', 'typo3-cms-extension');
905 $this->setComposerManifestValueIfEmpty($composerManifest, 'description', $extensionManagerConfiguration['title']);
906 $composerManifest->version = $extensionManagerConfiguration['version'];
907 if (isset($extensionManagerConfiguration['constraints']['depends']) && is_array($extensionManagerConfiguration['constraints']['depends'])) {
908 $composerManifest->require = new \stdClass();
909 foreach ($extensionManagerConfiguration['constraints']['depends'] as $requiredPackageKey => $requiredPackageVersion) {
910 if (!empty($requiredPackageKey)) {
911 if ($requiredPackageKey === 'typo3') {
912 // Add implicit dependency to 'core'
913 $composerManifest->require->core = $requiredPackageVersion;
914 } elseif ($requiredPackageKey !== 'php') {
915 // Skip php dependency
916 $composerManifest->require->{$requiredPackageKey} = $requiredPackageVersion;
917 }
918 } else {
919 throw new Exception\InvalidPackageManifestException(sprintf('The extension "%s" has invalid version constraints in depends section. Extension key is missing!', $packageKey), 1439552058);
920 }
921 }
922 }
923 if (isset($extensionManagerConfiguration['constraints']['conflicts']) && is_array($extensionManagerConfiguration['constraints']['conflicts'])) {
924 $composerManifest->conflict = new \stdClass();
925 foreach ($extensionManagerConfiguration['constraints']['conflicts'] as $conflictingPackageKey => $conflictingPackageVersion) {
926 if (!empty($conflictingPackageKey)) {
927 $composerManifest->conflict->$conflictingPackageKey = $conflictingPackageVersion;
928 } else {
929 throw new Exception\InvalidPackageManifestException(sprintf('The extension "%s" has invalid version constraints in conflicts section. Extension key is missing!', $packageKey), 1439552059);
930 }
931 }
932 }
933 if (isset($extensionManagerConfiguration['constraints']['suggests']) && is_array($extensionManagerConfiguration['constraints']['suggests'])) {
934 $composerManifest->suggest = new \stdClass();
935 foreach ($extensionManagerConfiguration['constraints']['suggests'] as $suggestedPackageKey => $suggestedPackageVersion) {
936 if (!empty($suggestedPackageKey)) {
937 $composerManifest->suggest->$suggestedPackageKey = $suggestedPackageVersion;
938 } else {
939 throw new Exception\InvalidPackageManifestException(sprintf('The extension "%s" has invalid version constraints in suggests section. Extension key is missing!', $packageKey), 1439552060);
940 }
941 }
942 }
943 if (isset($extensionManagerConfiguration['autoload'])) {
944 $composerManifest->autoload = json_decode(json_encode($extensionManagerConfiguration['autoload']));
945 }
946 // composer.json autoload-dev information must be discarded, as it may contain information only available after a composer install
947 unset($composerManifest->{'autoload-dev'});
948 if (isset($extensionManagerConfiguration['autoload-dev'])) {
949 $composerManifest->{'autoload-dev'} = json_decode(json_encode($extensionManagerConfiguration['autoload-dev']));
950 }
951
952 return $composerManifest;
953 }
954
955 /**
956 * @param \stdClass $manifest
957 * @param string $property
958 * @param mixed $value
959 * @return \stdClass
960 */
961 protected function setComposerManifestValueIfEmpty(\stdClass $manifest, $property, $value)
962 {
963 if (empty($manifest->{$property})) {
964 $manifest->{$property} = $value;
965 }
966
967 return $manifest;
968 }
969
970 /**
971 * Returns an array of dependent package keys for the given package. It will
972 * do this recursively, so dependencies of dependant packages will also be
973 * in the result.
974 *
975 * @param string $packageKey The package key to fetch the dependencies for
976 * @param array $dependentPackageKeys
977 * @param array $trace An array of already visited package keys, to detect circular dependencies
978 * @return array|NULL An array of direct or indirect dependant packages
979 * @throws Exception\InvalidPackageKeyException
980 */
981 protected function getDependencyArrayForPackage($packageKey, array &$dependentPackageKeys = [], array $trace = [])
982 {
983 if (!isset($this->packages[$packageKey])) {
984 return null;
985 }
986 if (in_array($packageKey, $trace, true) !== false) {
987 return $dependentPackageKeys;
988 }
989 $trace[] = $packageKey;
990 $dependentPackageConstraints = $this->packages[$packageKey]->getPackageMetaData()->getConstraintsByType(MetaData::CONSTRAINT_TYPE_DEPENDS);
991 foreach ($dependentPackageConstraints as $constraint) {
992 if ($constraint instanceof MetaData\PackageConstraint) {
993 $dependentPackageKey = $constraint->getValue();
994 if (in_array($dependentPackageKey, $dependentPackageKeys, true) === false && in_array($dependentPackageKey, $trace, true) === false) {
995 $dependentPackageKeys[] = $dependentPackageKey;
996 }
997 $this->getDependencyArrayForPackage($dependentPackageKey, $dependentPackageKeys, $trace);
998 }
999 }
1000 return array_reverse($dependentPackageKeys);
1001 }
1002
1003 /**
1004 * Resolves package key from Composer manifest
1005 *
1006 * If it is a TYPO3 package the name of the containing directory will be used.
1007 *
1008 * Else if the composer name of the package matches the first part of the lowercased namespace of the package, the mixed
1009 * case version of the composer name / namespace will be used, with backslashes replaced by dots.
1010 *
1011 * Else the composer name will be used with the slash replaced by a dot
1012 *
1013 * @param object $manifest
1014 * @param string $packagePath
1015 * @throws Exception\InvalidPackageManifestException
1016 * @return string
1017 */
1018 protected function getPackageKeyFromManifest($manifest, $packagePath)
1019 {
1020 if (!is_object($manifest)) {
1021 throw new Exception\InvalidPackageManifestException('Invalid composer manifest in package path: ' . $packagePath, 1348146451);
1022 }
1023 if (isset($manifest->type) && substr($manifest->type, 0, 10) === 'typo3-cms-') {
1024 $packageKey = basename($packagePath);
1025 return preg_replace('/[^A-Za-z0-9._-]/', '', $packageKey);
1026 } else {
1027 $packageKey = str_replace('/', '.', $manifest->name);
1028 return preg_replace('/[^A-Za-z0-9.]/', '', $packageKey);
1029 }
1030 }
1031
1032 /**
1033 * The order of paths is crucial for allowing overriding of system extension by local extensions.
1034 * Pay attention if you change order of the paths here.
1035 *
1036 * @return array
1037 */
1038 protected function getPackageBasePaths()
1039 {
1040 if (empty($this->packagesBasePaths)) {
1041 // Check if the directory even exists and if it is not empty
1042 if (is_dir(PATH_typo3conf . 'ext') && count(scandir(PATH_typo3conf . 'ext')) > 2) {
1043 $this->packagesBasePaths['local'] = PATH_typo3conf . 'ext/*/';
1044 }
1045 if (is_dir(PATH_typo3 . 'ext') && count(scandir(PATH_typo3 . 'ext')) > 2) {
1046 $this->packagesBasePaths['global'] = PATH_typo3 . 'ext/*/';
1047 }
1048 $this->packagesBasePaths['system'] = PATH_typo3 . 'sysext/*/';
1049 }
1050 return $this->packagesBasePaths;
1051 }
1052 }