public function filter_after($chain, $controller, $method, $args, $request, $controller_ret) { $scsses = \Clips\context('scss'); if ($scsses) { $cache = $this->filecache->cacheDir(); $forward_method = \Clips\context('forward_method'); if ($forward_method) { $method = $forward_method; } $full_name = \Clips\to_flat(get_class($controller)) . '_' . $method; $uri = \Clips\path_join(\Clips\path_join($cache, 'css'), $full_name); $cache_filename = \Clips\path_join($cache, 'css', \Clips\to_flat(get_class($controller) . '_' . $method) . '.css'); if (file_exists($cache_filename) && !\Clips\config('debug_sass')) { \Clips\add_css(\Clips\static_url($uri)); return; } if (\Clips\config('debug_sass')) { $this->sass->source_map_file = $cache_filename . '.map'; $this->sass->source_comments = true; $this->sass->source_map_embed = true; $this->sass->source_map_contents = true; } // Add the sass_dir into include pathes $result = $this->sass->compile($scsses); if ($result) { $this->filecache->save(\Clips\to_flat(get_class($controller) . '_' . $method) . '.css', $result, \Clips\path_join($cache, 'css'), true); \Clips\add_css(\Clips\static_url($uri)); } } }
public function testPathJoin() { $this->assertEquals(Clips\path_join(1, 2, 3, 4), '1/2/3/4'); $this->assertEquals(Clips\path_join("a/", "/b"), 'a/b'); $this->assertEquals(Clips\path_join("/a/", "/b/"), '/a/b/'); $this->assertEquals(Clips\path_join("a", "b"), 'a/b'); }
public function contents($uri) { $file = str_replace('app://', '', $uri); $path = \Clips\try_path(\Clips\path_join('application', $file)); if ($path) { return file_get_contents($path); } return null; }
function smarty_block_figure($params, $content = '', $template, &$repeat) { if ($repeat) { Clips\clips_context('indent_level', 1, true); return; } $src = Clips\get_default($params, 'src'); $path = Clips\get_default($params, 'path', 'responsive/size'); $resolutions = Clips\get_default($params, 'resolutions'); $img_path = Clips\find_image($src); if (!$img_path) { Clips\error('figure', array('Can\'t find image ' . $src . '!')); return ''; } $size = Clips\image_size($img_path); $size = $size['width']; if ($resolutions) { // If we are using auto resizing, skip the resolutions foreach ($resolutions as $res) { $attr['data-media' . $res] = Clips\site_url('responsive/size/' . (double) $res / 2880 * (double) $size . '/' . $src); } } else { $attr = array('path' => Clips\site_url($path)); } foreach ($params as $key => $value) { if ($key == 'path') { continue; } if (strpos($key, 'media') !== false) { $attr['data-' . $key] = Clips\site_url('responsive/size/' . $value . '/' . $src); } else { $attr[$key] = $value; } } $caption = Clips\create_tag_with_content('figcaption', $content); $image_dir = Clips\config('image_dir'); if ($image_dir) { $image_dir = $image_dir[0]; } else { $image_dir = 'application/static/img/'; } $img = Clips\create_tag('img', array('src' => Clips\static_url(Clips\path_join($image_dir, $src)))); $noscript = Clips\create_tag_with_content('noscript', $img); $level = Clips\context('indent_level'); if ($level === null) { $level = 0; } else { $level = count($level); } $indent = ''; for ($i = 0; $i < $level; $i++) { $indent .= "\t"; } Clips\context_pop('indent_level'); return Clips\create_tag_with_content('figure', $noscript . "\n{$indent}" . $caption, $attr); }
protected function getConfig() { if (!isset($this->_config)) { $this->_config = array(); // Load the config foreach ($this->value as $config) { $datatable_config_dir = \Clips\clips_config('pagination_config_dir'); if ($datatable_config_dir) { $datatable_config_dir = $datatable_config_dir[0]; $p = \Clips\path_join($datatable_config_dir, $config . '.json'); if (file_exists($p)) { $this->_config[$config] = \Clips\parse_json(file_get_contents($p)); } } } } return $this->_config; }
public function compile($file) { $babel = \Clips\config('babel'); if ($babel) { $babel = $babel[0]; $paths = explode('/', $file); $name = str_replace('.jsx', '', array_pop($paths)); // Remove the filename array_pop($paths); // Remove the jsx folder $folder_name = 'js/' . array_pop($paths); $time = filectime($file); $folder = $this->filecache->mkdir($folder_name); if ($this->filecache->shouldUpdate($name . '.js', $time, $folder)) { $this->logger->debug('Trying to compile {0} to path {1} using command {2}', array($file, $folder, $babel)); \Clips\run($babel, array('-o' => \Clips\path_join($folder, $name . '.js'), '-s' => \Clips\path_join($folder, $name . '.js.map'), $file)); } return $this->filecache->cacheDir() . '/' . $folder_name . '/' . $name . '.js'; } return false; }
public function readFile($file) { if (strpos($file, "://") !== false) { $r = new \Clips\Resource($file); return $r->contents(); } // Try the absolute path first if (file_exists($file)) { return file_get_contents($file); } foreach (\Clips\clips_config('sass_folders', array()) as $folder) { $r = \Clips\try_path(\Clips\path_join($folder, $file)); if ($r) { return file_get_contents($r); } } foreach ($this->includePathes as $path) { $filepath = $path . '/' . $file; if (file_exists($filepath) && is_file($filepath) && is_readable($filepath)) { return file_get_contents($filepath); } } return ''; }
public function createDict($input_file, $output_dir = "/usr/local/etc/") { mmseg_create_dict($input_file, Clips\path_join($output_dir, DICT_NAME)); }
public function show($path, $revision = null) { if (!isset($this->gitrepo)) { return false; } if ($this->has($path)) { if ($this->exists($path, $revision)) { if (!$revision) { $revision = 'HEAD'; } return $this->gitrepo->run('show', array($revision . ':' . $path)); } } else { if (file_exists(\Clips\path_join($this->path, $path))) { return file_get_contents(\Clips\path_join($this->path, $path)); } } return false; }
/** * Render the template using the mustache engine * * @args * template: The template uri for render * args: The args that used for render */ public function render($template, $args = array()) { if (func_num_args() > 2) { // They must using the variable args method $args = func_get_args(); $template = array_shift($args); return $this->render($template, $args); } // Get the cache file name by md5 it $hash = md5($template); $cacheDir = $this->filecache->cacheDir(); if (!file_exists($cacheDir)) { // Create the directory if not exists mkdir($cacheDir, 0755, true); } $phpname = \Clips\path_join($cacheDir, 'tpl_' . $hash . '.php'); $debug = \Clips\config('debug_template'); // Check if we can found the compiled php if (!file_exists($phpname) || $debug) { // Can't found this template in cache, read it from resource $resource = new \Clips\Resource($template); $str = $resource->contents(); $flags = \Clips\context('template_flags'); if (!$flags) { $flags = \Clips\config('template_flags'); if ($flags) { $flags = $flags[0]; } else { $flags = LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARS | LightnCandy::FLAG_HANDLEBARSJS; } } $opts = array('flags' => $flags); $partials = \Clips\context('template_partials'); if ($partials) { $opts['partials'] = $partials; } $default_helpers = array('site_url' => '\\Clips\\Libraries\\site_url', 'static_url' => '\\Clips\\Libraries\\static_url', 'php' => '\\Clips\\Libraries\\php_call'); $default_block_helpers = array('times' => '\\Clips\\Libraries\\times', 'randtimes' => '\\Clips\\Libraries\\randtimes'); $helpers = \Clips\context('template_helpers'); if ($helpers) { $opts['helpers'] = array_merge($default_helpers, $helpers); } else { $opts['helpers'] = $default_helpers; } $block_helpers = \Clips\context('template_block_helpers'); if ($block_helpers) { $opts['hbhelpers'] = array_merge($default_block_helpers, $block_helpers); } else { $opts['hbhelpers'] = $default_block_helpers; } if ($str) { $php = "<?php " . PHP_EOL . LightnCandy::compile($str, $opts); // Save it to php file file_put_contents($phpname, $php); } } if (file_exists($phpname)) { $renderer = (include $phpname); return $renderer((array) $args); } return ''; }
public function mkdir($folder) { $cache = $this->cacheDir(); $f = \Clips\path_join($cache, $folder); if (!file_exists($f)) { // If no folder exists, make it mkdir($f, 0777, true); } return \Clips\try_path($f); }
public function size() { if (func_num_args() < 2) { $this->error('Argument is too little for responsive!', 'responsive'); return; } $args = array_map(function ($item) { return urldecode($item); }, func_get_args()); $size = array_shift($args); $file = implode('/', $args); $size = preg_replace('/[^0-9\\.]/', '', $size); if (!is_numeric($size) || $size <= 0) { $this->error('Size [' . $size . '] must be positive number!', 'responsive'); return; } foreach (\Clips\config('image_dir') as $dir) { $path = \Clips\try_path(\Clips\path_join($dir, $file)); if ($path) { break; } } $n = array_pop($args); if ($args) { // If we still have args, make it as the folder $folder = implode('/', $args); } else { $folder = ''; } $cache = $this->filecache->cacheDir(); $folder = \Clips\path_join($cache, 'img', $folder, $size); if (!file_exists($folder)) { // If no folder exists, make it mkdir($folder, 0777, true); } if (!$path) { foreach (\Clips\config('image_url_base') as $base) { $url = $base . '/' . $file; $f = \Clips\path_join($cache, 'img', implode('/', $args)); $local_file = $this->filecache->fileName($n, $f); $path = \Clips\try_path($local_file); if ($path) { break; } $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, false); $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); $this->curl->setOpt(CURLOPT_HEADER, false); $this->curl->setOpt(CURLOPT_FOLLOWLOCATION, true); if ($this->curl->get($url) == 0) { file_put_contents($local_file, $this->curl->response); $path = \Clips\try_path($local_file); } break; } if (!$path) { $this->error('Can\'t find image file [' . $file . '] !', 'responsive'); return; } } $name = $this->filecache->fileName($n, $folder); if ($this->filecache->shouldUpdate($n, $path, $folder)) { $this->imageutils->thumbnail($path, $name, $size); } return $this->image($name); }
function smarty_block_action($params, $content = '', $template, &$repeat) { if ($repeat) { Clips\clips_context('indent_level', 1, true); return; } $default = array('class' => 'action'); $action = Clips\get_default($params, 'action'); if ($action && Clips\valid_obj($action, 'Clips\\Interfaces\\Action')) { $ps = $action->params(); $icon = $action->icon(); $caption = array(); if ($icon) { $caption[] = Clips\create_tag_with_content('i', '', array('class' => $icon)); $caption[] = Clips\create_tag_with_content('span', $action->label()); if ($action->children()) { $caption[] = Clips\create_tag_with_content('i', '', array('class' => array('fa', 'fa-angle-left', 'pull-right'))); } $caption = implode('', $caption); } else { $caption = $action->label(); } if (!isset($params['title'])) { // Add tooltip $params['title'] = $action->label(); } // This is valid action object switch ($action->type()) { case Action::CLIENT: $params['caption'] = $caption; if ($ps) { foreach ($ps as $k => $v) { $params['data-' . $k] = $v; } } $content = $action->content(); break; case Action::HEADER: return $caption; case Action::SERVER: $content = $caption; if ($ps) { $suffix = implode('/', array_map(function ($item) { return urlencode($item); }, $ps)); $params['uri'] = \Clips\path_join($action->content(), $suffix); } else { $params['uri'] = $action->content(); } break; case Action::EXTERNAL: $content = $caption; $suffix = array(); foreach ($ps as $k => $v) { $suffix[] = urlencode($k) . '=' . urlencode($v); } if ($suffix) { $suffix = implode('&', $suffix); $params['href'] = $action->content() . '?' . $suffix; } else { $params['href'] = $action->content(); } break; } unset($params['action']); } $value = Clips\get_default($params, 'caption'); if ($value) { // We did have value, so the content is the JavaScript $id = Clips\get_default($params, 'id', 'clips_action_' . Clips\sequence('action')); $js = "\$(\"#{$id}\").click(function(){\n\t\t" . trim($content) . "\n\t});"; $content = $template->fetch('string:' . $value); unset($params['caption']); $params['id'] = $id; $params['href'] = 'javascript:void(0)'; Clips\context('jquery_init', $js, true); } else { // Check for action uri $uri = Clips\get_default($params, 'uri'); if ($uri) { $params['href'] = Clips\site_url($uri); unset($params['uri']); } if (!isset($params['title'])) { // Add tooltip $params['title'] = trim($content); } } Clips\context_pop('indent_level'); $bundle_name = Clips\context('current_bundle'); $bundle = Clips\get_default($params, 'bundle', $bundle_name); if ($bundle !== null) { $bundle = Clips\bundle($bundle); $content = $bundle->message($content); } return Clips\create_tag_with_content('a', $content, $params, $default); }
function smarty_function_js($params, $template) { $tool = \Clips\get_clips_tool(); //Processing jquery init $init = \Clips\clips_context('jquery_init'); if ($init) { if (!is_array($init)) { $init = array($init); } $init[] = "if(typeof initialize === 'function') initialize();"; \Clips\add_init_js("//====================================\n// The jQuery initialize function \n//====================================\n\n" . 'jQuery(function($){' . "\n\t" . implode("\n\t", $init) . "\n" . '});'); } // Processing js $scripts = \Clips\context('js'); if ($scripts) { if (!is_array($scripts)) { $scripts = array($scripts); } $output = array(); $init = array(); foreach ($scripts as $item) { if (is_array($item)) { $js = explode("\n", $item['script']); $ret = array(); foreach ($js as $line) { $ret[] = "\t" . $line; } $init[] = implode("\n\t\t", $ret); } else { if (is_object($item)) { $js = explode("\n", $item->script); $ret = array(); foreach ($js as $line) { $ret[] = "\t" . trim($line); } $init[] = implode("\n\t\t", $ret); } else { $path = \Clips\safe_add_extension($item, 'js'); if (strpos($path, 'http:') === false) { $path = \Clips\static_url($path); } $output[] = '<script type="text/javascript" src="' . $path . '"></script>'; } } } $output[] = '<script type="text/javascript">' . "\n\t\t" . implode("\n\t\t", $init) . "\n\t\t" . '</script>'; // Added the jsx support for ReactJs $jsx = \Clips\context('jsx'); if ($jsx) { if (\Clips\config('babel')) { // TODO: This is a little bit ugly, needs refactor.... $babel = $tool->library('babel'); foreach ($jsx as $item) { $path = Clips\try_path(Clips\safe_add_extension($item, 'jsx')); if ($path) { $output[] = '<script type="text/javascript" src="' . Clips\static_url($babel->compile($path)) . '"></script>'; } } } else { foreach ($jsx as $item) { $output[] = '<script type="text/babel" src="' . Clips\static_url(Clips\safe_add_extension($item, 'jsx')) . '"></script>'; } } } $jsx = \Clips\context('jsx_script'); if ($jsx) { if (\Clips\config('babel')) { $babel = $tool->library('babel'); $cache = $tool->library('fileCache'); $c = \Clips\context('controller'); $cm = \Clips\context('controller_method'); $name = $c . '_' . $cm . '.jsx'; $cache_file = $cache->cacheDir() . '/js/cache/' . $c . '_' . $cm . '.js'; if (\Clips\try_path($cache_file) && !\Clips\config('debug_jsx')) { $output[] = '<script type="text/javascript" src="' . Clips\static_url($cache_file) . '"></script>'; } else { $cache->save($name, $jsx, \Clips\path_join($cache->cacheDir(), 'js')); $output[] = '<script type="text/javascript" src="' . Clips\static_url($babel->compile($cache->cacheDir() . '/js/' . $name)) . '"></script>'; } } else { $output[] = '<script type="text/babel">' . implode("\n\t\t", $jsx) . '</script>'; } } return implode("\n\t\t", $output); } else { return ''; } }
public function widget() { $config = \Clips\interactive('interactive/widget', $this); $config->date = strftime("%a %b %e %H:%M:%S %Y"); $config->widget = ucfirst($config->widget); $namespace = \Clips\config('namespace'); if ($namespace) { $config->namespace = $namespace[0]; } else { $config->namespace = 'Clips\\'; } $widget_dir = \Clips\clips_config('widget_dir'); foreach ($widget_dir as $p) { $path = \Clips\try_path($p); if ($path) { break; } } if (!$path) { $this->output('Can\'t find any widget directory, generation failed!'); return -1; } $dir = \Clips\path_join($path, $config->widget); if (\file_exists($dir)) { $this->output('Widget %s exists!', $config->widget); return -1; } // Make the widget dir first mkdir($dir, 0755, true); // Write the widget configuration \file_put_contents(\Clips\path_join($dir, 'widget.yml'), \Clips\clips_out('widget', $config, false)); // Write the widget class \file_put_contents(\Clips\path_join($dir, 'Widget.php'), \Clips\clips_out('widget_class', $config, false)); $this->output('Done!'); }
function smarty_function_picture($params, $template) { $src = Clips\get_default($params, 'src'); if ($src) { if (preg_match('/_\\([A-Za-z0-9_]+\\)/', $src)) { $is_template = true; } } if (!isset($is_template)) { $path = Clips\get_default($params, 'path', 'responsive/size'); $resolutions = Clips\get_default($params, 'resolutions'); $medias = Clips\get_default($params, 'medias'); $img_path = Clips\find_image($src); if (!$img_path) { Clips\error('picture', array('Can\'t find image ' . $src . '!')); return ''; } $size = Clips\image_size($img_path); $size = $size['width']; $content = array(); $level = Clips\context('indent_level'); if ($level === null) { $level = 0; } else { $level = count($level); } $indent = ''; for ($i = 0; $i < $level; $i++) { $indent .= "\t"; } if ($resolutions || $medias) { if ($resolutions) { // If we are using auto resizing, skip the resolutions unset($params['resolutions']); foreach ($resolutions as $res) { $content[] = "\t" . Clips\create_tag('source', array('src' => Clips\site_url('responsive/size/' . (double) $res / 2880 * (double) $size . '/' . $src), 'media' => '(min-width:' . $res . 'px)')); } } if ($medias) { unset($params['medias']); foreach ($medias as $media => $res) { $content[] = "\t" . Clips\create_tag('source', array('src' => Clips\site_url('responsive/size/' . $res . '/' . $src), 'media' => '(min-width:' . $media . 'px)')); } } } else { $params['path'] = Clips\site_url($path); } $image_dir = Clips\config('image_dir'); if ($image_dir) { $image_dir = $image_dir[0]; } else { $image_dir = 'application/static/img/'; } Clips\clips_context('indent_level', 1, true); $img = Clips\create_tag('img', array('src' => Clips\static_url(Clips\path_join($image_dir, $src)))); $content[] = "\t" . Clips\create_tag_with_content('noscript', $img); Clips\context_pop('indent_level'); } else { $params['data-role'] = 'item-picture'; $params['path'] = Clips\site_url(Clips\get_default($params, 'path', 'responsive/size')); $indent = ''; $content = array(); } return Clips\create_tag_with_content('picture', implode("\n{$indent}", $content), $params); }