@@ -74,6 +74,10 @@ |
||
74 | 74 | |
75 | 75 | |
76 | 76 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
77 | + |
|
78 | + /** |
|
79 | + * @param Template $template |
|
80 | + */ |
|
77 | 81 | private function doPostProcessing($template) { |
78 | 82 | $template->addHook('//*[@transphporm]', new Hook\PostProcess()); |
79 | 83 | return $template; |
@@ -41,9 +41,9 @@ discard block |
||
41 | 41 | } |
42 | 42 | |
43 | 43 | public function setLocale($locale) { |
44 | - $format = new \Transphporm\Module\Format($locale); |
|
45 | - $this->modules[get_class($format)] = $format; |
|
46 | - } |
|
44 | + $format = new \Transphporm\Module\Format($locale); |
|
45 | + $this->modules[get_class($format)] = $format; |
|
46 | + } |
|
47 | 47 | |
48 | 48 | public function addPath($dir) { |
49 | 49 | $this->filePath->addPath($dir); |
@@ -82,15 +82,15 @@ discard block |
||
82 | 82 | |
83 | 83 | //Load a template, firstly check if it's a file or a valid string |
84 | 84 | private function loadTemplate() { |
85 | - $result = ['body' => $this->template, 'headers' => []]; |
|
85 | + $result = ['body' => $this->template, 'headers' => []]; |
|
86 | 86 | if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
87 | 87 | return $result; |
88 | 88 | } |
89 | 89 | |
90 | - private function loadTemplateFromFile($file) { |
|
91 | - $xml = $this->cache->load($this->template, filemtime($this->template)); |
|
92 | - return $xml ? $xml : ['body' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
93 | - } |
|
90 | + private function loadTemplateFromFile($file) { |
|
91 | + $xml = $this->cache->load($this->template, filemtime($this->template)); |
|
92 | + return $xml ? $xml : ['body' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | public function setCache(\ArrayAccess $cache) { |
96 | 96 | $this->cache = new Cache($cache); |
@@ -57,8 +57,8 @@ |
||
57 | 57 | |
58 | 58 | $cachedOutput = $this->loadTemplate(); |
59 | 59 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
60 | - $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
|
61 | - $tssCache = new SheetLoader($this->cache, $this->filePath, $this->tss, $template->getPrefix(), $this->time); |
|
60 | + $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>'.$cachedOutput['body'].'</template>'); |
|
61 | + $tssCache = new SheetLoader($this->cache, $this->filePath, $this->tss, $template->getPrefix(), $this->time); |
|
62 | 62 | $valueParser = new Parser\Value($functionSet); |
63 | 63 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
64 | 64 |
@@ -27,7 +27,9 @@ discard block |
||
27 | 27 | $this->cache = new Cache(new \ArrayObject()); |
28 | 28 | $this->filePath = new FilePath(); |
29 | 29 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
30 | - foreach ($modules as $module) $this->loadModule(new $module); |
|
30 | + foreach ($modules as $module) { |
|
31 | + $this->loadModule(new $module); |
|
32 | + } |
|
31 | 33 | } |
32 | 34 | |
33 | 35 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
@@ -62,7 +64,9 @@ discard block |
||
62 | 64 | $valueParser = new Parser\Value($functionSet); |
63 | 65 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
64 | 66 | |
65 | - foreach ($this->modules as $module) $module->load($this->config); |
|
67 | + foreach ($this->modules as $module) { |
|
68 | + $module->load($this->config); |
|
69 | + } |
|
66 | 70 | |
67 | 71 | $tssCache->processRules($template, $this->config); |
68 | 72 | |
@@ -83,7 +87,9 @@ discard block |
||
83 | 87 | //Load a template, firstly check if it's a file or a valid string |
84 | 88 | private function loadTemplate() { |
85 | 89 | $result = ['body' => $this->template, 'headers' => []]; |
86 | - if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
|
90 | + if (strpos($this->template, "\n") === false && is_file($this->template)) { |
|
91 | + $result = $this->loadTemplateFromFile($this->template); |
|
92 | + } |
|
87 | 93 | return $result; |
88 | 94 | } |
89 | 95 | |
@@ -102,6 +108,8 @@ discard block |
||
102 | 108 | |
103 | 109 | public function __destruct() { |
104 | 110 | //Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks |
105 | - if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); |
|
111 | + if (isset($this->config)) { |
|
112 | + $this->config->getCssToXpath()->cleanup(); |
|
113 | + } |
|
106 | 114 | } |
107 | 115 | } |
@@ -30,6 +30,9 @@ discard block |
||
30 | 30 | return $rules; |
31 | 31 | } |
32 | 32 | |
33 | + /** |
|
34 | + * @param integer $indexStart |
|
35 | + */ |
|
33 | 36 | private function parseTokens($indexStart) { |
34 | 37 | $this->rules = []; |
35 | 38 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
@@ -55,6 +58,10 @@ discard block |
||
55 | 58 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
56 | 59 | } |
57 | 60 | |
61 | + /** |
|
62 | + * @param Tokens $selector |
|
63 | + * @param integer $index |
|
64 | + */ |
|
58 | 65 | private function CssToRules($selector, $index, $properties, $line) { |
59 | 66 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
60 | 67 | $rules = []; |
@@ -78,6 +85,9 @@ discard block |
||
78 | 85 | return $rules; |
79 | 86 | } |
80 | 87 | |
88 | + /** |
|
89 | + * @param integer $indexStart |
|
90 | + */ |
|
81 | 91 | private function processingInstructions($token, $indexStart) { |
82 | 92 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
83 | 93 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -24,7 +24,9 @@ discard block |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | public function parse($indexStart = 0) { |
27 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
27 | + if (!empty($this->rules)) { |
|
28 | + return $this->rules['rules']; |
|
29 | + } |
|
28 | 30 | $rules = $this->parseTokens($indexStart); |
29 | 31 | $this->checkError($rules); |
30 | 32 | return $rules; |
@@ -35,8 +37,9 @@ discard block |
||
35 | 37 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
36 | 38 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
37 | 39 | $this->rules = array_merge($this->rules, $processing); |
40 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
41 | + $this->addRules($token, $indexStart++); |
|
38 | 42 | } |
39 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart++); |
|
40 | 43 | } |
41 | 44 | |
42 | 45 | return $this->rules; |
@@ -46,13 +49,17 @@ discard block |
||
46 | 49 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
47 | 50 | |
48 | 51 | $this->tss->skip(count($selector)); |
49 | - if (count($selector) === 0) return; |
|
52 | + if (count($selector) === 0) { |
|
53 | + return; |
|
54 | + } |
|
50 | 55 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
51 | 56 | $this->rules = $this->writeRule($this->rules, $newRules); |
52 | 57 | } |
53 | 58 | |
54 | 59 | private function checkError($rules) { |
55 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
60 | + if (empty($rules) && count($this->tss) > 0) { |
|
61 | + throw new \Exception('No TSS rules parsed'); |
|
62 | + } |
|
56 | 63 | } |
57 | 64 | |
58 | 65 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -79,7 +86,9 @@ discard block |
||
79 | 86 | } |
80 | 87 | |
81 | 88 | private function processingInstructions($token, $indexStart) { |
82 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
89 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
90 | + return false; |
|
91 | + } |
|
83 | 92 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
84 | 93 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
85 | 94 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -13,6 +13,10 @@ discard block |
||
13 | 13 | private $time; |
14 | 14 | private $import = []; |
15 | 15 | |
16 | + /** |
|
17 | + * @param string $tss |
|
18 | + * @param string $prefix |
|
19 | + */ |
|
16 | 20 | public function __construct(Cache $cache, FilePath $filePath, $tss, $prefix, $time) { |
17 | 21 | $this->cache = $cache; |
18 | 22 | $this->filePath = $filePath; |
@@ -54,6 +58,9 @@ discard block |
||
54 | 58 | return $rules; |
55 | 59 | } |
56 | 60 | |
61 | + /** |
|
62 | + * @param Template $template |
|
63 | + */ |
|
57 | 64 | public function processRules($template, \Transphporm\Config $config) { |
58 | 65 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
59 | 66 | |
@@ -67,6 +74,11 @@ discard block |
||
67 | 74 | } |
68 | 75 | |
69 | 76 | //Load the TSS |
77 | + |
|
78 | + /** |
|
79 | + * @param Parser\CssToXpath $cssToXpath |
|
80 | + * @param Parser\Value $valueParser |
|
81 | + */ |
|
70 | 82 | public function getRules($tss, $cssToXpath, $valueParser) { |
71 | 83 | if (is_file($tss)) { |
72 | 84 | //$rules = $this->cache->load($tss); |
@@ -79,6 +91,10 @@ discard block |
||
79 | 91 | } |
80 | 92 | |
81 | 93 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
94 | + |
|
95 | + /** |
|
96 | + * @param Config $config |
|
97 | + */ |
|
82 | 98 | private function executeTssRule($rule, $template, $config) { |
83 | 99 | $rule->touch(); |
84 | 100 |
@@ -7,19 +7,19 @@ discard block |
||
7 | 7 | namespace Transphporm; |
8 | 8 | //Separates out TSS file loading/caching from parsing |
9 | 9 | class SheetLoader { |
10 | - private $cache; |
|
11 | - private $prefix; |
|
12 | - private $sheet; |
|
13 | - private $time; |
|
14 | - private $import = []; |
|
15 | - |
|
16 | - public function __construct(Cache $cache, FilePath $filePath, $tss, $prefix, $time) { |
|
17 | - $this->cache = $cache; |
|
18 | - $this->filePath = $filePath; |
|
19 | - $this->prefix = $prefix; |
|
20 | - $this->tss = $tss; |
|
21 | - $this->time = $time; |
|
22 | - } |
|
10 | + private $cache; |
|
11 | + private $prefix; |
|
12 | + private $sheet; |
|
13 | + private $time; |
|
14 | + private $import = []; |
|
15 | + |
|
16 | + public function __construct(Cache $cache, FilePath $filePath, $tss, $prefix, $time) { |
|
17 | + $this->cache = $cache; |
|
18 | + $this->filePath = $filePath; |
|
19 | + $this->prefix = $prefix; |
|
20 | + $this->tss = $tss; |
|
21 | + $this->time = $time; |
|
22 | + } |
|
23 | 23 | |
24 | 24 | private function getRulesFromCache($file) { |
25 | 25 | //The cache for the key: the filename and template prefix |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
45 | 45 | } |
46 | 46 | //write the sheet to cache |
47 | - public function write($file, $rules, $imports = []) { |
|
47 | + public function write($file, $rules, $imports = []) { |
|
48 | 48 | if (is_file($file)) { |
49 | 49 | $key = $this->getCacheKey($file); |
50 | 50 | $existing = $this->cache->load($key, filemtime($file)); |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | $this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
53 | 53 | } |
54 | 54 | return $rules; |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | 57 | public function processRules($template, \Transphporm\Config $config) { |
58 | 58 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
@@ -69,12 +69,12 @@ discard block |
||
69 | 69 | //Load the TSS |
70 | 70 | public function getRules($tss, $cssToXpath, $valueParser) { |
71 | 71 | if (is_file($tss)) { |
72 | - //$rules = $this->cache->load($tss); |
|
73 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
72 | + //$rules = $this->cache->load($tss); |
|
73 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
74 | 74 | $this->filePath->addPath(dirname(realpath($tss))); |
75 | 75 | if (empty($rules)) $tss = file_get_contents($tss); |
76 | 76 | else return $rules; |
77 | - } |
|
77 | + } |
|
78 | 78 | return (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
79 | 79 | } |
80 | 80 |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | } |
42 | 42 | |
43 | 43 | private function getCacheKey($file) { |
44 | - return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
|
44 | + return $file.$this->prefix.dirname(realpath($file)).DIRECTORY_SEPARATOR; |
|
45 | 45 | } |
46 | 46 | //write the sheet to cache |
47 | 47 | public function write($file, $rules, $imports = []) { |
@@ -101,6 +101,6 @@ discard block |
||
101 | 101 | |
102 | 102 | |
103 | 103 | private function sortPseudo($a, $b) { |
104 | - return count($a->pseudo) < count($b->pseudo) ? -1 :1; |
|
104 | + return count($a->pseudo) < count($b->pseudo) ? -1 : 1; |
|
105 | 105 | } |
106 | 106 | } |
@@ -30,7 +30,9 @@ discard block |
||
30 | 30 | $rules = $this->cache->load($key, filemtime($file)); |
31 | 31 | if ($rules) { |
32 | 32 | foreach ($rules['import'] as $file) { |
33 | - if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false; |
|
33 | + if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) { |
|
34 | + return false; |
|
35 | + } |
|
34 | 36 | } |
35 | 37 | } |
36 | 38 | return $rules; |
@@ -48,7 +50,9 @@ discard block |
||
48 | 50 | if (is_file($file)) { |
49 | 51 | $key = $this->getCacheKey($file); |
50 | 52 | $existing = $this->cache->load($key, filemtime($file)); |
51 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
53 | + if (isset($existing['import']) && empty($imports)) { |
|
54 | + $imports = $existing['import']; |
|
55 | + } |
|
52 | 56 | $this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
53 | 57 | } |
54 | 58 | return $rules; |
@@ -60,10 +64,14 @@ discard block |
||
60 | 64 | usort($rules, [$this, 'sortRules']); |
61 | 65 | |
62 | 66 | foreach ($rules as $rule) { |
63 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
67 | + if ($rule->shouldRun($this->time)) { |
|
68 | + $this->executeTssRule($rule, $template, $config); |
|
69 | + } |
|
64 | 70 | } |
65 | 71 | |
66 | - if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
72 | + if (is_file($this->tss)) { |
|
73 | + $this->write($this->tss, $rules, $this->import); |
|
74 | + } |
|
67 | 75 | } |
68 | 76 | |
69 | 77 | //Load the TSS |
@@ -72,8 +80,11 @@ discard block |
||
72 | 80 | //$rules = $this->cache->load($tss); |
73 | 81 | $rules = $this->getRulesFromCache($tss)['rules']; |
74 | 82 | $this->filePath->addPath(dirname(realpath($tss))); |
75 | - if (empty($rules)) $tss = file_get_contents($tss); |
|
76 | - else return $rules; |
|
83 | + if (empty($rules)) { |
|
84 | + $tss = file_get_contents($tss); |
|
85 | + } else { |
|
86 | + return $rules; |
|
87 | + } |
|
77 | 88 | } |
78 | 89 | return (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
79 | 90 | } |
@@ -91,10 +102,15 @@ discard block |
||
91 | 102 | |
92 | 103 | private function sortRules($a, $b) { |
93 | 104 | //If they have the same depth, compare on index |
94 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
105 | + if ($a->query === $b->query) { |
|
106 | + return $this->sortPseudo($a, $b); |
|
107 | + } |
|
95 | 108 | |
96 | - if ($a->depth === $b->depth) $property = 'index'; |
|
97 | - else $property = 'depth'; |
|
109 | + if ($a->depth === $b->depth) { |
|
110 | + $property = 'index'; |
|
111 | + } else { |
|
112 | + $property = 'depth'; |
|
113 | + } |
|
98 | 114 | |
99 | 115 | return ($a->$property < $b->$property) ? -1 : 1; |
100 | 116 | } |