Skip to content
Snippets Groups Projects
Commit d852b05a authored by GitLab CI's avatar GitLab CI
Browse files

Merge branch 'develop' into 'main'

Merging develop into main

See merge request !9
parents c388c49b b35d00eb
No related branches found
No related tags found
1 merge request!9Merging develop into main
Pipeline #1384006 passed
......@@ -28,7 +28,11 @@ class FlushCacheCommand extends BaseCommand {
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$count = 0;
foreach (glob(Plugin::getCacheDir($this->requireComposer()) . '*') as $file) {
$cache_dir = Plugin::getCacheDir($this->requireComposer());
if ($cache_dir === NULL) {
return 1;
}
foreach (glob($cache_dir . '*') as $file) {
if (is_file($file)) {
unlink($file);
$count++;
......
......@@ -5,12 +5,14 @@ namespace LakeDrops\DrupalConfig;
use Composer\Composer;
use Composer\Installers\Installer;
use Composer\IO\IOInterface;
use Composer\Plugin\Capability\CommandProvider as ComposerCommandProvider;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginInterface;
/**
* Composer plugin for handling drupal scaffold.
*/
class Plugin implements PluginInterface {
class Plugin implements PluginInterface, Capable {
/**
* The installer.
......@@ -19,17 +21,30 @@ class Plugin implements PluginInterface {
*/
private Installer $installer;
/**
* {@inheritdoc}
*/
public function getCapabilities(): array {
return [
ComposerCommandProvider::class => CommandProvider::class,
];
}
/**
* Determines and prepares the cache directory.
*
* @param \Composer\Composer $composer
* The composer instance.
*
* @return string
* @return string|null
* The cache directory.
*/
public static function getCacheDir(Composer $composer): string {
$cacheDir = $composer->getConfig()->get('cache-dir') . '/lakedrops-drupal-config/';
public static function getCacheDir(Composer $composer): ?string {
$composerCacheDir = $composer->getConfig()->get('cache-dir');
if ($composerCacheDir === '/dev/null') {
return NULL;
}
$cacheDir = $composerCacheDir . '/lakedrops-drupal-config/';
if (!file_exists($cacheDir)) {
mkdir($cacheDir);
}
......@@ -141,14 +156,20 @@ class Plugin implements PluginInterface {
*/
protected function readJson(string $type, string $filename, IOInterface $io, Composer $composer): array {
try {
$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>');
$cache_dir = self::getCacheDir($composer);
if ($cache_dir === NULL) {
$content = file_get_contents($filename);
file_put_contents($cache_file, $content);
}
else {
$content = file_get_contents($cache_file);
$cache_file = $cache_dir . 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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment