function smarty_block_less($params, $content, Smarty_Internal_Template $template, $open) { /** @var CM_Frontend_Render $render */ $render = $template->smarty->getTemplateVars('render'); $assetCss = new CM_Asset_Css($render); $assetCss->addVariables(); $assetCss->add($content); return $assetCss->get(); }
function smarty_function_lessVariable(array $params, Smarty_Internal_Template $template) { /** @var CM_Frontend_Render $render */ $render = $template->smarty->getTemplateVars('render'); $variableName = (string) $params['name']; $assetCss = new CM_Asset_Css($render); $assetCss->addVariables(); $assetCss->add('foo:@' . $variableName); $css = $assetCss->get(true); if (!preg_match('/^foo:(.+);$/', $css, $matches)) { throw new CM_Exception_Invalid('Cannot detect variable `' . $variableName . '` from CSS `' . $css . '`.'); } return $matches[1]; }
/** * @param CM_Frontend_Render $render * @throws CM_Exception */ public function __construct(CM_Frontend_Render $render) { parent::__construct($render); $this->addVariables(); $file = new CM_File(DIR_PUBLIC . 'static/css/library/icon.less'); if ($file->exists()) { $this->add($file->read()); } foreach (array_reverse($render->getSite()->getModules()) as $moduleName) { foreach (array_reverse($render->getSite()->getThemes()) as $theme) { foreach (CM_Util::rglob('*.less', $render->getThemeDir(true, $theme, $moduleName) . 'css/') as $path) { $file = new CM_File($path); $this->add($file->read()); } } } $viewClasses = CM_View_Abstract::getClassChildren(true); foreach ($viewClasses as $viewClassName) { $validModule = in_array(CM_Util::getNamespace($viewClassName), $render->getSite()->getModules()); $validViewClass = $this->_isValidViewClass($viewClassName); if ($validModule && $validViewClass) { $asset = new CM_Asset_Css_View($this->_render, $viewClassName); $this->add($asset->_getContent()); } } }
/** * @param CM_Frontend_Render $render * @param string $className * @throws CM_Exception */ public function __construct(CM_Frontend_Render $render, $className) { parent::__construct($render); if (!preg_match('#^([^_]+)_([^_]+)_?(.*)$#', $className, $matches)) { throw new CM_Exception('Cannot detect all className parts from view\'s className', null, ['className' => $className]); } list($className, $namespace, $viewType, $viewName) = $matches; $viewPath = $viewType . '/'; if ($viewName) { $viewPath .= $viewName . '/'; } $relativePaths = array(); foreach ($render->getSite()->getThemes() as $theme) { $basePath = $render->getThemeDir(true, $theme, $namespace) . $viewPath; foreach (CM_Util::rglob('*.less', $basePath) as $path) { $relativePaths[] = preg_replace('#^' . $basePath . '#', '', $path); } } foreach (array_unique($relativePaths) as $path) { $prefix = '.' . $className; if ($path !== 'default.less' && strpos($path, '/') === false) { $prefix .= '.' . preg_replace('#.less$#', '', $path); } $file = $render->getLayoutFile($viewPath . $path, $namespace); $this->add($file->read(), $prefix); } }
public function __construct(CM_Frontend_Render $render) { parent::__construct($render); $extensions = array('css', 'less'); foreach (array_reverse($render->getSite()->getModules()) as $moduleName) { $libraryPath = DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . 'client-vendor/'; foreach ($extensions as $extension) { foreach (CM_Util::rglob('*.' . $extension, $libraryPath) as $path) { $file = new CM_File($path); $this->add($file->read()); } } } }
public function testCompress() { $render = new CM_Frontend_Render(); $css = <<<'EOD' .foo { color: red; } EOD; $expected = '.foo{color:red;}'; $css = new CM_Asset_Css($render, $css, ['autoprefixerBrowsers' => 'Chrome 30']); $this->assertSame(trim($expected), $css->get(true)); }
/** * @param CM_Asset_Css $asset */ protected function _setAsset(CM_Asset_Css $asset) { $this->_setContent($asset->get()); }
/** * @param $variableName * @return string * @throws CM_Exception_Invalid */ public function getLessVariable($variableName) { $variableName = (string) $variableName; $cache = new CM_Cache_Local(); return $cache->get($cache->key(__METHOD__, $variableName), function () use($variableName) { $assetCss = new CM_Asset_Css($this); $assetCss->addVariables(); $assetCss->add('foo:@' . $variableName); $css = $assetCss->get(true); if (!preg_match('/^foo:(.+);$/', $css, $matches)) { throw new CM_Exception_Invalid('Cannot detect variable from CSS.', null, ['variableName' => $variableName, 'css' => $css]); } return (string) $matches[1]; }); }
/** * @param CM_Asset_Css $asset */ protected function _setAsset(CM_Asset_Css $asset) { $compress = !CM_Bootloader::getInstance()->isDebug(); $this->_setContent($asset->get($compress)); }