first commit
This commit is contained in:
171
catch/src/Support/Module/Driver/DatabaseDriver.php
Normal file
171
catch/src/Support/Module/Driver/DatabaseDriver.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Catch\Support\Module\Driver;
|
||||
|
||||
use Catch\CatchAdmin;
|
||||
use Catch\Contracts\ModuleRepositoryInterface;
|
||||
use Catch\Enums\Status;
|
||||
use Catch\Exceptions\FailedException;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* DatabaseDriver
|
||||
*/
|
||||
class DatabaseDriver implements ModuleRepositoryInterface
|
||||
{
|
||||
protected Model $model;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = $this->createModuleModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* all
|
||||
*
|
||||
* @param array $search
|
||||
* @return Collection
|
||||
*/
|
||||
public function all(array $search): Collection
|
||||
{
|
||||
return $this->model::query()
|
||||
->when($search['name'] ?? false, function ($query) use ($search) {
|
||||
$query->where('name', 'like', '%'.$search['name'].'%');
|
||||
})->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* create module json
|
||||
*
|
||||
* @param array $module
|
||||
* @return bool|int
|
||||
*/
|
||||
public function create(array $module): bool|int
|
||||
{
|
||||
$this->hasSameModule($module);
|
||||
|
||||
return $this->model->save([
|
||||
'name' => $module['name'],
|
||||
'path' => $module['path'],
|
||||
'description' => $module['desc'],
|
||||
'keywords' => $module['keywords'],
|
||||
'service' => sprintf('\\%s%s', CatchAdmin::getModuleNamespace($module['name']), ucfirst($module['name']).'ServiceProvider'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* module info
|
||||
*
|
||||
* @param string $name
|
||||
* @return Collection
|
||||
*/
|
||||
public function show(string $name): Collection
|
||||
{
|
||||
return $this->model->where('name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* update module json
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $module
|
||||
* @return bool|int
|
||||
*/
|
||||
public function update(string $name, array $module): bool|int
|
||||
{
|
||||
return $this->model->where('name', $name)
|
||||
|
||||
->update([
|
||||
'name' => $module['name'],
|
||||
'alias' => $module['alias'],
|
||||
'description' => $module['desc'],
|
||||
'keywords' => $module['keywords'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete module json
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool|int
|
||||
*/
|
||||
public function delete(string $name): bool|int
|
||||
{
|
||||
return $this->model->where('name', $name)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* disable or enable
|
||||
*
|
||||
* @param $name
|
||||
* @return bool|int
|
||||
*/
|
||||
public function disOrEnable($name): bool|int
|
||||
{
|
||||
$module = $this->show($name);
|
||||
|
||||
$module->status = (int) $module->status;
|
||||
|
||||
return $module->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* get enabled
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getEnabled(): Collection
|
||||
{
|
||||
// TODO: Implement getEnabled() method.
|
||||
return $this->model->where('status', Status::Enable->value())->get();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $module
|
||||
* @return void
|
||||
*/
|
||||
protected function hasSameModule(array $module): void
|
||||
{
|
||||
if ($this->model->where('name', $module['name'])->first()) {
|
||||
throw new FailedException(sprintf('Module [%s] has been created', $module['name']));
|
||||
}
|
||||
|
||||
if ($this->model->where('alias', $module['alias'])->first()) {
|
||||
throw new FailedException(sprintf('Module Alias [%s] has been exised', $module['alias']));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create model
|
||||
* @return Model
|
||||
*/
|
||||
protected function createModuleModel(): Model
|
||||
{
|
||||
return new class () extends Model {
|
||||
protected $table;
|
||||
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
parent::__construct($attributes);
|
||||
|
||||
$this->table = Container::getInstance()->make('config')->get('catch.module.driver.table_name');
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
193
catch/src/Support/Module/Driver/FileDriver.php
Normal file
193
catch/src/Support/Module/Driver/FileDriver.php
Normal file
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Catch\Support\Module\Driver;
|
||||
|
||||
use Catch\CatchAdmin;
|
||||
use Catch\Contracts\ModuleRepositoryInterface;
|
||||
use Catch\Exceptions\FailedException;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* FileDriver
|
||||
*/
|
||||
class FileDriver implements ModuleRepositoryInterface
|
||||
{
|
||||
protected string $moduleJson;
|
||||
|
||||
/**
|
||||
* construct
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->moduleJson = storage_path('app').DIRECTORY_SEPARATOR.'modules.json';
|
||||
}
|
||||
|
||||
/**
|
||||
* all
|
||||
*
|
||||
* @param array $search
|
||||
* @return Collection
|
||||
*/
|
||||
public function all(array $search = []): Collection
|
||||
{
|
||||
if (! File::exists($this->moduleJson)) {
|
||||
return Collection::make([]);
|
||||
}
|
||||
|
||||
if (! Str::length(File::get($this->moduleJson))) {
|
||||
return Collection::make([]);
|
||||
}
|
||||
|
||||
$modules = Collection::make(\json_decode(File::get($this->moduleJson), true))->values();
|
||||
|
||||
$name = $search['name'] ?? '';
|
||||
|
||||
if (! $name) {
|
||||
return $modules;
|
||||
}
|
||||
|
||||
return $modules->filter(function ($module) use ($name) {
|
||||
return Str::of($module['name'])->contains($name);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* create module json
|
||||
*
|
||||
* @param array $module
|
||||
* @return bool
|
||||
*/
|
||||
public function create(array $module): bool
|
||||
{
|
||||
$modules = $this->all();
|
||||
|
||||
$this->hasSameModule($module, $modules);
|
||||
|
||||
$module['service'] = sprintf('\\%s', CatchAdmin::getModuleServiceProvider($module['path']));
|
||||
$module['version'] = '1.0.0';
|
||||
$module['enable'] = true;
|
||||
|
||||
File::put($this->moduleJson, $modules->push($module)->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* module info
|
||||
*
|
||||
* @param string $name
|
||||
* @return Collection
|
||||
*/
|
||||
public function show(string $name): Collection
|
||||
{
|
||||
foreach ($this->all() as $module) {
|
||||
if (Str::of($module['name'])->exactly($name)) {
|
||||
return Collection::make($module);
|
||||
}
|
||||
}
|
||||
|
||||
throw new FailedException("Module [$name] not Found");
|
||||
}
|
||||
|
||||
/**
|
||||
* update module json
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $module
|
||||
* @return bool
|
||||
*/
|
||||
public function update(string $name, array $module): bool
|
||||
{
|
||||
File::put($this->moduleJson, $this->all()->map(function ($m) use ($module, $name) {
|
||||
if (Str::of($name)->exactly($m['name'])) {
|
||||
$m['path'] = $module['path'];
|
||||
$m['name'] = $module['name'];
|
||||
$m['description'] = $module['description'] ?? '';
|
||||
$m['keywords'] = $module['keywords'] ?? '';
|
||||
$m['enable'] = $module['enable'];
|
||||
}
|
||||
return $m;
|
||||
})->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete module json
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $name): bool
|
||||
{
|
||||
File::put($this->moduleJson, $this->all()->filter(function ($module) use ($name) {
|
||||
if (! Str::of($name)->exactly($module['name'])) {
|
||||
return $module;
|
||||
}
|
||||
})->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* disable or enable
|
||||
*
|
||||
* @param $name
|
||||
* @return bool|int
|
||||
*/
|
||||
public function disOrEnable($name): bool|int
|
||||
{
|
||||
return File::put($this->moduleJson, $this->all()->map(function ($module) use ($name) {
|
||||
if (Str::of($module['name'])->exactly($name)) {
|
||||
$module['enable'] = ! $module['enable'];
|
||||
}
|
||||
return $module;
|
||||
})->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
/**
|
||||
* get enabled
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getEnabled(): Collection
|
||||
{
|
||||
// TODO: Implement getEnabled() method.
|
||||
return $this->all()->where('enable', true)->values();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $module
|
||||
* @param Collection $modules
|
||||
* @return void
|
||||
*/
|
||||
protected function hasSameModule(array $module, Collection $modules): void
|
||||
{
|
||||
if ($modules->count()) {
|
||||
if ($modules->pluck('name')->contains($module['name'])) {
|
||||
throw new FailedException(sprintf('Module [%s] has been created', $module['name']));
|
||||
}
|
||||
|
||||
if ($modules->pluck('path')->contains($module['path'])) {
|
||||
throw new FailedException(sprintf('Module path [%s] has been existed', $module['path']));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
97
catch/src/Support/Module/Installer.php
Normal file
97
catch/src/Support/Module/Installer.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Catch\Support\Module;
|
||||
|
||||
use Catch\Contracts\ModuleRepositoryInterface;
|
||||
use Catch\Support\Composer;
|
||||
|
||||
/**
|
||||
* installer
|
||||
*/
|
||||
abstract class Installer
|
||||
{
|
||||
/**
|
||||
* construct
|
||||
*
|
||||
* @param ModuleRepositoryInterface $moduleRepository
|
||||
*/
|
||||
public function __construct(protected ModuleRepositoryInterface $moduleRepository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* module info
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function info(): array;
|
||||
|
||||
/**
|
||||
* migration
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function migration(): string;
|
||||
|
||||
/**
|
||||
* seed
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function seeder(): string;
|
||||
|
||||
/**
|
||||
* require packages
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function requirePackages(): void;
|
||||
|
||||
|
||||
/**
|
||||
* remove packages
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function removePackages(): void;
|
||||
|
||||
/**
|
||||
* uninstall
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function uninstall(): void
|
||||
{
|
||||
$this->moduleRepository->delete($this->info()['name']);
|
||||
|
||||
|
||||
$this->removePackages();
|
||||
}
|
||||
|
||||
/**
|
||||
* invoke
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(): void
|
||||
{
|
||||
// TODO: Implement __invoke() method.
|
||||
$this->moduleRepository->create($this->info());
|
||||
|
||||
// migration
|
||||
|
||||
// seed
|
||||
|
||||
$this->requirePackages();
|
||||
}
|
||||
|
||||
/**
|
||||
* composer installer
|
||||
*
|
||||
* @return Composer
|
||||
*/
|
||||
protected function composer(): Composer
|
||||
{
|
||||
return app(Composer::class);
|
||||
}
|
||||
}
|
63
catch/src/Support/Module/ModuleManager.php
Normal file
63
catch/src/Support/Module/ModuleManager.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Catch\Support\Module;
|
||||
|
||||
use Catch\Support\Module\Driver\DatabaseDriver;
|
||||
use Catch\Support\Module\Driver\FileDriver;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Manager;
|
||||
|
||||
class ModuleManager extends Manager
|
||||
{
|
||||
public function __construct(Container|\Closure $container)
|
||||
{
|
||||
if ($container instanceof \Closure) {
|
||||
$container = $container();
|
||||
}
|
||||
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultDriver(): string
|
||||
{
|
||||
// TODO: Implement getDefaultDriver() method.
|
||||
return $this->config->get('catch.module.driver.default');
|
||||
}
|
||||
|
||||
/**
|
||||
* create file driver
|
||||
*
|
||||
* @return FileDriver
|
||||
*/
|
||||
public function createFileDriver(): FileDriver
|
||||
{
|
||||
return new FileDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* create database driver
|
||||
*
|
||||
* @return DatabaseDriver
|
||||
*/
|
||||
public function createDatabaseDriver(): DatabaseDriver
|
||||
{
|
||||
return new DatabaseDriver();
|
||||
}
|
||||
}
|
144
catch/src/Support/Module/ModuleRepository.php
Normal file
144
catch/src/Support/Module/ModuleRepository.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Catch\Support\Module;
|
||||
|
||||
use Catch\Contracts\ModuleRepositoryInterface;
|
||||
use Catch\Events\Module\Created;
|
||||
use Catch\Events\Module\Creating;
|
||||
use Catch\Events\Module\Deleted;
|
||||
use Catch\Events\Module\Updated;
|
||||
use Catch\Events\Module\Updating;
|
||||
use Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
/**
|
||||
* FileDriver
|
||||
*/
|
||||
class ModuleRepository
|
||||
{
|
||||
protected ModuleRepositoryInterface $moduleRepository;
|
||||
|
||||
/**
|
||||
* construct
|
||||
*/
|
||||
public function __construct(ModuleRepositoryInterface $moduleRepository)
|
||||
{
|
||||
$this->moduleRepository = $moduleRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* all
|
||||
*
|
||||
* @param array $search
|
||||
* @return Collection
|
||||
*/
|
||||
public function all(array $search): Collection
|
||||
{
|
||||
return $this->moduleRepository->all($search);
|
||||
}
|
||||
|
||||
/**
|
||||
* create module json
|
||||
*
|
||||
* @param array $module
|
||||
* @return bool
|
||||
*/
|
||||
public function create(array $module): bool
|
||||
{
|
||||
Event::dispatch(new Creating($module));
|
||||
|
||||
$this->moduleRepository->create($module);
|
||||
|
||||
Event::dispatch(new Created($module));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* module info
|
||||
*
|
||||
* @param string $name
|
||||
* @return Collection
|
||||
* @throws Exception
|
||||
*/
|
||||
public function show(string $name): Collection
|
||||
{
|
||||
try {
|
||||
return $this->moduleRepository->show($name);
|
||||
} catch (Exception $e) {
|
||||
throw new $e();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* update module json
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $module
|
||||
* @return bool
|
||||
*/
|
||||
public function update(string $name, array $module): bool
|
||||
{
|
||||
Event::dispatch(new Updating($name, $module));
|
||||
|
||||
$this->moduleRepository->update($name, $module);
|
||||
|
||||
Event::dispatch(new Updated($name, $module));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete module json
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete(string $name): bool
|
||||
{
|
||||
$module = $this->show($name);
|
||||
|
||||
$this->moduleRepository->delete($name);
|
||||
|
||||
Event::dispatch(new Deleted($module));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* disable or enable
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool|int
|
||||
*/
|
||||
public function disOrEnable(string $name): bool|int
|
||||
{
|
||||
return $this->moduleRepository->disOrEnable($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* get enabled
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getEnabled(): Collection
|
||||
{
|
||||
// TODO: Implement getEnabled() method.
|
||||
return $this->moduleRepository->getEnabled();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user