public function output($data = null, $document = false)
{
$headers = [];
$elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data);
$data = new FunctionSet($elementData);
$cachedOutput = $this->loadTemplate();
//To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does
$template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>');
$valueParser = new Parser\Value($data);
$config = new Config($data, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($data, $template->getPrefix()), new FilePath($this->baseDir, $this->rootDir), $headers);
foreach ($this->modules as $module) {
$module->load($config);
}
$this->processRules($template, $config);
$result = ['body' => $template->output($document), 'headers' => array_merge($cachedOutput['headers'], $headers)];
$this->cache->write($this->template, $result);
$result['body'] = $this->doPostProcessing($template)->output($document);
return (object) $result;
}