Skip to content

Commit

Permalink
Dynamically fetch composer.json for plugin info
Browse files Browse the repository at this point in the history
For later use for Kirby version compatibility or other metadata
  • Loading branch information
lukasbestle committed Nov 3, 2023
1 parent 5134342 commit ab12ede
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions site/models/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class PluginPage extends Page
{
protected $info = null;
protected $latestTag = null;

public function cache(): Cache
Expand Down Expand Up @@ -84,11 +85,56 @@ public function icon(): string
return option('plugins.categories.' . $this->category() . '.icon');
}

public function info()
public function info(bool $onlyIfCached = false)
{
$info = Data::read($this->file('composer.json')->root());
if ($this->info !== null) {
return $this->info;
}

$cacheId = $this->cacheId('info');
$info = $this->cache()->get($cacheId) ?? false;

if ($info === false) {
if ($onlyIfCached === true) {
return null;
}

$repo = $this->repository();

if (Str::contains($repo, 'github') === true) {
$url = $repo->value() . '/raw/HEAD/composer.json';
} else {
$url = $this->composerUrl()->value();
}

if ($url) {
try {
$info = Data::read($url);
} catch (\Exception) {
// $info is still false
}
}

if ($info) {
// caches for 3 hours if we got a file
$this->cache()->set($cacheId, $info, 180);

// remove plugins representation cache
$this->kirby()->cache('pages')->remove('plugins.json');
} else {
// keeps the cache of an unsuccessful response longer (one day) for performance
$this->cache()->set($cacheId, $info, 1440);
}
}

// normalize the return value
// (`false` is only needed to differentiate
// between non-existing cache data and errors)
if ($info === false) {
return null;
}

return Nest::create($info, $this);
return $this->info = Nest::create($info, $this);
}

public function isNew(): bool
Expand Down

0 comments on commit ab12ede

Please sign in to comment.