Merge branch 'template_support' into dev

This commit is contained in:
Michal Kváček
2016-09-20 20:17:06 +02:00
148 changed files with 890 additions and 5299 deletions
+51 -3
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Handlers\Contracts\TemplateHandler;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
@@ -20,7 +21,8 @@ class Module extends Model
'module_settings',
'enabled',
'support_categories',
'manages_content'
'manages_content',
'is_template'
];
protected $guarded = [];
@@ -45,6 +47,7 @@ class Module extends Model
}
$managesContent = $handler ? in_array('App\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) {
@@ -62,8 +65,9 @@ class Module extends Model
'support_categories' => $supportCategories,
'alias' => $alias,
'module_settings' => json_encode($settings),
'parent_id' => $parentModuleId,
'enabled' => 1
'parent_id' => $parentModuleId ? $parentModuleId : null,
'enabled' => 1,
'is_template' => $isTemplate
]);
// install something module-specific
@@ -73,4 +77,48 @@ class Module extends Model
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(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)
{
$success = \File::deleteDirectory($directory, true);
if (!$success) {
throw new \ErrorException('Cannot delete ' . $directory);
}
}
}