diff --git a/src/CommandProvider.php b/src/CommandProvider.php new file mode 100644 index 0000000000000000000000000000000000000000..ec02f5fe84b0b0137b8c54830d87032b588e72de --- /dev/null +++ b/src/CommandProvider.php @@ -0,0 +1,26 @@ +<?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(), + ]; + } + +} diff --git a/src/FlushCacheCommand.php b/src/FlushCacheCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..516ef47105a771c2527332a6874422bcf3723ab9 --- /dev/null +++ b/src/FlushCacheCommand.php @@ -0,0 +1,41 @@ +<?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; + } + +} diff --git a/src/Plugin.php b/src/Plugin.php index 870c9c75a657e45a42e0f2b2dd4f5ce39669d22a..2df38fd440fc2a1c9ac8d5c8c6a73989b64390ae 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -19,6 +19,23 @@ class Plugin implements PluginInterface { */ 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} */ @@ -29,12 +46,12 @@ class Plugin implements PluginInterface { if ($extra['lakedrops-default'] ?? FALSE) { $default = [ - 'lakedrops-config-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/config.json', - 'lakedrops-curated-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/curated.json', - 'lakedrops-extra-drupal-scaffold-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/drupal-scaffold.json', - 'lakedrops-extra-drupal-libraries-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/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-paths-file' => 'https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/defaults/installer-paths.json', + 'lakedrops-config-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/config.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/drupal-default-config/-/raw/main/drupal-scaffold.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/drupal-default-config/-/raw/main/installer-types.json', + 'lakedrops-extra-installer-paths-file' => 'https://gitlab.lakedrops.com/composer/drupal-default-config/-/raw/main/installer-paths.json', ]; foreach ($default as $key => $value) { if (!isset($extra[$key])) { @@ -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'])) { - 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') { foreach ($extra['lakedrops-allow-plugins'] ?? [] as $pluginKey => $pluginValue) { $value[$pluginKey] = $pluginValue; @@ -57,7 +74,7 @@ class Plugin implements PluginInterface { } if (isset($extra['lakedrops-curated-file'])) { $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() ->getConfigSource() ->addConfigSetting('audit', $audit); @@ -83,7 +100,7 @@ class Plugin implements PluginInterface { $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; } } @@ -114,16 +131,26 @@ class Plugin implements PluginInterface { * The filename from where to read the data. * @param \Composer\IO\IOInterface $io * The io service for messages. + * @param \Composer\Composer $composer + * The composer instance. * * @return array * The read data. * * @throws \Exception */ - protected function readJson(string $type, string $filename, IOInterface $io): array { - $io->write('<info>Gathering ' . $type . ' from ' . $filename . '.</info>'); + protected function readJson(string $type, string $filename, IOInterface $io, Composer $composer): array { 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(); } catch (\JsonException) {