Bläddra i källkod

latest 20171218

Sebastian 7 år sedan
förälder
incheckning
5fa14fb838

+ 4 - 2
content/3_for-developers/03-twig.md

@@ -8,6 +8,8 @@ Twig is a flexible, fast and secure template engine for PHP. If you have never u
 
 
 The full Twig documentation for template designers is just one page long, so just head [over to Twig](http://twig.sensiolabs.org/doc/2.x/templates.html) and read it. You can learn the most important essentials for TYPEMILL in the following list.
 The full Twig documentation for template designers is just one page long, so just head [over to Twig](http://twig.sensiolabs.org/doc/2.x/templates.html) and read it. You can learn the most important essentials for TYPEMILL in the following list.
 
 
+[TOC]
+
 ## Basic Twig Syntax
 ## Basic Twig Syntax
 
 
 In a Twig template, you can use ordinary HTML markup. Statements and expressions are written in curly brackets.
 In a Twig template, you can use ordinary HTML markup. Statements and expressions are written in curly brackets.
@@ -183,9 +185,9 @@ Macros in Twig are like functions in PHP: You can use them for repeating tasks.
 This is an example for a navigation:
 This is an example for a navigation:
 
 
     {% macro loop_over(navigation) %}
     {% macro loop_over(navigation) %}
-    
+
       {% import _self as macros %}
       {% import _self as macros %}
-    
+
       {% for element in navigation %} 
       {% for element in navigation %} 
         <li>
         <li>
           {% if element.elementType == 'folder' %}
           {% if element.elementType == 'folder' %}

+ 2 - 0
content/3_for-developers/05-theme-variables/10-item.md

@@ -4,6 +4,8 @@ The item variable is an object. It provides informations about the actual page,
 
 
 Some informations are only available for the type `folder` while some other informations are specific to the type `file`. But most informations are shared by both.
 Some informations are only available for the type `folder` while some other informations are specific to the type `file`. But most informations are shared by both.
 
 
+[TOC]
+
 ## Example of the {{ item }} variable
 ## Example of the {{ item }} variable
 
 
 This is an example of an item variable:
 This is an example of an item variable:

+ 28 - 8
system/Assets.php

@@ -17,15 +17,14 @@ class Assets
 	
 	
 	public function addCSS(string $CSS)
 	public function addCSS(string $CSS)
 	{
 	{
-		$CSSpath = __DIR__ . '/../plugins' . $CSS;
-
-		if(file_exists($CSSfile))
+		$CSSfile = $this->getFileUrl($CSS);
+		
+		if($CSSfile)
 		{
 		{
-			$CSSfile = $this->baseUrl . '/plugins' . $CSS;
 			$this->CSS[] = '<link rel="stylesheet" href="' . $CSSfile . '" />';
 			$this->CSS[] = '<link rel="stylesheet" href="' . $CSSfile . '" />';
 		}
 		}
 	}
 	}
-	
+		
 	public function addInlineCSS($CSS)
 	public function addInlineCSS($CSS)
 	{
 	{
 		$this->inlineCSS[] = '<style>' . $CSS . '</style>';
 		$this->inlineCSS[] = '<style>' . $CSS . '</style>';
@@ -33,11 +32,10 @@ class Assets
 	
 	
 	public function addJS(string $JS)
 	public function addJS(string $JS)
 	{
 	{
-		$JSpath = __DIR__ . '/../plugins' . $JS;
+		$JSfile = $this->getFileUrl($JS);
 		
 		
-		if(file_exists($JSpath))
+		if($JSfile)
 		{
 		{
-			$JSfile = $this->baseUrl . '/plugins' . $JS;
 			$this->JS[] = '<script src="' . $JSfile . '"></script>';
 			$this->JS[] = '<script src="' . $JSfile . '"></script>';
 		}
 		}
 	}
 	}
@@ -56,4 +54,26 @@ class Assets
 	{
 	{
 		return implode('<br/>', $this->JS) . implode('<br/>', $this->inlineJS);
 		return implode('<br/>', $this->JS) . implode('<br/>', $this->inlineJS);
 	}
 	}
+
+	/**
+	 * Checks, if a string is a valid internal or external ressource like js-file or css-file
+	 * @params $path string
+	 * @return string or false 
+	 */
+	public function getFileUrl(string $path)
+	{				
+		$internalFile = __DIR__ . '/../plugins' . $path;
+		
+		if(file_exists($internalFile))
+		{
+			return $this->baseUrl . '/plugins' . $path;
+		}
+		
+		if(fopen($path, "r"))
+		{
+			return $path;
+		}
+		
+		return false;		
+	}
 }
 }

+ 10 - 6
system/Controllers/PageController.php

@@ -10,6 +10,8 @@ use \Symfony\Component\Yaml\Yaml;
 use Typemill\Models\VersionCheck;
 use Typemill\Models\VersionCheck;
 use Typemill\Models\Helpers;
 use Typemill\Models\Helpers;
 use Typemill\Events\LoadPagetreeEvent;
 use Typemill\Events\LoadPagetreeEvent;
+use Typemill\Events\LoadBreadcrumbEvent;
+use Typemill\Events\LoadItemEvent;
 use Typemill\Events\LoadMarkdownEvent;
 use Typemill\Events\LoadMarkdownEvent;
 use Typemill\Events\ParseHtmlEvent;
 use Typemill\Events\ParseHtmlEvent;
 use Typemill\Extensions\ParsedownExtension;
 use Typemill\Extensions\ParsedownExtension;
@@ -53,7 +55,7 @@ class PageController extends Controller
 					$this->render($response, '/index.twig', [ 'content' => $content ]);
 					$this->render($response, '/index.twig', [ 'content' => $content ]);
 				}
 				}
 				elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
 				elseif(!$cache->validate('cache', 'lastSitemap.txt', 86400))
-				{					
+				{
 					/* update sitemap */
 					/* update sitemap */
 					$sitemap = new WriteSitemap();
 					$sitemap = new WriteSitemap();
 					$sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
 					$sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
@@ -74,7 +76,7 @@ class PageController extends Controller
 		
 		
 		/* if the user is on startpage */
 		/* if the user is on startpage */
 		if(empty($args))
 		if(empty($args))
-		{			
+		{	
 			/* check, if there is an index-file in the root of the content folder */
 			/* check, if there is an index-file in the root of the content folder */
 			$contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
 			$contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
 		}
 		}
@@ -102,9 +104,11 @@ class PageController extends Controller
 			
 			
 			/* get breadcrumb for page */
 			/* get breadcrumb for page */
 			$breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
 			$breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
-			
+			$breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new LoadBreadcrumbEvent($breadcrumb))->getData();
+						
 			/* add the paging to the item */
 			/* add the paging to the item */
 			$item = Folder::getPagingForItem($structure, $item);
 			$item = Folder::getPagingForItem($structure, $item);
+			$item = $this->c->dispatcher->dispatch('onItemLoaded', new LoadItemEvent($item))->getData();
 			
 			
 			/* check if url is a folder. If so, check if there is an index-file in that 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)
 			if($item->elementType == 'folder' && $item->index)
@@ -128,7 +132,7 @@ class PageController extends Controller
 		/* parse markdown-file to html-string */
 		/* parse markdown-file to html-string */
 		$contentHTML 	= $Parsedown->text($contentMD);
 		$contentHTML 	= $Parsedown->text($contentMD);
 		$contentHTML 	= $this->c->dispatcher->dispatch('onHtmlParsed', new ParseHtmlEvent($contentHTML))->getData();
 		$contentHTML 	= $this->c->dispatcher->dispatch('onHtmlParsed', new ParseHtmlEvent($contentHTML))->getData();
-
+		
 		$excerpt		= substr($contentHTML,0,200);
 		$excerpt		= substr($contentHTML,0,200);
 		$excerpt		= explode("</h1>", $excerpt);
 		$excerpt		= explode("</h1>", $excerpt);
 		$title			= isset($excerpt[0]) ? strip_tags($excerpt[0]) : $settings['title'];
 		$title			= isset($excerpt[0]) ? strip_tags($excerpt[0]) : $settings['title'];
@@ -192,6 +196,6 @@ class PageController extends Controller
 					$yaml->updateYaml('settings', 'settings.yaml', $userSettings);									
 					$yaml->updateYaml('settings', 'settings.yaml', $userSettings);									
 				}
 				}
 			}
 			}
-		}	
-	}	
+		}
+	}
 }
 }

+ 25 - 0
system/Events/BaseEvent.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Typemill\Events;
+
+use Symfony\Component\EventDispatcher\Event;
+
+class BaseEvent extends Event
+{
+    protected $data;
+
+    public function __construct($data)
+    {
+        $this->data = $data;
+    }
+
+    public function getData()
+    {
+        return $this->data;		
+    }
+	
+	public function setData($data)
+	{
+		$this->data = $data;
+	}
+}

+ 14 - 0
system/Events/LoadBreadcrumbEvent.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Typemill\Events;
+
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Event for breadcrumb.
+ */
+ 
+class LoadBreadcrumbEvent extends BaseEvent
+{
+
+}

+ 14 - 0
system/Events/LoadItemEvent.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Typemill\Events;
+
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Event for item.
+ */
+ 
+class LoadItemEvent extends BaseEvent
+{
+
+}

+ 2 - 17
system/Events/LoadMarkdownEvent.php

@@ -5,25 +5,10 @@ namespace Typemill\Events;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\EventDispatcher\Event;
 
 
 /**
 /**
- * Event for the pure content.
+ * Event for markdown.
  */
  */
 
 
-class LoadMarkdownEvent extends Event
+class LoadMarkdownEvent extends BaseEvent
 {
 {
-    protected $data;
 
 
-    public function __construct($data)
-    {
-        $this->data = $data;
-    }
-
-    public function getData()
-    {
-        return $this->data;		
-    }
-	
-	public function setData($data)
-	{
-		$this->data = $data;
-	}
 }
 }

+ 2 - 17
system/Events/LoadPagetreeEvent.php

@@ -5,25 +5,10 @@ namespace Typemill\Events;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\EventDispatcher\Event;
 
 
 /**
 /**
- * Event for the folder structure.
+ * Event for the page tree.
  */
  */
  
  
-class LoadPagetreeEvent extends Event
+class LoadPagetreeEvent extends BaseEvent
 {
 {
-    protected $data;
 
 
-    public function __construct($data)
-    {
-        $this->data = $data;
-    }
-
-    public function getData()
-    {
-        return $this->data;		
-    }
-	
-	public function setData($data)
-	{
-		$this->data = $data;
-	}
 }
 }

+ 14 - 0
system/Events/LoadPluginsEvent.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Typemill\Events;
+
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Event for the folder structure.
+ */
+ 
+class LoadPluginsEvent extends BaseEvent
+{
+
+}

+ 14 - 0
system/Events/LoadSettingsEvent.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Typemill\Events;
+
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Event for settings
+ */
+ 
+class LoadSettingsEvent extends BaseEvent
+{
+
+}

+ 2 - 17
system/Events/ParseHtmlEvent.php

@@ -5,25 +5,10 @@ namespace Typemill\Events;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\EventDispatcher\Event;
 
 
 /**
 /**
- * Event for the pure content.
+ * Event for html page.
  */
  */
 
 
-class ParseHtmlEvent extends Event
+class ParseHtmlEvent extends BaseEvent
 {
 {
-    protected $data;
 
 
-    public function __construct($data)
-    {
-        $this->data = $data;
-    }
-
-    public function getData()
-    {
-        return $this->data;		
-    }
-	
-	public function setData($data)
-	{
-		$this->data = $data;
-	}
 }
 }

+ 2 - 17
system/Events/RenderPageEvent.php

@@ -5,25 +5,10 @@ namespace Typemill\Events;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\EventDispatcher\Event;
 
 
 /**
 /**
- * Event for the pure content.
+ * Event for the page rendering data.
  */
  */
 
 
-class RenderPageEvent extends Event
+class RenderPageEvent extends BaseEvent
 {
 {
-    protected $data;
 
 
-    public function __construct($data)
-    {
-        $this->data = $data;
-    }
-
-    public function getData()
-    {
-        return $this->data;		
-    }
-	
-	public function setData($data)
-	{
-		$this->data = $data;
-	}
 }
 }

+ 4 - 10
system/Plugin.php

@@ -5,23 +5,17 @@ namespace Typemill;
 use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 
 abstract class Plugin implements EventSubscriberInterface
 abstract class Plugin implements EventSubscriberInterface
-{
-	
-	private $app;
-	
+{	
 	private $container;
 	private $container;
 
 
     /**
     /**
-     * Constructor.
+     * Constructor
      *
      *
-     * @param string $name
-     * @param Grav   $grav
-     * @param Config $config
      */
      */
-    public function __construct($container, $app)
+	 
+    public function __construct($container)
     {
     {
 		$this->container 	= $container;
 		$this->container 	= $container;
-		$this->app			= $app;
     }
     }
 
 
 	protected function getRoute()
 	protected function getRoute()

+ 9 - 5
system/Plugins.php

@@ -12,7 +12,6 @@ class Plugins
 		/* iterate over plugin folders */
 		/* iterate over plugin folders */
 		foreach($pluginFolder as $plugin)
 		foreach($pluginFolder as $plugin)
 		{
 		{
-
 			$className = DIRECTORY_SEPARATOR . 'Plugins' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR . $plugin;
 			$className = DIRECTORY_SEPARATOR . 'Plugins' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR . $plugin;
 						
 						
 			/* if plugin-class and subscribe-method exists, add classname to array */			
 			/* if plugin-class and subscribe-method exists, add classname to array */			
@@ -26,7 +25,6 @@ class Plugins
 	
 	
 	public function getNewRoutes($className, $routes)
 	public function getNewRoutes($className, $routes)
 	{
 	{
-		
 		/* if route-method exists in plugin-class */
 		/* if route-method exists in plugin-class */
 		if(method_exists($className, 'addNewRoutes'))
 		if(method_exists($className, 'addNewRoutes'))
 		{
 		{
@@ -59,13 +57,19 @@ class Plugins
 		return $routes;
 		return $routes;
 	}
 	}
 	
 	
-	public function getNewMiddleware($className)
+	public function getNewMiddleware($className, $middleware)
 	{
 	{
 		if(method_exists($className, 'addNewMiddleware'))
 		if(method_exists($className, 'addNewMiddleware'))
 		{
 		{
-			/* check array */
-			return $className::addNewMiddleware();
+			$pluginMiddleware = $className::addNewMiddleware();
+			
+			if($pluginMiddleware)
+			{
+				$middleware[] = $pluginMiddleware;				
+			}
 		}
 		}
+		
+		return $middleware;
 	}
 	}
 	
 	
 	private function checkRouteArray($routes,$route)
 	private function checkRouteArray($routes,$route)

+ 29 - 14
system/system.php

@@ -1,7 +1,7 @@
 <?php
 <?php
 
 
-use Typemill\Models\Helpers;
-$timer['before session start']=microtime(true);
+use Typemill\Events\LoadSettingsEvent;
+use Typemill\Events\LoadPluginsEvent;
 
 
 /************************
 /************************
 * START SESSION			*
 * START SESSION			*
@@ -9,11 +9,18 @@ $timer['before session start']=microtime(true);
 
 
 session_start();
 session_start();
 
 
+/****************************
+* CREATE EVENT DISPATCHER	*
+****************************/
+
+$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
+
 /************************
 /************************
 * LOAD SETTINGS			*
 * LOAD SETTINGS			*
 ************************/
 ************************/
 
 
 $settings = Typemill\settings::loadSettings();
 $settings = Typemill\settings::loadSettings();
+$settings = $dispatcher->dispatch('onSettingsLoaded', new LoadSettingsEvent($settings))->getData();
 
 
 /************************
 /************************
 * INITIATE SLIM 		*
 * INITIATE SLIM 		*
@@ -27,12 +34,6 @@ $app = new \Slim\App($settings);
 
 
 $container = $app->getContainer();
 $container = $app->getContainer();
 
 
-/****************************
-* CREATE EVENT DISPATCHER	*
-****************************/
-
-$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
-
 /************************
 /************************
 * LOAD PLUGINS 			*
 * LOAD PLUGINS 			*
 ************************/
 ************************/
@@ -45,11 +46,11 @@ foreach($pluginClassNames as $pluginClassName)
 {
 {
 	$routes 			= $plugins->getNewRoutes($pluginClassName, $routes);
 	$routes 			= $plugins->getNewRoutes($pluginClassName, $routes);
 	$middleware			= $plugins->getNewMiddleware($pluginClassName, $middleware);
 	$middleware			= $plugins->getNewMiddleware($pluginClassName, $middleware);
-	
-	$dispatcher->addSubscriber(new $pluginClassName($container, $app));	
+
+	$dispatcher->addSubscriber(new $pluginClassName($container));	
 }
 }
 
 
-$dispatcher->dispatch('onPluginsInitialized');	
+$dispatcher->dispatch('onPluginsLoaded', new LoadPluginsEvent($pluginClassNames));
 
 
 /******************************
 /******************************
 * ADD DISPATCHER TO CONTAINER *
 * ADD DISPATCHER TO CONTAINER *
@@ -107,15 +108,29 @@ $container['view'] = function ($container) use ($settings)
 
 
 $container->dispatcher->dispatch('onTwigLoaded');
 $container->dispatcher->dispatch('onTwigLoaded');
 
 
-/************************
-* 	ADD MIDDLEWARE		*
-************************/
+/***************************
+* 	ADD NOT FOUND HANDLER  *
+***************************/
 
 
 $container['notFoundHandler'] = function($c)
 $container['notFoundHandler'] = function($c)
 {
 {
 	return new \Typemill\Handlers\NotFoundHandler($c['view']);
 	return new \Typemill\Handlers\NotFoundHandler($c['view']);
 };
 };
 
 
+/************************
+* 	ADD MIDDLEWARE  	*
+************************/
+
+foreach($middleware as $pluginMiddleware)
+{
+	$middlewareClass 	= $pluginMiddleware['classname'];
+	$middlewareParams	= $pluginMiddleware['params'];
+	if(class_exists($middlewareClass))
+	{
+		$app->add(new $middlewareClass($middlewareParams));
+	}
+}
+
 /************************
 /************************
 * 	ADD ROUTES			*
 * 	ADD ROUTES			*
 ************************/
 ************************/

+ 0 - 2
themes/typemill/partials/layout.twig

@@ -34,7 +34,6 @@
 			</header>
 			</header>
 			<article>
 			<article>
 				{% block content %}{% endblock %}
 				{% block content %}{% endblock %}
-				<small>{{content|rot13}}</small>
 			</article>
 			</article>
 			<aside id="navigation" class="close">
 			<aside id="navigation" class="close">
 				<nav>
 				<nav>
@@ -42,7 +41,6 @@
 				</nav>
 				</nav>
 			</aside>
 			</aside>
 			<footer>
 			<footer>
-				<p>hello {{ myName() }}</p>
 				{% include 'partials/footer.twig' %}
 				{% include 'partials/footer.twig' %}
 			</footer>
 			</footer>
 		</div>
 		</div>

BIN
typemill-1.0.5.zip