1+ <?php
2+
3+ /*!
4+ * Pattern Data Class
5+ *
6+ * Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+ * Licensed under the MIT license
8+ *
9+ * Gather together all of the general information related to patterns into one central location
10+ *
11+ */
12+
13+ namespace PatternLab ;
14+
15+ use \PatternLab \Config ;
16+ use \PatternLab \PatternData \Exporters \DataLinkExporter ;
17+ use \PatternLab \PatternData \Exporters \DataMergeExporter ;
18+ use \PatternLab \PatternData \Exporters \NavItemsExporter ;
19+ use \PatternLab \PatternData \Exporters \PatternPartialsExporter ;
20+ use \PatternLab \PatternData \Exporters \PatternPathSrcExporter ;
21+ use \PatternLab \PatternData \Exporters \ViewAllPathsExporter ;
22+ use \PatternLab \PatternData \Helpers \LineageHelper ;
23+ use \PatternLab \PatternData \Helpers \PatternCodeHelper ;
24+ use \PatternLab \PatternData \Helpers \PatternStateHelper ;
25+ use \PatternLab \PatternData \Helpers \Plugins \KSSHelperPlugin ;
26+
27+ class PatternData {
28+
29+ public static $ store = array ();
30+ public static $ patternSubtype = "" ;
31+ public static $ patternSubtypeClean = "" ;
32+ public static $ patternSubtypeDash = "" ;
33+ public static $ patternSubtypeSet = false ;
34+ public static $ patternType = "" ;
35+ public static $ patternTypeClean = "" ;
36+ public static $ patternTypeDash = "" ;
37+ public static $ rules = array ();
38+ public static $ dirSep = DIRECTORY_SEPARATOR ;
39+
40+ /**
41+ * Gather all of the information related to the patterns
42+ */
43+ public static function gather ($ options = array ()) {
44+
45+ // load up the rules for parsing patterns and the directories
46+ self ::loadRules ($ options );
47+
48+ // iterate over the patterns & related data and regenerate the entire site if they've changed
49+ $ patternObjects = new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator (__DIR__ .Config::$ options ["patternSourceDir " ]), \RecursiveIteratorIterator::SELF_FIRST );
50+ $ patternObjects ->setFlags (\FilesystemIterator::SKIP_DOTS );
51+
52+ $ patternObjects = iterator_to_array ($ patternObjects );
53+ ksort ($ patternObjects );
54+
55+ foreach ($ patternObjects as $ name => $ object ) {
56+
57+ $ ext = $ object ->getExtension ();
58+ $ isDir = $ object ->isDir ();
59+ $ isFile = $ object ->isFile ();
60+
61+ $ path = str_replace (__DIR__ .Config::$ options ["patternSourceDir " ],"" ,$ object ->getPath ());
62+ $ pathName = str_replace (__DIR__ .Config::$ options ["patternSourceDir " ],"" ,$ object ->getPathname ());
63+ $ name = $ object ->getFilename ();
64+ $ depth = substr_count ($ pathName ,DIRECTORY_SEPARATOR );
65+
66+ // iterate over the rules and see if the current file matches one, if so run the rule
67+ foreach (self ::$ rules as $ rule ) {
68+ if ($ rule ->test ($ depth , $ ext , $ isDir , $ isFile , $ name )) {
69+ $ rule ->run ($ depth , $ ext , $ path , $ pathName , $ name );
70+ }
71+ }
72+
73+ }
74+
75+ // make sure all of the appropriate pattern data is pumped into $this->d for rendering patterns
76+ $ dataLinkExporter = new DataLinkExporter ();
77+ $ dataLinkExporter ->run ();
78+
79+ // make sure all of the appropriate pattern data is pumped into $this->d for rendering patterns
80+ $ dataMergeExporter = new DataMergeExporter ();
81+ $ dataMergeExporter ->run ();
82+
83+ // add the lineage info to PatternData::$store
84+ $ lineageHelper = new LineageHelper ();
85+ $ lineageHelper ->run ();
86+
87+ // using the lineage info update the pattern states on PatternData::$store
88+ $ patternStateHelper = new PatternStateHelper ();
89+ $ patternStateHelper ->run ();
90+
91+ // set-up code pattern paths
92+ $ ppdExporter = new PatternPathSrcExporter ();
93+ $ patternPathSrc = $ ppdExporter ->run ();
94+ $ options = array ();
95+ $ options ["patternPaths " ] = $ patternPathSrc ;
96+
97+ // render out all of the patterns and store the generated info in PatternData::$store
98+ $ patternCodeHelper = new PatternCodeHelper ($ options );
99+ $ patternCodeHelper ->run ();
100+
101+ // loop through and check KSS (this will change in the future)
102+ $ KSSHelper = new KSSHelperPlugin ($ options );
103+ $ KSSHelper ->run ();
104+
105+ }
106+
107+ /**
108+ * Load all of the rules related to Pattern Data
109+ */
110+ public static function loadRules ($ options ) {
111+ foreach (glob (__DIR__ ."/PatternData/Rules/*.php " ) as $ filename ) {
112+ $ rule = str_replace (".php " ,"" ,str_replace (__DIR__ ."/PatternData/Rules/ " ,"" ,$ filename ));
113+ $ ruleClass = "\PatternLab\PatternData\Rules \\" .$ rule ;
114+ self ::$ rules [] = new $ ruleClass ($ options );
115+ }
116+ }
117+
118+ }
0 commit comments