Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit de79951

Browse files
committed
adding util class
1 parent 0ae0992 commit de79951

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

core/lib/PatternLab/Util.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
/*!
4+
* Util Class
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* Generic utilities for Pattern Lab
10+
*
11+
*/
12+
13+
namespace PatternLab;
14+
15+
use \PatternLab\Config;
16+
17+
class Util {
18+
19+
/**
20+
* Delete patterns and user-created directories and files in public/
21+
*/
22+
public static function cleanPublic() {
23+
24+
// make sure patterns exists before trying to clean it
25+
if (is_dir(Config::$options["patternPublicDir"])) {
26+
27+
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(Config::$options["patternPublicDir"]), \RecursiveIteratorIterator::CHILD_FIRST);
28+
29+
// make sure dots are skipped
30+
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
31+
32+
// for each file figure out what to do with it
33+
foreach($objects as $name => $object) {
34+
35+
if ($object->isDir()) {
36+
// if this is a directory remove it
37+
rmdir($name);
38+
} else if ($object->isFile() && ($object->getFilename() != "README")) {
39+
// if this is a file remove it
40+
unlink($name);
41+
}
42+
43+
}
44+
45+
}
46+
47+
// scan source/ & public/ to figure out what directories might need to be cleaned up
48+
$sourceDirs = glob(Config::$options["sourceDir"]."/*",GLOB_ONLYDIR);
49+
$publicDirs = glob(Config::$options["publicDir"]."/*",GLOB_ONLYDIR);
50+
51+
// make sure some directories aren't deleted
52+
$ignoreDirs = array("styleguide","snapshots");
53+
foreach ($ignoreDirs as $ignoreDir) {
54+
$key = array_search(Config::$options["publicDir"]."/".$ignoreDir,$publicDirs);
55+
if ($key !== false){
56+
unset($publicDirs[$key]);
57+
}
58+
}
59+
60+
// compare source dirs against public. remove those dirs w/ an underscore in source/ from the public/ list
61+
foreach ($sourceDirs as $sourceDir) {
62+
$cleanDir = str_replace(Config::$options["sourceDir"]."/","",$sourceDir);
63+
if ($cleanDir[0] == "_") {
64+
$key = array_search(Config::$options["publicDir"]."/".str_replace("_","",$cleanDir),$publicDirs);
65+
if ($key !== false){
66+
unset($publicDirs[$key]);
67+
}
68+
}
69+
}
70+
71+
// for the remaining dirs in public delete them and their files
72+
foreach ($publicDirs as $dir) {
73+
74+
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::CHILD_FIRST);
75+
76+
// make sure dots are skipped
77+
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
78+
79+
foreach($objects as $name => $object) {
80+
81+
if ($object->isDir()) {
82+
rmdir($name);
83+
} else if ($object->isFile()) {
84+
unlink($name);
85+
}
86+
87+
}
88+
89+
rmdir($dir);
90+
91+
}
92+
93+
}
94+
95+
/**
96+
* Go through data and replace any values that match items from the link.array
97+
* @param {String} an entry from one of the list-based config entries
98+
*
99+
* @return {String} trimmed version of the given $v var
100+
*/
101+
public static function compareReplace(&$value) {
102+
if (is_string($value)) {
103+
$valueCheck = strtolower($value);
104+
$valueThin = str_replace("link.","",$valueCheck);
105+
if ((strpos($valueCheck, 'link.') !== false) && array_key_exists($valueThin,Data::$store["link"])) {
106+
$value = Data::$store["link"][$valueThin];
107+
}
108+
}
109+
110+
}
111+
112+
/**
113+
* Lowercase the given string. Used in the array_walk() function in __construct as a sanity check
114+
* @param {String} an entry from one of the list-based config entries
115+
*
116+
* @return {String} lowercased version of the given $v var
117+
*/
118+
public static function strtolower(&$v) {
119+
$v = strtolower($v);
120+
}
121+
122+
/**
123+
* Trim a given string. Used in the array_walk() function in __construct as a sanity check
124+
* @param {String} an entry from one of the list-based config entries
125+
*
126+
* @return {String} trimmed version of the given $v var
127+
*/
128+
public static function trim(&$v) {
129+
$v = trim($v);
130+
}
131+
132+
/**
133+
* Write out the time tracking file so the content sync service will work. A holdover
134+
* from how I put together the original AJAX polling set-up.
135+
*/
136+
public static function updateChangeTime() {
137+
138+
if (is_dir(Config::$options["publicDir"]."/")) {
139+
file_put_contents(Config::$options["publicDir"]."/latest-change.txt",time());
140+
} else {
141+
print "Either the public directory for Pattern Lab doesn't exist or the builder is in the wrong location. Please fix.";
142+
exit;
143+
}
144+
145+
}
146+
147+
}

0 commit comments

Comments
 (0)