private static function _getOutputForUrlNoEncoding($url) { $cacheId = self::_getCacheIdByUrl($url); $ret = Kwf_Assets_Cache::getInstance()->load($cacheId); if ($ret === false) { $ret = Kwf_Assets_BuildCache::getInstance()->load($cacheId); if ($ret === false || $ret === 'outdated') { if ($ret === 'outdated' && Kwf_Config::getValue('assets.lazyBuild') == 'outdated') { Kwf_Assets_BuildCache::getInstance()->building = true; } $ret = self::_buildOutputForUrl($url); Kwf_Assets_BuildCache::getInstance()->building = false; if (substr($url, -4) != '.map') { //save generated caches for clear-cache-watcher $extension = substr($url, -(strlen($url) - strrpos($url, '/') - 1)); $fileName = 'cache/assets/output-cache-ids-' . $extension; if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) { file_put_contents($fileName, $cacheId . "\n", FILE_APPEND); } } } if (Kwf_Setup::getBaseUrl()) { if (substr($url, -4) == '.map') { $contentUrl = substr($url, 0, -4); $extension = substr($contentUrl, strrpos($contentUrl, '/') + 1); $replacements = self::_getBaseUrlReplacements($extension, $ret['contents']); $map = new Kwf_SourceMaps_SourceMap($ret['contents'], ''); foreach ($replacements as $i) { $map->stringReplace($i['search'], $i['replace']); } $map = $map->getMapContentsData(false); foreach ($map->sources as $k => $i) { $map->sources[$k] = Kwf_Setup::getBaseUrl() . $i; } $ret['contents'] = json_encode($map); } else { $extension = substr($url, strrpos($url, '/') + 1); $replacements = self::_getBaseUrlReplacements($extension, $ret['contents']); foreach ($replacements as $i) { $ret['contents'] = str_replace($i['search'], $i['replace'], $ret['contents']); } } } Kwf_Assets_Cache::getInstance()->save($ret, $cacheId); } return $ret; }
protected function _build($options) { if (!file_exists('build/assets')) { mkdir('build/assets'); } Kwf_Assets_BuildCache::getInstance()->building = true; Kwf_Assets_BuildCache::getInstance()->clean(); Kwf_Assets_BuildCache::getInstance()->save(time(), 'assetsVersion'); $langs = $this->_getAllLanguages(); $packages = $this->_getAllPackages(); $exts = array('js', 'defer.js', 'css', 'printcss'); echo "\ncalculating dependencies...\n"; $steps = count($packages) * count($exts); $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $progress = new Zend_ProgressBar($c, 0, $steps); $countDependencies = 0; foreach ($packages as $p) { $depName = $p->getDependencyName(); foreach ($exts as $extension) { $progress->next(1, "{$depName} {$extension}"); $countDependencies += count($p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension])); $cacheId = $p->getMaxMTimeCacheId(self::$_mimeTypeByExtension[$extension]); if (!$cacheId) { throw new Kwf_Exception("Didn't get cacheId for " . get_class($p)); } $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]); Kwf_Assets_BuildCache::getInstance()->save($maxMTime, $cacheId); //save generated caches for clear-cache-watcher $fileName = 'build/assets/package-max-mtime-' . $extension; if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) { file_put_contents($fileName, $cacheId . "\n", FILE_APPEND); } } } $progress->finish(); echo "compiling assets...\n"; $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $progress = new Zend_ProgressBar($c, 0, $countDependencies); foreach ($packages as $p) { foreach ($exts as $extension) { foreach ($p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]) as $dep) { $progress->next(1, "{$dep}"); $dep->warmupCaches(); } } } $progress->finish(); echo "generating packages...\n"; $steps = count($packages) * count($langs) * count($exts) * 4; $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $progress = new Zend_ProgressBar($c, 0, $steps); foreach ($packages as $p) { $depName = $p->getDependencyName(); foreach ($langs as $language) { foreach ($exts as $extension) { $packageContents = $p->getPackageContents(self::$_mimeTypeByExtension[$extension], $language); $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]); $progress->next(1, "{$depName} {$extension} {$language}"); $progress->next(1, "{$depName} {$extension} {$language} source"); $this->_buildPackageContents($packageContents, $maxMTime, $p, $extension, $language); $progress->next(1, "{$depName} {$extension} {$language} map"); $this->_buildPackageSourceMap($packageContents, $maxMTime, $p, $extension, $language); $progress->next(1, "{$depName} {$extension} {$language} url"); $urls = $p->getPackageUrls(self::$_mimeTypeByExtension[$extension], $language); if (Kwf_Setup::getBaseUrl()) { foreach ($urls as $k => $i) { $urls[$k] = substr($i, strlen(Kwf_Setup::getBaseUrl())); } } $cacheId = $p->getPackageUrlsCacheId(self::$_mimeTypeByExtension[$extension], $language); Kwf_Assets_BuildCache::getInstance()->save($urls, $cacheId); } } } Kwf_Assets_Cache::getInstance()->clean(); Kwf_Assets_BuildCache::getInstance()->building = false; $exts = array('js', 'defer.js', 'css'); foreach ($packages as $p) { $depName = $p->getDependencyName(); $language = $langs[0]; foreach ($exts as $extension) { $cacheId = Kwf_Assets_Dispatcher::getCacheIdByPackage($p, $extension, $language); $cacheContents = Kwf_Assets_BuildCache::getInstance()->load($cacheId); echo "{$depName} "; $h = new Kwf_View_Helper_FileSize(); echo "{$extension} size: " . $h->fileSize(strlen(gzencode($cacheContents['contents'], 9, FORCE_GZIP))); echo "\n"; } } $d = Kwf_Assets_Package_Default::getDefaultProviderList()->findDependency('Frontend'); foreach ($d->getFilteredUniqueDependencies('text/javascript') as $i) { if ($i instanceof Kwf_Assets_Dependency_File && $i->getType() == 'ext2') { echo "\n[WARNING] Frontend text/javascript contains ext2\n"; echo "To improve frontend performance all ext2 dependencies should be moved to defer\n\n"; break; } } }
private static function _clearAssetsAll($fileType = null) { if (!$fileType) { self::_clearAssetsAll('js'); self::_clearAssetsAll('defer.js'); self::_clearAssetsAll('css'); self::_clearAssetsAll('printcss'); return; } $fileNames = array('cache/assets/output-cache-ids-' . $fileType, 'build/assets/output-cache-ids-' . $fileType); foreach ($fileNames as $fileName) { if (file_exists($fileName)) { $cacheIds = file($fileName); unlink($fileName); foreach ($cacheIds as $cacheId) { $cacheId = trim($cacheId); echo $cacheId; if (Kwf_Assets_Cache::getInstance()->remove($cacheId)) { echo " [DELETED]"; } if (Kwf_Assets_BuildCache::getInstance()->remove($cacheId)) { echo " [build DELETED]"; } if (Kwf_Cache_SimpleStatic::_delete(array('as_' . $cacheId . '_gzip', 'as_' . $cacheId . '_deflate'))) { echo " [gzip DELETED]"; } if (Kwf_Assets_Cache::getInstance()->remove($cacheId . '_map')) { echo " [map DELETED]"; } if (Kwf_Assets_BuildCache::getInstance()->remove($cacheId . '_map')) { echo " [build map DELETED]"; } if (Kwf_Cache_SimpleStatic::_delete(array('as_' . $cacheId . '_map_gzip', 'as_' . $cacheId . '_map_deflate'))) { echo " [map_gzip DELETED]"; } echo "\n"; } } } $fileName = 'build/assets/package-max-mtime-' . $fileType; if (file_exists($fileName)) { $cacheIds = file($fileName); unlink($fileName); foreach ($cacheIds as $cacheId) { $cacheId = trim($cacheId); echo $cacheId; if (Kwf_Assets_BuildCache::getInstance()->remove($cacheId)) { echo " [DELETED]"; } echo "\n"; } } $a = new Kwf_Util_Build_Types_Assets(); $a->flagAllPackagesOutdated($fileType); self::_informDuckcast($fileType); }
public function previewWithCropAction() { $previewWidth = 390; $previewHeight = 184; $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception("Can't find upload"); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } //Scale dimensions $dimensions = array($previewWidth, $previewHeight, 'cover' => false); $cache = Kwf_Assets_Cache::getInstance(); $cacheId = 'previewLarge_' . $fileRow->id; if (!($output = $cache->load($cacheId))) { $output = array(); $output['contents'] = Kwf_Media_Image::scale($fileRow->getFileSource(), $dimensions, $fileRow->id); $output['mimeType'] = $fileRow->mime_type; $cache->save($output, $cacheId); } $cropX = $this->_getParam('cropX'); $cropY = $this->_getParam('cropY'); $cropWidth = $this->_getParam('cropWidth'); $cropHeight = $this->_getParam('cropHeight'); $imageOriginal = new Imagick($fileRow->getFileSource()); if ($this->_getParam('cropX') == null || $this->_getParam('cropY') == null || $this->_getParam('cropWidth') == null || $this->_getParam('cropHeight') == null) { //calculate default selection $dimension = $this->_getParam('dimension'); if (!$dimension) { Kwf_Media_Output::output($output); } $dimension = array('width' => $this->_getParam('dimension_width') ? $this->_getParam('dimension_width') : 0, 'height' => $this->_getParam('dimension_height') ? $this->_getParam('dimension_height') : 0, 'cover' => $this->_getParam('dimension_cover') ? $this->_getParam('dimension_cover') : false); if ($dimension['width'] == Kwf_Form_Field_Image_UploadField::USER_SELECT) { $dimension['width'] = $this->_getParam('width'); } if ($dimension['height'] == Kwf_Form_Field_Image_UploadField::USER_SELECT) { $dimension['height'] = $this->_getParam('height'); } if (!$dimension['cover']) { Kwf_Media_Output::output($output); } if ($dimension['width'] == Kwf_Form_Field_Image_UploadField::CONTENT_WIDTH) { Kwf_Media_Output::output($output); } if ($dimension['height'] == 0 || $dimension['width'] == 0) { Kwf_Media_Output::output($output); } $cropX = 0; $cropY = 0; $cropHeight = $imageOriginal->getImageHeight(); $cropWidth = $imageOriginal->getImageWidth(); if ($imageOriginal->getImageHeight() / $dimension['height'] > $imageOriginal->getImageWidth() / $dimension['width']) { // orientate on width $cropHeight = $dimension['height'] * $imageOriginal->getImageWidth() / $dimension['width']; $cropY = ($imageOriginal->getImageHeight() - $cropHeight) / 2; } else { // orientate on height $cropWidth = $dimension['width'] * $imageOriginal->getImageHeight() / $dimension['height']; $cropX = ($imageOriginal->getImageWidth() - $cropWidth) / 2; } } // Calculate values relative to preview image $image = new Imagick(); $image->readImageBlob($output['contents']); $previewFactor = 1; if ($image->getImageWidth() == $previewWidth) { $previewFactor = $image->getImageWidth() / $imageOriginal->getImageWidth(); } else { if ($image->getImageHeight() == $previewHeight) { $previewFactor = $image->getImageHeight() / $imageOriginal->getImageHeight(); } } $cropX = floor($cropX * $previewFactor); $cropY = floor($cropY * $previewFactor); $cropWidth = floor($cropWidth * $previewFactor); $cropHeight = floor($cropHeight * $previewFactor); $draw = new ImagickDraw(); if ($this->_isLightImage($output)) { $draw->setFillColor('black'); } else { $draw->setFillColor('white'); } $draw->setFillOpacity(0.3); // if cropX == 0 or cropY == 0 no rectangle should be drawn, because it // can't be 0 wide on topmost and leftmost position so it will result in // a 1px line which is wrong //Top region if ($cropY > 0) { $draw->rectangle(0, 0, $image->getImageWidth(), $cropY); } //Left region if ($cropX > 0) { if ($cropY > 0) { $draw->rectangle(0, $cropY + 1, $cropX, $cropY + $cropHeight - 1); } else { $draw->rectangle(0, $cropY, $cropX, $cropY + $cropHeight - 1); } } //Right region if ($cropY > 0) { $draw->rectangle($cropX + $cropWidth, $cropY + 1, $image->getImageWidth(), $cropY + $cropHeight - 1); } else { $draw->rectangle($cropX + $cropWidth, $cropY, $image->getImageWidth(), $cropY + $cropHeight - 1); } //Bottom region $draw->rectangle(0, $cropY + $cropHeight, $image->getImageWidth(), $image->getImageHeight()); $image->drawImage($draw); $output['contents'] = $image->getImageBlob(); Kwf_Media_Output::output($output); }
public function previewAction() { if (!$this->_getParam('uploadId')) { throw new Kwf_Exception_NotFound(); } $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception_NotFound(); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } $sizes = array('default' => array(40, 40), 'frontend' => array(100, 100, 'cover' => true), 'gridRow' => array(0, 20), 'gridRowLarge' => array(200, 200, 'cover' => false), 'imageGrid' => array(140, 140, 'cover' => false), 'imageGridLarge' => array(400, 400, 'cover' => false)); if (isset($sizes[$this->_getParam('size')])) { $size = $this->_getParam('size'); } else { $size = 'default'; } $cache = Kwf_Assets_Cache::getInstance(); $cacheId = $size . '_' . $fileRow->id; if (!($output = $cache->load($cacheId))) { $output = array(); $output['contents'] = Kwf_Media_Image::scale($fileRow->getFileSource(), $sizes[$size], $fileRow->id); $output['mimeType'] = $fileRow->mime_type; $cache->save($output, $cacheId); } Kwf_Media_Output::output($output); }
private static function _clearAssetsAll($fileType = null) { if (!$fileType) { self::_clearAssetsAll('js'); self::_clearAssetsAll('css'); return; } $fileNames = array('cache/assets/output-cache-ids-' . $fileType, 'build/assets/output-cache-ids-' . $fileType); $assetsCacheIds = array(); $assetsBuildCacheIds = array(); $simpleStaticCacheIds = array(); foreach ($fileNames as $fileName) { if (file_exists($fileName)) { $cacheIds = file($fileName); unlink($fileName); foreach ($cacheIds as $cacheId) { $cacheId = trim($cacheId); echo $cacheId . "\n"; $assetsCacheIds[] = $cacheId; $assetsBuildCacheIds[] = $cacheId; $simpleStaticCacheIds[] = 'as_' . $cacheId . '_gzip'; $simpleStaticCacheIds[] = 'as_' . $cacheId . '_deflate'; $assetsCacheIds[] = $cacheId . '_map'; $assetsBuildCacheIds[] = $cacheId . '_map'; $simpleStaticCacheIds[] = 'as_' . $cacheId . '_map_gzip'; $simpleStaticCacheIds[] = 'as_' . $cacheId . '_map_deflate'; } } } if ($assetsCacheIds) { Kwf_Assets_Cache::getInstance()->remove($assetsCacheIds); } if ($assetsBuildCacheIds) { Kwf_Assets_BuildCache::getInstance()->remove($assetsBuildCacheIds); } if ($simpleStaticCacheIds) { Kwf_Cache_SimpleStatic::_delete($simpleStaticCacheIds); } $a = new Kwf_Util_Build_Types_Assets(); $a->flagAllPackagesOutdated($fileType); self::_informDuckcast($fileType); if ($fileType == 'css') { self::_clearAssetsAll('0.css'); self::_clearAssetsAll('1.css'); self::_clearAssetsAll('ie8.css'); } if ($fileType == 'js') { self::_clearAssetsAll('defer.js'); } }
protected function _build($options) { if (!file_exists('build/assets')) { mkdir('build/assets'); } Kwf_Assets_BuildCache::getInstance()->building = true; Kwf_Assets_BuildCache::getInstance()->clean(); Kwf_Assets_BuildCache::getInstance()->save(time(), 'assetsVersion'); $langs = $this->getAllLanguages(); $packages = $this->getAllPackages(); $exts = array('js', 'defer.js', 'css'); echo "\ncalculating dependencies...\n"; $steps = count($packages) * count($exts); $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $progress = new Zend_ProgressBar($c, 0, $steps); $countDependencies = 0; foreach ($packages as $p) { $depName = $p->getDependencyName(); foreach ($exts as $extension) { $progress->next(1, "{$depName} {$extension}"); $p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]); $cacheId = $p->getMaxMTimeCacheId(self::$_mimeTypeByExtension[$extension]); if (!$cacheId) { throw new Kwf_Exception("Didn't get cacheId for " . get_class($p)); } $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]); Kwf_Assets_BuildCache::getInstance()->save($maxMTime, $cacheId); //save generated caches for clear-cache-watcher $fileName = 'build/assets/package-max-mtime-' . $extension; if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) { file_put_contents($fileName, $cacheId . "\n", FILE_APPEND); } } $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST); foreach ($it as $i) { $countDependencies++; } } $progress->finish(); echo "compiling assets...\n"; $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $progress = new Zend_ProgressBar($c, 0, $countDependencies); foreach ($packages as $p) { $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST); foreach ($it as $dep) { $progress->next(1, "{$dep}"); $dep->warmupCaches(); } } $progress->finish(); echo "generating packages...\n"; $steps = count($packages) * count($langs) * count($exts) * 4; $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $progress = new Zend_ProgressBar($c, 0, $steps); foreach ($packages as $p) { $depName = $p->getDependencyName(); foreach ($langs as $language) { foreach ($exts as $extension) { $progress->next(1, "{$depName} {$extension} {$language}"); $packageContents = $p->getPackageContents(self::$_mimeTypeByExtension[$extension], $language); $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]); $progress->next(1, "{$depName} {$extension} {$language} source"); $this->_buildPackageContents($packageContents, $maxMTime, $p, $extension, $language); $progress->next(1, "{$depName} {$extension} {$language} map"); $this->_buildPackageSourceMap($packageContents, $maxMTime, $p, $extension, $language); $progress->next(1, "{$depName} {$extension} {$language} url"); $urls = $p->getPackageUrls(self::$_mimeTypeByExtension[$extension], $language); if (Kwf_Setup::getBaseUrl()) { foreach ($urls as $k => $i) { $urls[$k] = substr($i, strlen(Kwf_Setup::getBaseUrl())); } } $cacheId = $p->getPackageUrlsCacheId(self::$_mimeTypeByExtension[$extension], $language); Kwf_Assets_BuildCache::getInstance()->save($urls, $cacheId); } } } Kwf_Assets_Cache::getInstance()->clean(); Kwf_Assets_BuildCache::getInstance()->building = false; }
public function getFileContents($file, $language = null) { $ret = array(); if (substr($file, -4) == '.gif') { $ret['mimeType'] = 'image/gif'; } else { if (substr($file, -4) == '.png') { $ret['mimeType'] = 'image/png'; } else { if (substr($file, -4) == '.jpg') { $ret['mimeType'] = 'image/jpeg'; } else { if (substr($file, -4) == '.mp4') { $ret['mimeType'] = 'video/mp4'; } else { if (substr($file, -5) == '.webm') { $ret['mimeType'] = 'video/webm'; } else { if (substr($file, -4) == '.css' || substr($file, -5) == '.scss') { $ret['mimeType'] = 'text/css; charset=utf-8'; if (!Kwf_Assets_Dispatcher::allowSourceAccess()) { throw new Kwf_Exception_AccessDenied(); } } else { if (substr($file, -3) == '.js') { $ret['mimeType'] = 'text/javascript; charset=utf-8'; if (!Kwf_Assets_Dispatcher::allowSourceAccess()) { throw new Kwf_Exception_AccessDenied(); } } else { if (substr($file, -4) == '.swf') { $ret['mimeType'] = 'application/flash'; } else { if (substr($file, -4) == '.ico') { $ret['mimeType'] = 'image/x-icon'; } else { if (substr($file, -5) == '.html') { $ret['mimeType'] = 'text/html; charset=utf-8'; } else { if (substr($file, -4) == '.otf') { // für Schriften $ret['mimeType'] = 'application/octet-stream'; } else { if (substr($file, -4) == '.eot') { // für Schriften $ret['mimeType'] = 'application/vnd.ms-fontobject'; } else { if (substr($file, -4) == '.svg') { // für Schriften $ret['mimeType'] = 'image/svg+xml'; } else { if (substr($file, -4) == '.ttf') { // für Schriften $ret['mimeType'] = 'application/octet-stream'; } else { if (substr($file, -5) == '.woff') { // für Schriften $ret['mimeType'] = 'application/font-woff'; } else { if (substr($file, -6) == '.woff2') { // für Schriften $ret['mimeType'] = 'application/font-woff2'; } else { if (substr($file, -4) == '.htc') { // für ie css3 $ret['mimeType'] = 'text/x-component'; } else { if (substr($file, -4) == '.pdf') { $ret['mimeType'] = 'application/pdf'; } else { throw new Kwf_Assets_NotFoundException("Invalid filetype ({$file})"); } } } } } } } } } } } } } } } } } } if (substr($ret['mimeType'], 0, 5) == 'text/') { $ret['mtime'] = time(); $file = new Kwf_Assets_Dependency_File($file); if (!$file->getAbsoluteFileName() || !file_exists($file->getAbsoluteFileName())) { throw new Kwf_Exception_NotFound(); } $ret['contents'] = $file->getContents(null); } else { $fx = substr($file, 0, strpos($file, '/')); if (substr($fx, 0, 3) == 'fx_') { $cache = Kwf_Assets_Cache::getInstance(); $cacheId = 'fileContents' . str_replace(array('/', '.', '-', ':'), array('_', '_', '_', '_'), $file); if (!($cacheData = $cache->load($cacheId))) { if (substr($ret['mimeType'], 0, 6) != 'image/') { throw new Kwf_Exception("Fx is only possible for images"); } $im = new Imagick(); if (substr($file, -4) == '.ico') { $im->setFormat('ico'); } //required because imagick can't autodetect ico format $file = new Kwf_Assets_Dependency_File(substr($file, strpos($file, '/') + 1)); $im->readImage($file->getAbsoluteFileName()); $fx = explode('_', substr($fx, 3)); foreach ($fx as $i) { $params = array(); if (($pos = strpos($i, '-')) !== false) { $params = explode('-', substr($i, $pos + 1)); $i = substr($i, 0, $pos); } call_user_func(array('Kwf_Assets_Effects', $i), $im, $params); } $cacheData['mtime'] = $file->getMTime(); $cacheData['contents'] = $im->getImagesBlob(); $im->destroy(); $cache->save($cacheData, $cacheId); } $ret['contents'] = $cacheData['contents']; $ret['mtime'] = time(); } else { $ret['mtime'] = time(); $file = new Kwf_Assets_Dependency_File($file); if (!file_exists($file->getAbsoluteFileName())) { throw new Kwf_Exception_NotFound(); } $ret['contents'] = $file->getContents(null); } } return $ret; }
protected function _build($options) { if (!file_exists('build/assets')) { mkdir('build/assets'); } Kwf_Assets_BuildCache::getInstance()->building = true; Kwf_Assets_BuildCache::getInstance()->clean(); Kwf_Assets_BuildCache::getInstance()->save(time(), 'assetsVersion'); $langs = $this->getAllLanguages(); $packages = $this->getAllPackages(); $exts = array('js', 'defer.js', 'css', 'ie8.css', 'ie8.js'); $providers = array(); foreach ($packages as $p) { foreach ($p->getProviderList()->getProviders() as $provider) { if (!in_array($provider, $providers)) { $providers[] = $provider; } } } echo "\ninitializing providers...\n"; $steps = count($providers); $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(80); $progress = new Zend_ProgressBar($c, 0, $steps); foreach ($providers as $provider) { $progress->next(1, get_class($provider)); $provider->initialize(); } $progress->finish(); echo "calculating dependencies...\n"; $steps = count($packages) * count($exts); $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(80); $progress = new Zend_ProgressBar($c, 0, $steps); $countDependencies = 0; foreach ($packages as $p) { $depName = $p->getDependencyName(); foreach ($exts as $extension) { $progress->next(1, "{$depName} {$extension}"); $p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]); } $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST); foreach ($it as $i) { $countDependencies++; } } $progress->finish(); echo "compiling assets...\n"; $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_ETA, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(80); $progress = new Zend_ProgressBar($c, 0, $countDependencies); foreach ($packages as $p) { $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST); foreach ($it as $dep) { $progress->next(1, "{$dep}"); if ($dep->getMimeType()) { $mimeType = $dep->getMimeType(); $p->warmupDependencyCaches($dep, $mimeType, $progress); if ($mimeType == 'text/css') { $p->warmupDependencyCaches($dep, 'text/css; ie8', $progress); } } } } $progress->finish(); echo "generating package urls...\n"; $steps = count($packages) * count($langs) * count($exts); $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(80); $progress = new Zend_ProgressBar($c, 0, $steps); $allUrls = array(); foreach ($packages as $p) { $depName = $p->getDependencyName(); foreach ($langs as $language) { foreach ($exts as $extension) { $progress->next(1, "{$depName} {$extension} {$language}"); $urls = $p->getPackageUrls(self::$_mimeTypeByExtension[$extension], $language); if (Kwf_Setup::getBaseUrl()) { foreach ($urls as $k => $i) { $urls[$k] = substr($i, strlen(Kwf_Setup::getBaseUrl())); } } $cacheId = $p->getPackageUrlsCacheId(self::$_mimeTypeByExtension[$extension], $language); Kwf_Assets_BuildCache::getInstance()->save($urls, $cacheId); foreach ($urls as $url) { if (!in_array($url, $allUrls)) { $allUrls[] = $url; } } } } } echo "generating package contents...\n"; $steps = count($allUrls) * 2; $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(80); $progress = new Zend_ProgressBar($c, 0, $steps); foreach ($allUrls as $url) { if (substr($url, 0, 8) != '/assets/') { continue; } $param = explode('/', $url); if (count($param) != 7) { continue; } $dependencyClass = $param[3]; $dependencyParams = $param[4]; $urlLanguage = $param[5]; $urlExtension = $param[6]; $urlExtension = substr($urlExtension, 0, strpos($urlExtension, '?')); if (!class_exists($dependencyClass)) { throw new Kwf_Exception(); } if (!is_instance_of($dependencyClass, 'Kwf_Assets_Interface_UrlResolvable')) { throw new Kwf_Exception(); } $p = call_user_func(array($dependencyClass, 'fromUrlParameter'), $dependencyClass, $dependencyParams); if (!$p instanceof Kwf_Assets_Package) { throw new Kwf_Exception(); } $contents = $p->getUrlContents($urlExtension, $urlLanguage); $progress->next(1, "{$p->getDependencyName()} {$urlExtension} {$urlLanguage} source"); $cacheId = Kwf_Assets_Dispatcher::getInstance()->getCacheIdByPackage($p, $urlExtension, $urlLanguage); Kwf_Assets_BuildCache::getInstance()->save($contents, $cacheId); //save generated caches for clear-cache-watcher $fileName = 'build/assets/output-cache-ids-' . $urlExtension; if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) { file_put_contents($fileName, $cacheId . "\n", FILE_APPEND); } $contents = $p->getUrlContents($urlExtension . '.map', $urlLanguage); $progress->next(1, "{$p->getDependencyName()} {$urlExtension} {$urlLanguage} map"); $cacheId = Kwf_Assets_Dispatcher::getInstance()->getCacheIdByPackage($p, $urlExtension . '.map', $urlLanguage); Kwf_Assets_BuildCache::getInstance()->save($contents, $cacheId); } Kwf_Assets_Cache::getInstance()->clean(); Kwf_Assets_BuildCache::getInstance()->building = false; }
protected function _clearCache($options) { Kwf_Assets_Cache::getInstance()->clean(); }