TYPO3  7.6
AutoloadConnector.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Composer\Plugin\Core;
3 
4 /*
5  * This file is part of the TYPO3 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  */
17 
24 
28  protected $filesystem;
29 
33  public function __construct($filesystem = NULL) {
34  $this->filesystem = $this->filesystem ?: new \TYPO3\CMS\Composer\Plugin\Util\Filesystem();
35  }
36 
40  public function linkAutoloader(\Composer\Script\Event $event) {
41  $composer = $event->getComposer();
42  $composerConfig = $composer->getConfig();
43  $localRepository = $composer->getRepositoryManager()->getLocalRepository();
44 
45  foreach ($localRepository->getCanonicalPackages() as $package) {
46  if ($package->getType() === 'typo3-cms-core') {
47  $defaultVendorDir = \Composer\Config::$defaultConfig['vendor-dir'];
48 
49  $packagePath = $composer->getInstallationManager()->getInstallPath($package);
50  $jsonFile = new \Composer\Json\JsonFile($packagePath . DIRECTORY_SEPARATOR . 'composer.json', new \Composer\Util\RemoteFilesystem($event->getIO()));
51  $packageJson = $jsonFile->read();
52  $packageVendorDir = !empty($packageJson['config']['vendor-dir']) ? $this->filesystem->normalizePath($packageJson['config']['vendor-dir']) : $defaultVendorDir;
53 
54  $autoloaderSourceDir = $composerConfig->get('vendor-dir');
55  $autoloaderTargetDir = $packagePath . DIRECTORY_SEPARATOR . $packageVendorDir;
56  $autoloaderFileName = 'autoload.php';
57 
58  $this->filesystem->ensureDirectoryExists($autoloaderTargetDir);
59  $this->filesystem->remove($autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName);
60  try {
61  $this->filesystem->symlink(
62  $autoloaderSourceDir . DIRECTORY_SEPARATOR . $autoloaderFileName,
63  $autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName,
64  FALSE
65  );
66  } catch (\RuntimeException $e) {
67  if ($e->getCode() !== 1430494084) {
68  throw $e;
69  }
70  $code = array(
71  '<?php',
72  'return require ' . $this->filesystem->findShortestPathCode(
73  $autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName,
74  $autoloaderSourceDir . DIRECTORY_SEPARATOR . $autoloaderFileName
75  ) . ';'
76  );
77  file_put_contents($autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName, implode(chr(10), $code));
78  }
79  $this->insertComposerModeConstant($event);
80  break;
81  }
82  }
83  }
84 
88  protected function insertComposerModeConstant(\Composer\Script\Event $event) {
89  $composer = $event->getComposer();
90  $pluginConfig = Config::load($composer);
91  if (!$pluginConfig->get('composer-mode')) {
92  return;
93  }
94  $composerConfig = $composer->getConfig();
95  $vendorDir = $composerConfig->get('vendor-dir');
96  $autoloadFile = $vendorDir . '/autoload.php';
97  $io = $event->getIO();
98  if (!file_exists($autoloadFile)) {
99  throw new \RuntimeException(sprintf(
100  'Could not adjust autoloader: The file %s was not found.',
101  $autoloadFile
102  ));
103  }
104 
105  $io->write('<info>Inserting TYPO3_COMPOSER_MODE constant</info>');
106 
107  $contents = file_get_contents($autoloadFile);
108  $constant = "if (!defined('TYPO3_COMPOSER_MODE')) {\n";
109  $constant .= " define('TYPO3_COMPOSER_MODE', TRUE);\n";
110  $constant .= "}\n\n";
111 
112  // Regex modifiers:
113  // "m": \s matches newlines
114  // "D": $ matches at EOF only
115  // Translation: insert before the last "return" in the file
116  $contents = preg_replace('/\n(?=return [^;]+;\s*$)/mD', "\n" . $constant, $contents);
117  file_put_contents($autoloadFile, $contents);
118  }
119 }