implements a modular view system

This commit is contained in:
Connor S. Parks
2016-08-18 13:33:43 +01:00
parent 445c1ed2d0
commit fa1b32c11a
30 changed files with 809 additions and 181 deletions
+71
View File
@@ -0,0 +1,71 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Composers;
use CachetHQ\Cachet\Services\Modules\Manager;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\View;
class ModuleComposer
{
/**
* The application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* The modules manager instance.
*
* @var \CachetHQ\Cachet\Services\Modules\Manager
*/
protected $manager;
/**
* Create a new modules composer.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \CachetHQ\Cachet\Services\Modules\Manager $manager
*
* @return void
*/
public function __construct(Application $app, Manager $manager)
{
$this->app = $app;
$this->manager = $manager;
}
/**
* Bind data to the view.
*
* @param \Illuminate\Contracts\View\View $view
*
* @return void
*/
public function compose(View $view)
{
$key = $view->getName();
$view->with('view', $key);
$modules = "view.modules: {$key}";
$groups = "view.groups: {$key}";
$modules = $this->app->bound($modules) ? $this->app[$modules] : [];
$groups = $this->app->bound($groups) ? $this->app[$groups] : [];
$modules = $this->manager->groupModules($modules, $groups);
$view->withModules($modules);
}
}
@@ -9,40 +9,20 @@
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Composers;
namespace CachetHQ\Cachet\Composers\Modules;
use CachetHQ\Cachet\Integrations\Core\System;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use Illuminate\Contracts\View\View;
/**
* This is the status page composer.
*
* @author James Brooks <james@alt-three.com>
* @author Connor S. Parks <connor@connorvg.tv>
*/
class StatusPageComposer
class ComponentsComposer
{
/**
* The system instance.
*
* @var \CachetHQ\Cachet\Integrations\Contracts\System
*/
protected $system;
/**
* Create a new status page composer instance.
*
* @param \CachetHQ\Cachet\Integrations\Contracts\System $system
*
* @return void
*/
public function __construct(System $system)
{
$this->system = $system;
}
/**
* Index page view composer.
*
@@ -52,19 +32,12 @@ class StatusPageComposer
*/
public function compose(View $view)
{
$status = $this->system->getStatus();
// Scheduled maintenance code.
$scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get();
// Component & Component Group lists.
$usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id');
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();
$view->with($status)
->withComponentGroups($componentGroups)
->withUngroupedComponents($ungroupedComponents)
->withScheduledMaintenance($scheduledMaintenance);
$view->withComponentGroups($componentGroups)
->withUngroupedComponents($ungroupedComponents);
}
}
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Composers;
namespace CachetHQ\Cachet\Composers\Modules;
use CachetHQ\Cachet\Models\Metric;
use Illuminate\Contracts\Config\Repository;
@@ -0,0 +1,38 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Composers\Modules;
use CachetHQ\Cachet\Models\Incident;
use Illuminate\Contracts\View\View;
/**
* This is the status page composer.
*
* @author James Brooks <james@alt-three.com>
* @author Connor S. Parks <connor@connorvg.tv>
*/
class ScheduledComposer
{
/**
* Index page view composer.
*
* @param \Illuminate\Contracts\View\View $view
*
* @return void
*/
public function compose(View $view)
{
$scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get();
$view->withScheduledMaintenance($scheduledMaintenance);
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Composers\Modules;
use CachetHQ\Cachet\Integrations\Contracts\System;
use Illuminate\Contracts\View\View;
/**
* This is the status page composer.
*
* @author James Brooks <james@alt-three.com>
* @author Connor S. Parks <connor@connorvg.tv>
*/
class StatusComposer
{
/**
* The system instance.
*
* @var \CachetHQ\Cachet\Integrations\Contracts\System
*/
protected $system;
/**
* Create a new status page composer instance.
*
* @param \CachetHQ\Cachet\Integrations\Contracts\System $system
*
* @return void
*/
public function __construct(System $system)
{
$this->system = $system;
}
/**
* Index page view composer.
*
* @param \Illuminate\Contracts\View\View $view
*
* @return void
*/
public function compose(View $view)
{
$view->with($this->system->getStatus());
}
}
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Composers\Modules;
use Illuminate\Contracts\View\View;
/**
* This is the status page composer.
*
* @author James Brooks <james@alt-three.com>
* @author Connor S. Parks <connor@connorvg.tv>
*/
class TimelineComposer
{
/**
* Index page view composer.
*
* @param \Illuminate\Contracts\View\View $view
*
* @return void
*/
public function compose(View $view)
{
// ...
}
}
@@ -14,8 +14,12 @@ namespace CachetHQ\Cachet\Foundation\Providers;
use CachetHQ\Cachet\Composers\AppComposer;
use CachetHQ\Cachet\Composers\CurrentUserComposer;
use CachetHQ\Cachet\Composers\DashboardComposer;
use CachetHQ\Cachet\Composers\MetricsComposer;
use CachetHQ\Cachet\Composers\StatusPageComposer;
use CachetHQ\Cachet\Composers\ModuleComposer;
use CachetHQ\Cachet\Composers\Modules\ComponentsComposer as ComponentsModuleComposer;
use CachetHQ\Cachet\Composers\Modules\MetricsComposer as MetricsModuleComposer;
use CachetHQ\Cachet\Composers\Modules\ScheduledComposer as ScheduledModuleComposer;
use CachetHQ\Cachet\Composers\Modules\StatusComposer as StatusModuleComposer;
use CachetHQ\Cachet\Composers\Modules\TimelineComposer as TimelineModuleComposer;
use CachetHQ\Cachet\Composers\ThemeComposer;
use CachetHQ\Cachet\Composers\TimezoneLocaleComposer;
use Illuminate\Contracts\View\Factory;
@@ -32,11 +36,16 @@ class ComposerServiceProvider extends ServiceProvider
{
$factory->composer('*', AppComposer::class);
$factory->composer('*', CurrentUserComposer::class);
$factory->composer(['index'], MetricsComposer::class);
$factory->composer(['index', 'single-incident', 'subscribe', 'signup'], StatusPageComposer::class);
$factory->composer(['index', 'single-incident', 'subscribe.*', 'signup', 'dashboard.settings.theme', 'emails.*'], ThemeComposer::class);
$factory->composer('dashboard.*', DashboardComposer::class);
$factory->composer(['setup', 'dashboard.settings.localization'], TimezoneLocaleComposer::class);
$factory->composer('*', ModuleComposer::class);
$factory->composer('partials.modules.components', ComponentsModuleComposer::class);
$factory->composer('partials.modules.metrics', MetricsModuleComposer::class);
$factory->composer('partials.modules.scheduled', ScheduledModuleComposer::class);
$factory->composer('partials.modules.status', StatusModuleComposer::class);
$factory->composer('partials.modules.timeline', TimelineModuleComposer::class);
}
/**
@@ -11,8 +11,9 @@
namespace CachetHQ\Cachet\Foundation\Providers;
use Illuminate\View\Compilers\BladeCompiler;
use CachetHQ\Cachet\Services\Modules\Renderer as ModulesRenderer;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
class ModuleServiceProvider extends ServiceProvider
{
@@ -44,7 +45,7 @@ class ModuleServiceProvider extends ServiceProvider
'components' => 30000,
'metrics' => 40000,
'scheduled' => 50000,
'timeline' => 60000,
'timeline' => 60000,
],
];
@@ -56,26 +57,17 @@ class ModuleServiceProvider extends ServiceProvider
public function boot(BladeCompiler $blade)
{
$blade->directive('modules', function ($group = null) {
$code = [
'foreach (array_numeric_sort($modules) as $group => $parts) {',
'foreach (array_numeric_sort($parts) as $part) {',
'if (empty($part[\'partial\'])) {',
'continue;',
'}',
'$params = array_except(get_defined_vars(), [\'__data\', \'__path\']);',
'$params = array_merge(array_get($part, \'parameters\', []), $params);',
'echo $__env->make($part[\'partial\'], $params)->render();',
'}',
'}',
];
if ($group) {
$code[0] = 'if (!empty($modules['.$group.'])) {'.PHP_EOL;
$code[0] .= '$parts = $modules['.$group.'];';
}
return '<?php '.implode(PHP_EOL, $code).'?>';
return sprintf(
'<?php echo $app->call(\'%s@%s\', [
\'factory\' => $__env,
\'data\' => array_except(get_defined_vars(), array(\'__data\', \'__path\')),
\'modules\' => $modules,
\'group\' => %s,
]); ?>',
ModulesRenderer::class,
'renderModules',
$group === null ? 'null' : $group
);
});
}
+80
View File
@@ -0,0 +1,80 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Services\Modules;
/**
* This is the module manager class.
*
* @author Connor S. Parks <connor@connorvg.tv>
*/
class Manager
{
/**
* Groups the modules.
*
* @param array $modules
* @param array $groups
*
* @return array
*/
public function groupModules(array $modules, array $groups = [])
{
$grouped = [];
$length = count($modules);
foreach ($modules as $order => $module) {
if (!is_array($module) || empty($module['group'])) {
$order = array_get($module, 'order', PHP_INT_MAX - $length + $order);
$grouped[] = [$module, 'order' => $order];
continue;
}
$group = $module['group'];
if (empty($grouped[$group])) {
$grouped[$group] = [$module];
continue;
}
$grouped[$group][] = $module;
}
foreach ($groups as $group => $order) {
if (empty($grouped[$group])) {
continue;
}
$grouped[$group]['order'] = $order;
}
return $grouped;
}
/**
* Orders the modules.
*
* @param array $modules
*
* @return array
*/
public function orderModules($modules)
{
$modules = array_numeric_sort($modules);
foreach ($modules as $group => $subModules) {
$modules[$group] = array_numeric_sort($subModules);
}
return $modules;
}
}
+75
View File
@@ -0,0 +1,75 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Services\Modules;
use Illuminate\Contracts\View\Factory;
/**
* This is the module renderer class.
*
* @author Connor S. Parks <connor@connorvg.tv>
*/
class Renderer
{
/**
* Render the modules.
*
* @param \Illuminate\Contracts\View\Factory $factory
* @param array $data
* @param array $modules
* @param string|null $group
*
* @return string
*/
public function renderModules(Factory $factory, array $data, array $modules, $group = null)
{
if ($group !== null) {
if (empty($modules[$group])) {
return '';
}
return $this->renderModulesGroup($factory, $data, $modules[$group]);
}
return array_reduce(
array_numeric_sort($modules),
function ($reduce, $module) use ($factory, $data) {
return $reduce.$this->renderModulesGroup($factory, $data, $module);
},
''
);
}
/**
* Render a group of modules.
*
* @param \Illuminate\Contracts\View\Factory $factory
* @param array $data
* @param array $modules
*
* @return string
*/
protected function renderModulesGroup(Factory $factory, array $data, array $modules)
{
return array_reduce(
array_numeric_sort($modules),
function ($reduce, $module) use ($factory, $data) {
if (empty($module['partial'])) {
return $reduce;
}
return $reduce.$factory->make($module['partial'], $data)->render();
},
''
);
}
}
+24
View File
@@ -115,3 +115,27 @@ if (!function_exists('color_contrast')) {
return ($yiq >= 128) ? 'black' : 'white';
}
}
if (!function_exists('array_numeric_sort')) {
/**
* Numerically sort an array based on a specific key.
*
* @param array $array
* @param string $key
*
* @return array
*/
function array_numeric_sort(array $array = [], $key = 'order')
{
uasort($array, function ($a, $b) use ($key) {
$a = array_get($a, $key, PHP_INT_MAX);
$b = array_get($b, $key, PHP_INT_MAX);
$default = PHP_MAJOR_VERSION < 7 && !defined('HHVM_VERSION') ? 1 : 0;
return $a < $b ? -1 : ($a === $b ? $default : 1);
});
return $array;
}
}