@@ -17,13 +17,13 @@ |
||
17 | 17 | |
18 | 18 | if ($this->isJsonFile($json)) { |
19 | 19 | $path = $this->filePath->getFilePath($json); |
20 | - if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
20 | + if (!file_exists($path)) throw new \Exception('File does not exist at: '.$path); |
|
21 | 21 | $json = file_get_contents($path); |
22 | 22 | } |
23 | 23 | |
24 | 24 | $map = json_decode($json, true); |
25 | 25 | |
26 | - if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
26 | + if (!is_array($map)) throw new \Exception('Could not decode json: '.json_last_error_msg()); |
|
27 | 27 | |
28 | 28 | return $map; |
29 | 29 | } |
@@ -17,13 +17,17 @@ |
||
17 | 17 | |
18 | 18 | if ($this->isJsonFile($json)) { |
19 | 19 | $path = $this->filePath->getFilePath($json); |
20 | - if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
20 | + if (!file_exists($path)) { |
|
21 | + throw new \Exception('File does not exist at: ' . $path); |
|
22 | + } |
|
21 | 23 | $json = file_get_contents($path); |
22 | 24 | } |
23 | 25 | |
24 | 26 | $map = json_decode($json, true); |
25 | 27 | |
26 | - if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
28 | + if (!is_array($map)) { |
|
29 | + throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
30 | + } |
|
27 | 31 | |
28 | 32 | return $map; |
29 | 33 | } |
@@ -25,7 +25,9 @@ |
||
25 | 25 | /** Returns the data that has been bound to $element, or, if no data is bound to $element climb the DOM tree to find the data bound to a parent node*/ |
26 | 26 | public function getData(\DomElement $element = null, $type = 'data') { |
27 | 27 | while ($element) { |
28 | - if (isset($this->elementMap[$element]) && array_key_exists($type, $this->elementMap[$element])) return $this->elementMap[$element][$type]; |
|
28 | + if (isset($this->elementMap[$element]) && array_key_exists($type, $this->elementMap[$element])) { |
|
29 | + return $this->elementMap[$element][$type]; |
|
30 | + } |
|
29 | 31 | $element = $element->parentNode; |
30 | 32 | } |
31 | 33 | return $this->data; |
@@ -33,14 +33,17 @@ |
||
33 | 33 | ]; |
34 | 34 | |
35 | 35 | if ($funcs[$this->mode] === 'concat' && is_numeric($newValue) |
36 | - && is_numeric($this->result[count($this->result)-1])) |
|
37 | - $this->add($newValue); |
|
38 | - else |
|
39 | - $this->{$funcs[$this->mode]}($newValue); |
|
36 | + && is_numeric($this->result[count($this->result)-1])) { |
|
37 | + $this->add($newValue); |
|
38 | + } else { |
|
39 | + $this->{$funcs[$this->mode]}($newValue); |
|
40 | + } |
|
40 | 41 | } |
41 | 42 | |
42 | 43 | public function in($value) { |
43 | - if (!is_array($value)) throw new \Exception(' `in` can only be used with arrays'); |
|
44 | + if (!is_array($value)) { |
|
45 | + throw new \Exception(' `in` can only be used with arrays'); |
|
46 | + } |
|
44 | 47 | $this->result[count($this->result)-1] = in_array($this->result[count($this->result)-1], $value); |
45 | 48 | } |
46 | 49 |
@@ -37,9 +37,13 @@ |
||
37 | 37 | } |
38 | 38 | |
39 | 39 | private function processLiterals($tokens, $name, $str) { |
40 | - if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
41 | - else if (method_exists($this, $name)) $this->$name($tokens); |
|
42 | - else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
40 | + if (is_numeric($name)) { |
|
41 | + $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
42 | + } else if (method_exists($this, $name)) { |
|
43 | + $this->$name($tokens); |
|
44 | + } else { |
|
45 | + $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
46 | + } |
|
43 | 47 | } |
44 | 48 | |
45 | 49 | private function true($tokens) { |
@@ -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 |
@@ -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 | } |
@@ -42,9 +42,9 @@ discard block |
||
42 | 42 | } |
43 | 43 | |
44 | 44 | public function setLocale($locale) { |
45 | - $format = new \Transphporm\Module\Format($locale); |
|
46 | - $this->modules[get_class($format)] = $format; |
|
47 | - } |
|
45 | + $format = new \Transphporm\Module\Format($locale); |
|
46 | + $this->modules[get_class($format)] = $format; |
|
47 | + } |
|
48 | 48 | |
49 | 49 | public function addPath($dir) { |
50 | 50 | $this->filePath->addPath($dir); |
@@ -100,15 +100,15 @@ discard block |
||
100 | 100 | |
101 | 101 | //Load a template, firstly check if it's a file or a valid string |
102 | 102 | private function loadTemplate() { |
103 | - $result = ['cache' => $this->template, 'headers' => []]; |
|
103 | + $result = ['cache' => $this->template, 'headers' => []]; |
|
104 | 104 | if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template); |
105 | 105 | return $result; |
106 | 106 | } |
107 | 107 | |
108 | - private function loadTemplateFromFile($file) { |
|
109 | - $xml = $this->cache->load($this->cacheKey . $file, filemtime($file)); |
|
110 | - return $xml ? $xml : ['cache' => file_get_contents($file) ?: "", 'headers' => []]; |
|
111 | - } |
|
108 | + private function loadTemplateFromFile($file) { |
|
109 | + $xml = $this->cache->load($this->cacheKey . $file, filemtime($file)); |
|
110 | + return $xml ? $xml : ['cache' => file_get_contents($file) ?: "", 'headers' => []]; |
|
111 | + } |
|
112 | 112 | |
113 | 113 | public function setCache(\ArrayAccess $cache) { |
114 | 114 | $this->cache = new Cache($cache); |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | 'headers' => array_merge($result['headers'], $headers), |
73 | 73 | 'body' => $this->doPostProcessing($template)->output($document) |
74 | 74 | ]; |
75 | - $this->cache->write($tssCache->getCacheKey($data) . $this->template, $result); |
|
75 | + $this->cache->write($tssCache->getCacheKey($data).$this->template, $result); |
|
76 | 76 | } |
77 | 77 | unset($result['cache'], $result['renderTime']); |
78 | 78 | return (object) $result; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data); |
83 | 83 | $functionSet = new FunctionSet($elementData); |
84 | 84 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
85 | - $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' ); |
|
85 | + $template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>'.$body.'</template>'); |
|
86 | 86 | |
87 | 87 | $valueParser = new Parser\Value($functionSet); |
88 | 88 | $this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers); |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | } |
107 | 107 | |
108 | 108 | private function loadTemplateFromFile($file) { |
109 | - $xml = $this->cache->load($this->cacheKey . $file, filemtime($file)); |
|
109 | + $xml = $this->cache->load($this->cacheKey.$file, filemtime($file)); |
|
110 | 110 | return $xml ? $xml : ['cache' => file_get_contents($file) ?: "", 'headers' => []]; |
111 | 111 | } |
112 | 112 |
@@ -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 | |
@@ -25,14 +27,17 @@ discard block |
||
25 | 27 | if ($this->getContentMode($rules) === 'replace') { |
26 | 28 | $contentReplace = new ContentReplace($this); |
27 | 29 | $contentReplace->replaceContent($element, $values); |
30 | + } else { |
|
31 | + $this->appendContent($element, $values); |
|
28 | 32 | } |
29 | - else $this->appendContent($element, $values); |
|
30 | 33 | } |
31 | 34 | } |
32 | 35 | |
33 | 36 | private function shouldRun($element) { |
34 | 37 | do { |
35 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
38 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
39 | + return false; |
|
40 | + } |
|
36 | 41 | } |
37 | 42 | while (($element = $element->parentNode) instanceof \DomElement); |
38 | 43 | return true; |
@@ -59,9 +64,10 @@ discard block |
||
59 | 64 | public function getNode($node, $document) { |
60 | 65 | foreach ($node as $n) { |
61 | 66 | if (is_array($n)) { |
62 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
63 | - } |
|
64 | - else { |
|
67 | + foreach ($this->getNode($n, $document) as $new) { |
|
68 | + yield $new; |
|
69 | + } |
|
70 | + } else { |
|
65 | 71 | yield $this->convertNode($n, $document); |
66 | 72 | } |
67 | 73 | } |
@@ -70,9 +76,10 @@ discard block |
||
70 | 76 | private function convertNode($node, $document) { |
71 | 77 | if ($node instanceof \DomElement || $node instanceof \DOMComment) { |
72 | 78 | $new = $document->importNode($node, true); |
73 | - } |
|
74 | - else { |
|
75 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
79 | + } else { |
|
80 | + if ($node instanceof \DomText) { |
|
81 | + $node = $node->nodeValue; |
|
82 | + } |
|
76 | 83 | $new = $document->createElement('text'); |
77 | 84 | |
78 | 85 | $new->appendChild($document->createTextNode($node)); |
@@ -89,6 +96,8 @@ discard block |
||
89 | 96 | } |
90 | 97 | |
91 | 98 | private function removeAllChildren($element) { |
92 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
99 | + while ($element->hasChildNodes()) { |
|
100 | + $element->removeChild($element->firstChild); |
|
101 | + } |
|
93 | 102 | } |
94 | 103 | } |
95 | 104 | \ No newline at end of file |
@@ -13,7 +13,9 @@ discard block |
||
13 | 13 | } |
14 | 14 | |
15 | 15 | public function replaceContent($element, $content) { |
16 | - if ($element->getAttribute('transphporm') == 'added') return; |
|
16 | + if ($element->getAttribute('transphporm') == 'added') { |
|
17 | + return; |
|
18 | + } |
|
17 | 19 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
18 | 20 | if ($element->getAttribute('transphporm')) { |
19 | 21 | $this->replaceCachedContent($element); |
@@ -27,7 +29,9 @@ discard block |
||
27 | 29 | |
28 | 30 | private function insertNodes($element, $content) { |
29 | 31 | foreach ($this->content->getNode($content, $element->ownerDocument) as $node) { |
30 | - if ($node instanceof \DomElement && !$node->getAttribute('transphporm')) $node->setAttribute('transphporm', 'added'); |
|
32 | + if ($node instanceof \DomElement && !$node->getAttribute('transphporm')) { |
|
33 | + $node->setAttribute('transphporm', 'added'); |
|
34 | + } |
|
31 | 35 | $element->parentNode->insertBefore($node, $element); |
32 | 36 | } |
33 | 37 | } |
@@ -42,6 +42,9 @@ discard block |
||
42 | 42 | } |
43 | 43 | |
44 | 44 | |
45 | + /** |
|
46 | + * @param \Transphporm\Template $template |
|
47 | + */ |
|
45 | 48 | public function processRules($template, \Transphporm\Config $config) { |
46 | 49 | $rules = $this->getRules($config->getCssToXpath(), $config->getValueParser()); |
47 | 50 | |
@@ -57,11 +60,20 @@ discard block |
||
57 | 60 | } |
58 | 61 | |
59 | 62 | //Load the TSS |
63 | + |
|
64 | + /** |
|
65 | + * @param \Transphporm\Parser\CssToXpath $cssToXpath |
|
66 | + * @param \Transphporm\Parser\Value $valueParser |
|
67 | + */ |
|
60 | 68 | public function getRules($cssToXpath, $valueParser, $indexStart = 0) { |
61 | 69 | return $this->tss->getRules($cssToXpath, $valueParser, $this, $indexStart); |
62 | 70 | } |
63 | 71 | |
64 | 72 | //Process a TSS rule e.g. `ul li {content: "foo"; format: bar} |
73 | + |
|
74 | + /** |
|
75 | + * @param \Transphporm\Config $config |
|
76 | + */ |
|
65 | 77 | private function executeTssRule($rule, $template, $config) { |
66 | 78 | $rule->touch(); |
67 | 79 |
@@ -84,6 +84,6 @@ |
||
84 | 84 | |
85 | 85 | |
86 | 86 | private function sortPseudo($a, $b) { |
87 | - return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
87 | + return count($a->pseudo) > count($b->pseudo) ? 1 : -1; |
|
88 | 88 | } |
89 | 89 | } |
@@ -39,8 +39,9 @@ discard block |
||
39 | 39 | if ($token['type'] == \Transphporm\Parser\Tokenizer::NAME && $token['value'] == 'data') { |
40 | 40 | $tokens->next(); |
41 | 41 | $newTokens = array_merge($newTokens, iterator_to_array($tokens->current()['value'])); |
42 | + } else { |
|
43 | + $newTokens[] = $token; |
|
42 | 44 | } |
43 | - else $newTokens[] = $token; |
|
44 | 45 | } |
45 | 46 | |
46 | 47 | $this->tss->setCacheKey(new \Transphporm\Parser\Tokens($newTokens)); |
@@ -58,7 +59,9 @@ discard block |
||
58 | 59 | usort($rules, [$this, 'sortRules']); |
59 | 60 | |
60 | 61 | foreach ($rules as $rule) { |
61 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
62 | + if ($rule->shouldRun($this->time)) { |
|
63 | + $this->executeTssRule($rule, $template, $config); |
|
64 | + } |
|
62 | 65 | } |
63 | 66 | |
64 | 67 | //if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import); |
@@ -83,10 +86,15 @@ discard block |
||
83 | 86 | |
84 | 87 | private function sortRules($a, $b) { |
85 | 88 | //If they have the same depth, compare on index |
86 | - if ($a->query === $b->query) return $this->sortPseudo($a, $b); |
|
89 | + if ($a->query === $b->query) { |
|
90 | + return $this->sortPseudo($a, $b); |
|
91 | + } |
|
87 | 92 | |
88 | - if ($a->depth === $b->depth) $property = 'index'; |
|
89 | - else $property = 'depth'; |
|
93 | + if ($a->depth === $b->depth) { |
|
94 | + $property = 'index'; |
|
95 | + } else { |
|
96 | + $property = 'depth'; |
|
97 | + } |
|
90 | 98 | |
91 | 99 | return ($a->$property < $b->$property) ? -1 : 1; |
92 | 100 | } |
@@ -7,17 +7,17 @@ |
||
7 | 7 | namespace Transphporm\SheetLoader; |
8 | 8 | //Separates out TSS file loading/caching from parsing |
9 | 9 | class SheetLoader { |
10 | - private $tss; |
|
11 | - private $filePath; |
|
12 | - private $time; |
|
13 | - private $import = []; |
|
14 | - |
|
15 | - public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) { |
|
16 | - $this->cache = $cache; |
|
17 | - $this->filePath = $filePath; |
|
18 | - $this->tss = $tss; |
|
19 | - $this->time = isset($time) ? $time : time(); |
|
20 | - } |
|
10 | + private $tss; |
|
11 | + private $filePath; |
|
12 | + private $time; |
|
13 | + private $import = []; |
|
14 | + |
|
15 | + public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) { |
|
16 | + $this->cache = $cache; |
|
17 | + $this->filePath = $filePath; |
|
18 | + $this->tss = $tss; |
|
19 | + $this->time = isset($time) ? $time : time(); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | //Allows controlling whether any updates are required to the template |
23 | 23 | //e.g. return false |