/** * 写入文件 * @param string $path 路径 * @param mixed $data 写入的值 * @param boolean $overwrite 是否覆盖已有文件 * @return boolean */ public static function write($path, $data, $overwrite = true) { $path = str_replace('\\', '/', $path); $pos = strrpos($path, '/'); $file_name = ""; $file_dir = ""; if ($pos === false) { $file_name = $path; } else { $file_dir = substr($path, 0, $pos + 1); $file_name = substr($path, $pos + 1); } $tmp_file = tempnam($file_dir, 'tsb_sfw'); Verify::isTrue(false !== file_put_contents($tmp_file, $data), "write to file: {$tmp_file} failed"); if ($overwrite) { @unlink($path); //删除原始文件 } if (!@rename($tmp_file, $path)) { @unlink($tmp_file); //删除原始文件 Verify::e("write to file: {$tmp_file} failed"); return false; } return true; }
/** * @param string|array $file_name 文件的绝对路径 */ function __construct($file_name) { $file_names = array(); if (is_string($file_name)) { $file_names[] = $file_name; } else { Verify::isTrue(is_array($file_name)); $file_names = $file_name; } foreach ($file_names as $file_name) { if (is_file($file_name)) { $this->file_name[$file_name] = @filemtime($file_name); } else { $this->file_name[$file_name] = @filemtime($file_name); if (!is_dir($file_name)) { continue; } $files = @dir($file_name); Verify::isTrue($files !== null, "open dir {$file_name} failed"); while (!!($file = $files->read())) { if ($file == '.' || $file == '..') { continue; } $this->file_name[$file_name . '/' . $file] = @filemtime($file_name . '/' . $file); } $files->close(); } } }
/** * 复制文件或目录 * @param string $src * @param string $dst * @param string $except * @return boolean */ public static function copys($src, $dst, $except = null) { if (is_file($src)) { if ($except !== null && (realpath($except) == realpath($src) || realpath($except) == realpath($dst))) { return true; } return @copy($src, $dst); } Verify::isTrue(is_dir($src), "{$src} not a file nor a dir"); $dir = dir($src); @mkdir($dst); while (false !== ($file = $dir->read())) { if ($file != '.' && $file != '..') { $src_path = $src . '/' . $file; $dst_path = $dst . '/' . $file; if (!self::copys($src_path, $dst_path)) { $dir->close(); return false; } Logger::debug("file copy from {$src_path} to {$dst_path}"); } } $dir->close(); return true; }
/** * 方法,绑定参数 * 如 * func,arg1,arg2 * array('a','method1'), arg1,arg2 */ public function __construct() { $args = func_get_args(); Verify::isTrue(count($args) > 0); Verify::isTrue(is_callable($args[0])); $this->func = $args[0]; $this->bind = array_slice($args, 1); }
/** * 初始化 * @param array $args 参数列表 */ public function initArgs($args) { $cnst = $this->refl->getConstructor(); if ($cnst !== null) { $cnst->invokeArgs($this->obj, $args); } else { Verify::isTrue(count($args) === 0, $this->refl->getName() . ' no constructor found with ' . func_num_args() . ' params'); } }
public function getCacheOptions($method) { if (!array_key_exists($method, $this->cachedMethods)) { $meta = $this->factory->getMetaInfo($this->factory->getClassName($this->id)); if (isset($meta['cache'][$method]['value'])) { $val = $meta['cache'][$method]['value']; list($k, $v) = $val; Verify::isTrue($k == 'ttl', "no TTL with @cache in {$method}"); $this->cachedMethods[$method][$k] = $v; } } return $this->cachedMethods[$method]; }
/** * 替换字典 * @see setDict * @param string|array $value * @return void */ private function replaceByDict($value, $dict) { if (is_string($value)) { $keys = $this->getDictKeys($value); foreach ($keys as $key) { Verify::isTrue(isset($dict[$key]), "{$key} not specified"); } foreach ($dict as $key => $replace) { $value = str_replace('{' . $key . '}', $replace, $value); } return $value; } else { if (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = $this->replaceByDict($v, $dict); } return $value; } else { return $value; } } }
/** * 运行消息循环 * @return void */ public function run() { if ($this->is_running) { return; } Logger::debug("[MQ Pump] begin"); $this->is_running = true; while (count($this->next_action) || count($this->next_idle)) { //没有活动事件,执行idle事件 if (count($this->next_action) === 0) { $queue_id = array_pop($this->next_idle); if (!isset($this->idle_queues[$queue_id])) { // 队列可能被关闭 continue; } $idle = array_pop($this->idle_queues[$queue_id]); Verify::isTrue($idle !== null, 'never been here!!'); if ($idle[2] !== null) { try { call_user_func_array($idle[0], $idle[1]); } catch (\Exception $e) { $idle[2]($e); } } else { call_user_func_array($idle[0], $idle[1]); } continue; } $queue_id = array_pop($this->next_action); if (!isset($this->action_queues[$queue_id])) { // 队列可能被关闭 continue; } $actions =& $this->action_queues[$queue_id]; $left = count($actions); $action = array_pop($actions); if ($action !== null) { $this->callAction($queue_id, $action); } elseif ($left !== 0) { //null插入队列,表示执行队列结束, 可以安全关闭队列了 $onend = $this->end_handles[$queue_id]; unset($this->action_queues[$queue_id]); unset($this->idle_queues[$queue_id]); unset($this->end_handles[$queue_id]); Logger::debug("[MQ {$queue_id}] closed"); if ($onend !== null) { $onend(); } } } //不是在单个队列为空时将其关闭,因为对于存在子流程的时候,其消息队列可能为空,但 //其他流程执行可能导致子流程产生活动消息,所以不能在队列为空时就关闭队列 foreach ($this->end_handles as $onend) { if ($onend !== null) { $onend(); } } $this->next_action = array(); $this->next_idle = array(); $this->action_queues = array(); $this->idle_queues = array(); $this->end_handles = array(); $this->is_running = false; Logger::debug("[MQ Pump] end"); }
/** * 设置key * @param string $key * @param mixed $var * @param int $ttl TODO: 暂不支持 * @return void */ public function set($key, $var, $ttl = 0) { Verify::isTrue(is_dir($this->cache_dir) || @mkdir($this->cache_dir, 0777, true)); $path = $this->cache_dir . '/' . sha1($key); return SaftyFileWriter::write($path, serialize($var)); }
static function testAnnotation() { Verify::isTrue(count(self::get(new AnnotationTest(), true)), 'Annotation dose not work! If opcache is enable, please set opcache.save_comments=1 and opcache.load_comments=1'); }