/** * Collects child blocks of the parent block * * @param BlocksCollection $collection * @param $path * @param Block|null $parent */ protected function collectBlocks(BlocksCollection $collection, $path, Block $parent = null) { $pageContents = new \DirectoryIterator($path); $block = new Block(); $block->setParent($parent); $block->setChildren(new BlocksCollection()); $block->setPath($path); foreach ($pageContents as $fileInfo) { if ($fileInfo->isDot()) { continue; } if ($fileInfo->isDir()) { $this->collectBlocks($block->getChildren(), $fileInfo->getPath() . '/' . $fileInfo->getFilename(), $block); } if ($fileInfo->getExtension() == 'md') { // TODO: check if all names will be compatible with URLs $filename = $fileInfo->getFilename(); $parentDirName = basename(dirname($path . '/' . $filename)); $block->setFilename($filename); /* Extract lead sort order order numbers from the directories names */ $blockNameParts = explode('.', $parentDirName, 2); $blockName = count($blockNameParts) > 1 ? $blockNameParts[1] : $blockNameParts[0]; $block->setName($blockName); } } $collection->addBlock($block); }
/** * Sort posts by 'post_date' parameter provided in * the .md file headers. If 'post_date' parameter is not provided * the file last modification time will be used for sorting * * @param BlocksCollection $postsCollection * @return BlocksCollection $postsCollection */ protected function sortPosts(BlocksCollection $postsCollection) { $sortedBlocks = []; /** @var Block $block */ foreach ($postsCollection->getBlocks() as $block) { $customBlockParams = $block->getParams(); if (isset($customBlockParams['post_date'])) { $postTimestamp = strtotime($customBlockParams['post_date']); $sortedBlocks[(int) $postTimestamp] = $block; } else { $postDate = $block->getFileAttrs()['updated_at']; $sortedBlocks[strtotime($postDate)] = $block; } } if (count($sortedBlocks) > 0) { ksort($sortedBlocks); $postsCollection->setBlocks($sortedBlocks); } return $postsCollection; }
/** * Collects and returns all navigation links * * @return array */ public function getNavigationItems() { if (0 == count($this->navigationItems)) { /** @var Block $block */ foreach ($this->blocksCollection->getBlocks() as $block) { if ($block->includeInNavigation()) { $this->navigationItems[$block->getSortOrderValue()] = ['name' => isset($block->getParams()['navigation_title']) ? $block->getParams()['navigation_title'] : '', 'route' => $block->getRoute()]; } } } ksort($this->navigationItems); return $this->navigationItems; }
/** * Searches for a page that matches current route * Returns true if the page has been found * * @return bool */ public function process() { $route = "/{$this->getRoute()}"; if ($route) { /** @var $block */ foreach ($this->blocksCollection->getBlocks() as $block) { if ($block->getRoute() == $route) { // TODO: check if block is routable $this->setDispatchedBlock($block); $this->isProcessed = true; return true; } } } else { throw new Exception('No route has been set for CMS Page entity'); } return false; }