AssetPackagist.php in Open Social 8.3
File
src/Composer/AssetPackagist.php
View source
<?php
namespace Social\Composer;
use Composer\Json\JsonFile;
use Composer\Script\Event;
final class AssetPackagist {
protected static function getRootPackage() {
$file = new JsonFile('composer.json');
$dir = explode(DIRECTORY_SEPARATOR, trim(getcwd(), DIRECTORY_SEPARATOR));
do {
if ($file
->exists()) {
$info = $file
->read();
if (isset($info['require']['goalgorilla/open_social'])) {
return $file;
}
}
chdir('..');
array_pop($dir);
} while ($dir);
throw new \RuntimeException('Could not locate the root package.');
}
public static function execute(Event $event) {
$io = $event
->getIO();
$io
->write('Searching for root package...');
$file = static::getRootPackage();
$package = $file
->read();
if (isset($package['repositories'])) {
$repository_key = NULL;
foreach ($package['repositories'] as $key => $repository) {
if ($repository['type'] == 'composer' && strpos($repository['url'], 'https://asset-packagist.org') === 0) {
$repository_key = $key;
break;
}
}
if (is_null($repository_key)) {
$package['repositories'][] = [
'type' => 'composer',
'url' => 'https://asset-packagist.org',
];
}
}
unset($package['require']['oomphinc/composer-installers-extender']);
if (!isset($package['extra']['installer-types']) || !in_array('bower-asset', $package['extra']['installer-types'])) {
$package['extra']['installer-types'][] = 'bower-asset';
}
if (!in_array('npm-asset', $package['extra']['installer-types'])) {
$package['extra']['installer-types'][] = 'npm-asset';
}
$root_path = 'html/';
foreach ($package['extra']['installer-paths'] as $path => $install_type) {
if (in_array('drupal/core', $install_type, TRUE)) {
if ($path == 'core') {
$root_path = '';
}
else {
$parts = explode('/core', $path);
if (!empty($parts)) {
$root_path = $parts[0] . '/';
}
}
break;
}
}
$package['extra']['installer-paths'][$root_path . 'libraries/{$name}'][] = 'type:drupal-library';
$package['extra']['installer-paths'][$root_path . 'libraries/{$name}'][] = 'type:bower-asset';
$package['extra']['installer-paths'][$root_path . 'libraries/{$name}'][] = 'type:npm-asset';
$file
->write($package);
$io
->write('Successfully updated your root composer.json file.');
}
}