public function testVersioning() { $url = new UrlManager(); $url->setBaseUrl('/root'); $url->setVersioning(true, '2.1'); $this->assertEquals('/root/test.html?v=2.1', $url->url('/test.html')); }
public function testManager() { $res = new ImageResourceManager(new ResourceGroups(), function (ImageResource $res) { $url = new UrlManager(); $url->setBaseUrl('/tmp/'); $res->setUrlManager($url); $imageStorage = new ImagineImagesStorage(); $res->setImagesStorage($imageStorage); }); $res->add(new ImageResource('Ford Mustang 1972', array('/images/1972_ford_mustang-wide.jpg', '/images/1972_ford_mustang-mini.jpg'), array('root_dir' => __DIR__ . '/tmp', 'output_dir' => '/images/test', 'sizes' => '100vw, (min-width: 40em) 80vw', 'media' => array('(min-width: 1000px)', '(min-width: 800px)'), 'attr' => array('class' => 'test', 'data-test' => 'ok'), 'images' => array(array('index' => 0, 'suffix' => 'test', 'width' => 1000, 'height' => 800, 'format' => 'jpg', 'srcset_x' => 2, 'media_index' => 0), array('index' => 1, 'suffix' => 'ok', 'width' => 300, 'height' => 150, 'format' => 'png'))))); $input = $res->render('html_picture'); libxml_use_internal_errors(true); foreach ($input as $code) { $dom = new DOMDocument(); $dom->loadHTML($code); $xpath = new DOMXPath($dom); $this->assertEquals(1, $xpath->query('body/picture')->length); $el = $xpath->query('body/picture')->item(0); $this->assertEquals('test', $el->getAttribute('class')); $el = $xpath->query('/descendant::img')->item(0); $this->assertEquals('/tmp/images/test/1972_ford_mustang-wide_test_1000x800.jpg', $el->getAttribute('src')); $el = $xpath->query('/descendant::source'); $this->assertEquals('(min-width: 1000px)', $el->item(0)->getAttribute('media')); $this->assertEquals('/tmp/images/test/1972_ford_mustang-wide_test_1000x800.jpg 2x', $el->item(0)->getAttribute('srcset')); $this->assertEquals('', $el->item(1)->getAttribute('media')); $this->assertEquals('/tmp/images/test/1972_ford_mustang-mini_ok_300x150.png', $el->item(1)->getAttribute('srcset')); } libxml_use_internal_errors(false); $input = $res->render('html_img'); foreach ($input as $code) { $dom = new DOMDocument(); $dom->loadHTML($code); $xpath = new DOMXPath($dom); $img = $xpath->query('body/img'); $this->assertEquals(1, $img->length); $img = $img->item(0); $this->assertEquals('Ford Mustang 1972', $img->getAttribute('alt')); $this->assertEquals('100vw, (min-width: 40em) 80vw', $img->getAttribute('sizes')); $this->assertEquals('/tmp/images/test/1972_ford_mustang-wide_test_1000x800.jpg', $img->getAttribute('src')); $this->assertEquals('/tmp/images/test/1972_ford_mustang-mini_ok_300x150.png', $img->getAttribute('srcset')); } }
/** * 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; } }