Exemplo n.º 1
0
 /**
  * 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");
 }
Exemplo n.º 2
0
 /**
  * Add an option and associated value to the base Config
  * @param  {String}       the name of the option to be added
  * @param  {String}       the value of the option to be added
  *
  * @return {Boolean}      whether the set was successful
  */
 public static function setOption($optionName = "", $optionValue = "")
 {
     if (empty($optionName) || empty($optionValue)) {
         return false;
     }
     $arrayFinder = new ArrayFinder(self::$options);
     $arrayFinder->set($optionName, $optionValue);
     self::$options = $arrayFinder->get();
 }