Skip to content
Snippets Groups Projects
Commit ef6ef20e authored by jurgenhaas's avatar jurgenhaas
Browse files

#2 Locally cache default config files

parent 5cc0a5e8
No related branches found
No related tags found
1 merge request!8Merging develop into main
Pipeline #1383877 passed
<?php
namespace LakeDrops\DrupalConfig;
use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;
/**
* Composer Command Provider for LakeDrops drupal configurations.
*
* @package LakeDrops\DrupalConfig
*/
class CommandProvider implements CommandProviderCapability {
/**
* Retrieves an array of commands.
*
* @return \Composer\Command\BaseCommand[]
* Array of available commands.
*/
public function getCommands(): array {
return [
new FlushCacheCommand(),
];
}
}
<?php
namespace LakeDrops\DrupalConfig;
use Composer\Command\BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Flush Cache Command for LakeDrops Drupal configurations.
*
* @package LakeDrops\DrupalConfig
*/
class FlushCacheCommand extends BaseCommand {
/**
* {@inheritdoc}
*/
protected function configure(): void {
parent::configure();
$this
->setName('lakedrops:configuration:flush_cache')
->setDescription('Flush configuration cache.');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$count = 0;
foreach (glob(Plugin::getCacheDir($this->requireComposer()) . '*') as $file) {
if (is_file($file)) {
unlink($file);
$count++;
}
}
$output->writeln('Deleted ' . $count . ' file(s).');
return 0;
}
}
...@@ -19,6 +19,23 @@ class Plugin implements PluginInterface { ...@@ -19,6 +19,23 @@ class Plugin implements PluginInterface {
*/ */
private Installer $installer; private Installer $installer;
/**
* Determines and prepares the cache directory.
*
* @param \Composer\Composer $composer
* The composer instance.
*
* @return string
* The cache directory.
*/
public static function getCacheDir(Composer $composer): string {
$cacheDir = $composer->getConfig()->get('cache-dir') . '/lakedrops-drupal-config/';
if (!file_exists($cacheDir)) {
mkdir($cacheDir);
}
return $cacheDir;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -29,12 +46,12 @@ class Plugin implements PluginInterface { ...@@ -29,12 +46,12 @@ class Plugin implements PluginInterface {
if ($extra['lakedrops-default'] ?? FALSE) { if ($extra['lakedrops-default'] ?? FALSE) {
$default = [ $default = [
'lakedrops-config-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/config.json', 'lakedrops-config-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/config.json',
'lakedrops-curated-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/curated.json', 'lakedrops-curated-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/curated.json',
'lakedrops-extra-drupal-scaffold-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/drupal-scaffold.json', 'lakedrops-extra-drupal-scaffold-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/drupal-scaffold.json',
'lakedrops-extra-drupal-libraries-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/drupal-libraries.json', 'lakedrops-extra-drupal-libraries-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/drupal-libraries.json',
'lakedrops-extra-installer-types-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/installer-types.json', 'lakedrops-extra-installer-types-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/installer-types.json',
'lakedrops-extra-installer-paths-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/installer-paths.json', 'lakedrops-extra-installer-paths-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/installer-paths.json',
]; ];
foreach ($default as $key => $value) { foreach ($default as $key => $value) {
if (!isset($extra[$key])) { if (!isset($extra[$key])) {
...@@ -44,7 +61,7 @@ class Plugin implements PluginInterface { ...@@ -44,7 +61,7 @@ class Plugin implements PluginInterface {
} }
if (isset($extra['lakedrops-config-file']) || isset($extra['lakedrops-curated-file'])) { if (isset($extra['lakedrops-config-file']) || isset($extra['lakedrops-curated-file'])) {
if (isset($extra['lakedrops-config-file'])) { if (isset($extra['lakedrops-config-file'])) {
foreach ($this->readJson('config', $extra['lakedrops-config-file'], $io) as $key => $value) { foreach ($this->readJson('config', $extra['lakedrops-config-file'], $io, $composer) as $key => $value) {
if ($key === 'allow-plugins') { if ($key === 'allow-plugins') {
foreach ($extra['lakedrops-allow-plugins'] ?? [] as $pluginKey => $pluginValue) { foreach ($extra['lakedrops-allow-plugins'] ?? [] as $pluginKey => $pluginValue) {
$value[$pluginKey] = $pluginValue; $value[$pluginKey] = $pluginValue;
...@@ -57,7 +74,7 @@ class Plugin implements PluginInterface { ...@@ -57,7 +74,7 @@ class Plugin implements PluginInterface {
} }
if (isset($extra['lakedrops-curated-file'])) { if (isset($extra['lakedrops-curated-file'])) {
$audit = $composer->getConfig()->get('audit'); $audit = $composer->getConfig()->get('audit');
$audit['ignore'] = $this->readJson('audit ignore', $extra['lakedrops-curated-file'], $io); $audit['ignore'] = $this->readJson('audit ignore', $extra['lakedrops-curated-file'], $io, $composer);
$composer->getConfig() $composer->getConfig()
->getConfigSource() ->getConfigSource()
->addConfigSetting('audit', $audit); ->addConfigSetting('audit', $audit);
...@@ -83,7 +100,7 @@ class Plugin implements PluginInterface { ...@@ -83,7 +100,7 @@ class Plugin implements PluginInterface {
$extra[$item][$path] = ['type:drupal-' . $type]; $extra[$item][$path] = ['type:drupal-' . $type];
} }
} }
$extra[$item] = array_merge($extra[$item], $this->readJson($item, $extra[$key], $io)); $extra[$item] = array_merge($extra[$item], $this->readJson($item, $extra[$key], $io, $composer));
$changed = TRUE; $changed = TRUE;
} }
} }
...@@ -114,16 +131,26 @@ class Plugin implements PluginInterface { ...@@ -114,16 +131,26 @@ class Plugin implements PluginInterface {
* The filename from where to read the data. * The filename from where to read the data.
* @param \Composer\IO\IOInterface $io * @param \Composer\IO\IOInterface $io
* The io service for messages. * The io service for messages.
* @param \Composer\Composer $composer
* The composer instance.
* *
* @return array * @return array
* The read data. * The read data.
* *
* @throws \Exception * @throws \Exception
*/ */
protected function readJson(string $type, string $filename, IOInterface $io): array { protected function readJson(string $type, string $filename, IOInterface $io, Composer $composer): array {
$io->write('<info>Gathering ' . $type . ' from ' . $filename . '.</info>');
try { try {
$data = json_decode(file_get_contents($filename), TRUE, 512, JSON_THROW_ON_ERROR); $cache_file = self::getCacheDir($composer) . md5($filename);
if (!file_exists($cache_file) || (filemtime($cache_file) + 86400 < time())) {
$io->write('<info>Gathering fresh ' . $type . ' from ' . $filename . '.</info>');
$content = file_get_contents($filename);
file_put_contents($cache_file, $content);
}
else {
$content = file_get_contents($cache_file);
}
$data = json_decode($content, TRUE, 512, JSON_THROW_ON_ERROR);
$error = json_last_error(); $error = json_last_error();
} }
catch (\JsonException) { catch (\JsonException) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment