View source
<?php
namespace Drupal\patchinfo_source_composer\Plugin\patchinfo\source;
use Drupal\Core\Extension\Extension;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\patchinfo\PatchInfoSourceBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ComposerJsonSource extends PatchInfoSourceBase {
protected $fileSystem;
public function __construct(array $configuration, string $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger_factory, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger_factory, $file_system);
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.factory'), $container
->get('file_system'));
}
public function getPatches(array $info, Extension $file, string $type) {
$return = [];
if (!in_array($type, [
'module',
'theme',
])) {
return $return;
}
$patches = $this
->parseComposerFile($file
->getPath());
$return = array_merge_recursive($return, $patches);
if ($file
->getName() == 'system') {
$core_base_path = str_replace('/modules/system', '', $file
->getPath());
$patches = $this
->parseComposerFile($core_base_path);
$return = array_merge_recursive($return, $patches);
$patches = $this
->parseComposerFile('.');
$return = array_merge_recursive($return, $patches);
$patches = $this
->parseComposerFile('..');
$return = array_merge_recursive($return, $patches);
}
return $return;
}
protected function parseComposerFile(string $path) {
$return = [];
if (stream_resolve_include_path($path . DIRECTORY_SEPARATOR . 'composer.json') === FALSE) {
return $return;
}
$path = $this->fileSystem
->realpath($path);
$config = $this
->getDecodedJson($path, 'composer.json');
$patches = [];
if (!empty($config['extra']['patches'])) {
$patches = $config['extra']['patches'];
}
elseif (!empty($config['extra']['patches-file'])) {
$patchfile = $this
->getDecodedJson($path, $config['extra']['patches-file']);
if (!empty($patchfile['patches'])) {
$patches = $patchfile['patches'];
}
}
foreach ($patches as $project => $project_patches) {
if (strpos($project, 'drupal/') !== 0) {
continue;
}
$project = str_replace('drupal/', '', $project);
if ($project == 'core' || $project == 'drupal') {
$project = 'system';
}
foreach ($project_patches as $description => $url) {
$info = $url . ' ' . $description;
$return[$project][] = [
'info' => trim($info),
'source' => $path . '/composer.json',
];
}
}
return $return;
}
protected function getDecodedJson(string $path, string $file) {
$return = [];
if (!file_exists($path . '/' . $file)) {
return $return;
}
if (!is_readable($path . '/' . $file)) {
$this->loggerFactory
->get('patchinfo_source_composer')
->warning($this
->t('Can not read @path/@file. Check your file permissions.', [
'@path' => $path,
'@file' => $file,
]));
return $return;
}
$content = file_get_contents($path . '/' . $file);
if ($content === FALSE) {
$this->loggerFactory
->get('patchinfo_source_composer')
->warning($this
->t('Can not get contents from @path/@file.', [
'@path' => $path,
'@file' => $file,
]));
return $return;
}
$config = json_decode($content, TRUE);
if ($config === NULL) {
$this->loggerFactory
->get('patchinfo_source_composer')
->warning($this
->t('Unable to parse @path/@file. Check your JSON syntax.', [
'@path' => $path,
'@file' => $file,
]));
return $return;
}
return $config;
}
}