-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #122 by falling back to a full-spec YAML parser by default
- Loading branch information
1 parent
f706f57
commit 2cf6119
Showing
8 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Stillat\Meerkat\Core\Contracts\Parsing; | ||
|
||
use Stillat\Meerkat\Core\Configuration; | ||
|
||
/** | ||
* Interface PrototypeParserContract | ||
* | ||
* Represents a parser capable of converting a YAML | ||
* text file into a Meerkat Comment prototype. | ||
* | ||
* @package Stillat\Meerkat\Core\Contracts\Parsing | ||
* @since 2.1.6 | ||
*/ | ||
interface PrototypeParserContract | ||
{ | ||
|
||
/** | ||
* Sets the comment's truthy prototype elements. | ||
* | ||
* @param array $elements The truthy prototype elements. | ||
*/ | ||
public function setTruthyElements($elements); | ||
|
||
/** | ||
* Sets the Meerkat Core configuration instance. | ||
* | ||
* @param Configuration $configuration The configuration. | ||
*/ | ||
public function setConfig(Configuration $configuration); | ||
|
||
/** | ||
* Sets the prototype elements. | ||
* | ||
* @param array $elements The prototype elements. | ||
*/ | ||
public function setPrototypeElements($elements); | ||
|
||
/** | ||
* Retrieves only the core meta-data for the comment. | ||
* | ||
* Supplemental data and content are ignored during this phase. | ||
* | ||
* @param string $path The full path to the comment data. | ||
* @return array | ||
*/ | ||
public function getCommentPrototype($path); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace Stillat\Meerkat\Core\Parsing; | ||
|
||
use Stillat\Meerkat\Core\Configuration; | ||
use Stillat\Meerkat\Core\Contracts\Comments\CommentContract; | ||
use Stillat\Meerkat\Core\Contracts\Parsing\PrototypeParserContract; | ||
use Stillat\Meerkat\Core\Contracts\Parsing\YAMLParserContract; | ||
use Stillat\Meerkat\Core\Storage\Drivers\Local\LocalCommentStorageManager; | ||
|
||
/** | ||
* Class FullCommentPrototypeParser | ||
* | ||
* Utilizes a full YAML-spec parser to produce a comment prototype. | ||
* | ||
* This prototype parser produces the most consistent results across a wide | ||
* range of Comment data structures, but with a higher performance cost. | ||
* | ||
* @package Stillat\Meerkat\Core\Parsing | ||
* @since 2.1.6 | ||
*/ | ||
class FullCommentPrototypeParser implements PrototypeParserContract | ||
{ | ||
|
||
/** | ||
* The prototype comment elements to parse. | ||
* | ||
* @var array | ||
*/ | ||
protected $prototypeElements = []; | ||
|
||
/** | ||
* The Meerkat Core configuration. | ||
* | ||
* @var Configuration | ||
*/ | ||
protected $config = null; | ||
|
||
/** | ||
* The prototype elements that should be converted to boolean values. | ||
* | ||
* @var array | ||
*/ | ||
protected $truthyPrototypeElements = []; | ||
|
||
/** | ||
* The YAMLParserContract implementation instance. | ||
* | ||
* @var YAMLParserContract | ||
*/ | ||
protected $yamlParser = null; | ||
|
||
public function __construct(YAMLParserContract $yamlParser) | ||
{ | ||
$this->yamlParser = $yamlParser; | ||
} | ||
|
||
/** | ||
* Sets the comment's truthy prototype elements. | ||
* | ||
* @param array $elements The truthy prototype elements. | ||
*/ | ||
public function setTruthyElements($elements) | ||
{ | ||
$this->truthyPrototypeElements = $elements; | ||
} | ||
|
||
/** | ||
* Sets the Meerkat Core configuration instance. | ||
* | ||
* @param Configuration $configuration The configuration. | ||
*/ | ||
public function setConfig(Configuration $configuration) | ||
{ | ||
$this->config = $configuration; | ||
} | ||
|
||
/** | ||
* Sets the prototype elements. | ||
* | ||
* @param array $elements The prototype elements. | ||
*/ | ||
public function setPrototypeElements($elements) | ||
{ | ||
$this->prototypeElements = $elements; | ||
} | ||
|
||
/** | ||
* Retrieves only the core meta-data for the comment. | ||
* | ||
* Supplemental data and content are ignored during this phase. | ||
* | ||
* @param string $path The full path to the comment data. | ||
* @return array | ||
*/ | ||
public function getCommentPrototype($path) | ||
{ | ||
$contents = file_get_contents($path); | ||
$parsedComment = $this->yamlParser->parseDocument($contents); | ||
$commentContent = ''; | ||
$alreadyFoundContent = false; | ||
|
||
if (array_key_exists(CommentContract::KEY_LEGACY_COMMENT, $parsedComment)) { | ||
$commentContent = $parsedComment[CommentContract::KEY_LEGACY_COMMENT]; | ||
$alreadyFoundContent = true; | ||
|
||
unset($parsedComment[CommentContract::KEY_LEGACY_COMMENT]); | ||
} else if (array_key_exists(CommentContract::KEY_CONTENT, $parsedComment)) { | ||
$commentContent = $parsedComment[CommentContract::KEY_CONTENT]; | ||
|
||
unset($parsedComment[CommentContract::KEY_CONTENT]); | ||
} | ||
|
||
// Reset some types. | ||
if (array_key_exists(CommentContract::KEY_ID, $parsedComment)) { | ||
if (is_int($parsedComment[CommentContract::KEY_ID])) { | ||
$parsedComment[CommentContract::KEY_ID] = strval($parsedComment[CommentContract::KEY_ID]); | ||
} | ||
} | ||
|
||
return [ | ||
LocalCommentStorageManager::KEY_HEADERS => $parsedComment, | ||
LocalCommentStorageManager::KEY_RAW_HEADERS => $parsedComment, | ||
LocalCommentStorageManager::KEY_CONTENT => $commentContent, | ||
LocalCommentStorageManager::KEY_NEEDS_MIGRATION => $alreadyFoundContent | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters