<?php
namespace Expand\Addon\Activator;
use Expand\Addon\Addon;
use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Nwidart\Modules\Activators\FileActivator;
class AddonFileActivator extends FileActivator
{
protected $statusesFile;
protected $config;
protected $cache;
protected $files;
protected $cacheKey;
protected $cacheLifetime;
protected $modulesStatuses;
public function __construct(Container $app)
{
$this->cache = $app['cache'];
$this->files = $app['files'];
$this->config = $app['config'];
$this->statusesFile = $this->config('statuses-file');
$this->cacheKey = $this->config('cache-key');
$this->cacheLifetime = $this->config('cache-lifetime');
$this->modulesStatuses = $this->getModulesStatuses();
}
public function getStatusesFilePath(): string
{
return config('addon.activators.file.statuses-file');
}
protected function config(string $key, $default = null)
{
return $this->config->get('addon.activators.file.' . $key, $default);
}
protected function getModulesStatuses(): array
{
if (!$this->config->get('addon.cache.enabled')) {
return $this->readJson();
}
return $this->cache->remember($this->cacheKey, $this->cacheLifetime, function () {
return $this->readJson();
});
}
protected function readJson(): array
{
if (!$this->files->exists($this->statusesFile)) {
return [];
}
return json_decode($this->files->get($this->statusesFile), true);
}
public function hasStatusForAddon(Addon $addon, bool $status): bool
{
if (!isset($this->modulesStatuses[$addon->getIdent()])) {
return $status === false;
}
return $this->modulesStatuses[$addon->getIdent()] === $status;
}
}