/** * test loadera zasobów */ public function testResourcesLoader() { $manager = $this->doc->resources('javascript'); $manager->setOnAdd("testAdd", 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/html_js.db')); $res->setCombineObject($combine); $res->setUrlManager(new UrlManager()); }); $manager->chooseOnAdd("testAdd"); $locator = new FileLocator(__DIR__ . '/tmp/config'); $loader = new JavaScriptResourcesLoader($locator, array('baseurl' => '/tmp', 'combine' => true, 'resources' => $manager, 'cache_dir' => __DIR__ . '/tmp/cache', 'cache_refresh' => true)); $loader->load('html_resources.yml', 'framework'); $loader->load('html_resources.yml', 'core'); $manager = $this->doc->resources('stylesheet'); $manager->setOnAdd("styleTestAdd", function (StyleSheetResource $res) { $combine = new StyleSheetCombineFiles(); $combine->setInputDir(__DIR__)->setOutputDir(__DIR__ . '/tmp/cache')->setOutputBaseUrl('/tmp/cache')->setOutputForceRefresh(true)->setOutputLifeTime(0)->setOutputStrategy('manual')->setCacheDb(CombineFilesCacheDB::openFile(__DIR__ . '/tmp/cache/html_less.db'))->setLessImportDirs(array(__DIR__ . '/tmp/cache/less'))->setLessVariables(array('foo' => 'bar')); $res->setCombineObject($combine); $res->setUrlManager(new UrlManager()); }); $manager->chooseOnAdd("styleTestAdd"); $manager->setOnAdd("hoho", function (StyleSheetResource $res) { }); $manager->chooseOnAdd("hoho"); $manager->chooseOnAdd("styleTestAdd"); //$manager->chooseOnAdd(null); $locator = new FileLocator(__DIR__ . '/tmp/config'); $loader = new StyleSheetResourcesLoader($locator, array('baseurl' => '/tmp', 'combine' => true, 'resources' => $manager, 'cache_dir' => __DIR__ . '/tmp/cache', 'cache_refresh' => true)); $loader->load('html_resources.yml', 'theme'); //var_dump($this->doc->render()); $dom = new DOMDocument(); $dom->loadHTML($this->doc->render()); $xpath = new DOMXPath($dom); $this->assertEquals('Sample Page', $xpath->query('head/title')->item(0)->nodeValue); $this->assertEquals('/tmp/cache/theme.css', $xpath->query('head/link[@rel="stylesheet"]')->item(0)->getAttribute('href')); $this->assertEquals('/tmp/cache/framework.js', $xpath->query('head/script')->item(0)->getAttribute('src')); $this->assertEquals('/tmp/cache/core.js', $xpath->query('head/script')->item(1)->getAttribute('src')); $this->assertEquals('lorem ipsum', $xpath->query('body/p')->item(0)->nodeValue); $this->assertEquals(1, $xpath->query('body/script')->length); $this->assertEquals(1, $xpath->query('head/meta[@name="geo.region"]')->length); $this->assertTrue(file_exists(__DIR__ . '/tmp/cache/html_js.db')); $this->assertTrue(file_exists(__DIR__ . '/tmp/cache/html_less.db')); $this->assertTrue(file_exists(__DIR__ . '/tmp/cache/core.js')); $this->assertTrue(file_exists(__DIR__ . '/tmp/cache/framework.js')); $this->assertTrue(file_exists(__DIR__ . '/tmp/cache/theme.css')); }
/** * Listener wykonywany przy dodawaniu nowego zasobu * * @param HtmlDocument $doc * @param string $type * @param string $name * * @throws \UnexpectedValueException */ public function createResOnAdd(HtmlDocument $doc, $type, $name) { $allowed = array('javascript', 'stylesheet'); if (!in_array($type, $allowed)) { throw new \UnexpectedValueException($type . 'is unexpected'); } $options = $this->options; $closure = function (CombineResourceAbstract $res) use($type, $options) { switch ($type) { case 'javascript': $combine = new JavaScriptCombineFiles(); break; case 'stylesheet': $combine = new StyleSheetCombineFiles(); break; } if (!empty($options['cache_db_dir'])) { $cacheDb = $options['cache_db_dir'] . '.db'; } else { $cacheDb = $options['web_cache_dir'] . '.db'; } $combine->setInputDir($options['web_dir'])->setOutputDir($options['web_cache_dir'])->setOutputBaseUrl($options['web_cache_url'])->setOutputForceRefresh($options['cache_refresh'])->setOutputLifeTime($options['cache_lifetime'])->setOutputStrategy('manual')->setCacheDb(CombineFilesCacheDB::openFile($cacheDb)); if ($type == 'stylesheet') { $combine->setLessImportDirs($options['less_import_dirs']); $combine->setLessVariables($options['less_variables']); $combine->setScssImportDirs($options['scss_import_dirs']); $combine->setScssVariables($options['scss_variables']); } $res->setCombineObject($combine); $urlManager = new UrlManager(); $urlManager->setBaseUrl($options['web_url']); $urlManager->setVersioning($options['versioning_enable'], $options['versioning_version'], $options['versioning_timestamp']); if ($options['cdn_enable']) { if ($type == 'javascript') { $urlManager->setDomainPath($options['cdn_javascript']); } elseif ($type == 'stylesheet') { $urlManager->setDomainPath($options['cdn_css']); } } $res->setUrlManager($urlManager); }; $manager = $doc->resources($type); switch ($type) { case 'javascript': $manager->setOnAdd($name, function (JavaScriptResource $res) use($closure) { $closure($res); }); break; case 'stylesheet': $manager->setOnAdd($name, function (StyleSheetResource $res) use($closure) { $closure($res); }); break; } }
public function testGrunt() { $groups = new ResourceGroups(); $groups->addGroup('grunt_scss'); $res = new StyleSheetResourceManager($groups, function (StyleSheetResource $res) { $combine = new StyleSheetCombineFiles(); $combine->setInputDir(__DIR__)->setOutputDir(__DIR__ . '/tmp/cache')->setOutputBaseUrl('/tmp/cache')->setOutputForceRefresh(true)->setOutputLifeTime(0)->setOutputStrategy('manual')->setCacheDb(CombineFilesCacheDB::openFile(__DIR__ . '/tmp/cache/grunt.db'))->setScssImportDirs(array(__DIR__ . '/tmp/scss/grunt_import', __DIR__ . '/tmp/scss/grunt', __DIR__))->setScssVariables(array('path-background' => '"/images/bg.png"', 'foo' => 'red')); $url = new UrlManager(); $res->setCombineObject($combine); $res->setUrlManager($url); }); $res->add(new StyleSheetResource('style', array('/tmp/scss/style.scss,grunt'), array('combine' => true)), 'grunt_scss'); $this->assertNotEmpty($res->render('html')); }