winter/modules/system/ServiceProvider.php

470 lines
16 KiB
PHP
Raw Normal View History

<?php namespace System;
2014-05-14 23:24:20 +10:00
use App;
use Lang;
use Event;
use Config;
use Backend;
use Request;
use Validator;
2014-05-14 23:24:20 +10:00
use BackendMenu;
use BackendAuth;
use Twig_Environment;
use Twig_Loader_String;
2014-05-14 23:24:20 +10:00
use System\Classes\ErrorHandler;
use System\Classes\MarkupManager;
2014-05-14 23:24:20 +10:00
use System\Classes\PluginManager;
use System\Classes\SettingsManager;
use System\Twig\Engine as TwigEngine;
use System\Twig\Loader as TwigLoader;
use System\Twig\Extension as TwigExtension;
use System\Models\EventLog;
use System\Models\MailSettings;
use System\Models\MailTemplate;
use System\Classes\CombineAssets;
2014-05-14 23:24:20 +10:00
use Backend\Classes\WidgetManager;
use October\Rain\Support\ModuleServiceProvider;
use October\Rain\Router\Helper as RouterHelper;
class ServiceProvider extends ModuleServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register('system');
$this->registerSingletons();
$this->registerPrivilegedActions();
2014-05-14 23:24:20 +10:00
/*
* Register all plugins
2014-05-14 23:24:20 +10:00
*/
PluginManager::instance()->registerAll();
$this->registerConsole();
$this->registerErrorHandler();
$this->registerLogging();
$this->registerTwigParser();
$this->registerMailer();
$this->registerMarkupTags();
$this->registerAssetBundles();
$this->registerValidator();
2014-05-14 23:24:20 +10:00
/*
* Register other module providers
*/
foreach (Config::get('cms.loadModules', []) as $module) {
if (strtolower(trim($module)) != 'system') {
App::register('\\' . $module . '\ServiceProvider');
}
}
2015-06-27 11:04:44 +10:00
/*
* Backend specific
*/
if (App::runningInBackend()) {
$this->registerBackendNavigation();
$this->registerBackendReportWidgets();
$this->registerBackendPermissions();
$this->registerBackendSettings();
2015-06-27 11:04:44 +10:00
}
}
/**
* Bootstrap the module events.
*
* @return void
*/
public function boot()
{
/*
* Boot plugins
2014-05-14 23:24:20 +10:00
*/
$pluginManager = PluginManager::instance()->bootAll();
parent::boot('system');
}
/**
* Register singletons
*/
protected function registerSingletons()
{
2014-10-18 11:58:50 +02:00
App::singleton('backend.helper', function () {
return new \Backend\Helpers\Backend;
2014-10-18 11:58:50 +02:00
});
App::singleton('backend.menu', function () {
return \Backend\Classes\NavigationManager::instance();
});
App::singleton('backend.auth', function () {
return \Backend\Classes\AuthManager::instance();
});
}
2014-05-14 23:24:20 +10:00
/**
* Check for CLI or system/updates route and disable any plugin initialization
*/
protected function registerPrivilegedActions()
{
$requests = ['/combine', '@/system/updates', '@/system/install', '@/backend/auth'];
$commands = ['october:up', 'october:update'];
2014-05-14 23:24:20 +10:00
/*
* Requests
2014-05-14 23:24:20 +10:00
*/
$path = RouterHelper::normalizeUrl(Request::path());
$backendUri = RouterHelper::normalizeUrl(Config::get('cms.backendUri', 'backend'));
foreach ($requests as $request) {
if (substr($request, 0, 1) == '@') {
$request = $backendUri . substr($request, 1);
}
if (stripos($path, $request) === 0) {
PluginManager::$noInit = true;
}
}
2014-05-14 23:24:20 +10:00
/*
* CLI
*/
if (App::runningInConsole() && count(array_intersect($commands, Request::server('argv'))) > 0) {
PluginManager::$noInit = true;
}
}
/*
* Register markup tags
*/
protected function registerMarkupTags()
{
MarkupManager::instance()->registerCallback(function ($manager) {
$manager->registerFunctions([
// Functions
'input' => 'input',
'post' => 'post',
'get' => 'get',
'link_to' => 'link_to',
'link_to_asset' => 'link_to_asset',
'link_to_route' => 'link_to_route',
'link_to_action' => 'link_to_action',
'asset' => 'asset',
'action' => 'action',
'url' => 'url',
'route' => 'route',
'secure_url' => 'secure_url',
'secure_asset' => 'secure_asset',
// Classes
'str_*' => ['Str', '*'],
'url_*' => ['Url', '*'],
'html_*' => ['Html', '*'],
'form_*' => ['Form', '*'],
'form_macro' => ['Form', '__call']
]);
$manager->registerFilters([
// Classes
'slug' => ['Str', 'slug'],
'plural' => ['Str', 'plural'],
'singular' => ['Str', 'singular'],
'finish' => ['Str', 'finish'],
'snake' => ['Str', 'snake'],
'camel' => ['Str', 'camel'],
'studly' => ['Str', 'studly'],
'trans' => ['Lang', 'get'],
'transchoice' => ['Lang', 'choice'],
'md' => ['Markdown', 'parse'],
'time_since' => ['System\Helpers\DateTime', 'timeSince'],
'time_tense' => ['System\Helpers\DateTime', 'timeTense'],
]);
});
}
/**
* Register command line specifics
*/
protected function registerConsole()
{
2015-02-09 21:50:41 +11:00
/*
* Allow plugins to use the scheduler
*/
Event::listen('console.schedule', function($schedule) {
$plugins = PluginManager::instance()->getPlugins();
foreach ($plugins as $plugin) {
2015-02-12 21:04:05 +11:00
if (method_exists($plugin, 'registerSchedule')) {
$plugin->registerSchedule($schedule);
2015-02-09 21:50:41 +11:00
}
}
});
2014-05-14 23:24:20 +10:00
/*
* Add CMS based cache clearing to native command
*/
Event::listen('cache:cleared', function() {
\System\Helpers\Cache::clear();
});
/*
* Register console commands
2014-05-14 23:24:20 +10:00
*/
$this->registerConsoleCommand('october.up', 'System\Console\OctoberUp');
$this->registerConsoleCommand('october.down', 'System\Console\OctoberDown');
$this->registerConsoleCommand('october.update', 'System\Console\OctoberUpdate');
$this->registerConsoleCommand('october.util', 'System\Console\OctoberUtil');
$this->registerConsoleCommand('october.mirror', 'System\Console\OctoberMirror');
$this->registerConsoleCommand('october.fresh', 'System\Console\OctoberFresh');
$this->registerConsoleCommand('plugin.install', 'System\Console\PluginInstall');
$this->registerConsoleCommand('plugin.remove', 'System\Console\PluginRemove');
$this->registerConsoleCommand('plugin.refresh', 'System\Console\PluginRefresh');
$this->registerConsoleCommand('theme.install', 'System\Console\ThemeInstall');
$this->registerConsoleCommand('theme.remove', 'System\Console\ThemeRemove');
$this->registerConsoleCommand('theme.list', 'System\Console\ThemeList');
$this->registerConsoleCommand('theme.use', 'System\Console\ThemeUse');
}
/*
* Error handling for uncaught Exceptions
*/
protected function registerErrorHandler()
{
2015-02-07 16:14:26 +11:00
Event::listen('exception.beforeRender', function ($exception, $httpCode, $request){
$handler = new ErrorHandler;
return $handler->handleException($exception);
2015-02-07 16:14:26 +11:00
});
}
2014-05-14 23:24:20 +10:00
/*
* Write all log events to the database
*/
protected function registerLogging()
{
2014-10-18 11:58:50 +02:00
Event::listen('illuminate.log', function ($level, $message, $context) {
if (EventLog::useLogging()) {
2015-02-12 21:04:05 +11:00
EventLog::add($message, $level);
2014-10-18 11:58:50 +02:00
}
});
}
/*
* Register text twig parser
*/
protected function registerTwigParser()
{
/*
* Register system Twig environment
*/
App::singleton('twig.environment', function ($app) {
$twig = new Twig_Environment(new TwigLoader, ['auto_reload' => true]);
$twig->addExtension(new TwigExtension);
return $twig;
});
2014-05-14 23:24:20 +10:00
/*
* Register .htm extension for Twig views
*/
2014-10-18 11:58:50 +02:00
App::make('view')->addExtension('htm', 'twig', function () {
return new TwigEngine(App::make('twig.environment'));
});
}
/**
* Register mail templating and settings override.
*/
protected function registerMailer()
{
2014-05-14 23:24:20 +10:00
/*
* Override system mailer with mail settings
2014-05-14 23:24:20 +10:00
*/
2014-10-18 11:58:50 +02:00
Event::listen('mailer.beforeRegister', function () {
if (MailSettings::isConfigured()) {
MailSettings::applyConfigValues();
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
});
/*
* Override standard Mailer content with template
*/
2015-02-10 17:45:27 +11:00
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data) {
2014-10-18 11:58:50 +02:00
if (MailTemplate::addContentToMailer($message, $view, $data)) {
2014-07-04 19:13:34 +10:00
return false;
2014-10-18 11:58:50 +02:00
}
});
}
/*
* Register navigation
*/
protected function registerBackendNavigation()
{
2014-10-18 11:58:50 +02:00
BackendMenu::registerCallback(function ($manager) {
2014-05-14 23:24:20 +10:00
$manager->registerMenuItems('October.System', [
'system' => [
2014-10-22 17:34:51 +11:00
'label' => 'system::lang.settings.menu_label',
2014-10-24 17:38:24 +11:00
'icon' => 'icon-cog',
2014-05-14 23:24:20 +10:00
'url' => Backend::url('system/settings'),
'permissions' => [],
2014-07-27 11:11:16 +11:00
'order' => 1000
2014-05-14 23:24:20 +10:00
]
]);
});
/*
* Register the sidebar for the System main menu
2014-05-14 23:24:20 +10:00
*/
BackendMenu::registerContextSidenavPartial(
'October.System',
'system',
'~/modules/system/partials/_system_sidebar.htm'
);
}
/*
* Register report widgets
*/
protected function registerBackendReportWidgets()
{
2014-10-18 11:58:50 +02:00
WidgetManager::instance()->registerReportWidgets(function ($manager) {
2014-05-14 23:24:20 +10:00
$manager->registerReportWidget('System\ReportWidgets\Status', [
'label' => 'backend::lang.dashboard.status.widget_title_default',
2014-05-14 23:24:20 +10:00
'context' => 'dashboard'
]);
});
}
/*
* Register permissions
*/
protected function registerBackendPermissions()
{
2014-10-18 11:58:50 +02:00
BackendAuth::registerCallback(function ($manager) {
2014-05-14 23:24:20 +10:00
$manager->registerPermissions('October.System', [
2014-10-19 10:58:18 +11:00
'system.manage_updates' => [
2014-10-18 11:58:50 +02:00
'label' => 'system::lang.permissions.manage_software_updates',
'tab' => 'system::lang.permissions.name'
2014-10-18 11:58:50 +02:00
],
'system.access_logs' => [
'label' => 'system::lang.permissions.access_logs',
'tab' => 'system::lang.permissions.name'
],
'system.manage_mail_settings' => [
'label' => 'system::lang.permissions.manage_mail_settings',
'tab' => 'system::lang.permissions.name'
],
2014-10-19 10:58:18 +11:00
'system.manage_mail_templates' => [
2014-10-18 11:58:50 +02:00
'label' => 'system::lang.permissions.manage_mail_templates',
'tab' => 'system::lang.permissions.name'
2015-01-22 09:06:30 +01:00
]
2014-05-14 23:24:20 +10:00
]);
});
}
2014-05-14 23:24:20 +10:00
/*
* Register settings
*/
protected function registerBackendSettings()
{
2014-10-18 11:58:50 +02:00
SettingsManager::instance()->registerCallback(function ($manager) {
2014-05-14 23:24:20 +10:00
$manager->registerSettingItems('October.System', [
'updates' => [
'label' => 'system::lang.updates.menu_label',
'description' => 'system::lang.updates.menu_description',
'category' => SettingsManager::CATEGORY_SYSTEM,
'icon' => 'icon-cloud-download',
'url' => Backend::url('system/updates'),
'permissions' => ['system.manage_updates'],
'order' => 300
],
2014-07-27 11:11:16 +11:00
'administrators' => [
'label' => 'backend::lang.user.menu_label',
'description' => 'backend::lang.user.menu_description',
'category' => SettingsManager::CATEGORY_SYSTEM,
2014-07-27 11:11:16 +11:00
'icon' => 'icon-users',
'url' => Backend::url('backend/users'),
'permissions' => ['backend.manage_users'],
'order' => 400
],
'mail_settings' => [
'label' => 'system::lang.mail.menu_label',
'description' => 'system::lang.mail.menu_description',
'category' => SettingsManager::CATEGORY_MAIL,
'icon' => 'icon-envelope',
'class' => 'System\Models\MailSettings',
'permissions' => ['system.manage_mail_settings'],
2014-07-27 11:11:16 +11:00
'order' => 600
],
'mail_templates' => [
'label' => 'system::lang.mail_templates.menu_label',
'description' => 'system::lang.mail_templates.menu_description',
'category' => SettingsManager::CATEGORY_MAIL,
'icon' => 'icon-envelope-square',
'url' => Backend::url('system/mailtemplates'),
'permissions' => ['system.manage_mail_templates'],
'order' => 610
],
'event_logs' => [
'label' => 'system::lang.event_log.menu_label',
'description' => 'system::lang.event_log.menu_description',
'category' => SettingsManager::CATEGORY_LOGS,
'icon' => 'icon-exclamation-triangle',
'url' => Backend::url('system/eventlogs'),
'permissions' => ['system.access_logs'],
2015-08-07 19:28:21 +10:00
'order' => 900,
'keywords' => 'error exception'
],
'request_logs' => [
'label' => 'system::lang.request_log.menu_label',
'description' => 'system::lang.request_log.menu_description',
'category' => SettingsManager::CATEGORY_LOGS,
'icon' => 'icon-file-o',
'url' => Backend::url('system/requestlogs'),
'permissions' => ['system.access_logs'],
2015-08-07 19:28:21 +10:00
'order' => 910,
'keywords' => '404 error'
2015-01-22 09:06:30 +01:00
]
2014-05-14 23:24:20 +10:00
]);
});
}
/**
* Register asset bundles
*/
protected function registerAssetBundles()
{
/*
* Register asset bundles
*/
CombineAssets::registerCallback(function($combiner) {
$combiner->registerBundle('~/modules/system/assets/less/styles.less');
2015-07-29 19:13:42 +10:00
$combiner->registerBundle('~/modules/system/assets/ui/storm.less');
$combiner->registerBundle('~/modules/system/assets/ui/storm.js');
});
}
/**
* Extends the validator with custom rules
*/
protected function registerValidator()
{
/*
* Allowed file extensions, as opposed to mime types.
* - extensions: png,jpg,txt
*/
Validator::extend('extensions', function($attribute, $value, $parameters) {
2015-12-05 08:47:07 +11:00
$extension = strtolower($value->getClientOriginalExtension());
return in_array($extension, $parameters);
});
Validator::replacer('extensions', function($message, $attribute, $rule, $parameters) {
return strtr($message, [':values' => implode(', ', $parameters)]);
});
}
}