Last active
March 23, 2018 14:47
-
-
Save RobinRadic/10c3090294a416df4b7b459bfe914cc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Pycraft\PycraftModule\Ui\ControlPanel; | |
use Anomaly\Streams\Platform\Addon\Module\Module; | |
use Anomaly\Streams\Platform\View\Event\TemplateDataIsLoading; | |
use Illuminate\Foundation\Bus\DispatchesJobs; | |
use Illuminate\Support\ServiceProvider; | |
use Pycraft\PycraftModule\Ui\ControlPanel\Command\BuildFullControlPanelNavigation; | |
class ControlPanelServiceProvider extends ServiceProvider | |
{ | |
use DispatchesJobs; | |
/** | |
* boot method | |
* | |
* @return void | |
* @throws \Exception | |
*/ | |
public function boot() | |
{ | |
if ( $this->isAdminUrl()) { | |
if (config('pycraft.module.pycraft::cp.replace_sidebar', false)) { | |
$this->replaceControlPanelSidebar(); | |
} | |
} | |
} | |
protected function isAdminUrl() | |
{ | |
return starts_with(url()->current(), url()->to('admin')); | |
} | |
/** | |
* replaceAdminMenu method | |
* | |
* @return void | |
* @throws \Exception | |
*/ | |
protected function replaceControlPanelSidebar() | |
{ | |
$this->app->make('events')->listen(TemplateDataIsLoading::class, function (TemplateDataIsLoading $event) { | |
debugbar()->startMeasure('BuildFullControlPanelNavigation'); | |
$cache = $this->app->make('cache.store'); | |
$modifiedCheckStr = md5($this->app->make('module.collection')->enabled()->transform(function (Module $module) { | |
return $module->getSlug(); | |
})->implode('_')); | |
$modifiedCheckStrCached = $cache->get('pycraft.module.pycraft::BuildFullControlPanelNavigation.modifiedCheck'); | |
// refresh cached menu | |
if ($modifiedCheckStr !== $modifiedCheckStrCached || ! $cache->has('pycraft.module.pycraft::BuildFullControlPanelNavigation')) { | |
$cache->forever('pycraft.module.pycraft::BuildFullControlPanelNavigation.modifiedCheck', $modifiedCheckStr); | |
$menu = $cache->rememberForever('pycraft.module.pycraft::BuildFullControlPanelNavigation', function () { | |
$menu = new NavigationCollection; | |
$this->dispatch(new BuildFullControlPanelNavigation($menu)); | |
return $menu; | |
}); | |
} else { | |
$menu = $cache->get('pycraft.module.pycraft::BuildFullControlPanelNavigation'); | |
} | |
$template = $event->getTemplate(); | |
// workaround bug fix | |
$menu = $menu->filter(function (NavigationItem $item) { | |
$isArray = false; | |
$item->getSections()->each(function (NavigationItemChild $section) use ($item, &$isArray) { | |
$title = $section->getTitle(); | |
if (is_array($title)) { | |
$isArray = true; | |
} | |
}); | |
return $isArray === false; | |
}); | |
$template->set('admin_menu', $menu); | |
debugbar()->stopMeasure('BuildFullControlPanelNavigation'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment