@@ -26,11 +26,12 @@ |
||
26 | 26 | private function getArgs0($name, $args) { |
27 | 27 | if (isset($this->functions[$name]) && !($this->functions[$name] instanceof TSSFunction\Data)) { |
28 | 28 | $tokens = $args[0]; |
29 | - if ($tokens->count() == 0) return []; |
|
29 | + if ($tokens->count() == 0) { |
|
30 | + return []; |
|
31 | + } |
|
30 | 32 | $parser = new \Transphporm\Parser\Value($this); |
31 | 33 | return $parser->parseTokens($tokens, $this->elementData->getData($this->element)); |
32 | - } |
|
33 | - else { |
|
34 | + } else { |
|
34 | 35 | return iterator_to_array($args[0]); |
35 | 36 | } |
36 | 37 |
@@ -14,7 +14,9 @@ discard block |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
17 | - if (!$this->shouldRun($element)) return false; |
|
17 | + if (!$this->shouldRun($element)) { |
|
18 | + return false; |
|
19 | + } |
|
18 | 20 | |
19 | 21 | $values = $this->formatter->format($values, $rules); |
20 | 22 | |
@@ -22,14 +24,19 @@ discard block |
||
22 | 24 | //Remove the current contents |
23 | 25 | $this->removeAllChildren($element); |
24 | 26 | //Now make a text node |
25 | - if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values); |
|
26 | - else $this->appendContent($element, $values); |
|
27 | + if ($this->getContentMode($rules) === 'replace') { |
|
28 | + $this->replaceContent($element, $values); |
|
29 | + } else { |
|
30 | + $this->appendContent($element, $values); |
|
31 | + } |
|
27 | 32 | } |
28 | 33 | } |
29 | 34 | |
30 | 35 | private function shouldRun($element) { |
31 | 36 | do { |
32 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
37 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
38 | + return false; |
|
39 | + } |
|
33 | 40 | } |
34 | 41 | while (($element = $element->parentNode) instanceof \DomElement); |
35 | 42 | return true; |
@@ -56,9 +63,10 @@ discard block |
||
56 | 63 | public function getNode($node, $document) { |
57 | 64 | foreach ($node as $n) { |
58 | 65 | if (is_array($n)) { |
59 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
60 | - } |
|
61 | - else { |
|
66 | + foreach ($this->getNode($n, $document) as $new) { |
|
67 | + yield $new; |
|
68 | + } |
|
69 | + } else { |
|
62 | 70 | yield $this->convertNode($n, $document); |
63 | 71 | } |
64 | 72 | } |
@@ -69,9 +77,10 @@ discard block |
||
69 | 77 | $new = $document->importNode($node, true); |
70 | 78 | //Removing this might cause problems with caching... |
71 | 79 | //$new->setAttribute('transphporm', 'added'); |
72 | - } |
|
73 | - else { |
|
74 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
80 | + } else { |
|
81 | + if ($node instanceof \DomText) { |
|
82 | + $node = $node->nodeValue; |
|
83 | + } |
|
75 | 84 | $new = $document->createElement('text'); |
76 | 85 | |
77 | 86 | $new->appendChild($document->createTextNode($node)); |
@@ -82,14 +91,18 @@ discard block |
||
82 | 91 | |
83 | 92 | |
84 | 93 | private function replaceContent($element, $content) { |
85 | - if ($element->getAttribute('transphporm') == 'added') return; |
|
94 | + if ($element->getAttribute('transphporm') == 'added') { |
|
95 | + return; |
|
96 | + } |
|
86 | 97 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
87 | 98 | if ($element->getAttribute('transphporm')) { |
88 | 99 | $this->replaceCachedContent($element); |
89 | 100 | } |
90 | 101 | |
91 | 102 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
92 | - if (!$node->getAttribute('transphporm')) $node->setAttribute('transphporm', 'added'); |
|
103 | + if (!$node->getAttribute('transphporm')) { |
|
104 | + $node->setAttribute('transphporm', 'added'); |
|
105 | + } |
|
93 | 106 | $element->parentNode->insertBefore($node, $element); |
94 | 107 | } |
95 | 108 | |
@@ -123,6 +136,8 @@ discard block |
||
123 | 136 | } |
124 | 137 | |
125 | 138 | private function removeAllChildren($element) { |
126 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
139 | + while ($element->hasChildNodes()) { |
|
140 | + $element->removeChild($element->firstChild); |
|
141 | + } |
|
127 | 142 | } |
128 | 143 | } |
129 | 144 | \ No newline at end of file |
@@ -85,6 +85,10 @@ |
||
85 | 85 | } |
86 | 86 | |
87 | 87 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
88 | + |
|
89 | + /** |
|
90 | + * @param Template $template |
|
91 | + */ |
|
88 | 92 | private function doPostProcessing($template) { |
89 | 93 | $template->addHook('//*[@transphporm]', new Hook\PostProcess()); |
90 | 94 | 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); |
@@ -93,15 +93,15 @@ discard block |
||
93 | 93 | |
94 | 94 | //Load a template, firstly check if it's a file or a valid string |
95 | 95 | private function loadTemplate() { |
96 | - $result = ['cache' => $this->template, 'headers' => []]; |
|
96 | + $result = ['cache' => $this->template, 'headers' => []]; |
|
97 | 97 | if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
98 | 98 | return $result; |
99 | 99 | } |
100 | 100 | |
101 | - private function loadTemplateFromFile($file) { |
|
102 | - $xml = $this->cache->load($this->template, filemtime($this->template)); |
|
103 | - return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
104 | - } |
|
101 | + private function loadTemplateFromFile($file) { |
|
102 | + $xml = $this->cache->load($this->template, filemtime($this->template)); |
|
103 | + return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []]; |
|
104 | + } |
|
105 | 105 | |
106 | 106 | public function setCache(\ArrayAccess $cache) { |
107 | 107 | $this->cache = new Cache($cache); |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | public function output($data = null, $document = false) { |
53 | 53 | $headers = []; |
54 | 54 | $result = $this->loadTemplate(); |
55 | - $tssCache = new SheetLoader($this->cache, $this->filePath, $this->tss, $this->time); |
|
55 | + $tssCache = new SheetLoader($this->cache, $this->filePath, $this->tss, $this->time); |
|
56 | 56 | |
57 | 57 | //If an update is required, run any rules that need to be run. Otherwise, return the result from cache |
58 | 58 | //without creating any further objects, loading a DomDocument, etc |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
76 | 76 | $functionSet = new FunctionSet($elementData); |
77 | 77 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
78 | - $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' ); |
|
78 | + $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>'.$body.'</template>'); |
|
79 | 79 | |
80 | 80 | $valueParser = new Parser\Value($functionSet); |
81 | 81 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
@@ -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 |
@@ -80,7 +82,9 @@ discard block |
||
80 | 82 | $valueParser = new Parser\Value($functionSet); |
81 | 83 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
82 | 84 | |
83 | - foreach ($this->modules as $module) $module->load($this->config); |
|
85 | + foreach ($this->modules as $module) { |
|
86 | + $module->load($this->config); |
|
87 | + } |
|
84 | 88 | return $template; |
85 | 89 | } |
86 | 90 | |
@@ -94,7 +98,9 @@ discard block |
||
94 | 98 | //Load a template, firstly check if it's a file or a valid string |
95 | 99 | private function loadTemplate() { |
96 | 100 | $result = ['cache' => $this->template, 'headers' => []]; |
97 | - if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
|
101 | + if (strpos($this->template, "\n") === false && is_file($this->template)) { |
|
102 | + $result = $this->loadTemplateFromFile($this->template); |
|
103 | + } |
|
98 | 104 | return $result; |
99 | 105 | } |
100 | 106 | |
@@ -113,6 +119,8 @@ discard block |
||
113 | 119 | |
114 | 120 | public function __destruct() { |
115 | 121 | //Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks |
116 | - if (isset($this->config)) $this->config->getCssToXpath()->cleanup(); |
|
122 | + if (isset($this->config)) { |
|
123 | + $this->config->getCssToXpath()->cleanup(); |
|
124 | + } |
|
117 | 125 | } |
118 | 126 | } |
@@ -12,6 +12,9 @@ discard block |
||
12 | 12 | private $time; |
13 | 13 | private $import = []; |
14 | 14 | |
15 | + /** |
|
16 | + * @param string $tss |
|
17 | + */ |
|
15 | 18 | public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
16 | 19 | $this->cache = $cache; |
17 | 20 | $this->filePath = $filePath; |
@@ -59,6 +62,9 @@ discard block |
||
59 | 62 | return $rules; |
60 | 63 | } |
61 | 64 | |
65 | + /** |
|
66 | + * @param Template $template |
|
67 | + */ |
|
62 | 68 | public function processRules($template, \Transphporm\Config $config) { |
63 | 69 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
64 | 70 | |
@@ -72,6 +78,11 @@ discard block |
||
72 | 78 | } |
73 | 79 | |
74 | 80 | //Load the TSS |
81 | + |
|
82 | + /** |
|
83 | + * @param Parser\CssToXpath $cssToXpath |
|
84 | + * @param Parser\Value $valueParser |
|
85 | + */ |
|
75 | 86 | public function getRules($tss, $cssToXpath, $valueParser) { |
76 | 87 | if (is_file($tss)) { |
77 | 88 | //$rules = $this->cache->load($tss); |
@@ -84,6 +95,10 @@ discard block |
||
84 | 95 | } |
85 | 96 | |
86 | 97 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
98 | + |
|
99 | + /** |
|
100 | + * @param Config $config |
|
101 | + */ |
|
87 | 102 | private function executeTssRule($rule, $template, $config) { |
88 | 103 | $rule->touch(); |
89 | 104 |
@@ -129,6 +129,6 @@ |
||
129 | 129 | |
130 | 130 | |
131 | 131 | private function sortPseudo($a, $b) { |
132 | - return count($a->pseudo) < count($b->pseudo) ? -1 :1; |
|
132 | + return count($a->pseudo) < count($b->pseudo) ? -1 : 1; |
|
133 | 133 | } |
134 | 134 | } |
@@ -7,17 +7,17 @@ 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 $sheet; |
|
12 | - private $time; |
|
13 | - private $import = []; |
|
14 | - |
|
15 | - public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
16 | - $this->cache = $cache; |
|
17 | - $this->filePath = $filePath; |
|
18 | - $this->tss = $tss; |
|
19 | - $this->time = $time ?? time(); |
|
20 | - } |
|
10 | + private $cache; |
|
11 | + private $sheet; |
|
12 | + private $time; |
|
13 | + private $import = []; |
|
14 | + |
|
15 | + public function __construct(Cache $cache, FilePath $filePath, $tss, $time) { |
|
16 | + $this->cache = $cache; |
|
17 | + $this->filePath = $filePath; |
|
18 | + $this->tss = $tss; |
|
19 | + $this->time = $time ?? time(); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | private function getRulesFromCache($file) { |
23 | 23 | $key = $file; |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | return dirname(realpath($file)); |
71 | 71 | } |
72 | 72 | //write the sheet to cache |
73 | - public function write($file, $rules, $imports = []) { |
|
73 | + public function write($file, $rules, $imports = []) { |
|
74 | 74 | |
75 | 75 | if (is_file($file)) { |
76 | 76 | $key = $this->getCacheKey($file); |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | $this->cache->write($file, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => time()]); |
80 | 80 | } |
81 | 81 | return $rules; |
82 | - } |
|
82 | + } |
|
83 | 83 | |
84 | 84 | public function processRules($template, \Transphporm\Config $config) { |
85 | 85 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
@@ -96,12 +96,12 @@ discard block |
||
96 | 96 | //Load the TSS |
97 | 97 | public function getRules($tss, $cssToXpath, $valueParser) { |
98 | 98 | if (is_file($tss)) { |
99 | - //$rules = $this->cache->load($tss); |
|
100 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
99 | + //$rules = $this->cache->load($tss); |
|
100 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
101 | 101 | $this->filePath->addPath(dirname(realpath($tss))); |
102 | 102 | if (empty($rules)) $tss = file_get_contents($tss); |
103 | 103 | else return $rules; |
104 | - } |
|
104 | + } |
|
105 | 105 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
106 | 106 | } |
107 | 107 |
@@ -29,7 +29,9 @@ discard block |
||
29 | 29 | if ($rules) { |
30 | 30 | foreach ($rules['import'] as $file) { |
31 | 31 | //Check that the import file hasn't been changed since the cache was written |
32 | - if (filemtime($file) > $rules['ctime']) return false; |
|
32 | + if (filemtime($file) > $rules['ctime']) { |
|
33 | + return false; |
|
34 | + } |
|
33 | 35 | } |
34 | 36 | } |
35 | 37 | return $rules; |
@@ -40,12 +42,18 @@ discard block |
||
40 | 42 | // 2. If the data hasn't changed since the last run |
41 | 43 | //If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code |
42 | 44 | public function updateRequired($data) { |
43 | - if (!is_file($this->tss)) return true; |
|
45 | + if (!is_file($this->tss)) { |
|
46 | + return true; |
|
47 | + } |
|
44 | 48 | $rules = $this->getRulesFromCache($this->tss); |
45 | 49 | //Nothing was cached or the TSS file has changed, update is required |
46 | - if (empty($rules)) return true; |
|
50 | + if (empty($rules)) { |
|
51 | + return true; |
|
52 | + } |
|
47 | 53 | //Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required |
48 | - if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true; |
|
54 | + if ($rules['ctime']+$rules['minFreq'] <= $this->time) { |
|
55 | + return true; |
|
56 | + } |
|
49 | 57 | |
50 | 58 | return false; |
51 | 59 | } |
@@ -56,7 +64,9 @@ discard block |
||
56 | 64 | |
57 | 65 | foreach ($rules as $rule) { |
58 | 66 | $ruleFreq = $rule->getUpdateFrequency(); |
59 | - if ($ruleFreq < $min) $min = $ruleFreq; |
|
67 | + if ($ruleFreq < $min) { |
|
68 | + $min = $ruleFreq; |
|
69 | + } |
|
60 | 70 | } |
61 | 71 | |
62 | 72 | return $min; |
@@ -75,7 +85,9 @@ discard block |
||
75 | 85 | if (is_file($file)) { |
76 | 86 | $key = $this->getCacheKey($file); |
77 | 87 | $existing = $this->cache->load($key, filemtime($file)); |
78 | - if (isset($existing['import']) && empty($imports)) $imports = $existing['import']; |
|
88 | + if (isset($existing['import']) && empty($imports)) { |
|
89 | + $imports = $existing['import']; |
|
90 | + } |
|
79 | 91 | $this->cache->write($file, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => time()]); |
80 | 92 | } |
81 | 93 | return $rules; |
@@ -87,10 +99,14 @@ discard block |
||
87 | 99 | usort($rules, [$this, 'sortRules']); |
88 | 100 | |
89 | 101 | foreach ($rules as $rule) { |
90 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
102 | + if ($rule->shouldRun($this->time)) { |
|
103 | + $this->executeTssRule($rule, $template, $config); |
|
104 | + } |
|
91 | 105 | } |
92 | 106 | |
93 | - if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
|
107 | + if (is_file($this->tss)) { |
|
108 | + $this->write($this->tss, $rules, $this->import); |
|
109 | + } |
|
94 | 110 | } |
95 | 111 | |
96 | 112 | //Load the TSS |
@@ -99,8 +115,11 @@ discard block |
||
99 | 115 | //$rules = $this->cache->load($tss); |
100 | 116 | $rules = $this->getRulesFromCache($tss)['rules']; |
101 | 117 | $this->filePath->addPath(dirname(realpath($tss))); |
102 | - if (empty($rules)) $tss = file_get_contents($tss); |
|
103 | - else return $rules; |
|
118 | + if (empty($rules)) { |
|
119 | + $tss = file_get_contents($tss); |
|
120 | + } else { |
|
121 | + return $rules; |
|
122 | + } |
|
104 | 123 | } |
105 | 124 | return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
106 | 125 | } |
@@ -118,10 +137,15 @@ discard block |
||
118 | 137 | |
119 | 138 | private function sortRules($a, $b) { |
120 | 139 | //If they have the same depth, compare on index |
121 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
140 | + if ($a->query === $b->query) { |
|
141 | + return $this->sortPseudo($a, $b); |
|
142 | + } |
|
122 | 143 | |
123 | - if ($a->depth === $b->depth) $property = 'index'; |
|
124 | - else $property = 'depth'; |
|
144 | + if ($a->depth === $b->depth) { |
|
145 | + $property = 'index'; |
|
146 | + } else { |
|
147 | + $property = 'depth'; |
|
148 | + } |
|
125 | 149 | |
126 | 150 | return ($a->$property < $b->$property) ? -1 : 1; |
127 | 151 | } |