From 7537159868b6c3b4430004de02235328375d4eae Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Mon, 5 Oct 2015 01:50:55 +0200 Subject: [PATCH] Remove the need to register headers during onMetaHeaders() Why? I'm currently writing the user docs and I really have no idea how to explain this whole process in a non-technical way... It is very likely that a normal user wants to use custom tags and it would be absurd to tell him,that he should learn a programming language to do so. On the other hand, providing a copy-and-paste template makes the whole idea of explicitly registering headers worthless. The only reasonable solution is to remove the need to register headers. Anyway, I think @PontusHorn is right to say that registering headers makes the whole system more predictable. So plugin developers are still instructed to register their meta headers during . We actually can't check and ensure this, but that's imho the best solution. --- lib/Pico.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index 408d4ae..11e0cd2 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -616,9 +616,10 @@ class Pico * * Meta data MUST start on the first line of the file, either opened and * closed by --- or C-style block comments (deprecated). The headers are - * parsed by the YAML component of the Symfony project. You MUST register - * new headers during the `onMetaHeaders` event first, otherwise they are - * ignored and won't be returned. + * parsed by the YAML component of the Symfony project, keys are lowered. + * If you're a plugin developer, you MUST register new headers during the + * `onMetaHeaders` event first. The implicit availability of headers is + * for users and pure (!) theme developers ONLY. * * @see * @param string $rawContent the raw file contents @@ -632,16 +633,19 @@ class Pico . "(.*?)(?:\r)?\n(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s"; if (preg_match($pattern, $rawContent, $rawMetaMatches)) { $yamlParser = new \Symfony\Component\Yaml\Parser(); - $rawMeta = $yamlParser->parse($rawMetaMatches[3]); - $rawMeta = array_change_key_case($rawMeta, CASE_LOWER); + $meta = $yamlParser->parse($rawMetaMatches[3]); + $meta = array_change_key_case($meta, CASE_LOWER); - // TODO: maybe we should change this to pass all headers, no matter - // they are registered during the `onMetaHeaders` event or not... foreach ($headers as $fieldId => $fieldName) { $fieldName = strtolower($fieldName); - if (isset($rawMeta[$fieldName])) { - $meta[$fieldId] = $rawMeta[$fieldName]; + if (isset($meta[$fieldName])) { + // rename field (e.g. remove whitespaces) + if ($fieldId != $fieldName) { + $meta[$fieldId] = $meta[$fieldName]; + unset($meta[$fieldName]); + } } else { + // guarantee array key existance $meta[$fieldId] = ''; } } @@ -653,6 +657,7 @@ class Pico $meta['time'] = $meta['date_formatted'] = ''; } } else { + // guarantee array key existance foreach ($headers as $id => $field) { $meta[$id] = ''; }