/** * Set an option for the data store * @param {String} a string in dot notation dictating where the option is in the data structure * @param {Mixed} the value for the key * * @return {Array} the store */ public static function setOption($key = "", $value = "") { if (empty($key)) { return false; } $arrayFinder = new ArrayFinder(self::$store); $arrayFinder->set($key, $value); self::$store = $arrayFinder->get(); }
/** * Gather data from any JSON and YAML files in source/_data * * Reserved attributes: * - Data::$store["listItems"] : listItems from listitems.json, duplicated into separate arrays for Data::$store["listItems"]["one"], Data::$store["listItems"]["two"]... etc. * - Data::$store["link"] : the links to each pattern * - Data::$store["cacheBuster"] : the cache buster value to be appended to URLs * - Data::$store["patternSpecific"] : holds attributes from the pattern-specific data files * * @return {Array} populates Data::$store */ public static function gather($options = array()) { // set-up the dispatcher $dispatcherInstance = Dispatcher::getInstance(); // dispatch that the data gather has started $dispatcherInstance->dispatch("data.gatherStart"); // default vars $found = false; $dataJSON = array(); $dataYAML = array(); $listItemsJSON = array(); $listItemsYAML = array(); $sourceDir = Config::getOption("sourceDir"); // iterate over all of the other files in the source directory if (!is_dir($sourceDir . "/_data/")) { Console::writeWarning("<path>_data/</path> doesn't exist so you won't have dynamic data..."); mkdir($sourceDir . "/_data/"); } // find the markdown-based annotations $finder = new Finder(); $finder->files()->in($sourceDir . "/_data/"); $finder->sortByName(); foreach ($finder as $name => $file) { $ext = $file->getExtension(); $data = array(); $fileName = $file->getFilename(); $hidden = $fileName[0] == "_"; $isListItems = strpos($fileName, "listitems"); $pathName = $file->getPathname(); $pathNameClean = str_replace($sourceDir . "/", "", $pathName); if (!$hidden && ($ext == "json" || $ext == "yaml")) { if ($isListItems === false) { if ($ext == "json") { $file = file_get_contents($pathName); $data = json_decode($file, true); if ($jsonErrorMessage = JSON::hasError()) { JSON::lastErrorMsg($pathNameClean, $jsonErrorMessage, $data); } } else { if ($ext == "yaml") { $file = file_get_contents($pathName); try { $data = YAML::parse($file); } catch (ParseException $e) { printf("unable to parse " . $pathNameClean . ": %s..\n", $e->getMessage()); } // single line of text won't throw a YAML error. returns as string if (gettype($data) == "string") { $data = array(); } } } if (is_array($data)) { self::$store = array_replace_recursive(self::$store, $data); } } else { if ($isListItems !== false) { $data = $ext == "json" ? self::getListItems("_data/" . $fileName) : self::getListItems("_data/" . $fileName, "yaml"); if (!isset(self::$store["listItems"])) { self::$store["listItems"] = array(); } self::$store["listItems"] = array_replace_recursive(self::$store["listItems"], $data); } } } } if (is_array(self::$store)) { foreach (self::$reservedKeys as $reservedKey) { if (array_key_exists($reservedKey, self::$store)) { Console::writeWarning("\"" . $reservedKey . "\" is a reserved key in Pattern Lab. the data using that key will be overwritten. please choose a new key..."); } } } self::$store["cacheBuster"] = Config::getOption("cacheBuster"); self::$store["link"] = array(); self::$store["patternSpecific"] = array(); $dispatcherInstance->dispatch("data.gatherEnd"); }