protected function _writeFile() { if ($this->_file_writed) { return; } $files = array(); $join_contents = ''; foreach ($this->children as $child) { if ($child instanceof lmbJsRequireOnceMacroTag) { $file = $child->getFilePath(); if (!is_file($file) || !realpath($file) || null === ($content = file_get_contents($file))) { if ($child->getBool('safe')) { $join_contents .= "\n/* include " . basename($file) . " - NOT FOUND */\n"; continue; } else { throw new lmbMacroException('File "' . $file . '" not found in ' . $this->getRootDir() . ', src: "' . $child->get('src') . '"'); } } $file = lmbFs::normalizePath(realpath($file)); if (!in_array($file, $files)) { $files[] = $file; $join_contents .= "\n/* include " . basename($file) . " */\n" . $content; } } } sort($files, SORT_STRING); $url = lmbFs::normalizePath(ltrim($this->get('dir') . '/' . md5(implode("\n", $files)) . '.js', '/')); $path = $this->getRootDir() . '/' . $url; lmbFs::safeWrite($path, trim($join_contents)); $this->_file_path = $path; $this->_file_url = $url; $this->_file_writed = true; }
protected function scanForFiles($dir) { $result = lmbFs::findRecursive($dir, $types = 'f', $include_regex = '#.ph(tml|p)$#is'); foreach ($result as $name) { $this->_found_files[] = $name; } }
protected function _writeFile() { if ($this->_file_writed) { return; } $this->_file_writed = true; $file = lmbFs::normalizePath($this->get('src'), lmbFs::UNIX); $abs_file = $this->getRootDir() . '/' . $file; if (!file_exists($abs_file)) { if ($this->getBool('safe', false)) { $this->_file_path = $abs_file; $this->_file_url = $this->get('src'); return; } else { throw new lmbMacroException('File ' . $abs_file . ' not found!'); } } $contents = file_get_contents($abs_file); $css_dir = dirname($file); if (preg_match_all('~url\\(([^\\)]+)\\)~', $contents, $matches)) { $replaces = array(); foreach ($matches[1] as $key => $match) { $match = trim($match, '\'" '); $replaces[$matches[0][$key]] = 'url(' . $this->addVersion(lmbFs::normalizePath($css_dir . '/' . $match, lmbFs::UNIX)) . ')'; } $contents = str_replace(array_keys($replaces), array_values($replaces), $contents); } $url = lmbFs::normalizePath(ltrim($this->get('dir') . '/' . str_replace('/', '-', $file), '/')); $path = $this->getRootDir() . '/' . $url; lmbFs::safeWrite($path, trim($contents)); $this->_file_path = $path; $this->_file_url = $url; }
function flush() { $files = lmbFs::find($this->getCacheDir(), 'f'); foreach ($files as $file) { lmbFs::rm($file); } }
function build($argv) { $input = new lmbCliInput(); $input->setMinimumArguments(2); $input->read($argv, false); $arguments = $input->getArguments(); if (!($dest_file = array_pop($arguments))) { echo "Error: You must specify output file.\n"; return 1; } $src_files = array(); foreach ($arguments as $src_file) { $src_files[] = realpath($src_file); } if (empty($src_files)) { echo "Error: You must specify at least one input file.\n"; return 1; } $builder = new lmbJsPreprocessor(); try { $contents = $builder->processFiles($src_files); } catch (lmbException $e) { echo 'Build error: ' . $e->getMessage(); return 1; } lmbFs::safeWrite($dest_file, $contents); return 0; }
protected function _saveCache() { if (!$this->isCacheEnabled()) { return; } lmbFs::safeWrite($this->getCacheFile(), serialize($this->export())); }
function getConf($name) { $name = $this->_normalizeConfName($name); if (isset($this->confs[$name])) { return $this->confs[$name]; } $ext = substr($name, strpos($name, '.')); if ($ext == '.ini') { $file = $this->_locateConfFiles($name); if (lmb_env_has('LIMB_VAR_DIR')) { $this->confs[$name] = new lmbCachedIni($file, lmb_env_get('LIMB_VAR_DIR') . '/ini/'); } else { $this->confs[$name] = new lmbIni($file); } } elseif ($ext == '.yml') { $file = $this->_locateConfFiles($name); $this->confs[$name] = $this->parseYamlFile(lmbFs::normalizePath($file)); } elseif ($ext == '.conf.php') { $file = $this->_locateConfFiles($name); if (!count($file)) { throw new lmbFileNotFoundException($name); } $this->confs[$name] = new lmbConf(lmbFs::normalizePath($file)); } else { throw new lmbException("'{$ext}' type configuration is not supported!"); } return $this->confs[$name]; }
protected function _appendToFile($file_name, $message, $stamp) { lmbFs::mkdir(dirname($file_name), 0775); $file_existed = file_exists($file_name); if ($fh = fopen($file_name, 'a')) { @flock($fh, LOCK_EX); $time = strftime("%b %d %Y %H:%M:%S", $stamp); $log_message = PHP_EOL . "=====[" . $time . "]"; if (isset($_SERVER['REMOTE_ADDR'])) { $log_message .= "[" . $_SERVER['REMOTE_ADDR'] . "]"; } if (isset($_SERVER['REQUEST_URI'])) { $log_message .= "["; if (isset($_SERVER['REQUEST_METHOD'])) { $log_message .= $_SERVER['REQUEST_METHOD'] . " - "; } $log_message .= $_SERVER['REQUEST_URI'] . "]"; } $log_message .= "=====" . PHP_EOL . $message; fwrite($fh, $log_message); @flock($fp, LOCK_UN); fclose($fh); if (!$file_existed) { chmod($file_name, 0664); } } else { throw new lmbFsException("Cannot open log file '{$file_name}' for writing" . PHP_EOL . "The web server must be allowed to modify the file." . PHP_EOL . "File logging for '{$file_name}' is disabled."); } }
function setUp() { $this->cache_dir = LIMB_VAR_DIR . '/fpcache/'; lmbFs::rm($this->cache_dir); $this->filter2 = new MockInterceptingFilter(); $this->user = new lmbFullPageCacheUser(); $this->toolkit = lmbToolkit::save(); }
function setUp() { parent::setUp(); $this->conn = lmbToolkit::instance()->getDefaultDbConnection(); $dir_for_tests = lmb_var_dir() . '/constructor/'; $this->dir_for_test_case = $dir_for_tests . '/' . get_class($this); lmbFs::mkdir($this->dir_for_test_case); }
function saveCache() { if (!$this->_changed) { return; } $content = serialize($this->_cached_paths); lmbFs::safeWrite($this->getCacheFile(), $content); }
function locateSourceTemplate($file_name) { if (!lmbFs::isPathAbsolute($file_name)) { return $this->toolkit->tryFindFileByAlias($file_name, $this->scan_dirs, 'macro'); } elseif (file_exists($file_name)) { return $file_name; } }
function testOnceRender() { lmb_env_set('LIMB_DOCUMENT_ROOT', lmb_env_get('LIMB_VAR_DIR') . '/www'); lmbFs::safeWrite(lmb_env_get('LIMB_VAR_DIR') . '/www/js/main.js', 'function() { alert(1); }'); $template = '{{js:require_once src="js/main.js" }}{{js_once src="js/main.js" }}'; $page = $this->_createMacroTemplate($template, 'tpl.html'); $content = '<script type="text/javascript" src="' . $this->toolkit->addVersionToUrl('js/main.js') . '" ></script>'; $this->assertEqual($content, $page->render()); }
function lmb_tests_init_var_dir($value) { if (file_exists($value)) { lmbFs::rm($value); } lmbFs::mkdir($value); $real = realpath($value) . '/'; lmb_var_dir($real); }
function testLocateAll() { $locator = new lmbFileLocator($mock = new MockFileLocations()); $mock->expectOnce('getLocations'); $mock->setReturnValue('getLocations', array(dirname(__FILE__) . '/design/', dirname(__FILE__) . '/design/_en/')); $all_files = $locator->locateAll('*.html'); sort($all_files); $this->assertEqual(lmbFs::normalizePath($all_files[0]), lmbFs::normalizePath(dirname(__FILE__) . '/design/test1.html')); $this->assertEqual(lmbFs::normalizePath($all_files[1]), lmbFs::normalizePath(dirname(__FILE__) . '/design/_en/test1.html')); }
function setUp() { parent::setUp(); lmbFs::mkdir($this->tpl_dir . '/_mail'); $toolkit = lmbToolkit::instance(); $toolkit->setConf('macro', $this->_createMacroConfig()); $mail_config = $toolkit->getConf('mail'); $mail_config->set('mode', 'testing'); $toolkit->setConf('mail', $mail_config); }
protected function _addFile($dir, $name, $content, $always_override = false) { $file = $dir . '/' . $name; if (!$always_override && !$this->_override_files && file_exists($file)) { if (!lmb_cli_ask_for_accept("Overwrite '{$file}'")) { return false; } } return lmbFs::safeWrite($file, $content); }
function save($cache, $contents) { $file = $this->_getFilePath($cache); $dir = dirname($file); //cache conflict if (basename($dir) == $this->cache_file) { return false; } lmbFs::mkdir($dir); lmbFs::safeWrite($file, $contents); }
function processFile($file) { $file = lmbFs::normalizePath($file); if ($this->_isProcessed($file)) { return ''; } $contents = file_get_contents($file); $this->_markAsProcessed($file); $result = $this->_processDirectives($contents); return $result; }
function _createConfig($name = 'conf.php', $content = false) { if (!$content) { $content = <<<EOD <?php \$conf = array('foo' => 1, 'bar' => 2); EOD; } lmbFs::safeWrite($this->_getConfigPath($name), $content); return new lmbConf($this->_getConfigPath($name)); }
function getLogDSNes() { $default_dsn = 'file://' . lmbFs::normalizePath(lmb_env_get('LIMB_VAR_DIR') . '/log/error.log'); if (!$this->toolkit->hasConf('common')) { return array($default_dsn); } $conf = $this->toolkit->getConf('common'); if (!isset($conf['logs'])) { return array($default_dsn); } return $conf['logs']; }
function setUp() { $this->base_dir = lmb_var_dir() . '/tpl'; $this->tpl_dir = $this->base_dir; $this->cache_dir = $this->base_dir . '/compiled'; $this->tags_dir = dirname(__FILE__) . '/../../src/tags'; $this->filters_dir = dirname(__FILE__) . '/../../src/filters'; lmbFs::rm(lmb_var_dir()); lmbFs::mkdir(lmb_var_dir()); lmbFs::mkdir($this->base_dir); lmbFs::mkdir($this->tpl_dir); lmbFs::mkdir($this->cache_dir); }
function testOnceRender() { $root = lmb_env_get('LIMB_VAR_DIR') . '/www/'; lmb_env_set('LIMB_DOCUMENT_ROOT', $root); lmbFs::safeWrite($root . 'style/main.css', 'body {background-url: url("../images/one.jpg");}'); lmbFs::safeWrite($root . 'images/one.jpg', 'simple content'); $template = '{{css_compiled src="style/main.css" dir="media/css" /}}'; $page = $this->_createMacroTemplate($template, 'tpl.html'); $content = $page->render(); $src = $this->toolkit->addVersionToUrl('media/css/style-main.css'); $this->assertEqual('<link rel="stylesheet" type="text/css" href="' . $src . '" />', $content); $compiled_file = $root . 'media/css/style-main.css'; $src = $this->toolkit->addVersionToUrl('images/one.jpg'); $this->assertEqual('body {background-url: url(' . $src . ');}', file_get_contents($compiled_file)); }
function testGzipStatic() { if (!function_exists('gzencode')) { return print "Skip: function gzencode not exists.\n"; } lmb_env_set('LIMB_DOCUMENT_ROOT', lmb_env_get('LIMB_VAR_DIR') . '/www/'); lmbFs::safeWrite(lmb_env_get('LIMB_VAR_DIR') . '/www/one.js', 'var window = {};'); $doc_root = lmb_env_get('LIMB_DOCUMENT_ROOT'); $template = '{{file:version src="one.js" gzip_static_dir="media/var/gz" gzip_level="9" }}'; $page = $this->_createMacroTemplate($template, 'tpl.html'); $content = $page->render(); $file = 'media/var/gz/one.js'; $this->assertEqual($content, $this->toolkit->addVersionToUrl($file, false)); $this->assertEqual('var window = {};', file_get_contents($doc_root . $file)); $gz_file = $doc_root . $file . '.gz'; $this->assertTrue(file_exists($gz_file)); $this->assertEqual(gzencode('var window = {};', 9, FORCE_DEFLATE), file_get_contents($gz_file)); }
protected function _appendToFile($file_name, $title, $message, $stamp) { lmbFs::mkdir(dirname($file_name), 0775); $file_existed = file_exists($file_name); if ($fh = fopen($file_name, 'a')) { @flock($fh, LOCK_EX); $time = strftime("%b %d %Y %H:%M:%S", $stamp); $log_message = "========================= {$title} [{$time}]"; $log_message .= "=========================\n" . $message; fwrite($fh, $log_message); @flock($fh, LOCK_UN); fclose($fh); if (!$file_existed) { chmod($file_name, 0664); } } else { throw new lmbFsException("Cannot open log file '{$file_name}' for writing" . PHP_EOL . "The web server must be allowed to modify the file." . PHP_EOL . "File logging for '{$file_name}' is disabled."); } }
function create($argv) { $input = new lmbCliInput(); $input->setMinimumArguments(1); if (!$input->read($argv, false)) { $this->help($argv); return 1; } $dst_dir = $input->getArgument(0); if (file_exists($dst_dir)) { echo "Directory or file '{$dst_dir}' already exists\n"; return 1; } echo "Copying skeleton Limb3 WEB_APP application to '{$dst_dir}'...\n"; lmbFs::cp(dirname(__FILE__) . '/../skel', $dst_dir, '~^\\.svn~'); echo "Generating code from templates...\n"; $this->_resolveTemplate("{$dst_dir}/setup.override.php.tpl", array('%LIMB_PARENT_DIR%' => realpath(dirname(__FILE__) . '/../../../'))); echo "done!"; }
function locate($alias, $params = array()) { if (false !== strpos($alias, "")) { return false; } if (lmbFs::isPathAbsolute($alias)) { if (file_exists($alias)) { return $alias; } else { $this->_handleNotResolvedAlias($alias); } } $paths = $this->locations->getLocations($params); foreach ($paths as $path) { if (file_exists($path . '/' . $alias)) { return $path . '/' . $alias; } } $this->_handleNotResolvedAlias($alias); }
function testNotFoundFile() { $root = lmb_env_get('LIMB_VAR_DIR') . '/www'; lmb_env_set('LIMB_DOCUMENT_ROOT', $root); lmbFs::rm($root); $template = '{{js_combined dir="media/"}}{{js_once src="js/main.js" }}{{/js_combined}}'; $page = $this->_createMacroTemplate($template, 'tpl.html'); try { $page->render(); $this->assertTrue(false); } catch (lmbException $e) { $this->assertTrue(true); } lmbFs::safeWrite($root . '/js/blog.js', 'function blog() {};'); $template = '{{js_combined dir="media"}}{{js_once src="js/main.js" safe="true" }}{{js_once src="js/blog.js" }}{{/js_combined}}'; $page = $this->_createMacroTemplate($template, 'tpl.html'); $page->render(); $file = array_shift(lmbFs::ls($root . '/media/')); $js_content = "/* include main.js - NOT FOUND */\n\n/* include blog.js */\nfunction blog() {};"; $this->assertEqual(file_get_contents($root . '/media/' . $file), $js_content); }
function setUp() { parent::setUp(); lmbFs::mkdir(LIMB_VAR_DIR . '/tpl/tags/'); lmbFs::mkdir(LIMB_VAR_DIR . '/tpl/tags/subfolder/'); }
function tearDown() { lmbFs::rm($this->tmp_dir); }