92 lines
2.4 KiB
PHP
92 lines
2.4 KiB
PHP
<?php
|
|
namespace App\CMS\Helpers;
|
|
|
|
use App\CMS\Contracts\Modules\TemplateHandler;
|
|
use App\Models\Content\Content;
|
|
use App\Models\Module;
|
|
use App\Models\User;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class CurrentTemplate implements TemplateHandler
|
|
{
|
|
protected static $template;
|
|
protected static $initialized = false;
|
|
|
|
/**
|
|
* Getter for currently set template
|
|
* @return Module
|
|
* @throws \ErrorException
|
|
*/
|
|
public static function get()
|
|
{
|
|
if (!self::$initialized) {
|
|
self::$template = Cache::rememberForever('current-template', function () {
|
|
return Module::where('is_template', true)
|
|
->where('enabled', true)
|
|
->first();
|
|
});
|
|
|
|
|
|
self::$initialized = true;
|
|
}
|
|
|
|
return self::$template;
|
|
}
|
|
|
|
/**
|
|
* Shortcut for getting template handler
|
|
*
|
|
* @return string
|
|
* @throws \ErrorException
|
|
*/
|
|
public static function handler()
|
|
{
|
|
$template = self::get();
|
|
|
|
if (!$template) {
|
|
throw new \ErrorException('No template selected, no handler available!');
|
|
}
|
|
|
|
return $template->handler;
|
|
}
|
|
|
|
/**
|
|
* Reset currently set template and load another one on next call of get() method.
|
|
*/
|
|
public static function reset()
|
|
{
|
|
self::$initialized = false;
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
call_user_func_array([self::handler(), 'registerAdminMenu'], [$user]);
|
|
}
|
|
|
|
/**
|
|
* Get list of registered templates and supported modules.
|
|
*
|
|
* Returned array is expected to be array of arrays with following items: module, namespace and templateName.
|
|
* Example:
|
|
* return [
|
|
* ['module' => 'Article', 'templateName' => 'article::template.name']
|
|
* ];
|
|
*
|
|
* When no "namespace" is specified, value strtolower(module) is used instaed.
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getPublicTemplatePaths()
|
|
{
|
|
return call_user_func([self::handler(), 'getPublicTemplatePaths']);
|
|
}
|
|
} |