From 1b24cdb1c5f8c15ed2988a90e5256172c49efdc8 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 29 Jan 2016 22:49:06 +0000 Subject: [PATCH 1/4] Improved the config system --- app/Composers/AppComposer.php | 42 +++++----- app/Composers/MetricsComposer.php | 4 +- app/Composers/ThemeComposer.php | 26 +++---- app/Config/Repository.php | 45 +++++------ app/Facades/Setting.php | 30 -------- .../Providers/ConfigServiceProvider.php | 76 ++++++++++++------- .../Dashboard/DashboardController.php | 4 +- .../Dashboard/SettingsController.php | 14 ++-- app/Http/Controllers/FeedController.php | 10 +-- app/Http/Controllers/SetupController.php | 5 +- app/Http/Controllers/StatusPageController.php | 6 +- app/Http/Controllers/SubscribeController.php | 6 +- app/Http/Middleware/ReadyForUse.php | 4 +- app/Http/Middleware/SetupAlreadyCompleted.php | 4 +- app/Http/helpers.php | 5 +- app/Presenters/AbstractPresenter.php | 39 ---------- app/Presenters/ComponentGroupPresenter.php | 4 +- app/Presenters/ComponentPresenter.php | 4 +- app/Presenters/IncidentPresenter.php | 28 +++---- app/Presenters/MetricPointPresenter.php | 4 +- app/Presenters/MetricPresenter.php | 4 +- app/Presenters/SubscriberPresenter.php | 7 +- app/Presenters/Traits/TimestampsTrait.php | 7 +- app/Repositories/Metric/MetricRepository.php | 4 +- app/Repositories/Metric/MySqlRepository.php | 4 +- app/Repositories/Metric/PgSqlRepository.php | 4 +- app/Repositories/Metric/SqliteRepository.php | 4 +- config/app.php | 1 - .../dashboard/settings/app-setup.blade.php | 4 +- .../dashboard/settings/localization.blade.php | 6 +- .../dashboard/settings/security.blade.php | 2 +- .../dashboard/settings/stylesheet.blade.php | 2 +- .../views/dashboard/settings/theme.blade.php | 2 +- resources/views/partials/footer.blade.php | 2 +- tests/Api/MetricPointTest.php | 7 +- 35 files changed, 188 insertions(+), 232 deletions(-) delete mode 100644 app/Facades/Setting.php delete mode 100644 app/Presenters/AbstractPresenter.php diff --git a/app/Composers/AppComposer.php b/app/Composers/AppComposer.php index c66ceccd..e9c7f727 100644 --- a/app/Composers/AppComposer.php +++ b/app/Composers/AppComposer.php @@ -11,7 +11,6 @@ namespace CachetHQ\Cachet\Composers; -use CachetHQ\Cachet\Facades\Setting; use GrahamCampbell\Markdown\Facades\Markdown; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\Config; @@ -27,26 +26,27 @@ class AppComposer */ public function compose(View $view) { - $support = Setting::get('show_support'); - $view->withAboutApp(Markdown::convertToHtml(Setting::get('app_about'))); - $view->withAppAnalytics(Setting::get('app_analytics')); - $view->withAppAnalyticsGoSquared(Setting::get('app_analytics_go_squared')); - $view->withAppAnalyticsPiwikUrl(Setting::get('app_analytics_piwik_url')); - $view->withAppAnalyticsPiwikSiteId(Setting::get('app_analytics_piwik_siteid')); - $view->withAppBanner(Setting::get('app_banner')); - $view->withAppBannerStyleFullWidth(Setting::get('style_fullwidth_header')); - $view->withAppBannerType(Setting::get('app_banner_type')); - $view->withAppDomain(Setting::get('app_domain')); - $view->withAppGraphs(Setting::get('display_graphs')); - $view->withAppLocale(Setting::get('app_locale')); - $view->withAppName(Setting::get('app_name')); - if ($support) { - $view->withSiteTitle(Setting::get('app_name').' | Cachet'); - } else { - $view->withSiteTitle(Setting::get('app_name')); - } - $view->withAppStylesheet(Setting::get('stylesheet')); + $view->withAboutApp(Markdown::convertToHtml(Config::get('setting.app_about'))); + $view->withAppAnalytics(Config::get('setting.app_analytics')); + $view->withAppAnalyticsGoSquared(Config::get('setting.app_analytics_go_squared')); + $view->withAppAnalyticsPiwikUrl(Config::get('setting.app_analytics_piwik_url')); + $view->withAppAnalyticsPiwikSiteId(Config::get('setting.app_analytics_piwik_siteid')); + $view->withAppBanner(Config::get('setting.app_banner')); + $view->withAppBannerStyleFullWidth(Config::get('setting.style_fullwidth_header')); + $view->withAppBannerType(Config::get('setting.app_banner_type')); + $view->withAppDomain(Config::get('setting.app_domain')); + $view->withAppGraphs(Config::get('setting.display_graphs')); + $view->withAppLocale(Config::get('setting.app_locale')); + $view->withAppStylesheet(Config::get('setting.stylesheet')); $view->withAppUrl(Config::get('app.url')); - $view->withShowSupport($support); + + $view->withAppName($name =Config::get('setting.app_name')); + $view->withShowSupport($support = Config::get('setting.show_support')); + + if ($support) { + $view->withSiteTitle(Config::get('setting.app_name').' | Cachet'); + } else { + $view->withSiteTitle(Config::get('setting.app_name')); + } } } diff --git a/app/Composers/MetricsComposer.php b/app/Composers/MetricsComposer.php index 1127f454..55230890 100644 --- a/app/Composers/MetricsComposer.php +++ b/app/Composers/MetricsComposer.php @@ -11,10 +11,10 @@ namespace CachetHQ\Cachet\Composers; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Repositories\Metric\MetricRepository; use Illuminate\Contracts\View\View; +use Illuminate\Support\Facades\Config; class MetricsComposer { @@ -46,7 +46,7 @@ class MetricsComposer { $metrics = null; $metricData = []; - if ($displayMetrics = Setting::get('display_graphs')) { + if ($displayMetrics = Config::get('setting.display_graphs')) { $metrics = Metric::where('display_chart', 1)->orderBy('id')->get(); $metrics->map(function ($metric) use (&$metricData) { diff --git a/app/Composers/ThemeComposer.php b/app/Composers/ThemeComposer.php index 5b3d0f6e..5f608a0a 100644 --- a/app/Composers/ThemeComposer.php +++ b/app/Composers/ThemeComposer.php @@ -11,8 +11,8 @@ namespace CachetHQ\Cachet\Composers; -use CachetHQ\Cachet\Facades\Setting; use Illuminate\Contracts\View\View; +use Illuminate\Support\Facades\Config; class ThemeComposer { @@ -26,17 +26,17 @@ class ThemeComposer public function compose(View $view) { // Theme colors. - $view->withThemeBackgroundColor(Setting::get('style_background_color', '#F0F3F4')); - $view->withThemeBackgroundFills(Setting::get('style_background_fills', '#FFFFFF')); - $view->withThemeBannerBackgroundColor(Setting::get('style_banner_background_color', '')); - $view->withThemeBannerPadding(Setting::get('style_banner_padding', '40px 0')); - $view->withThemeTextColor(Setting::get('style_text_color', '#333333')); - $view->withThemeReds(Setting::get('style_reds', '#ff6f6f')); - $view->withThemeBlues(Setting::get('style_blues', '#3498db')); - $view->withThemeGreens(Setting::get('style_greens', '#7ED321')); - $view->withThemeYellows(Setting::get('style_yellows', '#F7CA18')); - $view->withThemeOranges(Setting::get('style_oranges', '#FF8800')); - $view->withThemeMetrics(Setting::get('style_metrics', '#0dccc0')); - $view->withThemeLinks(Setting::get('style_links', '#7ED321')); + $view->withThemeBackgroundColor(Config::get('setting.style_background_color', '#F0F3F4')); + $view->withThemeBackgroundFills(Config::get('setting.style_background_fills', '#FFFFFF')); + $view->withThemeBannerBackgroundColor(Config::get('setting.style_banner_background_color', '')); + $view->withThemeBannerPadding(Config::get('setting.style_banner_padding', '40px 0')); + $view->withThemeTextColor(Config::get('setting.style_text_color', '#333333')); + $view->withThemeReds(Config::get('setting.style_reds', '#ff6f6f')); + $view->withThemeBlues(Config::get('setting.style_blues', '#3498db')); + $view->withThemeGreens(Config::get('setting.style_greens', '#7ED321')); + $view->withThemeYellows(Config::get('setting.style_yellows', '#F7CA18')); + $view->withThemeOranges(Config::get('setting.style_oranges', '#FF8800')); + $view->withThemeMetrics(Config::get('setting.style_metrics', '#0dccc0')); + $view->withThemeLinks(Config::get('setting.style_links', '#7ED321')); } } diff --git a/app/Config/Repository.php b/app/Config/Repository.php index 2c069990..d245318c 100644 --- a/app/Config/Repository.php +++ b/app/Config/Repository.php @@ -23,11 +23,11 @@ class Repository protected $model; /** - * Cache of the settings. + * Is the config state stale? * - * @var array|null + * @var bool */ - protected $settings; + protected $stale = false; /** * Create a new settings service instance. @@ -44,26 +44,15 @@ class Repository /** * Returns a setting from the database. * - * @param string $name - * @param string|null $default - * - * @return string|null + * @return array */ - public function get($name, $default = null) + public function all() { - if (!$this->settings) { - $this->settings = $this->model->all()->pluck('value', 'name'); - } - - if (!empty($this->settings[$name])) { - return $this->settings[$name]; - } - - return $default; + return $this->model->all(['name', 'value'])->pluck('value', 'name')->toArray(); } /** - * Creates or updates a setting value. + * Updates a setting value. * * @param string $name * @param string|null $value @@ -72,18 +61,22 @@ class Repository */ public function set($name, $value) { + $this->stale = true; + if ($value === null) { $this->model->where('name', $name)->delete(); - - if ($this->settings && isset($this->settings[$name])) { - unset($this->settings[$name]); - } } else { $this->model->updateOrCreate(compact('name'), compact('value')); - - if ($this->settings) { - $this->settings[$name] = $value; - } } } + + /** + * Is the config state stale? + * + * @return bool + */ + public function stale() + { + return $this->stale; + } } diff --git a/app/Facades/Setting.php b/app/Facades/Setting.php deleted file mode 100644 index 8dc9a4ae..00000000 --- a/app/Facades/Setting.php +++ /dev/null @@ -1,30 +0,0 @@ -app->config->get('cors.defaults.allowedOrigins'); - $allowedOrigins[] = Setting::get('app_domain'); - - // Add our allowed domains too. - if ($allowedDomains = Setting::get('allowed_domains')) { - $domains = explode(',', $allowedDomains); - foreach ($domains as $domain) { - $allowedOrigins[] = $domain; - } + if ($this->app->configurationIsCached()) { + if ($this->app->environment() === 'production') { + $this->app->terminating(function () { + if ($this->app->setting->stale()) { + $this->app->make(Kernel::class)->call('config:cache'); + } + }); } else { - $allowedOrigins[] = $this->app->config->get('app.url'); + $this->app->make(Kernel::class)->call('config:clear'); } - $this->app->config->set('cors.paths.api/v1/*.allowedOrigins', $allowedOrigins); - } catch (Exception $e) { - // Don't throw any errors, we may not be setup yet. + return; } - // Override default app values. - $this->app->config->set('app.url', $appDomain ?: $this->app->config->get('app.url')); - $this->app->config->set('app.locale', $appLocale ?: $this->app->config->get('app.locale')); - $this->app->config->set('cachet.timezone', $appTimezone ?: $this->app->config->get('cachet.timezone')); + try { + $this->app->config->set('setting', $this->app->setting->all()); + } catch (Exception $e) { + // + } - // Set custom lang. - $this->app->translator->setLocale($appLocale); + if ($appDomain = $this->app->config->get('setting.app_domain')) { + $this->app->config->set('app.url', $appDomain); + } + + if ($appLocale = $this->app->config->get('setting.app.locale')) { + $this->app->config->set('app.locale', $appLocale); + $this->app->translator->setLocale($appLocale); + } + + if ($appTimezone = $this->app->config->get('setting.app_timezone')) { + $this->app->config->set('cachet.timezone', $appTimezone); + } + + $allowedOrigins = $this->app->config->get('cors.defaults.allowedOrigins'); + + if ($allowedDomains = $this->app->config->get('setting.allowed_domains')) { + $domains = explode(',', $allowedDomains); + foreach ($domains as $domain) { + $allowedOrigins[] = $domain; + } + } else { + $allowedOrigins[] = $this->app->config->get('app.url'); + } + + $this->app->config->set('cors.paths.api/v1/*.allowedOrigins', $allowedOrigins); + + if ($this->app->environment() === 'production') { + $this->app->terminating(function () { + $this->app->make(Kernel::class)->call('config:cache'); + }); + } } /** diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index e9913236..ec103531 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -11,11 +11,11 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\Subscriber; use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\View; use Jenssegers\Date\Date; @@ -43,7 +43,7 @@ class DashboardController extends Controller public function __construct() { $this->startDate = new Date(); - $this->dateTimeZone = Setting::get('app_timezone'); + $this->dateTimeZone = Config::get('cachet.timezone'); } /** diff --git a/app/Http/Controllers/Dashboard/SettingsController.php b/app/Http/Controllers/Dashboard/SettingsController.php index 36b7facd..8592290c 100644 --- a/app/Http/Controllers/Dashboard/SettingsController.php +++ b/app/Http/Controllers/Dashboard/SettingsController.php @@ -11,11 +11,11 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\User; use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Session; @@ -102,7 +102,7 @@ class SettingsController extends Controller return View::make('dashboard.settings.app-setup') ->withPageTitle(trans('dashboard.settings.app-setup.app-setup').' - '.trans('dashboard.dashboard')) ->withSubMenu($this->subMenu) - ->withRawAppAbout(Setting::get('app_about')); + ->withRawAppAbout(Config::get('setting.app_about')); } /** @@ -197,8 +197,10 @@ class SettingsController extends Controller { $redirectUrl = Session::get('redirect_to', route('dashboard.settings.setup')); + $setting = app('setting'); + if (Binput::get('remove_banner') === '1') { - Setting::set('app_banner', null); + $setting->set('app_banner', null); } if (Binput::hasFile('app_banner')) { @@ -221,10 +223,10 @@ class SettingsController extends Controller } // Store the banner. - Setting::set('app_banner', base64_encode(file_get_contents($file->getRealPath()))); + $setting->set('app_banner', base64_encode(file_get_contents($file->getRealPath()))); // Store the banner type. - Setting::set('app_banner_type', $file->getMimeType()); + $setting->set('app_banner_type', $file->getMimeType()); } try { @@ -233,7 +235,7 @@ class SettingsController extends Controller $settingValue = rtrim($settingValue, '/'); } - Setting::set($settingName, $settingValue); + $setting->set($settingName, $settingValue); } } catch (Exception $e) { return Redirect::to($redirectUrl)->withErrors(trans('dashboard.settings.edit.failure')); diff --git a/app/Http/Controllers/FeedController.php b/app/Http/Controllers/FeedController.php index 73f213bb..68439235 100644 --- a/app/Http/Controllers/FeedController.php +++ b/app/Http/Controllers/FeedController.php @@ -11,11 +11,11 @@ namespace CachetHQ\Cachet\Http\Controllers; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Incident; use GrahamCampbell\Markdown\Facades\Markdown; use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Str; use Roumen\Feed\Facades\Feed; @@ -36,9 +36,9 @@ class FeedController extends Controller public function __construct() { $this->feed = Feed::make(); - $this->feed->title = Setting::get('app_name'); + $this->feed->title = Config::get('setting.app_name'); $this->feed->description = trans('cachet.feed'); - $this->feed->link = Str::canonicalize(Setting::get('app_domain')); + $this->feed->link = Str::canonicalize(Config::get('setting.app_domain')); $this->feed->setDateFormat('datetime'); } @@ -63,7 +63,7 @@ class FeedController extends Controller */ public function rssAction(ComponentGroup $group = null) { - $this->feed->lang = Setting::get('app_locale'); + $this->feed->lang = Config::get('setting.app_locale'); return $this->feedAction($group, true); } @@ -103,7 +103,7 @@ class FeedController extends Controller { $this->feed->add( $incident->name, - Setting::get('app_name'), + Config::get('setting.app_name'), Str::canonicalize(route('incident', ['id' => $incident->id])), $isRss ? $incident->created_at->toRssString() : $incident->created_at->toAtomString(), $isRss ? $incident->message : Markdown::convertToHtml($incident->message) diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 4426d67f..75a01abd 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -11,7 +11,6 @@ namespace CachetHQ\Cachet\Http\Controllers; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\User; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Routing\Controller; @@ -179,10 +178,12 @@ class SetupController extends Controller Auth::login($user); + $setting = app('setting'); + $settings = array_pull($postData, 'settings'); foreach ($settings as $settingName => $settingValue) { - Setting::set($settingName, $settingValue); + $setting->set($settingName, $settingValue); } $envData = array_pull($postData, 'env'); diff --git a/app/Http/Controllers/StatusPageController.php b/app/Http/Controllers/StatusPageController.php index 1161b0ea..e83e0d78 100644 --- a/app/Http/Controllers/StatusPageController.php +++ b/app/Http/Controllers/StatusPageController.php @@ -11,12 +11,12 @@ namespace CachetHQ\Cachet\Http\Controllers; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\View; use Jenssegers\Date\Date; @@ -47,14 +47,14 @@ class StatusPageController extends Controller } } - $daysToShow = Setting::get('app_incident_days', 0) - 1; + $daysToShow = Config::get('setting.app_incident_days', 0) - 1; if ($daysToShow < 0) { $daysToShow = 0; $incidentDays = []; } else { $incidentDays = range(0, $daysToShow); } - $dateTimeZone = Setting::get('app_timezone'); + $dateTimeZone = Config::get('cachet.timezone'); $incidentVisibility = Auth::check() ? 0 : 1; diff --git a/app/Http/Controllers/SubscribeController.php b/app/Http/Controllers/SubscribeController.php index 105b92e6..3a68fc43 100644 --- a/app/Http/Controllers/SubscribeController.php +++ b/app/Http/Controllers/SubscribeController.php @@ -16,13 +16,13 @@ use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand; use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriberCommand; use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriptionCommand; use CachetHQ\Cachet\Bus\Commands\Subscriber\VerifySubscriberCommand; -use CachetHQ\Cachet\Bus\Exceptions\Subscriber\AlreadySubscribedException; -use CachetHQ\Cachet\Facades\Setting; +use CachetHQ\Cachet\Exceptions\AlreadySubscribedException; use CachetHQ\Cachet\Models\Subscriber; use CachetHQ\Cachet\Models\Subscription; use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Markdown\Facades\Markdown; use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -43,7 +43,7 @@ class SubscribeController extends Controller public function showSubscribe() { return View::make('subscribe') - ->withAboutApp(Markdown::convertToHtml(Setting::get('app_about'))); + ->withAboutApp(Markdown::convertToHtml(Config::get('setting.app_about'))); } /** diff --git a/app/Http/Middleware/ReadyForUse.php b/app/Http/Middleware/ReadyForUse.php index 44cdfa79..7cdc4bcd 100644 --- a/app/Http/Middleware/ReadyForUse.php +++ b/app/Http/Middleware/ReadyForUse.php @@ -11,9 +11,9 @@ namespace CachetHQ\Cachet\Http\Middleware; -use CachetHQ\Cachet\Facades\Setting; use Closure; use Exception; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; class ReadyForUse @@ -29,7 +29,7 @@ class ReadyForUse public function handle($request, Closure $next) { try { - if (!Setting::get('app_name')) { + if (!Config::get('setting.app_name')) { return Redirect::to('setup'); } } catch (Exception $e) { diff --git a/app/Http/Middleware/SetupAlreadyCompleted.php b/app/Http/Middleware/SetupAlreadyCompleted.php index 3149c121..0933e8d3 100644 --- a/app/Http/Middleware/SetupAlreadyCompleted.php +++ b/app/Http/Middleware/SetupAlreadyCompleted.php @@ -11,8 +11,8 @@ namespace CachetHQ\Cachet\Http\Middleware; -use CachetHQ\Cachet\Facades\Setting; use Closure; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; class SetupAlreadyCompleted @@ -27,7 +27,7 @@ class SetupAlreadyCompleted */ public function handle($request, Closure $next) { - if (Setting::get('app_name')) { + if (Config::get('setting.app_name')) { return Redirect::to('dashboard'); } diff --git a/app/Http/helpers.php b/app/Http/helpers.php index 8aca0585..6338fb4f 100644 --- a/app/Http/helpers.php +++ b/app/Http/helpers.php @@ -9,7 +9,6 @@ * file that was distributed with this source code. */ -use CachetHQ\Cachet\Facades\Setting; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Request; use Jenssegers\Date\Date; @@ -47,7 +46,7 @@ if (!function_exists('formatted_date')) { */ function formatted_date($date) { - $dateFormat = Setting::get('date_format', 'jS F Y'); + $dateFormat = Config::get('setting.date_format', 'jS F Y'); return (new Date($date))->format($dateFormat); } @@ -61,7 +60,7 @@ if (!function_exists('subscribers_enabled')) { */ function subscribers_enabled() { - $isEnabled = Setting::get('enable_subscribers', false); + $isEnabled = Config::get('setting.enable_subscribers', false); $mailAddress = Config::get('mail.from.address', false); $mailFrom = Config::get('mail.from.name', false); diff --git a/app/Presenters/AbstractPresenter.php b/app/Presenters/AbstractPresenter.php deleted file mode 100644 index a6cf45f8..00000000 --- a/app/Presenters/AbstractPresenter.php +++ /dev/null @@ -1,39 +0,0 @@ -setting = app('setting'); - } -} diff --git a/app/Presenters/ComponentGroupPresenter.php b/app/Presenters/ComponentGroupPresenter.php index 0c97b35e..17fc1bfb 100644 --- a/app/Presenters/ComponentGroupPresenter.php +++ b/app/Presenters/ComponentGroupPresenter.php @@ -12,9 +12,11 @@ namespace CachetHQ\Cachet\Presenters; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; +use Illuminate\Contracts\Support\Arrayable; +use McCool\LaravelAutoPresenter\BasePresenter; use McCool\LaravelAutoPresenter\Facades\AutoPresenter; -class ComponentGroupPresenter extends AbstractPresenter +class ComponentGroupPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php index 3fbb686c..06f17345 100644 --- a/app/Presenters/ComponentPresenter.php +++ b/app/Presenters/ComponentPresenter.php @@ -12,8 +12,10 @@ namespace CachetHQ\Cachet\Presenters; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; +use Illuminate\Contracts\Support\Arrayable; +use McCool\LaravelAutoPresenter\BasePresenter; -class ComponentPresenter extends AbstractPresenter +class ComponentPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; diff --git a/app/Presenters/IncidentPresenter.php b/app/Presenters/IncidentPresenter.php index 149c6a0b..05ae6dee 100644 --- a/app/Presenters/IncidentPresenter.php +++ b/app/Presenters/IncidentPresenter.php @@ -11,12 +11,14 @@ namespace CachetHQ\Cachet\Presenters; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; use GrahamCampbell\Markdown\Facades\Markdown; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Support\Facades\Config; use Jenssegers\Date\Date; +use McCool\LaravelAutoPresenter\BasePresenter; -class IncidentPresenter extends AbstractPresenter +class IncidentPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; @@ -38,7 +40,7 @@ class IncidentPresenter extends AbstractPresenter public function created_at_diff() { return (new Date($this->wrappedObject->created_at)) - ->setTimezone($this->setting->get('app_timezone')) + ->setTimezone(Config::get('cachet.timezone')) ->diffForHumans(); } @@ -50,8 +52,8 @@ class IncidentPresenter extends AbstractPresenter public function created_at_formatted() { return ucfirst((new Date($this->wrappedObject->created_at)) - ->setTimezone($this->setting->get('app_timezone')) - ->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s'))); + ->setTimezone(Config::get('cachet.timezone')) + ->format(Config::get('setting.incident_date_format', 'l jS F Y H:i:s'))); } /** @@ -61,7 +63,7 @@ class IncidentPresenter extends AbstractPresenter */ public function created_at_datetimepicker() { - return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i'); + return $this->wrappedObject->created_at->setTimezone(Config::get('cachet.timezone'))->format('d/m/Y H:i'); } /** @@ -71,7 +73,7 @@ class IncidentPresenter extends AbstractPresenter */ public function created_at_iso() { - return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String(); + return $this->wrappedObject->created_at->setTimezone(Config::get('cachet.timezone'))->toISO8601String(); } /** @@ -82,7 +84,7 @@ class IncidentPresenter extends AbstractPresenter public function scheduled_at() { return (new Date($this->wrappedObject->scheduled_at)) - ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); } /** @@ -93,7 +95,7 @@ class IncidentPresenter extends AbstractPresenter public function scheduled_at_diff() { return (new Date($this->wrappedObject->scheduled_at)) - ->setTimezone($this->setting->get('app_timezone')) + ->setTimezone(Config::get('cachet.timezone')) ->diffForHumans(); } @@ -105,8 +107,8 @@ class IncidentPresenter extends AbstractPresenter public function scheduled_at_formatted() { return ucfirst((new Date($this->wrappedObject->scheduled_at)) - ->setTimezone($this->setting->get('app_timezone')) - ->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s'))); + ->setTimezone(Config::get('cachet.timezone')) + ->format(Config::get('setting.incident_date_format', 'l jS F Y H:i:s'))); } /** @@ -116,7 +118,7 @@ class IncidentPresenter extends AbstractPresenter */ public function scheduled_at_iso() { - return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String(); + return $this->wrappedObject->scheduled_at->setTimezone(Config::get('cachet.timezone'))->toISO8601String(); } /** @@ -126,7 +128,7 @@ class IncidentPresenter extends AbstractPresenter */ public function scheduled_at_datetimepicker() { - return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i'); + return $this->wrappedObject->scheduled_at->setTimezone(Config::get('cachet.timezone'))->format('d/m/Y H:i'); } /** diff --git a/app/Presenters/MetricPointPresenter.php b/app/Presenters/MetricPointPresenter.php index 39971d68..5cbfdd60 100644 --- a/app/Presenters/MetricPointPresenter.php +++ b/app/Presenters/MetricPointPresenter.php @@ -12,8 +12,10 @@ namespace CachetHQ\Cachet\Presenters; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; +use Illuminate\Contracts\Support\Arrayable; +use McCool\LaravelAutoPresenter\BasePresenter; -class MetricPointPresenter extends AbstractPresenter +class MetricPointPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; diff --git a/app/Presenters/MetricPresenter.php b/app/Presenters/MetricPresenter.php index 79d9eef9..66a7cdda 100644 --- a/app/Presenters/MetricPresenter.php +++ b/app/Presenters/MetricPresenter.php @@ -12,8 +12,10 @@ namespace CachetHQ\Cachet\Presenters; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; +use Illuminate\Contracts\Support\Arrayable; +use McCool\LaravelAutoPresenter\BasePresenter; -class MetricPresenter extends AbstractPresenter +class MetricPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; diff --git a/app/Presenters/SubscriberPresenter.php b/app/Presenters/SubscriberPresenter.php index 0e313bec..87b3be4e 100644 --- a/app/Presenters/SubscriberPresenter.php +++ b/app/Presenters/SubscriberPresenter.php @@ -12,9 +12,12 @@ namespace CachetHQ\Cachet\Presenters; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Support\Facades\Config; use Jenssegers\Date\Date; +use McCool\LaravelAutoPresenter\BasePresenter; -class SubscriberPresenter extends AbstractPresenter +class SubscriberPresenter extends BasePresenter implements Arrayable { use TimestampsTrait; @@ -26,7 +29,7 @@ class SubscriberPresenter extends AbstractPresenter public function verified_at() { return (new Date($this->wrappedObject->verified_at)) - ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); } /** diff --git a/app/Presenters/Traits/TimestampsTrait.php b/app/Presenters/Traits/TimestampsTrait.php index 1ec048d4..655e37ba 100644 --- a/app/Presenters/Traits/TimestampsTrait.php +++ b/app/Presenters/Traits/TimestampsTrait.php @@ -11,6 +11,7 @@ namespace CachetHQ\Cachet\Presenters\Traits; +use Illuminate\Support\Facades\Config; use Jenssegers\Date\Date; trait TimestampsTrait @@ -23,7 +24,7 @@ trait TimestampsTrait public function created_at() { return (new Date($this->wrappedObject->created_at)) - ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); } /** @@ -34,7 +35,7 @@ trait TimestampsTrait public function updated_at() { return (new Date($this->wrappedObject->updated_at)) - ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); } /** @@ -45,6 +46,6 @@ trait TimestampsTrait public function deleted_at() { return (new Date($this->wrappedObject->deleted_at)) - ->setTimezone($this->setting->get('app_timezone'))->toDateTimeString(); + ->setTimezone(Config::get('cachet.timezone'))->toDateTimeString(); } } diff --git a/app/Repositories/Metric/MetricRepository.php b/app/Repositories/Metric/MetricRepository.php index 7f1d6f54..7fa24e40 100644 --- a/app/Repositories/Metric/MetricRepository.php +++ b/app/Repositories/Metric/MetricRepository.php @@ -11,9 +11,9 @@ namespace CachetHQ\Cachet\Repositories\Metric; -use CachetHQ\Cachet\Facades\Setting as SettingFacade; use CachetHQ\Cachet\Models\Metric; use DateInterval; +use Illuminate\Support\Facades\Config; use Jenssegers\Date\Date; class MetricRepository @@ -40,7 +40,7 @@ class MetricRepository public function __construct(MetricInterface $repository) { $this->repository = $repository; - $this->dateTimeZone = SettingFacade::get('app_timezone'); + $this->dateTimeZone = Config::get('cachet.timezone'); } /** diff --git a/app/Repositories/Metric/MySqlRepository.php b/app/Repositories/Metric/MySqlRepository.php index 261c6b20..ba3a7a1f 100644 --- a/app/Repositories/Metric/MySqlRepository.php +++ b/app/Repositories/Metric/MySqlRepository.php @@ -11,9 +11,9 @@ namespace CachetHQ\Cachet\Repositories\Metric; -use CachetHQ\Cachet\Facades\Setting as SettingFacade; use CachetHQ\Cachet\Models\Metric; use DateInterval; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Jenssegers\Date\Date; @@ -33,7 +33,7 @@ class MySqlRepository implements MetricInterface */ public function __construct() { - $this->dateTimeZone = SettingFacade::get('app_timezone'); + $this->dateTimeZone = Config::get('cachet.timezone'); } /** diff --git a/app/Repositories/Metric/PgSqlRepository.php b/app/Repositories/Metric/PgSqlRepository.php index adaa43f6..3000c8e8 100644 --- a/app/Repositories/Metric/PgSqlRepository.php +++ b/app/Repositories/Metric/PgSqlRepository.php @@ -11,9 +11,9 @@ namespace CachetHQ\Cachet\Repositories\Metric; -use CachetHQ\Cachet\Facades\Setting as SettingFacade; use CachetHQ\Cachet\Models\Metric; use DateInterval; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Jenssegers\Date\Date; @@ -33,7 +33,7 @@ class PgSqlRepository implements MetricInterface */ public function __construct() { - $this->dateTimeZone = SettingFacade::get('app_timezone'); + $this->dateTimeZone = Config::get('cachet.timezone'); } /** diff --git a/app/Repositories/Metric/SqliteRepository.php b/app/Repositories/Metric/SqliteRepository.php index d002865c..c5d9ab73 100644 --- a/app/Repositories/Metric/SqliteRepository.php +++ b/app/Repositories/Metric/SqliteRepository.php @@ -11,9 +11,9 @@ namespace CachetHQ\Cachet\Repositories\Metric; -use CachetHQ\Cachet\Facades\Setting as SettingFacade; use CachetHQ\Cachet\Models\Metric; use DateInterval; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Jenssegers\Date\Date; @@ -33,7 +33,7 @@ class SqliteRepository implements MetricInterface */ public function __construct() { - $this->dateTimeZone = SettingFacade::get('app_timezone'); + $this->dateTimeZone = Config::get('cachet.timezone'); } /** diff --git a/config/app.php b/config/app.php index 078a6e2b..3540f318 100644 --- a/config/app.php +++ b/config/app.php @@ -232,7 +232,6 @@ return [ 'View' => 'Illuminate\Support\Facades\View', 'Binput' => 'GrahamCampbell\Binput\Facades\Binput', - 'Setting' => 'CachetHQ\Cachet\Facades\Setting', 'Str' => 'Illuminate\Support\Str', ], diff --git a/resources/views/dashboard/settings/app-setup.blade.php b/resources/views/dashboard/settings/app-setup.blade.php index 160d3d8c..a301f520 100644 --- a/resources/views/dashboard/settings/app-setup.blade.php +++ b/resources/views/dashboard/settings/app-setup.blade.php @@ -47,7 +47,7 @@
- +
@@ -56,7 +56,7 @@
diff --git a/resources/views/dashboard/settings/localization.blade.php b/resources/views/dashboard/settings/localization.blade.php index 23ee99f6..d994e909 100644 --- a/resources/views/dashboard/settings/localization.blade.php +++ b/resources/views/dashboard/settings/localization.blade.php @@ -26,7 +26,7 @@ @foreach($timezones as $region => $list) @foreach($list as $timezone => $name) - @endforeach @@ -43,7 +43,7 @@ {{ trans('forms.settings.localization.date-format') }} - + @@ -54,7 +54,7 @@ {{ trans('forms.settings.localization.incident-date-format') }} - + diff --git a/resources/views/dashboard/settings/security.blade.php b/resources/views/dashboard/settings/security.blade.php index 8b333ef3..36be4dc9 100644 --- a/resources/views/dashboard/settings/security.blade.php +++ b/resources/views/dashboard/settings/security.blade.php @@ -21,7 +21,7 @@
- +
{{ trans('forms.settings.security.allowed-domains-help') }}
diff --git a/resources/views/dashboard/settings/stylesheet.blade.php b/resources/views/dashboard/settings/stylesheet.blade.php index 3a39703b..e98503f9 100644 --- a/resources/views/dashboard/settings/stylesheet.blade.php +++ b/resources/views/dashboard/settings/stylesheet.blade.php @@ -21,7 +21,7 @@
- +
diff --git a/resources/views/dashboard/settings/theme.blade.php b/resources/views/dashboard/settings/theme.blade.php index 7e17bb1b..9fee5845 100644 --- a/resources/views/dashboard/settings/theme.blade.php +++ b/resources/views/dashboard/settings/theme.blade.php @@ -136,7 +136,7 @@
diff --git a/resources/views/partials/footer.blade.php b/resources/views/partials/footer.blade.php index 63f60529..bf0df995 100644 --- a/resources/views/partials/footer.blade.php +++ b/resources/views/partials/footer.blade.php @@ -8,7 +8,7 @@
    - @if($current_user || Setting::get('dashboard_login_link')) + @if($current_user || Config::get('setting.dashboard_login_link'))
  • {{ trans('dashboard.dashboard') }}
  • diff --git a/tests/Api/MetricPointTest.php b/tests/Api/MetricPointTest.php index 5fbeaac2..65874907 100644 --- a/tests/Api/MetricPointTest.php +++ b/tests/Api/MetricPointTest.php @@ -88,16 +88,15 @@ class MetricPointTest extends AbstractApiTestCase $timezone = 'America/Mexico_City'; $metric = factory('CachetHQ\Cachet\Models\Metric')->create(); - $timestamp = Carbon::now()->timezone($timezone)->timestamp; - $datetime = Carbon::now()->toDateTimeString(); + $datetime = Carbon::now()->timezone($timezone); $metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([ 'metric_id' => $metric->id, ]); $postData = $metricPoint->toArray(); - $postData['timestamp'] = $timestamp; + $postData['timestamp'] = $datetime->timestamp; $this->post("/api/v1/metrics/{$metric->id}/points", $postData, ['Time-Zone' => $timezone]); - $this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime]); + $this->seeJson(['value' => $metricPoint->value, 'created_at' => $datetime->toDateTimeString()]); } public function testPutMetricPoint() From dc8b8eae78427956379a5a5d6ceb0cc8ccfe82f5 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 29 Jan 2016 23:01:07 +0000 Subject: [PATCH 2/4] Removed config caching system for now --- .../Providers/ConfigServiceProvider.php | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/app/Foundation/Providers/ConfigServiceProvider.php b/app/Foundation/Providers/ConfigServiceProvider.php index 363c6373..866742eb 100644 --- a/app/Foundation/Providers/ConfigServiceProvider.php +++ b/app/Foundation/Providers/ConfigServiceProvider.php @@ -14,7 +14,6 @@ namespace CachetHQ\Cachet\Foundation\Providers; use CachetHQ\Cachet\Config\Repository; use CachetHQ\Cachet\Models\Setting as SettingModel; use Exception; -use Illuminate\Contracts\Console\Kernel; use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; @@ -27,20 +26,6 @@ class ConfigServiceProvider extends ServiceProvider */ public function boot() { - if ($this->app->configurationIsCached()) { - if ($this->app->environment() === 'production') { - $this->app->terminating(function () { - if ($this->app->setting->stale()) { - $this->app->make(Kernel::class)->call('config:cache'); - } - }); - } else { - $this->app->make(Kernel::class)->call('config:clear'); - } - - return; - } - try { $this->app->config->set('setting', $this->app->setting->all()); } catch (Exception $e) { @@ -72,12 +57,6 @@ class ConfigServiceProvider extends ServiceProvider } $this->app->config->set('cors.paths.api/v1/*.allowedOrigins', $allowedOrigins); - - if ($this->app->environment() === 'production') { - $this->app->terminating(function () { - $this->app->make(Kernel::class)->call('config:cache'); - }); - } } /** From 6869d6b2574111937a72280070fa837f04ab532a Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 29 Jan 2016 23:01:41 +0000 Subject: [PATCH 3/4] Fixed env writing functions --- app/Http/Controllers/SetupController.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 75a01abd..0971a7dd 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -12,6 +12,8 @@ namespace CachetHQ\Cachet\Http\Controllers; use CachetHQ\Cachet\Models\User; +use Dotenv\Dotenv; +use Dotenv\Exception\InvalidPathException; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Auth; @@ -219,13 +221,16 @@ class SetupController extends Controller */ protected function writeEnv($key, $value) { - static $path = null; + $path = app()->environmentFile(); + + try { + (new Dotenv(app()->environmentPath(), $path))->load(); - if ($path === null || ($path !== null && file_exists($path))) { - $path = base_path('.env'); file_put_contents($path, str_replace( env(strtoupper($key)), $value, file_get_contents($path) )); + } catch (InvalidPathException $e) { + // } } @@ -238,7 +243,9 @@ class SetupController extends Controller { $key = str_random(32); - $path = base_path('.env'); + $path = app()->environmentFile(); + + (new Dotenv(app()->environmentPath(), $path))->load(); file_put_contents($path, str_replace( Config::get('app.key'), $key, file_get_contents($path) From b968c38605b27d0b0fb5b3ea8e84e453612b6075 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 29 Jan 2016 18:03:09 -0500 Subject: [PATCH 4/4] Applied fixes from StyleCI --- app/Composers/AppComposer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Composers/AppComposer.php b/app/Composers/AppComposer.php index e9c7f727..f039c0ad 100644 --- a/app/Composers/AppComposer.php +++ b/app/Composers/AppComposer.php @@ -40,7 +40,7 @@ class AppComposer $view->withAppStylesheet(Config::get('setting.stylesheet')); $view->withAppUrl(Config::get('app.url')); - $view->withAppName($name =Config::get('setting.app_name')); + $view->withAppName($name = Config::get('setting.app_name')); $view->withShowSupport($support = Config::get('setting.show_support')); if ($support) {