Skip to content

Commit

Permalink
feat: cleaning up variable names for slightly better consistency and …
Browse files Browse the repository at this point in the history
…clarity -- passing in the actual filename + path to the JSON and YAML data functions so errors are more helpful. Related to #146
  • Loading branch information
bolt-bot committed Oct 28, 2017
1 parent ac0e138 commit 8f0fe89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
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

0 comments on commit 8f0fe89

Please sign in to comment.