Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Helpful JSON / YAML Error Messages + Minor cleanup of JSON / YAML related variables in Pattern Rules #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/PatternLab/PatternData/Rules/PatternInfoRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ public function run($depth, $ext, $path, $pathName, $name) {

$patternStoreData = array("category" => "pattern");

$file = file_get_contents(Config::getOption("patternSourceDir")."/".$pathName);
$filePath = Config::getOption("patternSourceDir")."/".$pathName;
$file = file_get_contents($filePath);

if ($ext == "json") {
$data = json_decode($file,true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($name,$jsonErrorMessage,$data);
JSON::lastErrorMsg($filePath,$jsonErrorMessage,$data);
}
} else {

try {
$data = YAML::parse($file);
} catch (ParseException $e) {
printf("unable to parse ".$pathNameClean.": %s..\n", $e->getMessage());
printf("unable to parse ".$filePath.": %s..\n", $e->getMessage());
}

// single line of text won't throw a YAML error. returns as string
Expand Down
40 changes: 21 additions & 19 deletions src/PatternLab/PatternData/Rules/PseudoPatternRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,55 +120,57 @@ public function run($depth, $ext, $path, $pathName, $name) {
$patternStoreData["breadcrumb"] = array("patternType" => $patternTypeClean, "patternSubtype" => $patternSubtypeClean);
}

$patternDataBase = array();
if (file_exists(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$patternBaseData)) {
$data = file_get_contents(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$patternBaseData);
$patternBaseData = array();
$patternBaseFilePath = Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$patternBaseData;

if (file_exists($patternBaseFilePath)) {
$patternBaseData = file_get_contents($patternBaseFilePath);
if ($ext == "json") {
$patternDataBase = json_decode($data,true);
$patternBaseData = json_decode($patternBaseData,true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($patternBaseJSON,$jsonErrorMessage,$data);
JSON::lastErrorMsg($patternBaseFilePath,$jsonErrorMessage,$patternBaseData);
}
} else {

try {
$patternDataBase = YAML::parse($data);
$patternBaseData = YAML::parse($patternBaseData);
} catch (ParseException $e) {
printf("unable to parse ".$pathNameClean.": %s..\n", $e->getMessage());
printf("unable to parse ".$patternBaseFilePath.": %s..\n", $e->getMessage());
}

// single line of text won't throw a YAML error. returns as string
if (gettype($patternDataBase) == "string") {
$patternDataBase = array();
if (gettype($patternBaseData) == "string") {
$patternBaseData = array();
}

}

}

// get the data for the pseudo-pattern
$data = file_get_contents(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$pathName);
$patternPsuedoFilePath = Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$pathName;
$patternPsuedoData = file_get_contents($patternPsuedoFilePath);

if ($ext == "json") {
$patternData = json_decode($data,true);
$patternPsuedoData = json_decode($patternPsuedoData,true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($name,$jsonErrorMessage,$data);
JSON::lastErrorMsg($patternPsuedoFilePath,$jsonErrorMessage,$patternPsuedoData);
}
} else {

try {
$patternData = YAML::parse($data);
$patternPsuedoData = YAML::parse($patternPsuedoData);
} catch (ParseException $e) {
printf("unable to parse ".$pathNameClean.": %s..\n", $e->getMessage());
printf("unable to parse ".$patternPsuedoFilePath.": %s..\n", $e->getMessage());
}

// single line of text won't throw a YAML error. returns as string
if (gettype($patternData) == "string") {
$patternData = array();
if (gettype($patternPsuedoData) == "string") {
$patternPsuedoData = array();
}

}

// make sure the pattern data is an array before merging the data
$patternStoreData["data"] = is_array($patternData) ? array_replace_recursive($patternDataBase, $patternData) : $patternDataBase;
$patternStoreData["data"] = is_array($patternPsuedoData) ? array_replace_recursive($patternBaseData, $patternPsuedoData) : $patternBaseData;

// if the pattern data store already exists make sure it is merged and overwrites this data
if (PatternData::checkOption($patternStoreKey)) {
Expand Down