1
0
mirror of https://github.com/pattern-lab/patternlab-php.git synced 2025-01-17 22:29:12 +01:00

pattern info and pattern info rules standardization

This commit is contained in:
Dave Olsen 2014-05-19 16:49:24 -04:00
parent f930350f51
commit f0821d984f
7 changed files with 657 additions and 0 deletions

View File

@ -0,0 +1,74 @@
<?php
/*!
* Pattern Lab Pattern Info Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy public/ into a snapshot/v* directory
*
*/
namespace PatternLab;
class PatternInfo {
public static $bi = 0;
public static $ni = 0;
public static $d = array();
public static $navItems = array();
public static $patternLineages = array();
public static $patternPaths = array();
public static $patternPartials = array();
public static $patternSubtype = "";
public static $patternSubtypeSet = false;
public static $patternSubtypeDash = "";
public static $patternType = "";
public static $patternTypes = array();
public static $patternTypeDash = "";
public static $rules = array();
public static $viewAllPaths = array();
public static function gather($options) {
// iterate over the patterns & related data and regenerate the entire site if they've changed
$patternObjects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($options["patternSourceDir"]), \RecursiveIteratorIterator::SELF_FIRST);
$patternObjects->setFlags(\FilesystemIterator::SKIP_DOTS);
$patternObjects = iterator_to_array($patternObjects);
ksort($patternObjects);
self::$d["link"] = array();
self::$navItems["patternTypes"] = array();
foreach ($patternObjects as $name => $object) {
$ext = $object->getExtension();
$isDir = $object->isDir();
$isFile = $object->isFile();
$path = $object->getPath();
$pathName = $object->getPathname();
$name = $object->getFilename();
$depth = substr_count(str_replace($options["patternSourceDir"],"",$pathName),DIRECTORY_SEPARATOR);
foreach (self::$rules as $rule) {
if ($rule->testRule($depth, $ext, $isDir, $isFile, $name)) {
$rule->runRule($depth, $ext, $path, $pathName, $name);
}
}
}
}
public static function loadRules($options) {
foreach (glob(__DIR__."/PatternInfoRules/*.php") as $filename) {
$rule = str_replace(".php","",str_replace(__DIR__."/PatternInfoRules/","",$filename));
$ruleClass = "PatternLab\PatternInfoRules\\".$rule;
self::$rules[] = new $ruleClass($options);
}
}
}

View File

@ -0,0 +1,70 @@
<?php
/*!
* Pattern Lab Pattern Info Rules Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
*/
namespace PatternLab;
class PatternInfoRule {
public $depthProp;
public $extProp;
public $isDirProp;
public $isFileProp;
public $searchProp;
protected $sourceDir;
protected $dirSep;
public function __construct($options) {
$this->patternSourceDir = $options["patternSourceDir"];
}
public function testRule($depth, $ext, $isDir, $isFile, $name) {
if (($this->depthProp != 3) && ($depth != $this->depthProp)) {
return false;
}
if (($ext == $this->extProp) && ($isDir == $this->isDirProp) && ($isFile == $this->isFileProp)) {
if ($this->searchProp != "") {
return (strpos($name,$this->searchProp) !== false) ? true : false;
} else {
return true;
}
}
return false;
}
/**
* Get the name for a given pattern sans any possible digits used for reordering
* @param {String} the pattern based on the filesystem name
* @param {Boolean} whether or not to strip slashes from the pattern name
*
* @return {String} a lower-cased version of the pattern name
*/
protected function getPatternName($pattern, $clean = true) {
$patternBits = explode("-",$pattern,2);
$patternName = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern;
return ($clean) ? (str_replace("-"," ",$patternName)) : $patternName;
}
protected function findKey($haystack,$needle,$attribute) {
$key = false;
foreach ($haystack as $strawKey => $strawValues) {
if ($strawValues[$attribute] == $needle) {
$key = $strawKey;
break;
}
}
return $key;
}
}

View File

@ -0,0 +1,90 @@
<?php
/*!
* Pattern Lab Pattern Info Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy public/ into a snapshot/v* directory
*
*/
namespace PatternLab\PatternInfoRules;
use PatternLab\PatternInfo;
use PatternLab\DocumentationParser;
class DocumentationRule extends \PatternLab\PatternInfoRule {
public function __construct($options) {
parent::__construct($options);
$this->depthProp = 3; // 3 means that depth won't be checked
$this->extProp = "md";
$this->isDirProp = false;
$this->isFileProp = true;
$this->searchProp = "";
}
public function runRule($depth, $ext, $path, $pathName, $name) {
$bi = PatternInfo::$bi;
$ni = PatternInfo::$ni;
$patternSubtype = PatternInfo::$patternSubtype;
$patternTypeDash = PatternInfo::$patternTypeDash;
$patternFull = $name; // 00-colors.md
$pattern = str_replace(".".$this->extProp,"",$patternFull); // 00-colors
// make sure the pattern isn't hidden
if ($patternFull[0] != "_") {
// parse data
$text = file_get_contents($pathName);
list($yaml,$markdown) = DocumentationParser::parse($text);
if ($depth == 1) {
// add to pattern subtype
if (isset($yaml["title"])) {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeLC"] = strtolower($yaml["title"]);
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeUC"] = ucwords($yaml["title"]);
unset($yaml["title"]);
}
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeDesc"] = $markdown;
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeMeta"] = $yaml;
} else if ($depth == 2) {
// get base info
$patternDash = $this->getPatternName($pattern,false); // colors
$patternPartial = $patternTypeDash."-".$patternDash; // atoms-colors
// see if the pattern is already part of the nav
$key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"],$patternPartial,"patternPartial");
if ($key === false) {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][] = array();
$a = PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"];
end($a);
$key = key($a);
}
// add to the pattern
if (isset($yaml["title"])) {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key]["patternName"] = ucwords($yaml["title"]);
unset($yaml["title"]);
}
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key]["patternDesc"] = $markdown;
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key]["patternMeta"] = $yaml;
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key]["patternPartial"] = $patternPartial;
}
}
}
}

View File

@ -0,0 +1,135 @@
<?php
/*!
* Pattern Lab Pattern Info Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy public/ into a snapshot/v* directory
*
*/
namespace PatternLab\PatternInfoRules;
use PatternLab\PatternInfo;
class PatternRule extends \PatternLab\PatternInfoRule {
protected $patternExtension;
public function __construct($options) {
parent::__construct($options);
$this->patternExtension = $options["patternExtension"];
$this->depthProp = 3; // 3 means that depth won't be checked
$this->extProp = $this->patternExtension;
$this->isDirProp = false;
$this->isFileProp = true;
$this->searchProp = "";
}
public function runRule($depth, $ext, $path, $pathName, $name) {
$bi = PatternInfo::$bi;
$ni = PatternInfo::$ni;
$patternSubtype = PatternInfo::$patternSubtype;
$patternSubtypeDash = PatternInfo::$patternSubtypeDash;
$patternSubtypeSet = PatternInfo::$patternSubtypeSet;
$patternType = PatternInfo::$patternType;
$patternTypeDash = PatternInfo::$patternTypeDash;
$dirSep = DIRECTORY_SEPARATOR;
$patternFull = $name; // 00-colors.mustache
$pattern = str_replace(".".$this->patternExtension,"",$patternFull); // 00-colors
// check for pattern state
$patternState = "";
if (strpos($pattern,"@") !== false) {
$patternBits = explode("@",$pattern,2);
$pattern = $patternBits[0];
$patternState = $patternBits[1];
}
if ($patternSubtypeSet) {
$patternPath = $patternType.$dirSep.$patternSubtype.$dirSep.$pattern; // 00-atoms/01-global/00-colors
$patternPathDash = str_replace($dirSep,"-",$patternPath); // 00-atoms-01-global-00-colors (file path)
} else {
$patternPath = $patternType.$dirSep.$pattern; // 00-atoms/00-colors
$patternPathDash = str_replace($dirSep,"-",$patternPath); // 00-atoms-00-colors (file path)
}
// track to see if this pattern should get rendered
$render = false;
// make sure the pattern isn't hidden
if ($patternFull[0] != "_") {
// set-up the names
$patternDash = $this->getPatternName($pattern,false); // colors
$patternClean = str_replace("-"," ",$patternDash); // colors (dashes replaced with spaces)
$patternPartial = $patternTypeDash."-".$patternDash; // atoms-colors
// see if the pattern name is already set via .md file
$patternName = ucwords($patternClean);
if ($depth == 2) {
$key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"],$patternPartial,"patternPartial");
if ($key !== false) {
$patternName = PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key]["patternName"];
}
}
// set-up the info for the nav
$patternInfo = array("patternPath" => $patternPathDash."/".$patternPathDash.".html",
"patternSrcPath" => str_replace($this->patternSourceDir,"",$pathName),
"patternName" => $patternName,
"patternState" => $patternState,
"patternPartial" => $patternPartial);
// add to the nav
if ($depth == 1) {
$key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternItems"],$patternPartial,"patternPartial");
if ($key !== false) {
PatternInfo::$navItems["patternTypes"][$bi]["patternItems"][$key] = array_merge(PatternInfo::$navItems["patternTypes"][$bi]["patternItems"][$key], $patternInfo);
} else {
PatternInfo::$navItems["patternTypes"][$bi]["patternItems"][] = $patternInfo;
}
} else {
$key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"],$patternPartial,"patternPartial");
if ($key !== false) {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key] = array_merge(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key], $patternInfo);
} else {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][] = $patternInfo;
}
}
// add to the link var for inclusion in patterns
PatternInfo::$d["link"][$patternPartial] = "../../patterns/".$patternPathDash."/".$patternPathDash.".html";
// yup, this pattern should get rendered
$render = true;
} else {
// replace the underscore to generate a good file pattern name
$patternDash = $this->getPatternName(str_replace("_","",$pattern),false); // colors
$patternPartial = $patternTypeDash."-".$patternDash; // atoms-colors
}
// add all patterns to patternPaths
$patternSrcPath = str_replace($this->patternSourceDir,"",str_replace(".".$this->patternExtension,"",$pathName));
$patternDestPath = $patternPathDash;
PatternInfo::$patternPaths[$patternTypeDash][$patternDash] = array("patternSrcPath" => $patternSrcPath,
"patternDestPath" => $patternDestPath,
"patternPartial" => $patternPartial,
"patternState" => $patternState,
"patternType" => $patternTypeDash,
"render" => $render);
}
}

View File

@ -0,0 +1,62 @@
<?php
/*!
* Pattern Lab Pattern Info Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy public/ into a snapshot/v* directory
*
*/
namespace PatternLab\PatternInfoRules;
use PatternLab\PatternInfo;
class PatternSubtypeRule extends \PatternLab\PatternInfoRule {
public function __construct($options) {
parent::__construct($options);
$this->depthProp = 1;
$this->extProp = "";
$this->isDirProp = true;
$this->isFileProp = false;
$this->searchProp = "";
}
public function runRule($depth, $ext, $path, $pathName, $name) {
// is this the first bucket to be set?
PatternInfo::$ni = (!PatternInfo::$patternSubtypeSet) ? 0 : PatternInfo::$ni + 1;
$ni = PatternInfo::$ni;
$bi = PatternInfo::$bi;
$patternTypeDash = PatternInfo::$patternTypeDash;
// set-up the names
$patternSubtype = $name; // 02-blocks
$patternSubtypeDash = $this->getPatternName($name,false); // blocks
$patternSubtypeClean = str_replace("-"," ",$patternSubtypeDash); // blocks (dashes replaced with spaces)
// add to patternPartials
PatternInfo::$patternPartials[$patternTypeDash."-".$patternSubtypeDash] = array();
// add a new patternSubtype to the nav
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni] = array("patternSubtypeLC" => strtolower($patternSubtypeClean),
"patternSubtypeUC" => ucwords($patternSubtypeClean),
"patternSubtype" => $patternSubtype,
"patternSubtypeDash" => $patternSubtypeDash,
"patternSubtypeItems" => array());
// starting a new set of pattern types. it might not have any pattern subtypes
PatternInfo::$patternSubtype = $patternSubtype;
PatternInfo::$patternSubtypeDash = $patternSubtypeDash;
PatternInfo::$patternSubtypeSet = true;
}
}

View File

@ -0,0 +1,66 @@
<?php
/*!
* Pattern Lab Pattern Info Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy public/ into a snapshot/v* directory
*
*/
namespace PatternLab\PatternInfoRules;
use PatternLab\PatternInfo;
class PatternTypeRule extends \PatternLab\PatternInfoRule {
public $depthProp;
public $extProp;
public $isDirProp;
public $isFileProp;
public $searchProp;
public function __construct($options) {
parent::__construct($options);
$this->depthProp = 0;
$this->extProp = "";
$this->isDirProp = true;
$this->isFileProp = false;
$this->searchProp = "";
}
public function runRule($depth, $ext, $path, $pathName, $name) {
PatternInfo::$bi = (count(PatternInfo::$navItems["patternTypes"]) == 0) ? 0 : PatternInfo::$bi + 1;
$bi = PatternInfo::$bi;
// set-up the names
$patternType = $name; // 00-atoms
$patternTypeDash = $this->getPatternName($name,false); // atoms
$patternTypeClean = str_replace("-"," ",$patternTypeDash); // atoms (dashes replaced with spaces)
// add to pattern types & pattern paths
$patternTypes[] = $patternType;
$patternPaths[$patternTypeDash] = array();
// add a new patternType to the nav
PatternInfo::$navItems["patternTypes"][$bi] = array("patternTypeLC" => strtolower($patternTypeClean),
"patternTypeUC" => ucwords($patternTypeClean),
"patternType" => $patternType,
"patternTypeDash" => $patternTypeDash,
"patternTypeItems" => array(),
"patternItems" => array());
// starting a new set of pattern types. it might not have any pattern subtypes
PatternInfo::$patternSubtypeSet = false;
PatternInfo::$patternType = $patternType;
PatternInfo::$patternTypeDash = $patternTypeDash;
}
}

View File

@ -0,0 +1,160 @@
<?php
/*!
* Pattern Lab Pattern Info Class - v0.7.12
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Copy public/ into a snapshot/v* directory
*
*/
namespace PatternLab\PatternInfoRules;
use PatternLab\PatternInfo;
use PatternLab\JSON;
class PseudoPatternRule extends \PatternLab\PatternInfoRule {
protected $patternExtension;
public function __construct($options) {
parent::__construct($options);
$this->patternExtension = $options["patternExtension"];
$this->depthProp = 3; // 3 means that depth won't be checked
$this->extProp = "json";
$this->isDirProp = false;
$this->isFileProp = true;
$this->searchProp = "~";
}
public function runRule($depth, $ext, $path, $pathName, $name) {
$bi = PatternInfo::$bi;
$ni = PatternInfo::$ni;
$patternSubtype = PatternInfo::$patternSubtype;
$patternSubtypeDash = PatternInfo::$patternSubtypeDash;
$patternSubtypeSet = PatternInfo::$patternSubtypeSet;
$patternType = PatternInfo::$patternType;
$patternTypeDash = PatternInfo::$patternTypeDash;
$dirSep = DIRECTORY_SEPARATOR;
$patternSubtypeInclude = ($patternSubtypeSet) ? $patternSubtype."-" : "";
$patternFull = $name;
if ($patternFull[0] != "_") {
// check for a pattern state
$patternState = "";
$patternBits = explode("@",$patternFull,2);
if (isset($patternBits[1])) {
$patternState = str_replace(".json","",$patternBits[1]);
$patternFull = preg_replace("/@(.*?)\./",".",$patternFull);
}
// set-up the names
// $patternFull is defined above 00-colors.mustache
$patternBits = explode("~",$patternFull);
$patternBase = $patternBits[0].".".$this->patternExtension; // 00-homepage.mustache
$patternBaseDash = $this->getPatternName($patternBits[0],false); // homepage
$patternBaseJSON = $patternBits[0].".json"; // 00-homepage.json
$stripJSON = str_replace(".json","",$patternBits[1]);
$patternBitClean = preg_replace("/@(.*?)/","",$patternBits[0]);
$pattern = $patternBitClean."-".$stripJSON; // 00-homepage-00-emergency
$patternInt = $patternBitClean."-".$this->getPatternName($stripJSON, false); // 00-homepage-emergency
$patternDash = $this->getPatternName($patternInt,false); // homepage-emergency
$patternClean = str_replace("-"," ",$patternDash); // homepage emergency
$patternPartial = $patternTypeDash."-".$patternDash; // pages-homepage-emergency
// add to patternPaths
if ($patternSubtypeSet) {
$patternPath = $patternType.$dirSep.$patternSubtype.$dirSep.$pattern; // 00-atoms/01-global/00-colors
$patternPathDash = str_replace($dirSep,"-",$patternPath); // 00-atoms-01-global-00-colors (file path)
} else {
$patternPath = $patternType.$dirSep.$pattern; // 00-atoms/00-colors
$patternPathDash = str_replace($dirSep,"-",$patternPath); // 00-atoms-00-colors (file path)
}
// add all patterns to patternPaths
$patternSrcPath = PatternInfo::$patternPaths[$patternTypeDash][$patternBaseDash]["patternSrcPath"];
$patternDestPath = $patternPathDash;
PatternInfo::$patternPaths[$patternTypeDash][$patternDash] = array("patternSrcPath" => $patternSrcPath,
"patternDestPath" => $patternDestPath,
"patternPartial" => $patternPartial,
"patternState" => $patternState,
"patternType" => $patternTypeDash,
"render" => true);
// see if the pattern name is already set via .md file
$patternName = ucwords($patternClean);
if ($depth == 2) {
$key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"],$patternPartial,"patternPartial");
if ($key !== false) {
$patternName = PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key]["patternName"];
}
}
// set-up the info for the nav
$patternInfo = array("patternPath" => $patternPathDash."/".$patternPathDash.".html",
"patternSrcPath" => str_replace($this->patternSourceDir,"",preg_replace("/\~(.*)\.json/",".".$this->patternExtension,$pathName)),
"patternName" => $patternName,
"patternState" => $patternState,
"patternPartial" => $patternPartial);
// add to the nav
if ($depth == 1) {
$key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternItems"],$patternPartial,"patternPartial");
if ($key !== false) {
PatternInfo::$navItems["patternTypes"][$bi]["patternItems"][$key] = array_merge(PatternInfo::$navItems["patternTypes"][$bi]["patternItems"][$key], $patternInfo);
} else {
PatternInfo::$navItems["patternTypes"][$bi]["patternItems"][] = $patternInfo;
}
} else {
if ($key = $this->findKey(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"],$patternPartial,"patternPartial")) {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key] = array_merge(PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key], $patternInfo);
} else {
PatternInfo::$navItems["patternTypes"][$bi]["patternTypeItems"][$ni]["patternSubtypeItems"][$key] = $patternInfo;
}
}
// add to the link var for inclusion in patterns
PatternInfo::$d["link"][$patternPartial] = "../../patterns/".$patternPathDash."/".$patternPathDash.".html";
// get the base data
$patternDataBase = array();
if (file_exists($path."/".$patternBaseJSON)) {
$data = file_get_contents($path."/".$patternBaseJSON);
$patternDataBase = json_decode($data,true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($patternBaseJSON,$jsonErrorMessage,$data);
}
}
// get the special pattern data
$data = file_get_contents($pathName);
$patternData = (array) json_decode($data);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($name,$jsonErrorMessage,$data);
}
// merge them for the file
if (!isset(PatternInfo::$d["patternSpecific"][$patternPartial])) {
PatternInfo::$d["patternSpecific"][$patternPartial] = array();
PatternInfo::$d["patternSpecific"][$patternPartial]["data"] = array();
PatternInfo::$d["patternSpecific"][$patternPartial]["listItems"] = array();
}
if (is_array($patternDataBase) && is_array($patternData)) {
PatternInfo::$d["patternSpecific"][$patternPartial]["data"] = array_merge($patternDataBase, $patternData);
}
}
}
}