public function testJavaScript() { $groups = new ResourceGroups(); $groups->addGroup('foo'); $groups->addGroup('bar'); $res = new JavaScriptResourceManager($groups, function (JavaScriptResource $res) { $combine = new JavaScriptCombineFiles(); $combine->setInputDir(__DIR__)->setOutputDir(__DIR__ . '/tmp/cache')->setOutputBaseUrl('/tmp/cache')->setOutputForceRefresh(true)->setOutputLifeTime(0)->setOutputStrategy('manual')->setCacheDb(CombineFilesCacheDB::openFile(__DIR__ . '/tmp/cache/js.db')); $url = new UrlManager(); $res->setCombineObject($combine); $res->setUrlManager($url); }); $this->assertSame($groups, $res->getGroups()); $res->add(new JavaScriptResource('foo_and_bar', array('/tmp/js/foo.js', '/tmp/js/bar.js'), array('combine' => true)), 'foo'); $res->add(new JavaScriptResource('bar_and_foo', array('/tmp/js/bar.js', '/tmp/js/foo.js'), array('combine' => true)), 'bar'); $this->assertTrue(file_exists(__DIR__ . '/tmp/cache/js.db')); foreach ($res->resources() as $js) { foreach ($js->getUrl() as $url) { $this->assertTrue(file_exists(__DIR__ . $url)); } } $this->assertNotEmpty($res->render('json')); $this->assertNotEmpty($res->render('html')); }
/** * {@inheritdoc} */ public function render() { $output = preg_match('/^<!DOCTYPE\\s.*>$/', trim($this->doctype->render())) ? trim($this->doctype->render()) . PHP_EOL : '<!DOCTYPE html>' . PHP_EOL; preg_match('/<html.*?>/', $this->html->render(), $result); $output .= isset($result[0]) ? $result[0] . PHP_EOL : '<html>' . PHP_EOL; $output .= substr($this->head->render(), 0, -7); $favicon = $this->favicon->render(); if (!empty($favicon)) { $output .= $favicon . PHP_EOL; } if ($this->styleSheet->length()) { $output .= $this->styleSheet->render('html') . PHP_EOL; } if ($this->javaScript->length()) { $tmp = call_user_func($this->scriptOutput, $this->javaScript, $this->translations); if (!empty($tmp)) { $output .= $tmp . PHP_EOL; } } if (!$this->script->isEmpty()) { $output .= '<script type="text/javascript">' . $this->script->render() . '</script>' . PHP_EOL; } $output .= $this->customHeadCode; $output .= '</head>' . PHP_EOL; $bodyEndPos = strrpos($this->body, "</body>"); if ($bodyEndPos !== false) { $output .= substr($this->body, 0, $bodyEndPos); $output .= $this->customBottomCode . '</body>'; } else { $output .= $this->body; } $output .= '</html>'; return $output; }