/** * Adds the config options to a var to be accessed from the rest of the system * If it's an old config or no config exists this will update and generate it. * @param {Boolean} whether we should print out the status of the config being loaded */ public static function init($baseDir = "", $verbose = true) { // make sure a base dir was supplied if (empty($baseDir)) { Console::writeError("need a base directory to initialize the config class..."); } // normalize the baseDir $baseDir = FileUtil::normalizePath($baseDir); // double-check the default config file exists if (!is_dir($baseDir)) { Console::writeError("make sure " . $baseDir . " exists..."); } // set the baseDir option self::$options["baseDir"] = $baseDir[strlen($baseDir) - 1] == DIRECTORY_SEPARATOR ? $baseDir : $baseDir . DIRECTORY_SEPARATOR; // can't add __DIR__ above so adding here if (!self::$dirAdded) { // set-up the paths self::$userConfigDirClean = self::$options["baseDir"] . self::$userConfigDirClean; self::$userConfigDirDash = self::$options["baseDir"] . self::$userConfigDirDash; self::$userConfigDir = is_dir(self::$userConfigDirDash) ? self::$userConfigDirDash : self::$userConfigDirClean; self::$userConfigPath = self::$userConfigDir . DIRECTORY_SEPARATOR . self::$userConfig; self::$plConfigPath = self::$options["baseDir"] . "vendor/pattern-lab/core/" . self::$plConfigPath; self::$dirAdded = true; // just in case the config directory doesn't exist at all if (!is_dir(self::$userConfigDir)) { mkdir(self::$userConfigDir); } } // make sure migrate doesn't happen by default $migrate = false; $diffVersion = false; // double-check the default config file exists if (!file_exists(self::$plConfigPath)) { Console::writeError("make sure <path>" . self::$plConfigPath . "</path> exists before trying to have Pattern Lab build the config.yml file automagically..."); } // set the default config using the pattern lab config try { $data = Yaml::parse(file_get_contents(self::$plConfigPath)); } catch (ParseException $e) { Console::writeError("Config parse error in <path>" . self::$plConfigPath . "</path>: " . $e->getMessage()); } // load the options from the default file self::loadOptions($data); // make sure these are copied $defaultOptions = self::$options; // check to see if the user config exists, if not create it if ($verbose) { Console::writeLine("configuring pattern lab..."); } if (!file_exists(self::$userConfigPath)) { $migrate = true; } else { try { $data = Yaml::parse(file_get_contents(self::$userConfigPath)); } catch (ParseException $e) { Console::writeError("Config parse error in <path>" . self::$userConfigPath . "</path>: " . $e->getMessage()); } self::loadOptions($data); } // compare version numbers $diffVersion = self::$options["v"] != $defaultOptions["v"] ? true : false; // run an upgrade and migrations if necessary if ($migrate || $diffVersion) { if ($verbose) { Console::writeInfo("upgrading your version of pattern lab..."); } if ($migrate) { if (!@copy(self::$plConfigPath, self::$userConfigPath)) { Console::writeError("make sure that Pattern Lab can write a new config to " . self::$userConfigPath . "..."); exit; } } else { self::$options = self::writeNewConfigFile(self::$options, $defaultOptions); } } // making sure the config isn't empty if (empty(self::$options) && $verbose) { Console::writeError("a set of configuration options is required to use Pattern Lab..."); exit; } // set-up the various dirs self::$options["coreDir"] = is_dir(self::$options["baseDir"] . "_core") ? self::$options["baseDir"] . "_core" : self::$options["baseDir"] . "core"; self::$options["exportDir"] = isset(self::$options["exportDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["exportDir"]) : self::$options["baseDir"] . "exports"; self::$options["packagesDir"] = isset(self::$options["packagesDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["packagesDir"]) : self::$options["baseDir"] . "packages"; self::$options["publicDir"] = isset(self::$options["publicDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["publicDir"]) : self::$options["baseDir"] . "public"; self::$options["scriptsDir"] = isset(self::$options["scriptsDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["scriptsDir"]) : self::$options["baseDir"] . "scripts"; self::$options["sourceDir"] = isset(self::$options["sourceDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["sourceDir"]) : self::$options["baseDir"] . "source"; self::$options["componentDir"] = self::$options["publicDir"] . "/patternlab-components"; self::$options["dataDir"] = self::$options["sourceDir"] . "/_data"; self::$options["patternExportDir"] = self::$options["exportDir"] . "/patterns"; self::$options["patternPublicDir"] = self::$options["publicDir"] . "/patterns"; self::$options["patternSourceDir"] = self::$options["sourceDir"] . "/_patterns"; // make sure styleguideExcludes is set to an array even if it's empty if (is_string(self::$options["styleGuideExcludes"])) { self::$options["styleGuideExcludes"] = array(); } // set the cacheBuster self::$options["cacheBuster"] = self::$options["cacheBusterOn"] == "false" ? 0 : time(); // provide the default for enable CSS. performance hog so it should be run infrequently self::$options["enableCSS"] = false; // which of these should be exposed in the front-end? self::$options["exposedOptions"] = array(); self::setExposedOption("cacheBuster"); self::setExposedOption("ishFontSize"); self::setExposedOption("ishMaximum"); self::setExposedOption("ishMinimum"); }
/** * Adds the config options to a var to be accessed from the rest of the system * If it's an old config or no config exists this will update and generate it. * @param {Boolean} whether we should print out the status of the config being loaded */ public static function init($baseDir = "", $verbose = true) { // make sure a base dir was supplied if (empty($baseDir)) { Console::writeError("need a base directory to initialize the config class..."); } // normalize the baseDir $baseDir = FileUtil::normalizePath($baseDir); // double-check the default config file exists if (!is_dir($baseDir)) { Console::writeError("make sure " . $baseDir . " exists..."); } // set the baseDir option self::$options["baseDir"] = $baseDir[strlen($baseDir) - 1] == DIRECTORY_SEPARATOR ? $baseDir : $baseDir . DIRECTORY_SEPARATOR; // set-up the paths self::$userConfigDirClean = self::$options["baseDir"] . self::$userConfigDirClean; self::$userConfigDirDash = self::$options["baseDir"] . self::$userConfigDirDash; self::$userConfigDir = is_dir(self::$userConfigDirDash) ? self::$userConfigDirDash : self::$userConfigDirClean; self::$userConfigPath = self::$userConfigDir . DIRECTORY_SEPARATOR . self::$userConfig; self::$plConfigPath = self::$options["baseDir"] . "vendor/pattern-lab/core/" . self::$plConfigPath; // can't add __DIR__ above so adding here if (!is_dir(self::$userConfigDir)) { mkdir(self::$userConfigDir); } // check to see if the user config exists, if not create it if ($verbose) { Console::writeLine("configuring pattern lab..."); } // make sure migrate doesn't happen by default $migrate = false; $diffVersion = false; $defaultOptions = array(); $userOptions = array(); // double-check the default config file exists if (!file_exists(self::$plConfigPath)) { Console::writeError("the default options for Pattern Lab don't seem to exist at <path>" . Console::getHumanReadablePath(self::$plConfigPath) . "</path>. please check on the install location of pattern lab..."); } // set the default config using the pattern lab config try { $defaultOptions = Yaml::parse(file_get_contents(self::$plConfigPath)); self::$options = array_merge(self::$options, $defaultOptions); } catch (ParseException $e) { Console::writeError("Config parse error in <path>" . Console::getHumanReadablePath(self::$plConfigPath) . "</path>: " . $e->getMessage()); } // double-check the user's config exists. if not mark that we should migrate the default one if (file_exists(self::$userConfigPath)) { try { $userOptions = Yaml::parse(file_get_contents(self::$userConfigPath)); self::$options = array_merge(self::$options, $userOptions); } catch (ParseException $e) { Console::writeError("Config parse error in <path>" . Console::getHumanReadablePath(self::$userConfigPath) . "</path>: " . $e->getMessage()); } } else { $migrate = true; } // compare version numbers $diffVersion = isset($userOptions["v"]) && $userOptions["v"] == $defaultOptions["v"] ? false : true; // run an upgrade and migrations if necessary if ($migrate || $diffVersion) { if ($verbose) { Console::writeInfo("upgrading your version of pattern lab..."); } if ($migrate) { if (!@copy(self::$plConfigPath, self::$userConfigPath)) { Console::writeError("make sure that Pattern Lab can write a new config to " . self::$userConfigPath . "..."); exit; } } else { self::$options = self::writeNewConfigFile(self::$options, $defaultOptions); } } // making sure the config isn't empty if (empty(self::$options) && $verbose) { Console::writeError("a set of configuration options is required to use Pattern Lab..."); exit; } // set-up the various dirs self::$options["configDir"] = self::$userConfigDir; self::$options["configPath"] = self::$userConfigPath; self::$options["coreDir"] = is_dir(self::$options["baseDir"] . "_core") ? self::$options["baseDir"] . "_core" : self::$options["baseDir"] . "core"; self::$options["exportDir"] = isset(self::$options["exportDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["exportDir"]) : self::$options["baseDir"] . "exports"; self::$options["publicDir"] = isset(self::$options["publicDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["publicDir"]) : self::$options["baseDir"] . "public"; self::$options["scriptsDir"] = isset(self::$options["scriptsDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["scriptsDir"]) : self::$options["baseDir"] . "scripts"; self::$options["sourceDir"] = isset(self::$options["sourceDir"]) ? self::$options["baseDir"] . self::cleanDir(self::$options["sourceDir"]) : self::$options["baseDir"] . "source"; self::$options["componentDir"] = isset(self::$options["componentDir"]) ? self::$options["publicDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["componentDir"]) : self::$options["publicDir"] . DIRECTORY_SEPARATOR . "patternlab-components"; self::$options["dataDir"] = isset(self::$options["dataDir"]) ? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["dataDir"]) : self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_data"; self::$options["patternExportDir"] = isset(self::$options["patternExportDir"]) ? self::$options["exportDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["patternExportDir"]) : self::$options["exportDir"] . DIRECTORY_SEPARATOR . "patterns"; self::$options["patternPublicDir"] = isset(self::$options["patternPublicDir"]) ? self::$options["publicDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["patternPublicDir"]) : self::$options["publicDir"] . DIRECTORY_SEPARATOR . "patterns"; self::$options["patternSourceDir"] = isset(self::$options["patternSourceDir"]) ? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["patternSourceDir"]) : self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_patterns"; self::$options["metaDir"] = isset(self::$options["metaDir"]) ? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["metaDir"]) : self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_meta/"; self::$options["annotationsDir"] = isset(self::$options["annotationsDir"]) ? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["annotationsDir"]) : self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_annotations/"; // set-up outputFileSuffixes self::$options["outputFileSuffixes"]["rendered"] = isset(self::$options["outputFileSuffixes"]["rendered"]) ? self::$options["outputFileSuffixes"]["rendered"] : ''; self::$options["outputFileSuffixes"]["rawTemplate"] = isset(self::$options["outputFileSuffixes"]["rawTemplate"]) ? self::$options["outputFileSuffixes"]["rawTemplate"] : ''; self::$options["outputFileSuffixes"]["markupOnly"] = isset(self::$options["outputFileSuffixes"]["markupOnly"]) ? self::$options["outputFileSuffixes"]["markupOnly"] : '.markup-only'; // handle a pre-2.1.0 styleguideKitPath before saving it if (isset(self::$options["styleguideKitPath"])) { self::$options["styleguideKitPath"] = self::$options["baseDir"] . self::cleanDir(self::getStyleguideKitPath(self::$options["styleguideKitPath"])); } // double-check a few directories are real and set-up FileUtil::checkPathFromConfig(self::$options["sourceDir"], self::$userConfigPath, "sourceDir"); FileUtil::checkPathFromConfig(self::$options["publicDir"], self::$userConfigPath, "publicDir"); // make sure styleguideExcludes is set to an array even if it's empty if (is_string(self::$options["styleGuideExcludes"])) { self::$options["styleGuideExcludes"] = array(); } // set the cacheBuster self::$options["cacheBuster"] = self::$options["cacheBusterOn"] == "false" ? 0 : time(); // provide the default for enable CSS. performance hog so it should be run infrequently self::$options["enableCSS"] = false; // which of these should be exposed in the front-end? self::$options["exposedOptions"] = array(); self::setExposedOption("cacheBuster"); self::setExposedOption("defaultPattern"); self::setExposedOption("defaultShowPatternInfo"); self::setExposedOption("ishFontSize"); self::setExposedOption("ishMaximum"); self::setExposedOption("ishMinimum"); self::setExposedOption("outputFileSuffixes"); self::setExposedOption("plugins"); }