FEATURE: add blade template registration into templates

This commit is contained in:
Michal Kváček
2016-09-20 11:50:41 +02:00
parent f087da1b43
commit d43073a011
17 changed files with 191 additions and 108 deletions
+3 -2
View File
@@ -1,8 +1,9 @@
# published files from modules
/resources/views/modules
/public/modules
/resources/views/vendor
/public/assets
/resources/views/modules
/resources/views/visitor
/resources/views/vendor
# directories with custom templates
**/views/**/custom/*
+18 -15
View File
@@ -33,14 +33,21 @@ class SetTemplate extends Command
}
// try to find currently installed template
$current = Module::where('is_template', true)->first();
$current = Module::where('is_template', true)
->first();
// install template
$config = @include $templatePath . '/config.php';
$config['staticName'] = $template;
if ($current) {
$this->line('Template is already set up. Currently using '.trans($current->name).'.');
$confirm = $this->ask('Uninstall it and install '.$template.'? [y/n]');
$info = json_decode($current->module_settings);
$this->line('Template is already installed. Currently using template ' . trans($current->name) . ' located in /templates/' . $info->staticName.'.');
$confirm = $this->ask('Uninstall it and install *' . trans($config['name']) . '*? All data associated with installed template will be lost! [y/n]');
if ($confirm === 'y') {
$this->line('Uninstalling '.$current->name.'.');
$this->line('Uninstalling ' . $current->name . '.');
$current->uninstall();
} else {
$this->line('Aborting installation.');
@@ -48,24 +55,20 @@ class SetTemplate extends Command
}
}
// install template
$config = @include $templatePath . '/config.php';
try {
$config['staticName'] = $template;
Module::install($config['name'], $config['alias'], $config['description'], $config['handler'], false, $config);
} catch (\Exception $e) {
$this->error('Cannot install template '.$template.'.');
$this->error('Cannot install template ' . $template . '. Message: ' . $e->getMessage());
return;
}
// theme written
$this->line('Template successfully setup.');
// $this->line('');
// $this->line(' ** PUBLISHING ASSETS AND VIEWS **');
// // publish assets and views for module and theme
// $this->call('vendor:publish', ['--tag' => ['public-assets'], '--force' => true]);
// $this->call('vendor:publish', ['--tag' => ['module-views'], '--force' => true]);
// $this->call('vendor:publish');
$this->line('');
$this->line(' ** PUBLISHING ASSETS AND VIEWS **');
// publish assets and views for module and theme
$this->call('vendor:publish', ['--tag' => ['public-assets'], '--force' => true]);
$this->call('vendor:publish', ['--tag' => ['module-views'], '--force' => true]);
$this->call('vendor:publish');
}
}
+16 -3
View File
@@ -21,10 +21,8 @@ class Template
$namespace .= '::';
}
$theme = config('app.theme');
// list of prefixes
$prefixes = [$theme . '.custom.', $theme . '.', ''];
$prefixes = ['custom.', ''];
$view = null;
foreach ($prefixes as $prefix) {
@@ -59,6 +57,13 @@ class Template
return config('app.upload_url') . '/' . $filename;
}
/**
* Get path to given asset
*
* @param $path
*
* @return string
*/
public static function asset($path)
{
if (env('APP_DEBUG')) {
@@ -75,6 +80,14 @@ class Template
return config('app.url') . '/assets/' . config('app.theme') . '/' . $path;
}
/**
* Print date in configured format
*
* @param $datestamp
* @param bool $withTime
*
* @return false|string
*/
public static function date($datestamp, $withTime = true)
{
$timestamp = strtotime($datestamp);
+23 -11
View File
@@ -78,6 +78,11 @@ 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()) {
@@ -85,23 +90,30 @@ class Module extends Model
}
// delete everything in /resources/views/visitor
$directories = \File::directories(resource_path('views/visitor'));
foreach ($directories as $directory) {
$success = \File::deleteDirectory($directory);
if (!$success) {
throw new \ErrorException('Cannot delete '.$directory);
}
}
$this->deleteDirectory(resource_path('views/visitor'));
$this->deleteDirectory(public_path('assets'));
$handler = $this->handler;
if ($handler) {
if ($handler && is_callable([$handler, 'uninstall'])) {
call_user_func([$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);
}
}
}
-34
View File
@@ -30,8 +30,6 @@ class AppServiceProvider extends ServiceProvider
{
// ignore running from artisan
if (App::runningInConsole()) {
$this->publishViewResources();
return;
}
@@ -143,37 +141,5 @@ class AppServiceProvider extends ServiceProvider
$registrator->register(new Files(), new AdminFiles(), SearchEngineRegistrator::ADMIN);
}
/**
* Publish all public assets from all themes
*/
private function publishViewResources()
{
$theme = config('app.theme');
$themeDir = resource_path('views/visitor/' . $theme);
// publish assets if exists for current theme
if (is_dir($themeDir . '/' . Template::ASSETS_DIRECTORY)) {
$this->publishes([$themeDir . '/' . Template::ASSETS_DIRECTORY => public_path('assets/' . $theme)], 'public-assets');
}
if (is_dir($themeDir . '/' . Template::MODULES_DIRECTORY)) {
$this->publishModuleViews($themeDir . '/' . Template::MODULES_DIRECTORY);
}
}
private function publishModuleViews($themeModules)
{
$theme = config('app.theme');
$modules = File::directories($themeModules);
$moduleViews = [];
foreach ($modules as $module) {
$moduleName = strtolower(basename($module));
$moduleViews[$module] = resource_path('views/modules/' . $moduleName . '/visitor/' . $theme);
}
if (!empty($moduleViews)) {
$this->publishes($moduleViews, 'module-views');
}
}
}
+63 -20
View File
@@ -4,34 +4,47 @@ namespace App\Providers;
use App\Handlers\Registrators\TemplateRegistrator;
use App\Models\Module;
use App\Models\Template;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
class TemplateServiceProvider extends ServiceProvider
{
public function boot()
private function getTemplate()
{
if (App::runningInConsole())
return;
parent::boot();
// select template
$template = Module::where('is_template', true)
->where('enabled', true)
->first();
if ($template) {
$template->settings = json_decode($template->module_settings);
}
return $template;
}
public function boot()
{
// ignore running from artisan
if (App::runningInConsole()) {
$this->publishViewResources();
return;
}
$template = $this->getTemplate();
if (!$template) {
throw new \ErrorException('Template is not set, cannot continue');
}
$templateSettings = json_decode($template->module_settings);
// include files
$files = isset($templateSettings->files) ? $templateSettings->files : [];
$files = isset($template->settings->files) ? $template->settings->files : [];
foreach ($files as $file) {
include_once app_path('../templates/' . $templateSettings->staticName . '/' . $file);
include_once $file;
}
// register templates
@@ -51,16 +64,18 @@ class TemplateServiceProvider extends ServiceProvider
$registrator = $this->app->make('registerTemplateDir');
foreach ($publicTemplates as $template) {
// setup some global variables
// try {
// $registrator->setBaseDirectory('resources/views/modules/gallery/visitor');
// $registrator->setNamespace('gallery');
// $registrator->setModuleId(call_user_func([config('gallery.handler'), 'getModuleId']));
//
// // register path with current template name
// $registrator->register(config('app.theme'), 'gallery::admin/adminMenu.templatePath');
// } catch (InvalidArgumentException $e) {
// // ignore this exception
// }
$module = strtolower($template['module']);
$namespace = isset($template['namespace']) ? $template['namespace'] : strtolower($template['module']);
try {
$registrator->setBaseDirectory('resources/views/modules/' . $module);
$registrator->setNamespace($namespace);
$registrator->setModuleId(call_user_func([config($module . '.handler'), 'getModuleId']));
// register path with current template name
$registrator->register('visitor', $template['templateName']);
} catch (InvalidArgumentException $e) {
// ignore this exception
}
}
}
@@ -72,4 +87,32 @@ class TemplateServiceProvider extends ServiceProvider
public function register()
{
}
/**
* Publish all public assets from all themes
*/
private function publishViewResources()
{
$template = $this->getTemplate();
if (!$template) {
return;
}
$templateDir = app_path('../templates/' . $template->settings->staticName . '/Resources');
$assetsDir = $templateDir . '/' . Template::ASSETS_DIRECTORY;
$viewsDir = $templateDir . '/views';
// publish assets if exists for current theme
if (is_dir($assetsDir)) {
$this->publishes([$assetsDir => public_path('assets/visitor')], 'public-assets');
}
// publish base visitors views
$this->publishes([$viewsDir . '/visitor' => resource_path('views/visitor')], 'module-views');
// publish admin views
if (is_dir($viewsDir . '/modules')) {
$this->publishes([$viewsDir . '/modules' => resource_path('views/modules')], 'module-views');
}
}
}
+1 -2
View File
@@ -159,8 +159,6 @@ return [
Pingpong\Modules\ModulesServiceProvider::class,
Pingpong\Menus\MenusServiceProvider::class,
App\Providers\SubmodulesManager::class,
\App\Providers\TemplateServiceProvider::class,
Franzose\ClosureTable\ClosureTableServiceProvider::class,
\Watson\Sitemap\SitemapServiceProvider::class,
/*
@@ -171,6 +169,7 @@ return [
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\GlobalViewDataProvider::class,
\App\Providers\TemplateServiceProvider::class,
],
/*
@@ -57,30 +57,14 @@ class StaticPagesServiceProvider extends ServiceProvider
*/
public function registerViews()
{
$viewPath = base_path('resources/views/modules/staticpages');
$sourcePath = __DIR__ . '/../Resources/views';
$viewPath = base_path('resources/views/modules/staticpages');
$this->publishes([$sourcePath => $viewPath], 'module-views');
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/staticpages';
}, \Config::get('view.paths')), [$sourcePath]), 'staticpages');
/**
* @var $registrator TemplateRegistrator
*/
$registrator = $this->app->make('registerTemplateDir');
// try {
// // setup some global variables
// $registrator->setBaseDirectory('resources/views/modules/staticpages/visitor');
// $registrator->setNamespace('staticpages');
// $registrator->setModuleId(call_user_func([config('staticpages.handler'), 'getModuleId']));
//
// // register path with current template name
// $registrator->register(config('app.theme'), 'staticpages::admin.templatePath');
// } catch (InvalidArgumentException $e) {
// // ignore this exception
// }
}
/**
+1 -1
View File
@@ -5,6 +5,6 @@ return [
'alias' => 'sablona',
'description' => 'Sablona jednostrankova',
'files' => [
'routes.php'
__DIR__.'/routes.php'
]
];
+4 -3
View File
@@ -1,6 +1,7 @@
<?php
Route::group(['middleware' => 'web', 'prefix' => 'test', 'namespace' => 'Modules\Blog\Comments\Http\Controllers'], function()
Route::group(['middleware' => 'web', 'prefix' => 'test'], function()
{
Route::get('/neco-neco', 'CommentsController@index')->name('template.route');
Route::get('/', function() {
echo "ahoj";
})->name('template.route');
});
+51
View File
@@ -0,0 +1,51 @@
<?php
namespace Templates\Jednostrankovka;
use App\Models\Module;
use App\Models\User;
use Templates\Base\OnePage\Handlers\OnePage;
class Handler extends OnePage
{
/**
* Register admin menu (or multiple ones)
*
* This cannot be in ServiceProvider because Session is started at middleware. And ServiceProvider is called before
* starting any middleware..
*
* @param User $user model of logged user
*/
public static function registerAdminMenu(User $user)
{
// parent::registerAdminMenu($user);
}
/**
* Install module
*
* @return mixed
*/
public static function install()
{
// parent::install();
}
public static function uninstall(Module $template)
{
// parent::uninstall($template);
}
public static function getPublicTemplatePaths()
{
return [
[
'module' => 'Article',
'templateName' => 'article::templates.name'
],
[
'module' => 'StaticPages',
'templateName' => 'staticpages::templates.name'
]
];
}
}
@@ -0,0 +1 @@
{{dd($page)}}
@@ -0,0 +1 @@
tu nic neni
+7
View File
@@ -0,0 +1,7 @@
<?php
$config = include_once __DIR__.'/../Base/OnePage/config.php';
$config['handler'] = \Templates\Jednostrankovka\Handler::class;
$config['alias'] = 'jednostrankovka';
return $config;