128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\CMS\Handlers\Contracts\TemplateHandler;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
/**
|
|
* Class Module
|
|
*/
|
|
class Module extends Model
|
|
{
|
|
public $timestamps = false;
|
|
protected $fillable = [
|
|
'handler',
|
|
'parent_id',
|
|
'name',
|
|
'description',
|
|
'alias',
|
|
'module_settings',
|
|
'enabled',
|
|
'support_categories',
|
|
'manages_content',
|
|
'is_template'
|
|
];
|
|
protected $guarded = [];
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
} |