Fire onMetaHeaders event only once, cache results of Pico::getMetaHeaders()

This commit is contained in:
Daniel Rudolf 2016-12-06 20:01:38 +01:00
parent 82c6dd9795
commit bc5729629d
No known key found for this signature in database
GPG Key ID: A061F02CD8DE4538

View File

@ -153,6 +153,14 @@ class Pico
*/ */
protected $is404Content = false; protected $is404Content = false;
/**
* List of known meta headers
*
* @see Pico::getMetaHeaders()
* @var string[]|null
*/
protected $metaHeaders;
/** /**
* Meta data of the page to serve * Meta data of the page to serve
* *
@ -344,10 +352,11 @@ class Pico
$this->triggerEvent('onContentLoaded', array(&$this->rawContent)); $this->triggerEvent('onContentLoaded', array(&$this->rawContent));
// parse file meta // parse file meta
$headers = $this->getMetaHeaders(); $this->metaHeaders = $this->getMetaHeaders();
$this->triggerEvent('onMetaHeaders', array(&$this->metaHeaders));
$this->triggerEvent('onMetaParsing', array(&$this->rawContent, &$headers)); $this->triggerEvent('onMetaParsing', array(&$this->rawContent, &$this->metaHeaders));
$this->meta = $this->parseFileMeta($this->rawContent, $headers); $this->meta = $this->parseFileMeta($this->rawContent, $this->metaHeaders);
$this->triggerEvent('onMetaParsed', array(&$this->meta)); $this->triggerEvent('onMetaParsed', array(&$this->meta));
// register parsedown // register parsedown
@ -960,10 +969,7 @@ class Pico
} }
/** /**
* Returns known meta headers and triggers the onMetaHeaders event * Returns known meta headers
*
* Heads up! Calling this method triggers the `onMetaHeaders` event.
* Keep this in mind to prevent a infinite loop!
* *
* @return string[] known meta headers; the array value specifies the * @return string[] known meta headers; the array value specifies the
* YAML key to search for, the array key is later used to access the * YAML key to search for, the array key is later used to access the
@ -971,7 +977,11 @@ class Pico
*/ */
public function getMetaHeaders() public function getMetaHeaders()
{ {
$headers = array( if ($this->metaHeaders !== null) {
return $this->metaHeaders;
}
return array(
'title' => 'Title', 'title' => 'Title',
'description' => 'Description', 'description' => 'Description',
'author' => 'Author', 'author' => 'Author',
@ -979,9 +989,6 @@ class Pico
'robots' => 'Robots', 'robots' => 'Robots',
'template' => 'Template' 'template' => 'Template'
); );
$this->triggerEvent('onMetaHeaders', array(&$headers));
return $headers;
} }
/** /**