@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | $this->save = function($content = null) { |
| 39 | 39 | return $this->document->saveHtml($content); |
| 40 | 40 | }; |
| 41 | - $this->document->loadHtml('<' . '?xml encoding="UTF-8">' . $doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
| 41 | + $this->document->loadHtml('<'.'?xml encoding="UTF-8">'.$doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | else { |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | if ($document) return $this->document; |
| 90 | 90 | |
| 91 | 91 | //Print the doctype... saveHtml inexplicably does not support $doc->doctype as an argument |
| 92 | - $output = ($this->document->doctype) ? $this->document->saveXml($this->document->doctype) . "\n" : ''; |
|
| 92 | + $output = ($this->document->doctype) ? $this->document->saveXml($this->document->doctype)."\n" : ''; |
|
| 93 | 93 | |
| 94 | 94 | if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
| 95 | 95 | else $output = $this->printDocument(); |
@@ -41,8 +41,7 @@ discard block |
||
| 41 | 41 | }; |
| 42 | 42 | $this->document->loadHtml('<' . '?xml encoding="UTF-8">' . $doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
| 43 | 43 | |
| 44 | - } |
|
| 45 | - else { |
|
| 44 | + } else { |
|
| 46 | 45 | $this->document->loadXml($doc); |
| 47 | 46 | //XML was loaded, save as XML. |
| 48 | 47 | $this->save = function($content = null) { |
@@ -79,7 +78,9 @@ discard block |
||
| 79 | 78 | /** Prints out the current DomDocument as HTML */ |
| 80 | 79 | private function printDocument() { |
| 81 | 80 | $output = ''; |
| 82 | - foreach ($this->document->documentElement->childNodes as $node) $output .= call_user_func($this->save, $node); |
|
| 81 | + foreach ($this->document->documentElement->childNodes as $node) { |
|
| 82 | + $output .= call_user_func($this->save, $node); |
|
| 83 | + } |
|
| 83 | 84 | return $output; |
| 84 | 85 | } |
| 85 | 86 | |
@@ -91,13 +92,18 @@ discard block |
||
| 91 | 92 | //Generate the document by taking only the childnodes of the template, ignoring the <template> and </template> tags |
| 92 | 93 | //TODO: Is there a faster way of doing this without string manipulation on the output or this loop through childnodes? |
| 93 | 94 | //Either return a whole DomDocument or return the output HTML |
| 94 | - if ($document) return $this->document; |
|
| 95 | + if ($document) { |
|
| 96 | + return $this->document; |
|
| 97 | + } |
|
| 95 | 98 | |
| 96 | 99 | //Print the doctype... saveHtml inexplicably does not support $doc->doctype as an argument |
| 97 | 100 | $output = ($this->document->doctype) ? $this->document->saveXml($this->document->doctype) . "\n" : ''; |
| 98 | 101 | |
| 99 | - if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
|
| 100 | - else $output = $this->printDocument(); |
|
| 102 | + if ($this->document->documentElement->tagName !== 'template') { |
|
| 103 | + $output .= call_user_func($this->save, $this->document->documentElement); |
|
| 104 | + } else { |
|
| 105 | + $output = $this->printDocument(); |
|
| 106 | + } |
|
| 101 | 107 | |
| 102 | 108 | //repair empty tags. Browsers break on <script /> and <div /> so can't avoid LIBXML_NOEMPTYTAG but they also break on <base></base> so repair them |
| 103 | 109 | $output = str_replace(['></img>', '></br>', '></meta>', '></base>', '></link>', '></hr>', '></input>'], ' />', $output); |
@@ -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 | } |