2020-10-19 22:10:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support\Modules;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Config\Repository as Config;
|
|
|
|
use Illuminate\Container\Container;
|
|
|
|
use Illuminate\Filesystem\Filesystem;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Nwidart\Modules\Contracts\ActivatorInterface;
|
|
|
|
use Nwidart\Modules\Module;
|
|
|
|
|
|
|
|
class DatabaseActivator implements ActivatorInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Laravel config instance
|
|
|
|
*
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
|
|
|
private $files;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The module path.
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The scanned paths.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $paths = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of modules activation statuses
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $modulesStatuses;
|
|
|
|
|
|
|
|
public function __construct(Container $app, $path = null)
|
|
|
|
{
|
|
|
|
$this->config = $app['config'];
|
|
|
|
$this->files = $app['files'];
|
|
|
|
$this->modulesStatuses = $this->getModulesStatuses();
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:28:54 +08:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return \App\Models\Module|null
|
|
|
|
*/
|
|
|
|
public function getModuleByName(string $name): ?\App\Models\Module
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return \App\Models\Module::where(['name' => $name])->first();
|
|
|
|
} catch (Exception $e) { // Catch any database/connection errors
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
/**
|
|
|
|
* Get modules statuses, from the database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getModulesStatuses(): array
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$modules = \App\Models\Module::all();
|
|
|
|
$retVal = [];
|
|
|
|
foreach ($modules as $i) {
|
|
|
|
$retVal[$i->name] = $i->enabled;
|
|
|
|
}
|
2020-10-22 01:28:54 +08:00
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
return $retVal;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function reset(): void
|
|
|
|
{
|
|
|
|
(new \App\Models\Module())->truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function enable(Module $module): void
|
|
|
|
{
|
2020-10-22 01:28:54 +08:00
|
|
|
$this->setActive($module, true);
|
2020-10-19 22:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function disable(Module $module): void
|
|
|
|
{
|
2020-10-22 01:28:54 +08:00
|
|
|
$this->setActive($module, false);
|
2020-10-19 22:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \Nwidart\Modules\Module instance passed
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function hasStatus(Module $module, bool $status): bool
|
|
|
|
{
|
2020-10-22 01:28:54 +08:00
|
|
|
$module = $this->getModuleByName($module->getName());
|
|
|
|
if (!$module) {
|
2020-10-19 22:10:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-22 01:28:54 +08:00
|
|
|
|
|
|
|
return $module->enabled;
|
2020-10-19 22:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setActive(Module $module, bool $active): void
|
|
|
|
{
|
2020-10-22 01:28:54 +08:00
|
|
|
$module = $this->getModuleByName($module->getName());
|
|
|
|
if (!$module) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$module->enabled = $active;
|
|
|
|
$module->save();
|
2020-10-19 22:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setActiveByName(string $name, bool $status): void
|
|
|
|
{
|
2020-10-22 01:28:54 +08:00
|
|
|
$module = $this->getModuleByName($name);
|
|
|
|
if (!$module) {
|
|
|
|
return;
|
2020-10-19 22:10:28 +08:00
|
|
|
}
|
2020-10-22 01:28:54 +08:00
|
|
|
|
|
|
|
$module->enabled = $status;
|
|
|
|
$module->save();
|
2020-10-19 22:10:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function delete(Module $module): void
|
|
|
|
{
|
|
|
|
$name = $module->getName();
|
|
|
|
|
|
|
|
try {
|
|
|
|
(new \App\Models\Module())->where([
|
|
|
|
'name' => $name,
|
|
|
|
])->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error('Module '.$module.' Delete failed! Exception : '.$e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|