FEATURE: move bussiness logic out of models
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
namespace App\Models\ModelTraits;
|
||||
|
||||
use App\CMS\Handlers\Contracts\TemplateHandler;
|
||||
use App\Models\Templates\TemplatePath;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
trait Module {
|
||||
/**
|
||||
* Create new module in database
|
||||
*
|
||||
* @param $name
|
||||
* @param $alias
|
||||
* @param string $description
|
||||
* @param null $handler
|
||||
* @param $parentModuleId
|
||||
* @param array $settings
|
||||
*
|
||||
* @return Module
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public static function install($name, $alias, $description, $handler, $parentModuleId, $settings)
|
||||
{
|
||||
// check if user does not want to install module in runtime
|
||||
if (!App::runningInConsole()) {
|
||||
throw new \ErrorException('Modules can be installed only via Artisan!');
|
||||
}
|
||||
|
||||
$managesContent = $handler ? in_array('App\CMS\Handlers\Contracts\AppHandler', class_implements($handler)) : false;
|
||||
$isTemplate = $handler ? in_array(TemplateHandler::class, class_implements($handler)) : false;
|
||||
$supportCategories = $managesContent ? call_user_func([$handler, 'supportCategories']) : false;
|
||||
|
||||
if ($parentModuleId) {
|
||||
$parentModule = Module::findOrFail($parentModuleId);
|
||||
$parentModule->has_submodules = true;
|
||||
$parentModule->save();
|
||||
}
|
||||
|
||||
// insert module into database
|
||||
$module = Module::create([
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'handler' => $handler,
|
||||
'manages_content' => $managesContent,
|
||||
'support_categories' => $supportCategories,
|
||||
'alias' => $alias,
|
||||
'module_settings' => json_encode($settings),
|
||||
'parent_id' => $parentModuleId ? $parentModuleId : null,
|
||||
'enabled' => 1,
|
||||
'is_template' => $isTemplate
|
||||
]);
|
||||
|
||||
// install something module-specific
|
||||
if ($handler) {
|
||||
call_user_func([$handler, 'install']);
|
||||
}
|
||||
|
||||
return $module;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete module from database and uninstall its dependencies
|
||||
*
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
// check if user does not want to install module in runtime
|
||||
if (!App::runningInConsole()) {
|
||||
throw new \ErrorException('Modules can be uninstalled only via Artisan!');
|
||||
}
|
||||
|
||||
// delete everything in /resources/views/visitor
|
||||
if ($this->is_template) {
|
||||
$this->deleteDirectory(resource_path('views/visitor'));
|
||||
$this->deleteDirectory(resource_path('views/modules'));
|
||||
$this->deleteDirectory(public_path('assets'));
|
||||
|
||||
// delete all template paths
|
||||
TemplatePath::getQuery()->delete();
|
||||
}
|
||||
|
||||
if ($this->handler && is_callable([$this->handler, 'uninstall'])) {
|
||||
call_user_func([$this->handler, 'uninstall'], $this);
|
||||
}
|
||||
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete directory when uninstalling module.
|
||||
*
|
||||
* @param $directory
|
||||
*
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
private function deleteDirectory($directory)
|
||||
{
|
||||
if (!is_dir($directory))
|
||||
return;
|
||||
|
||||
$success = \File::deleteDirectory($directory, true);
|
||||
|
||||
if (!$success) {
|
||||
throw new \ErrorException('Cannot delete ' . $directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user