@@ -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 | |
@@ -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; |
|
| 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; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | 22 | private function getRulesFromCache($file) { |
| 23 | 23 | //The cache for the key: the filename and template prefix |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | return $file . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
| 50 | 50 | } |
| 51 | 51 | //write the sheet to cache |
| 52 | - public function write($file, $rules, $imports = []) { |
|
| 52 | + public function write($file, $rules, $imports = []) { |
|
| 53 | 53 | if (is_file($file)) { |
| 54 | 54 | $key = $this->getCacheKey($file); |
| 55 | 55 | $existing = $this->cache->load($key, filemtime($file)); |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $this->cache->write($key, ['rules' => $rules, 'import' => $imports]); |
| 58 | 58 | } |
| 59 | 59 | return $rules; |
| 60 | - } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | public function processRules($template, \Transphporm\Config $config) { |
| 63 | 63 | $rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser()); |
@@ -74,12 +74,12 @@ discard block |
||
| 74 | 74 | //Load the TSS |
| 75 | 75 | public function getRules($tss, $cssToXpath, $valueParser) { |
| 76 | 76 | if (is_file($tss)) { |
| 77 | - //$rules = $this->cache->load($tss); |
|
| 78 | - $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 77 | + //$rules = $this->cache->load($tss); |
|
| 78 | + $rules = $this->getRulesFromCache($tss)['rules']; |
|
| 79 | 79 | $this->filePath->addPath(dirname(realpath($tss))); |
| 80 | 80 | if (empty($rules)) $tss = file_get_contents($tss); |
| 81 | 81 | else return $rules; |
| 82 | - } |
|
| 82 | + } |
|
| 83 | 83 | return (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse(); |
| 84 | 84 | } |
| 85 | 85 | |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | private function getCacheKey($file) { |
| 49 | - return $file . dirname(realpath($file)) . DIRECTORY_SEPARATOR; |
|
| 49 | + return $file.dirname(realpath($file)).DIRECTORY_SEPARATOR; |
|
| 50 | 50 | } |
| 51 | 51 | //write the sheet to cache |
| 52 | 52 | public function write($file, $rules, $imports = []) { |
@@ -106,6 +106,6 @@ discard block |
||
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | private function sortPseudo($a, $b) { |
| 109 | - return count($a->pseudo) < count($b->pseudo) ? -1 :1; |
|
| 109 | + return count($a->pseudo) < count($b->pseudo) ? -1 : 1; |
|
| 110 | 110 | } |
| 111 | 111 | } |