[BUGFIX] Remove symlink to typo3 folder in web dir
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Composer / InstallerScripts / WebDirectory.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Composer\InstallerScripts;
4
5 /*
6 * This file is part of the TYPO3 project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Composer\Composer;
19 use Composer\IO\IOInterface;
20 use Composer\Script\Event;
21 use Composer\Semver\Constraint\EmptyConstraint;
22 use TYPO3\CMS\Composer\Plugin\Config;
23 use TYPO3\CMS\Composer\Plugin\Core\InstallerScript;
24 use TYPO3\CMS\Composer\Plugin\Util\Filesystem;
25
26 /**
27 * Setting up TYPO3 web directory
28 */
29 class WebDirectory implements InstallerScript
30 {
31 /**
32 * @var string
33 */
34 private static $typo3Dir = '/typo3';
35
36 /**
37 * @var string
38 */
39 private static $systemExtensionsDir = '/sysext';
40
41 /**
42 * @var IOInterface
43 */
44 private $io;
45
46 /**
47 * @var Composer
48 */
49 private $composer;
50
51 /**
52 * @var Filesystem
53 */
54 private $filesystem;
55
56 /**
57 * @var Config
58 */
59 private $pluginConfig;
60
61 /**
62 * Prepare the web directory with symlinks
63 *
64 * @param Event $event
65 * @return bool
66 */
67 public function run(Event $event): bool
68 {
69 $this->io = $event->getIO();
70 $this->composer = $event->getComposer();
71 $this->filesystem = new Filesystem();
72 $this->pluginConfig = Config::load($this->composer);
73
74 $symlinks = $this->initializeSymlinks();
75 if ($this->filesystem->someFilesExist($symlinks)) {
76 $this->filesystem->removeSymlinks($symlinks);
77 }
78 $this->filesystem->establishSymlinks($symlinks);
79
80 return true;
81 }
82
83 /**
84 * Initialize symlinks from configuration
85 * @return array
86 */
87 private function initializeSymlinks(): array
88 {
89 $webDir = $this->filesystem->normalizePath($this->pluginConfig->get('web-dir'));
90 $backendDir = $webDir . self::$typo3Dir;
91 // Ensure we delete a previously existing symlink to typo3 folder in web directory
92 if ($this->filesystem->isSymlinkedDirectory($backendDir)) {
93 $this->filesystem->removeDirectory($backendDir);
94 }
95 $this->filesystem->ensureDirectoryExists($backendDir);
96 $localRepository = $this->composer->getRepositoryManager()->getLocalRepository();
97 $package = $localRepository->findPackage('typo3/cms', new EmptyConstraint());
98 $sourcesDir = $this->composer->getInstallationManager()->getInstallPath($package);
99 return [
100 $sourcesDir . self::$typo3Dir . self::$systemExtensionsDir => $backendDir . self::$systemExtensionsDir,
101 ];
102 }
103 }