@@ -7,6 +7,9 @@ |
||
7 | 7 | $this->templateFunction = $templateFunction; |
8 | 8 | } |
9 | 9 | |
10 | + /** |
|
11 | + * @param string $val |
|
12 | + */ |
|
10 | 13 | public function html($val) { |
11 | 14 | return $this->templateFunction->run([$val]); |
12 | 15 | } |
@@ -1,13 +1,13 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm\Formatter; |
3 | 3 | class HTMLFormatter { |
4 | - private $templateFunction; |
|
4 | + private $templateFunction; |
|
5 | 5 | |
6 | - public function __construct(\Transphporm\TSSFunction\Template $templateFunction) { |
|
7 | - $this->templateFunction = $templateFunction; |
|
8 | - } |
|
6 | + public function __construct(\Transphporm\TSSFunction\Template $templateFunction) { |
|
7 | + $this->templateFunction = $templateFunction; |
|
8 | + } |
|
9 | 9 | |
10 | - public function html($val) { |
|
10 | + public function html($val) { |
|
11 | 11 | return $this->templateFunction->run([$val]); |
12 | 12 | } |
13 | 13 |
@@ -14,6 +14,6 @@ |
||
14 | 14 | public function debug($val) { |
15 | 15 | ob_start(); |
16 | 16 | var_dump($val); |
17 | - return $this->html('<pre>' . ob_get_clean() . '</pre>'); |
|
17 | + return $this->html('<pre>'.ob_get_clean().'</pre>'); |
|
18 | 18 | } |
19 | 19 | } |
@@ -62,6 +62,10 @@ discard block |
||
62 | 62 | return (object) $result; |
63 | 63 | } |
64 | 64 | |
65 | + /** |
|
66 | + * @param Template $template |
|
67 | + * @param Config $config |
|
68 | + */ |
|
65 | 69 | private function processRules($template, $config) { |
66 | 70 | $rules = $this->getRules($template, $config); |
67 | 71 | |
@@ -71,6 +75,10 @@ discard block |
||
71 | 75 | } |
72 | 76 | |
73 | 77 | //Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed |
78 | + |
|
79 | + /** |
|
80 | + * @param Template $template |
|
81 | + */ |
|
74 | 82 | private function doPostProcessing($template) { |
75 | 83 | $template->addHook('//*[@transphporm]', new Hook\PostProcess()); |
76 | 84 | return $template; |
@@ -47,7 +47,7 @@ |
||
47 | 47 | |
48 | 48 | $cachedOutput = $this->loadTemplate(); |
49 | 49 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
50 | - $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
|
50 | + $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>'.$cachedOutput['body'].'</template>'); |
|
51 | 51 | $valueParser = new Parser\Value($data); |
52 | 52 | $config = new Config($data, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($data, $template->getPrefix()), $headers, $this->baseDir); |
53 | 53 |
@@ -26,7 +26,9 @@ discard block |
||
26 | 26 | $this->cache = new Cache(new \ArrayObject()); |
27 | 27 | |
28 | 28 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
29 | - foreach ($modules as $module) $this->loadModule(new $module); |
|
29 | + foreach ($modules as $module) { |
|
30 | + $this->loadModule(new $module); |
|
31 | + } |
|
30 | 32 | } |
31 | 33 | |
32 | 34 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
@@ -51,7 +53,9 @@ discard block |
||
51 | 53 | $valueParser = new Parser\Value($data); |
52 | 54 | $config = new Config($data, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($data, $template->getPrefix()), $headers, $this->baseDir); |
53 | 55 | |
54 | - foreach ($this->modules as $module) $module->load($config); |
|
56 | + foreach ($this->modules as $module) { |
|
57 | + $module->load($config); |
|
58 | + } |
|
55 | 59 | |
56 | 60 | $this->processRules($template, $config); |
57 | 61 | |
@@ -66,7 +70,9 @@ discard block |
||
66 | 70 | $rules = $this->getRules($template, $config); |
67 | 71 | |
68 | 72 | foreach ($rules as $rule) { |
69 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
73 | + if ($rule->shouldRun($this->time)) { |
|
74 | + $this->executeTssRule($rule, $template, $config); |
|
75 | + } |
|
70 | 76 | } |
71 | 77 | } |
72 | 78 | |
@@ -91,8 +97,9 @@ discard block |
||
91 | 97 | if (trim($this->template)[0] !== '<') { |
92 | 98 | $xml = $this->cache->load($this->template, filemtime($this->template)); |
93 | 99 | return $xml ? $xml : ['body' => file_get_contents($this->template), 'headers' => []]; |
100 | + } else { |
|
101 | + return ['body' => $this->template, 'headers' => []]; |
|
94 | 102 | } |
95 | - else return ['body' => $this->template, 'headers' => []]; |
|
96 | 103 | } |
97 | 104 | |
98 | 105 | //Load the TSS rules either from a file or as a string |
@@ -1,9 +1,9 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm; |
3 | 3 | class RunException extends \Exception { |
4 | - public function __construct($operationType, $operationName, \Exception $previous) { |
|
5 | - $message = 'TSS Error: Problem carrying out ' . $operationType . ' \'' . $operationName . '\''; |
|
4 | + public function __construct($operationType, $operationName, \Exception $previous) { |
|
5 | + $message = 'TSS Error: Problem carrying out ' . $operationType . ' \'' . $operationName . '\''; |
|
6 | 6 | |
7 | - parent::__construct($message, 0, $previous); |
|
8 | - } |
|
7 | + parent::__construct($message, 0, $previous); |
|
8 | + } |
|
9 | 9 | } |
@@ -2,7 +2,7 @@ |
||
2 | 2 | namespace Transphporm; |
3 | 3 | class RunException extends \Exception { |
4 | 4 | public function __construct($operationType, $operationName, \Exception $previous) { |
5 | - $message = 'TSS Error: Problem carrying out ' . $operationType . ' \'' . $operationName . '\''; |
|
5 | + $message = 'TSS Error: Problem carrying out '.$operationType.' \''.$operationName.'\''; |
|
6 | 6 | |
7 | 7 | parent::__construct($message, 0, $previous); |
8 | 8 | } |
@@ -1,14 +1,14 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm; |
3 | 3 | class Exception extends \Exception { |
4 | - const PROPERTY = 'property'; |
|
5 | - const TSS_FUNCTION = 'function'; |
|
6 | - const PSEUDO = 'pseudo'; |
|
7 | - const FORMATTER = 'formatter'; |
|
4 | + const PROPERTY = 'property'; |
|
5 | + const TSS_FUNCTION = 'function'; |
|
6 | + const PSEUDO = 'pseudo'; |
|
7 | + const FORMATTER = 'formatter'; |
|
8 | 8 | |
9 | - public function __construct(RunException $runException, $file, $line) { |
|
10 | - $message = $runException->getMessage() . ' on Line ' . $line . ' of ' . ($file === null ? 'tss' : $file); |
|
9 | + public function __construct(RunException $runException, $file, $line) { |
|
10 | + $message = $runException->getMessage() . ' on Line ' . $line . ' of ' . ($file === null ? 'tss' : $file); |
|
11 | 11 | |
12 | - parent::__construct($message, 0, $runException->getPrevious()); |
|
13 | - } |
|
12 | + parent::__construct($message, 0, $runException->getPrevious()); |
|
13 | + } |
|
14 | 14 | } |
@@ -7,7 +7,7 @@ |
||
7 | 7 | const FORMATTER = 'formatter'; |
8 | 8 | |
9 | 9 | public function __construct(RunException $runException, $file, $line) { |
10 | - $message = $runException->getMessage() . ' on Line ' . $line . ' of ' . ($file === null ? 'tss' : $file); |
|
10 | + $message = $runException->getMessage().' on Line '.$line.' of '.($file === null ? 'tss' : $file); |
|
11 | 11 | |
12 | 12 | parent::__construct($message, 0, $runException->getPrevious()); |
13 | 13 | } |
@@ -14,7 +14,9 @@ discard block |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function format($value, $rules) { |
17 | - if (!isset($rules['format'])) return $value; |
|
17 | + if (!isset($rules['format'])) { |
|
18 | + return $value; |
|
19 | + } |
|
18 | 20 | $tokens = $rules['format']; |
19 | 21 | |
20 | 22 | $functionName = $tokens->from(\Transphporm\Parser\Tokenizer::NAME, true)->read(); |
@@ -27,8 +29,7 @@ discard block |
||
27 | 29 | |
28 | 30 | try { |
29 | 31 | return $this->processFormat($options, $functionName, $value); |
30 | - } |
|
31 | - catch (\Exception $e) { |
|
32 | + } catch (\Exception $e) { |
|
32 | 33 | throw new \Transphporm\RunException(\Transphporm\Exception::FORMATTER, $functionName, $e); |
33 | 34 | } |
34 | 35 | } |
@@ -43,7 +44,9 @@ discard block |
||
43 | 44 | } |
44 | 45 | } |
45 | 46 | } |
46 | - if (!$functionExists) throw new \Exception("Formatter '$functionName' does not exist"); |
|
47 | + if (!$functionExists) { |
|
48 | + throw new \Exception("Formatter '$functionName' does not exist"); |
|
49 | + } |
|
47 | 50 | return $value; |
48 | 51 | } |
49 | 52 | } |
@@ -30,7 +30,7 @@ |
||
30 | 30 | |
31 | 31 | public function run(\DomElement $element) { |
32 | 32 | $this->functionSet->setElement($element); |
33 | - if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; |
|
33 | + if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)).DIRECTORY_SEPARATOR; |
|
34 | 34 | $this->configLine = $this->line; |
35 | 35 | try { |
36 | 36 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
@@ -30,11 +30,15 @@ discard block |
||
30 | 30 | |
31 | 31 | public function run(\DomElement $element) { |
32 | 32 | $this->functionSet->setElement($element); |
33 | - if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; |
|
33 | + if ($this->file !== null) { |
|
34 | + $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; |
|
35 | + } |
|
34 | 36 | $this->configLine = $this->line; |
35 | 37 | try { |
36 | 38 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
37 | - if (!$this->pseudoMatcher->matches($element)) return; |
|
39 | + if (!$this->pseudoMatcher->matches($element)) { |
|
40 | + return; |
|
41 | + } |
|
38 | 42 | |
39 | 43 | // TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens |
40 | 44 | // problem with this is that anything in data changed by run properties is not shown |
@@ -42,10 +46,11 @@ discard block |
||
42 | 46 | |
43 | 47 | foreach ($this->rules as $name => $value) { |
44 | 48 | $result = $this->callProperty($name, $element, $this->getArgs($value)); |
45 | - if ($result === false) break; |
|
49 | + if ($result === false) { |
|
50 | + break; |
|
51 | + } |
|
46 | 52 | } |
47 | - } |
|
48 | - catch (\Transphporm\RunException $e) { |
|
53 | + } catch (\Transphporm\RunException $e) { |
|
49 | 54 | throw new \Transphporm\Exception($e, $this->file, $this->line); |
50 | 55 | } |
51 | 56 | } |
@@ -62,9 +67,10 @@ discard block |
||
62 | 67 | if (isset($this->properties[$name])) { |
63 | 68 | try { |
64 | 69 | return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
65 | - } |
|
66 | - catch (\Exception $e) { |
|
67 | - if ($e instanceof \Transphporm\RunException) throw $e; |
|
70 | + } catch (\Exception $e) { |
|
71 | + if ($e instanceof \Transphporm\RunException) { |
|
72 | + throw $e; |
|
73 | + } |
|
68 | 74 | throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e); |
69 | 75 | } |
70 | 76 | } |
@@ -11,20 +11,28 @@ |
||
11 | 11 | private $lastParentNode; |
12 | 12 | |
13 | 13 | public function match($name, $args, \DomElement $element) { |
14 | - if ($element->parentNode !== $this->lastParentNode) $this->count = 0; |
|
14 | + if ($element->parentNode !== $this->lastParentNode) { |
|
15 | + $this->count = 0; |
|
16 | + } |
|
15 | 17 | |
16 | 18 | $this->lastParentNode = $element->parentNode; |
17 | 19 | |
18 | 20 | |
19 | 21 | |
20 | - if ($name !== 'nth-child') return true; |
|
22 | + if ($name !== 'nth-child') { |
|
23 | + return true; |
|
24 | + } |
|
21 | 25 | |
22 | 26 | $this->count++; |
23 | 27 | $criteria = $args[0]; |
24 | 28 | |
25 | - if (is_callable([$this, $criteria])) return $this->$criteria($this->count); |
|
26 | - else if (!is_numeric($criteria)) throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
|
27 | - else return $this->count == $criteria; |
|
29 | + if (is_callable([$this, $criteria])) { |
|
30 | + return $this->$criteria($this->count); |
|
31 | + } else if (!is_numeric($criteria)) { |
|
32 | + throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
|
33 | + } else { |
|
34 | + return $this->count == $criteria; |
|
35 | + } |
|
28 | 36 | } |
29 | 37 | |
30 | 38 | private function odd($num) { |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm; |
3 | 3 | class TSSCache { |
4 | - private $cache; |
|
5 | - private $prefix; |
|
4 | + private $cache; |
|
5 | + private $prefix; |
|
6 | 6 | |
7 | - public function __construct(Cache $cache, $prefix) { |
|
8 | - $this->cache = $cache; |
|
9 | - $this->prefix = $prefix; |
|
10 | - } |
|
7 | + public function __construct(Cache $cache, $prefix) { |
|
8 | + $this->cache = $cache; |
|
9 | + $this->prefix = $prefix; |
|
10 | + } |
|
11 | 11 | |
12 | 12 | private function getRulesFromCache($file) { |
13 | 13 | //The cache for the key: the filename and template prefix |
@@ -28,12 +28,12 @@ discard block |
||
28 | 28 | return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
29 | 29 | } |
30 | 30 | |
31 | - public function load($tss) { |
|
32 | - return $this->getRulesFromCache($tss); |
|
33 | - } |
|
31 | + public function load($tss) { |
|
32 | + return $this->getRulesFromCache($tss); |
|
33 | + } |
|
34 | 34 | |
35 | - public function write($file, $rules, $imports = []) { |
|
36 | - if (is_file($file)) $this->cache->write($this->getCacheKey($file), ['rules' => $rules, 'import' => $imports]); |
|
37 | - return $rules; |
|
38 | - } |
|
35 | + public function write($file, $rules, $imports = []) { |
|
36 | + if (is_file($file)) $this->cache->write($this->getCacheKey($file), ['rules' => $rules, 'import' => $imports]); |
|
37 | + return $rules; |
|
38 | + } |
|
39 | 39 | } |
@@ -25,7 +25,7 @@ |
||
25 | 25 | } |
26 | 26 | |
27 | 27 | private function getCacheKey($file) { |
28 | - return $file . $this->prefix . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
|
28 | + return $file.$this->prefix.dirname(realpath($file)).DIRECTORY_SEPARATOR; |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public function load($tss) { |
@@ -18,7 +18,9 @@ discard block |
||
18 | 18 | $rules = $this->cache->load($key, filemtime($file)); |
19 | 19 | if ($rules) { |
20 | 20 | foreach ($rules['import'] as $file) { |
21 | - if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false; |
|
21 | + if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) { |
|
22 | + return false; |
|
23 | + } |
|
22 | 24 | } |
23 | 25 | } |
24 | 26 | return $rules; |
@@ -33,7 +35,9 @@ discard block |
||
33 | 35 | } |
34 | 36 | |
35 | 37 | public function write($file, $rules, $imports = []) { |
36 | - if (is_file($file)) $this->cache->write($this->getCacheKey($file), ['rules' => $rules, 'import' => $imports]); |
|
38 | + if (is_file($file)) { |
|
39 | + $this->cache->write($this->getCacheKey($file), ['rules' => $rules, 'import' => $imports]); |
|
40 | + } |
|
37 | 41 | return $rules; |
38 | 42 | } |
39 | 43 | } |
@@ -17,6 +17,9 @@ |
||
17 | 17 | $this->xPath = $xPath; |
18 | 18 | } |
19 | 19 | |
20 | + /** |
|
21 | + * @param integer $index |
|
22 | + */ |
|
20 | 23 | private function readArray($array, $index) { |
21 | 24 | return isset($array[$index]) ? $array[$index] : null; |
22 | 25 | } |
@@ -25,13 +25,18 @@ discard block |
||
25 | 25 | $selector = $this->readArray($args, 1); |
26 | 26 | $tss = $this->readArray($args, 2); |
27 | 27 | |
28 | - if (trim($args[0])[0] === '<') $xml = $args[0]; |
|
29 | - else $xml = $this->filePath->getFilePath($args[0]); |
|
28 | + if (trim($args[0])[0] === '<') { |
|
29 | + $xml = $args[0]; |
|
30 | + } else { |
|
31 | + $xml = $this->filePath->getFilePath($args[0]); |
|
32 | + } |
|
30 | 33 | |
31 | 34 | $newTemplate = new \Transphporm\Builder($xml, $tss ? $this->filePath->getFilePath($tss) : null); |
32 | 35 | |
33 | 36 | $doc = $newTemplate->output($this->elementData->getData($element), true)->body; |
34 | - if ($selector != '') return $this->templateSubsection($doc, $selector); |
|
37 | + if ($selector != '') { |
|
38 | + return $this->templateSubsection($doc, $selector); |
|
39 | + } |
|
35 | 40 | |
36 | 41 | return $this->getTemplateContent($doc, $tss); |
37 | 42 | |
@@ -62,7 +67,9 @@ discard block |
||
62 | 67 | |
63 | 68 | private function getClonedElement($node, $tss) { |
64 | 69 | $clone = $node->cloneNode(true); |
65 | - if ($tss != null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate'); |
|
70 | + if ($tss != null && $clone instanceof \DomElement) { |
|
71 | + $clone->setAttribute('transphporm', 'includedtemplate'); |
|
72 | + } |
|
66 | 73 | return $clone; |
67 | 74 | } |
68 | 75 | } |