Source of file StyleGuideController.php
Size: 5,769 Bytes - Last Modified: 2022-01-13T10:01:28+00:00
/var/www/docs.ssmods.com/process/src/code/controllers/StyleGuideController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 | <?php namespace BenManu\StyleGuide; use SilverStripe\Control\Director; use SilverStripe\Core\Injector\Injector; use SilverStripe\View\Requirements; use SilverStripe\Control\Controller; use SilverStripe\CMS\Controllers\ContentController; use BenManu\StyleGuide\StyleGuideController; /** * StyleGuideController * * @package StyleGuide */ class StyleGuideController extends ContentController { /** * @var StyleGuide service */ protected $styleguide_service; /** * @var ArrayData */ protected $fixture; /** * @var PageService */ protected $pageService; /** * @config */ private static $service = ''; /** * @config */ private static $prettify_url = ''; /** * @config */ private static $paths = array(); /** * @config */ private static $css_files = array(); /** * @config */ private static $js_files = array(); public function init() { parent::init(); if(!$this->config()->service) { $this->httpError(404); } $this->setService($this->config()->service); $this->pageService = new PageService($this); // redirect to the first action route if(!$this->request->param('Action')) { $page = $this->pageService->getPages()->first(); $this->redirect($page->Link); } // if no template set on the action route then redirect to the first child if(!$this->request->param('ChildAction') && !$this->pageService->getTemplate()) { $page = $this->pageService->getActivePage(); if(isset($page->Children)) { $childPage = $page->Children->first(); $this->redirect($childPage->Link); } } // set the service $this->setRequirements(); // load the fixture file $this->loadFixture(); } public function index() { // render the set template for the route if($template = $this->pageService->getTemplate()) { return $this->renderWith(array( $template, StyleGuideController::class )); } return $this->renderWith(array(StyleGuideController::class)); } /** * Set the styleguide service on init. * @param String $name The name of the styleguide service. * @param String $paths Project base url. */ public function setService($name, $paths = null) { if(!$paths) { // set the paths as the document root $paths = array(); if(is_array($this->config()->paths)) { foreach($this->config()->paths as $path) { $paths[] = Director::BaseFolder() . "/" . $path; } } elseif(is_string($this->config()->paths)) { $paths[] = Director::BaseFolder() . "/" . $this->config()->paths; } } $this->styleguide_service = Injector::inst()->create($name); $this->styleguide_service->setURL($paths); } /** * Set the styleguides css and js requirements. */ public function setRequirements() { // styleguide requirements Requirements::css( 'benmanu/silverstripe-styleguide: dist/css/screen.css'); Requirements::javascript('benmanu/silverstripe-styleguide: dist/js/core.js'); Requirements::javascript($this->config()->prettify_url); // theme requirements if($files = $this->config()->css_files) { foreach($files as $file) { Requirements::css($file); } } if($files = $this->config()->js_files) { foreach($files as $file) { Requirements::javascript($file); } } } /** * Load a yml fixture file if defined into an {@link StyleGuideFixtureFactory}. * Used to populate templates. */ public function loadFixture() { $path = project() . '/styleguide/fixture.yml'; if(YamlParser::hasYaml($path)) { $parser = new YamlParser($path); $this->fixture = $parser->get('Template'); } } public function getFixture() { return $this->fixture; } public function getNavigation() { return $this->pageService->getPages(); } /** * Return sections for sub-navigation. * @return ArrayList */ public function getSubNavigation() { return $this->getSections(); } /** * Return sections filtered by the current url action. * @return ArrayList */ public function getSections() { $sections = null; if($action = $this->request->param('ChildAction')) { if($action == 'all') { $sections = $this->styleguide_service->getSections(); } else { $sections = $this->styleguide_service->getSectionChildren($action); $sections->unshift($this->styleguide_service->getSection($action)); } } return $sections; } /** * Get a link to the controller with optional action parameter. * @param String $action * @param String $childAction * @return String */ public function Link($action = null, $childAction = null) { return self::getLink($action, $childAction); } /** * Create a string suitable for a link in the style guide. * @param String $action * @param String $childAction * @return String */ public static function getLink($action = null, $childAction = null) { return Controller::join_links('sg', $action, $childAction); } } |