public function testDomain() { $url = new UrlManager(); $url->setDomainPath('www.google.pl'); $this->assertEquals('http://www.google.pl/test.html', $url->url('/test.html')); $url->setDomainPath('http://www.google.pl'); $this->assertEquals('http://www.google.pl/test.html', $url->url('/test.html')); $url->setDomainPath('https://www.google.pl'); $this->assertEquals('https://www.google.pl/test.html', $url->url('/test.html')); $url->setDomainPath('ftp://google.pl'); $this->assertEquals('ftp://google.pl/test.html', $url->url('/test.html')); $url->setDomainPath('http://google.pl'); $url->setBaseUrl('/root'); $this->assertEquals('http://google.pl/root/test.html', $url->url('/test.html')); }
/** * 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; } }
/** * Rozszerzenie natywnej funkcji asset o automatyczne wstawianie hosta CDN w URL-u * @param string $path ścieżka do zasobu * @param string $type type zasobu * @return string */ public function cdnAsset($path, $type = 'image') { $params = $this->container->getParameter('vsymfo_core.document'); $asset = $this->container->get('assets.packages'); if (!$params['cdn_enable']) { return $asset->getUrl($path); } switch ($type) { case 'css': $domain = $params['cdn_css']; break; case 'javascript': case 'js': $domain = $params['cdn_javascript']; break; case 'image': default: $domain = $params['cdn_image']; } if (empty($domain)) { return $asset->getUrl($path); } $urlManager = new UrlManager(); $urlManager->setDomainPath($domain); return $urlManager->url($asset->getUrl($path), false); }