/** * 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); }
/** * Returns preview of the blog post * * @param Block $post * @return string */ public function getExcerpt(Block $post) { // TODO: replace by ability to set custom excerpt $excerpt = $postBody = $post->getBodyForInsertion(); if (preg_match('/^.{1,260}\\b/s', $postBody, $match)) { $excerpt = $match[0]; } return $excerpt; }
/** * Goes recursively trough blocks and attempts to dispatch * the specified route * * @param \Plugins\Cms\Model\Block $block * @param string $route * @return bool */ protected function routeDispatch(Block $block, $route) { if (!$this->getDispatchedBlock()) { if ($block->getRoute() == $route) { // TODO: check if block is routable $this->setDispatchedBlock($block); $this->isProcessed = true; return true; } elseif (count($block->getChildren()->getBlocks()) > 0) { // Go trough child blocks /** @var Block $childBlock */ foreach ($block->getChildren()->getBlocks() as $childBlock) { $this->routeDispatch($childBlock, $route); if ($this->getDispatchedBlock()) { break; } } } } return true; }
/** * Generates file for the result view * * @param Block $block * @return string * @throws \Exception */ protected function generateResultViewFile(Block $block) { $filePath = $this->getGeneratedViewsDir() . $block->getRoute() . '.blade.php'; $fileDirPath = dirname($filePath); // Create directory if it does not exist if (!is_dir($fileDirPath)) { mkdir($fileDirPath, 0755, true); } try { $fileHandle = fopen($filePath, 'w+'); fwrite($fileHandle, $this->content); fclose($fileHandle); return $filePath; } catch (\Exception $e) { throw new \Exception(sprintf('Cannot create view file. Make sure %s dir is writable', $this->getGeneratedViewsDir())); } }