/** * @param \Composer\Script\Event $event */ public function linkAutoloader(\Composer\Script\Event $event) { $composer = $event->getComposer(); $composerConfig = $composer->getConfig(); $localRepository = $composer->getRepositoryManager()->getLocalRepository(); foreach ($localRepository->getCanonicalPackages() as $package) { if ($package->getType() === 'typo3-cms-core') { $defaultVendorDir = \Composer\Config::$defaultConfig['vendor-dir']; $packagePath = $composer->getInstallationManager()->getInstallPath($package); $jsonFile = new \Composer\Json\JsonFile($packagePath . DIRECTORY_SEPARATOR . 'composer.json', new \Composer\Util\RemoteFilesystem($event->getIO())); $packageJson = $jsonFile->read(); $packageVendorDir = !empty($packageJson['config']['vendor-dir']) ? $this->filesystem->normalizePath($packageJson['config']['vendor-dir']) : $defaultVendorDir; $autoloaderSourceDir = $composerConfig->get('vendor-dir'); $autoloaderTargetDir = $packagePath . DIRECTORY_SEPARATOR . $packageVendorDir; $autoloaderFileName = 'autoload.php'; $this->filesystem->ensureDirectoryExists($autoloaderTargetDir); $this->filesystem->remove($autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName); try { $this->filesystem->symlink($autoloaderSourceDir . DIRECTORY_SEPARATOR . $autoloaderFileName, $autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName, FALSE, TRUE); } catch (\RuntimeException $e) { if ($e->getCode() !== 1430494084) { throw $e; } $code = array('<?php', 'return require ' . $this->filesystem->findShortestPathCode($autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName, $autoloaderSourceDir . DIRECTORY_SEPARATOR . $autoloaderFileName) . ';'); file_put_contents($autoloaderTargetDir . DIRECTORY_SEPARATOR . $autoloaderFileName, implode(chr(10), $code)); } $this->insertComposerModeConstant($event); break; } } }
/** * Download file and decode the JSON string to PHP object * * @param string $json * @return stdClass */ public function getJson($url) { $key = 'json://' . $url; if (array_key_exists($key, $this->cache)) { return $this->cache[$key]; } $json = new \Composer\Json\JsonFile($url, $this->remoteFileSystem); return $this->cache[$key] = $json->read(); }
public function testGetModules() { $filesystemTmp = CM_Service_Manager::getInstance()->getFilesystems()->getTmp(); $dirRoot = $filesystemTmp->getAdapter()->getPathPrefix() . '/foo-app/'; $composerFile = new \Composer\Json\JsonFile($dirRoot . 'composer.json'); $composerFile->write(array('name' => 'foo/bar', 'require' => array('cargomedia/cm' => '*'), 'extra' => ['cm-modules' => ['Package' => ['path' => 'package/']]])); $filesystemTmp->ensureDirectory('foo-app/vendor/composer/'); $installedFile = new \Composer\Json\JsonFile($dirRoot . 'vendor/composer/installed.json'); $installedFile->write([['name' => 'cargomedia/cm', 'version' => '1.3.10', 'type' => 'library', 'extra' => ['cm-modules' => ['CM' => ['path' => '']]], 'autoload' => ['psr-0' => ['CM_' => 'library/']]]]); $installation = new CM_App_Installation($dirRoot); $expectedModules = [new CM_App_Module('CM', 'vendor/cargomedia/cm/'), new CM_App_Module('Package', 'package/')]; $this->assertEquals($expectedModules, $installation->getModules()); }
/** * @param string $rootDir * @return \Composer\Composer */ public function createComposerFromRootDir($rootDir) { $composerPath = $rootDir . 'composer.json'; $composerFile = new Composer\Json\JsonFile($composerPath); $composerFile->validateSchema(Composer\Json\JsonFile::LAX_SCHEMA); $localConfig = $composerFile->read(); $composerFactory = new CM_App_ComposerFactory(); $composer = $composerFactory->createComposer($localConfig); $vendorDir = $rootDir . $composer->getConfig()->get('vendor-dir'); $vendorConfig = new Composer\Json\JsonFile($vendorDir . '/composer/installed.json'); $vendorRepository = new Composer\Repository\InstalledFilesystemRepository($vendorConfig); $composer->getRepositoryManager()->setLocalRepository($vendorRepository); return $composer; }
/** * @param \Composer\IO\IOInterface $io * @param \Composer\Config $composerConfig * @return Config * @throws \RuntimeException * @throws \InvalidArgumentException */ public static function load(\Composer\IO\IOInterface $io, \Composer\Config $composerConfig) { static $config; if ($config === null) { $baseDir = realpath(substr($composerConfig->get('vendor-dir'), 0, -strlen($composerConfig->get('vendor-dir', Config::RELATIVE_PATHS)))); $localConfig = \Composer\Factory::getComposerFile(); $file = new \Composer\Json\JsonFile($localConfig, new \Composer\Util\RemoteFilesystem($io)); $config = new static($baseDir); $config->merge($file->read()); } return $config; }
#!/usr/bin/env php <?php require_once __DIR__ . '/vendor/autoload.php'; $args = array_reverse($argv); $version = array_shift($args); $package_name = array_shift($args); print 'Update: ' . $package_name . ' - ' . $version . PHP_EOL; if (empty($package_name) || empty($version)) { return; } $filepath = $_SERVER['PWD'] . '/composer.lock'; $composer_lock = json_decode(file_get_contents($filepath), TRUE); $write = FALSE; if (isset($composer_lock['packages'])) { foreach ($composer_lock['packages'] as &$package) { if ($package['name'] == $package_name) { $git_url = $package['source']['url']; $output = shell_exec('git ls-remote ' . $git_url . ' ' . $version); $hash = substr($output, 0, 40); print $hash . PHP_EOL; $package['source']['reference'] = $hash; $package['time'] = date('Y-m-d H:i:s'); $write = TRUE; } } } if ($write) { file_put_contents($filepath, json_encode($composer_lock, 448 & 128)); $file = new \Composer\Json\JsonFile($filepath); $file->write($composer_lock); }
/** * @param array $hash */ private function _writeToComposerFile(array $hash) { $composerFile = new Composer\Json\JsonFile($this->_installation->getDirRoot() . 'composer.json'); $configCurrent = $composerFile->read(); $this->_filesystemHelper->notify('modify', 'composer.json'); $configMerged = $this->_mergeConfigs($configCurrent, $hash); $composerFile->write($configMerged); $this->_installation->reload(); }
/** * @param string $cacheDir * @return array * @throws \Exception */ protected function getInstalledPackages($cacheDir) { $file = new \Composer\Json\JsonFile($cacheDir . '/composer/installed.json'); if (!$file->exists()) { throw new \Exception('no packages installed in repository'); } $packages = $file->read(); usort($packages, function ($a, $b) { return strnatcasecmp($a['name'], $b['name']); }); return $packages; }
/** * @throws CM_Exception_Invalid * @throws Exception * @throws \Mocka\Exception * @return CM_Tools_AppInstallation|\Mocka\AbstractClassTrait */ private function _mockAppInstallation() { $appName = uniqid('foo-app-'); $filesystemTmp = CM_Service_Manager::getInstance()->getFilesystems()->getTmp(); $dirRoot = $filesystemTmp->getAdapter()->getPathPrefix() . '/' . $appName . '/'; $frameworkLocal = DIR_ROOT . CM_Bootloader::getInstance()->getModulePath('CM'); $frameworkTest = $dirRoot . 'vendor/cargomedia/cm'; $filesystemTmp->ensureDirectory("{$appName}/vendor/cargomedia"); if (false === symlink($frameworkLocal, $frameworkTest)) { throw new CM_Exception_Invalid('Symlink failed to be created'); } $composerFile = new \Composer\Json\JsonFile($dirRoot . 'composer.json'); $composerFile->write(array('name' => 'foo/bar', 'require' => array('cargomedia/cm' => '*'))); $filesystemTmp->ensureDirectory("{$appName}/vendor/composer/"); $installedFile = new \Composer\Json\JsonFile($dirRoot . 'vendor/composer/installed.json'); $installedFile->write([['name' => 'cargomedia/cm', 'version' => '1.3.10', 'type' => 'library', 'extra' => ['cm-modules' => ['CM' => ['path' => '']]], 'autoload' => ['psr-0' => ['CM_' => 'library/']]]]); return $this->mockObject('CM_Tools_AppInstallation', [$dirRoot]); }