mirror of
https://github.com/typemill/typemill.git
synced 2025-08-06 06:07:31 +02:00
event system and plugins
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Typemill\Controllers;
|
||||
|
||||
/* Use the slim-container */
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Typemill\Events\RenderSiteEvent;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
@@ -14,10 +15,15 @@ abstract class Controller
|
||||
$this->c = $c;
|
||||
}
|
||||
|
||||
protected function render404($response, $content = NULL)
|
||||
protected function render($response, $route, $data)
|
||||
{
|
||||
return $this->c->view->render($response->withStatus(404), '/404.twig', $content);
|
||||
$data = $this->c->dispatcher->dispatch('beforeRenderSite', new RenderSiteEvent($data))->getData();
|
||||
|
||||
return $this->c->view->render($response, $route, $data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
protected function render404($response, $data = NULL)
|
||||
{
|
||||
return $this->c->view->render($response->withStatus(404), '/404.twig', $data);
|
||||
}
|
||||
}
|
@@ -9,12 +9,14 @@ use Typemill\Models\WriteYaml;
|
||||
use \Symfony\Component\Yaml\Yaml;
|
||||
use Typemill\Models\VersionCheck;
|
||||
use Typemill\Models\Helpers;
|
||||
use Typemill\Events\LoadStructureEvent;
|
||||
use Typemill\Events\LoadMarkdownEvent;
|
||||
use Typemill\Events\ParseHtmlEvent;
|
||||
|
||||
class PageController extends Controller
|
||||
{
|
||||
public function index($request, $response, $args)
|
||||
{
|
||||
|
||||
{
|
||||
/* Initiate Variables */
|
||||
$structure = false;
|
||||
$contentHTML = false;
|
||||
@@ -29,38 +31,39 @@ class PageController extends Controller
|
||||
|
||||
try
|
||||
{
|
||||
/* if the cached structure is still valid, use it */
|
||||
if($cache->validate('cache', 'lastCache.txt',600))
|
||||
{
|
||||
{
|
||||
$structure = $this->getCachedStructure($cache);
|
||||
$cached = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* if not, get a fresh structure of the content folder */
|
||||
$structure = $this->getFreshStructure($pathToContent, $cache, $uri);
|
||||
$cached = false;
|
||||
|
||||
$cached = false;
|
||||
|
||||
/* if there is no structure at all, the content folder is probably empty */
|
||||
if(!$structure)
|
||||
{
|
||||
{
|
||||
$content = '<h1>No Content</h1><p>Your content folder is empty.</p>';
|
||||
$this->c->view->render($response, '/index.twig', [ 'content' => $content ]);
|
||||
|
||||
$this->render($response, '/index.twig', [ 'content' => $content ]);
|
||||
}
|
||||
elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
|
||||
{
|
||||
{
|
||||
/* update sitemap */
|
||||
$sitemap = new WriteSitemap();
|
||||
$sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
|
||||
|
||||
$version = new VersionCheck();
|
||||
$latestVersion = $version->checkVersion($uri->getBaseUrl());
|
||||
if($latestVersion)
|
||||
{
|
||||
$yaml = new WriteYaml();
|
||||
$yamlContent = $yaml->getYaml('settings', 'settings.yaml');
|
||||
$yamlContent['latestVersion'] = $latestVersion;
|
||||
$yaml->updateYaml('settings', 'settings.yaml', $yamlContent);
|
||||
}
|
||||
/* check and update the typemill-version in the user settings */
|
||||
$this->updateVersion($uri->getBaseUrl());
|
||||
}
|
||||
}
|
||||
|
||||
/* dispatch event and let others manipulate the structure */
|
||||
$structure = $this->c->dispatcher->dispatch('onStructureLoaded', new LoadStructureEvent($structure))->getData();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
@@ -81,21 +84,28 @@ class PageController extends Controller
|
||||
|
||||
/* find the url in the content-item-tree and return the item-object for the file */
|
||||
$item = Folder::getItemForUrl($structure, $urlRel);
|
||||
|
||||
if(!$item && $cached)
|
||||
|
||||
/* if structure is cached and there is no item
|
||||
if($cached && !$item)
|
||||
{
|
||||
/* get a fresh structure and search for the item again
|
||||
$structure = $this->getFreshStructure($pathToContent, $cache, $uri);
|
||||
$item = Folder::getItemForUrl($structure, $urlRel);
|
||||
}
|
||||
if(!$item){ return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url )); }
|
||||
|
||||
/* if there is still no item, return a 404-page */
|
||||
if(!$item)
|
||||
{
|
||||
return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
|
||||
}
|
||||
|
||||
/* get breadcrumb for page */
|
||||
$breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
|
||||
|
||||
|
||||
/* add the paging to the item */
|
||||
$item = Folder::getPagingForItem($structure, $item);
|
||||
|
||||
/* check if url is a folder. If so, check if there is an index-file for the folder */
|
||||
|
||||
/* check if url is a folder. If so, check if there is an index-file in that folder */
|
||||
if($item->elementType == 'folder' && $item->index)
|
||||
{
|
||||
$filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
|
||||
@@ -104,16 +114,20 @@ class PageController extends Controller
|
||||
{
|
||||
$filePath = $pathToContent . $item->path;
|
||||
}
|
||||
|
||||
|
||||
/* read the content of the file */
|
||||
$contentMD = isset($filePath) ? file_get_contents($filePath) : false;
|
||||
}
|
||||
|
||||
$contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new LoadMarkdownEvent($contentMD))->getData();
|
||||
|
||||
/* initialize parsedown */
|
||||
$Parsedown = new \ParsedownExtra();
|
||||
|
||||
|
||||
/* parse markdown-file to html-string */
|
||||
$contentHTML = $Parsedown->text($contentMD);
|
||||
$contentHTML = $this->c->dispatcher->dispatch('onHtmlParsed', new ParseHtmlEvent($contentHTML))->getData();
|
||||
|
||||
$excerpt = substr($contentHTML,0,200);
|
||||
$excerpt = explode("</h1>", $excerpt);
|
||||
$title = isset($excerpt[0]) ? strip_tags($excerpt[0]) : $settings['title'];
|
||||
@@ -127,8 +141,8 @@ class PageController extends Controller
|
||||
*/
|
||||
|
||||
$route = empty($args) && $settings['startpage'] ? '/cover.twig' : '/index.twig';
|
||||
|
||||
$this->c->view->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'title' => $title, 'description' => $description, 'base_url' => $base_url ));
|
||||
|
||||
$this->render($response, $route, array('navigation' => $structure, 'content' => $contentHTML, 'item' => $item, 'breadcrumb' => $breadcrumb, 'settings' => $settings, 'title' => $title, 'description' => $description, 'base_url' => $base_url ));
|
||||
}
|
||||
|
||||
protected function getCachedStructure($cache)
|
||||
@@ -155,4 +169,28 @@ class PageController extends Controller
|
||||
|
||||
return $structure;
|
||||
}
|
||||
|
||||
protected function updateVersion($baseUrl)
|
||||
{
|
||||
/* check the latest public typemill version */
|
||||
$version = new VersionCheck();
|
||||
$latestVersion = $version->checkVersion($baseUrl);
|
||||
|
||||
if($latestVersion)
|
||||
{
|
||||
/* check, if user-settings exist */
|
||||
$yaml = new WriteYaml();
|
||||
$userSettings = $yaml->getYaml('settings', 'settings.yaml');
|
||||
if($userSettings)
|
||||
{
|
||||
/* if there is no version info in the settings or if the version info is outdated */
|
||||
if(!isset($userSettings['latestVersion']) || $userSettings['latestVersion'] != $latestVersion)
|
||||
{
|
||||
/* write the latest version into the user-settings */
|
||||
$userSettings['latestVersion'] = $latestVersion;
|
||||
$yaml->updateYaml('settings', 'settings.yaml', $userSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user