Beispiel #1
0
 /**
  * @param string[] $mainPaths
  * @param string[] $sourcePaths
  * @param bool     $generateSourceMaps
  * @return string
  */
 protected function _browserify(array $mainPaths, array $sourcePaths, $generateSourceMaps)
 {
     if (!count($mainPaths)) {
         return '';
     }
     $involvedFiles = [];
     foreach ($sourcePaths as $sourcePath) {
         $involvedFiles = array_merge($involvedFiles, CM_Util::rglob('*.js', $sourcePath));
     }
     foreach ($mainPaths as $mainPath) {
         $involvedFiles[] = $mainPath;
     }
     $cacheKeyContent = \Functional\reduce_left(array_unique($involvedFiles), function ($path, $index, $collection, $carry) {
         return md5($carry . (new CM_File($path))->read());
     }, '');
     $cacheKeyMainPaths = \Functional\reduce_left($mainPaths, function ($path, $index, $collection, $carry) {
         return md5($carry . $path);
     }, '');
     $cache = CM_Cache_Persistent::getInstance();
     $cacheKey = $cache->key(__METHOD__, $cacheKeyContent, $cacheKeyMainPaths, $generateSourceMaps);
     return $cache->get($cacheKey, function () use($mainPaths, $sourcePaths, $generateSourceMaps) {
         $args = $mainPaths;
         if ($generateSourceMaps) {
             $args[] = '--debug';
         }
         return CM_Util::exec('NODE_PATH="' . implode(':', $sourcePaths) . '" browserify', $args);
     });
 }
Beispiel #2
0
 public function testCacheCustom()
 {
     $source = new CM_PagingSource_Sql('`num`', 'test');
     $fileCache = CM_Cache_Persistent::getInstance();
     $source->enableCache(null, $fileCache);
     $this->assertEquals(100, $source->getCount());
     CM_Db_Db::delete('test', array('num' => 0));
     $this->assertEquals(100, $source->getCount());
     $source->clearCache();
     $this->assertEquals(99, $source->getCount());
     CM_Db_Db::delete('test', array('num' => 1));
     $this->assertEquals(99, $source->getCount());
     $fileCache->flush();
     $this->assertEquals(98, $source->getCount());
 }
Beispiel #3
0
 /**
  * @param CM_Model_Language $language
  * @param bool|null         $javascriptOnly
  */
 public function __construct(CM_Model_Language $language, $javascriptOnly = null)
 {
     $this->_language = $language;
     $this->_javascriptOnly = (bool) $javascriptOnly;
     $where = null;
     if ($javascriptOnly) {
         $where = 'k.javascript = 1';
         // Include the javascript version in the cache key, so the paging invalidates when the version changes.
         $javascriptVersion = CM_Model_Language::getVersionJavascript();
         $where .= " AND '{$javascriptVersion}' = '{$javascriptVersion}'";
     }
     $orderBy = 'k.name ASC';
     $join = 'LEFT JOIN `cm_languageValue` AS v ON k.id = v.languageKeyId AND v.languageId = ' . $this->_language->getId() . ' ';
     $groupBy = 'BINARY k.name';
     $source = new CM_PagingSource_Sql_Deferred('k.name AS `key`, v.value, k.variables', 'cm_model_languagekey` as `k', $where, $orderBy, $join, $groupBy);
     $source->enableCache(null, CM_Cache_Persistent::getInstance());
     parent::__construct($source);
 }
Beispiel #4
0
 /**
  * @param string $content
  * @param bool   $compress
  * @return string
  */
 private function _compileLess($content, $compress)
 {
     $render = $this->_render;
     $lessCompiler = new lessc();
     $lessCompiler->registerFunction('image', function ($arg) use($render) {
         /** @var CM_Frontend_Render $render */
         list($type, $delimiter, $values) = $arg;
         return array('function', 'url', array('string', $delimiter, array($render->getUrlResource('layout', 'img/' . $values[0]))));
     });
     $lessCompiler->registerFunction('image-inline', function ($arg) use($render) {
         /** @var CM_Frontend_Render $render */
         list($type, $delimiter, $values) = $arg;
         if (2 == sizeof($values) && is_array($values[0]) && is_array($values[1])) {
             $delimiter = (string) $values[0][1];
             $path = (string) $values[0][2][0];
             $size = (int) $values[1][1];
         } else {
             $path = $values[0];
             $size = 0;
         }
         $imagePath = $render->getLayoutPath('resource/img/' . $path, null, null, true, true);
         $cache = CM_Cache_Persistent::getInstance();
         $imageBase64 = $cache->get($cache->key(__METHOD__, md5($imagePath), 'size:' . $size), function () use($imagePath, $size) {
             $file = new CM_File($imagePath);
             $img = new CM_Image_Image($file->read());
             if ($size > 0) {
                 $img->resize($size, $size);
             }
             $img->setFormat(CM_Image_Image::FORMAT_GIF);
             return base64_encode($img->getBlob());
         });
         $url = 'data:image/gif;base64,' . $imageBase64;
         return array('function', 'url', array('string', $delimiter, array($url)));
     });
     $lessCompiler->registerFunction('urlFont', function ($arg) use($render) {
         /** @var CM_Frontend_Render $render */
         list($type, $delimiter, $values) = $arg;
         return array($type, $delimiter, array($render->getUrlStatic('/font/' . $values[0])));
     });
     if ($compress) {
         $lessCompiler->setFormatter('compressed');
     }
     return $lessCompiler->compile($content);
 }