/** * 构造函数 * * @access public * @param mixed $tpl */ public function __construct($tpl) { $this->tplfile = Tiny::app()->getRuntimePath() . $tpl . '.php'; if (!file_exists($this->tplfile) || filemtime($this->tplfile) < filemtime($tpl)) { $file = new File($this->tplfile, 'w+'); $template = $file->getContents($tpl); $t = new Tag(); $tem = $t->resolve($template); $file->write($tem); } }
public static function renderPartial($templateFile, array $vars = null) { if (!$vars) { $vars = array(); } extract($vars); ob_start(); eval('?>' . File::getContents(self::$baseDir . "{$templateFile}.php", $vars)); $content = ob_get_contents(); ob_end_clean(); return $content; }
public static function renderPartial($templateFile, array $vars = null) { if ($vars) { foreach ($vars as $key => $value) { global ${$key}; ${$key} = $value; } } ob_start(); eval('?>' . File::getContents(self::$baseDir . "{$templateFile}.php", $vars)); $content = ob_get_contents(); ob_end_clean(); return $content; }
/** * @covers mychaelstyle\storage\File::__construct * @covers mychaelstyle\storage\File::open * @covers mychaelstyle\storage\File::write * @covers mychaelstyle\storage\File::isOpened * @covers mychaelstyle\storage\File::checkOpen * @covers mychaelstyle\storage\File::close * @covers mychaelstyle\storage\File::commit * @covers mychaelstyle\storage\File::clean * @covers mychaelstyle\storage\File::initialize * @covers mychaelstyle\storage\Storage::commit * @covers mychaelstyle\storage\File::remove * @covers mychaelstyle\storage\File::getContents */ public function testWriteWithTransaction() { // remove first $this->object->remove(); // create new storage without transaction $this->storage = new \mychaelstyle\Storage($this->dsn, $this->options, false); $this->object = $this->storage->createFile($this->uri, $this->options, false); // none transaxtion $expected = $this->test_string; $this->object->open('w'); $this->object->write($expected); $this->object->close(); $localPath = $this->getLocalPathUsingUri($this->dsn, $this->uri); $this->assertFalse(file_exists($localPath)); $this->object->commit(); $this->assertTrue(file_exists($localPath)); $this->assertLocalWritten($this->dsn, $expected, $this->uri); // get contents $result = $this->object->getContents(); $this->assertEquals($expected, $result); // remove finaly $this->object->remove(); }
/** * Sends the file associated with the given model to the user. * @param File $file the file model. * @param boolean $terminate whether to terminate the current application after calling this method. * @see CHttpRequest::sendFile */ public function sendFile($file, $terminate = true) { Yii::app()->request->sendFile($file->resolveFilename(), $file->getContents(), $file->mimeType, $terminate); }
/** * @param string $filename * @param bool $result * * @return $this|Document */ public function loadHTMLFile($filename, &$result = false) { $file = new File($filename); return $this->loadHTML($file->getContents(), $options); }
/** * 处理设定的每一个标签 * * @access public * @param mixed $matches * @return mixed */ public function translate($matches) { $suffix = '_' . self::$num++; if ($matches[1] !== '/') { switch ($matches[2] . $matches[3]) { case '$': $str = trim($matches[4]); $data = explode('|', $str); if ($str[0] == '.' || $str[0] == '(') { return $matches[0]; } $len = count($data); if ($len == 1) { return '<?php echo isset($' . $matches[4] . ')?$' . $matches[4] . ':"";?>'; } else { if ($len == 2) { return '<?php echo isset($' . $data[0] . ')?$' . $data[0] . ':' . $data[1] . ';?>'; } else { if ($len > 2) { $filter = strtolower($data[$len - 1]); switch ($filter) { case 'encode': return '<?php echo isset($' . $data[0] . ')?htmlspecialchars($' . $data[0] . '):' . $data[1] . ';?>'; case 'int': case 'str': case 'float': case 'txt': case 'sql': case 'text': return '<?php echo isset($' . $data[0] . ')?Filter::' . $filter . '($' . $data[0] . '):' . $data[1] . ';?>'; default: return '<?php echo isset($' . $data[0] . ') && $' . $data[0] . '?' . $data[1] . ':' . $data[2] . ';?>'; } } } } case 'echo:': return '<?php echo ' . rtrim($matches[4], ';/') . ';?>'; case 'url:': return '<?php echo urldecode(Url::urlFormat("' . trim($matches[4]) . '"));?>'; case 'if:': return '<?php if(' . $matches[4] . '){?>'; case 'elseif:': return '<?php }elseif(' . $matches[4] . '){?>'; case 'else:': return '<?php }else{' . $matches[4] . '?>'; case 'set:': return '<?php ' . rtrim($matches[4], ';/') . ';?>'; case 'while:': return '<?php while(' . $matches[4] . '){?>'; case 'dump:': return '<pre><?php var_dump(' . $matches[4] . '); ?></pre>'; case 'list:': case 'foreach:': $attr = $this->getAttrs($matches[4]); if (!isset($attr['items'])) { $attr['items'] = '$items'; } else { $attr['items'] = $attr['items']; } if (!isset($attr['key'])) { $attr['key'] = '$key'; } else { $attr['key'] = $attr['key']; } if (!isset($attr['item'])) { $attr['item'] = '$item'; } else { $attr['item'] = $attr['item']; } return '<?php foreach(' . $attr['items'] . ' as ' . $attr['key'] . ' => ' . $attr['item'] . '){?>'; case 'for:': $attr = $this->getAttrs($matches[4]); if (!isset($attr['item'])) { $attr['item'] = '$i'; } else { $attr['item'] = $attr['item']; } if (!isset($attr['from'])) { $attr['from'] = 0; } if (!isset($attr['to'])) { $attr['to'] = 10; } if (!isset($attr['step'])) { $attr['step'] = 1; } return '<?php for($total' . $suffix . ' = (int) ceil((' . $attr['step'] . ' > 0 ? ' . $attr['to'] . '+1 - (' . $attr['from'] . ') : ' . $attr['from'] . '-(' . $attr['to'] . ')+1)/abs(' . $attr['step'] . ')),' . $attr['item'] . ' = ' . $attr['from'] . ',$start' . $suffix . '=1 ; $total' . $suffix . '>0 && $start' . $suffix . '<=$total' . $suffix . ' ; ' . $attr['item'] . ' += ' . $attr['step'] . ',$start' . $suffix . ' += 1){?>'; case 'widget:': $attr = $this->getAttrs($matches[4]); $className = isset($attr['name']) ? $attr['name'] : null; $method = isset($attr['method']) ? $attr['method'] : 'init'; $args = isset($attr['args']) ? $attr['args'] : null; $attr['cache'] = isset($attr['cache']) ? "true" : "false"; //$cacheTime = isset($attr['cachetime'])?intval($attr['cachetime']):30; $old_char = array(' ne ', ' eq ', ' lt ', ' gt ', ' le ', ' ge '); $new_char = array(' != ', ' = ', ' < ', ' > ', ' <= ', ' >= '); $tem = "<div id='widget_{$className}'><?php \$widget = Widget::createWidget('{$className}');"; foreach ($attr as $k => $v) { if ($k != 'name') { $v = str_replace($old_char, $new_char, $v); if (substr($v, 0, 1) == '$') { $tem .= '$widget->' . $k . ' = ' . $v . ';'; } else { $tem .= '$widget->' . $k . ' = "' . $v . '";'; } } } $tem .= "\$widget->run();?></div>"; return $tem; case 'query:': $endchart = substr(trim($matches[4]), -1); $attrs = $this->getAttrs(rtrim($matches[4], '/')); if (!isset($attrs['id'])) { $id = '$query'; } else { $id = $attrs['id']; } if (!isset($attrs['items'])) { $items = '$items'; } else { $items = $attrs['items']; } $tem = $id . ' = new Query("' . $attrs['name'] . '");'; //实现属性中符号表达式的问题 $old_char = array(' ne ', ' eq ', ' lt ', ' gt ', ' le ', ' ge '); $new_char = array(' != ', ' = ', ' < ', ' > ', ' <= ', ' >= '); foreach ($attrs as $k => $v) { if ($k != 'name' && $k != 'id' && $k != 'items') { $tem .= $id . '->' . $k . ' = "' . str_replace($old_char, $new_char, $v) . '";'; } } $tem .= $items . ' = ' . $id . '->find();'; if (!isset($attrs['key'])) { $attrs['key'] = '$key'; } else { $attrs['key'] = $attrs['key']; } if (!isset($attrs['item'])) { $attrs['item'] = '$item'; } else { $attrs['item'] = $attrs['item']; } if ($endchart == '/') { return '<?php ' . $tem . '?>'; } else { return '<?php ' . $attrs['item'] . '=null; ' . $tem . ' foreach(' . $items . ' as ' . $attrs['key'] . ' => ' . $attrs['item'] . '){?>'; } case 'token:': $attr = $this->getAttrs(rtrim($matches[4], '/')); if (isset($attr['key']) && is_string($attr['key'])) { $key = $attr['key']; } else { $key = ''; } return "<input type='hidden' name='tiny_token_" . $key . "' value='<?php echo Tiny::app()->getToken(\"" . $key . "\");?>'/>"; case 'debug:': $matches[4] = rtrim($matches[4], ';/'); if ($matches[4] != '') { return '<pre>' . $matches[4] . ' = <?php var_dump(' . $matches[4] . ');?></pre>'; } else { return '<?php $debug = new Debug(); $out = get_defined_vars(); $debug->out($out); $debug->display();?>'; } case 'code:': return '<?php ' . $matches[4]; case 'require:': case 'include:': $fileName = trim($matches[4]); $viewfile = Tiny::app()->getViewPath() . DIRECTORY_SEPARATOR . $this->viewPath . DIRECTORY_SEPARATOR . $fileName; $runfile = Tiny::app()->getRuntimePath() . DIRECTORY_SEPARATOR . $this->viewPath . DIRECTORY_SEPARATOR . $fileName; if (!file_exists($runfile) || filemtime($runfile) < filemtime($viewfile)) { $file = new File($runfile, 'w+'); $template = $file->getContents($viewfile); $t = new Tag(); $tem = $t->resolve($template, dirname($viewfile)); $file->write($tem); } return '<?php include("' . trim($matches[4]) . '")?>'; default: return $matches[0]; } } else { if ($matches[2] == 'code') { return '?>'; } else { if ($matches[2] != 'widget') { return '<?php }?>'; } } } }
/** * 渲染文件视图 * * @access public * @param mixed $view * @param mixed $data * @param bool $return * @return mixed */ public function render($view, $data = null, $return = false) { $viewfile = $this->getViewFile($view) . $this->templateExt; $runfile = Tiny::app()->getRuntimePath() . DIRECTORY_SEPARATOR . $this->getId() . DIRECTORY_SEPARATOR . $view . $this->scriptExt; //if(file_exists($viewfile)) if (is_file($viewfile)) { $layoutfile = $this->getLayoutFile() . $this->templateExt; if (file_exists($layoutfile)) { $layoutexists = true; } else { $layoutexists = false; } if (!file_exists($runfile) || filemtime($runfile) < filemtime($viewfile) || $layoutexists && filemtime($runfile) < filemtime($layoutfile)) { $file = new File($runfile, 'w+'); $template = $file->getContents($viewfile); $t = new Tag(); $tem = $t->resolve($template, dirname($view)); if ($layoutexists) { $theme_str = $t->resolve(file_get_contents($layoutfile)); $tem = str_replace("{__viewcontents}", $tem, $theme_str); } $file->write($tem); unset($file); } echo $this->renderExecute($runfile, $data); } else { Tiny::Msg($this, '请求的页面不存在!', 404); } }
public function testPutGetContents() { $this->assertEquals(2, File::putContents($this->path, 'Oo')); $this->assertEquals('Oo', File::getContents($this->path)); }
/** * Read the contents of a csv file, and throw a RuntimeException if it cannot be done. * * @param string $filename The path to the file * * @return array The data read from the file */ public static function getContents($filename) { $contents = File::getContents($filename); # Remove any trailing blank lines $contents = rtrim($contents); # Break up the file by newlines $data = explode("\n", $contents); # Trim each line $data = array_map("trim", $data); # Convert each line to an array $data = array_map("str_getcsv", $data); return $data; }
/** * 渲染 * * @access public * @param mixed $viewFile * @param bool $return * @return mixed */ public function renderInternal($viewFile, $return = false) { $data = $this->properties; if (is_array($data)) { extract($data, EXTR_PREFIX_SAME, 'data'); } $tplfile = $this->getViewFile($viewFile); if (file_exists($tplfile)) { $runfile = Tiny::app()->getRuntimePath() . DIRECTORY_SEPARATOR . 'widgets/' . $viewFile . '.php'; if (!file_exists($runfile) || filemtime($runfile) < filemtime($tplfile)) { $file = new File($runfile, 'w+'); $template = $file->getContents($tplfile); $t = new Tag(); $tem = $t->resolve($template); $file->write($tem); } header("Content-type: text/html; charset=" . $this->encoding); if ($return) { ob_start(); ob_implicit_flush(false); require $runfile; return ob_get_clean(); } else { require $runfile; } } else { if ($this->id != Tiny::app()->getId()) { trigger_error("{$this->id}Widget->{$this->getAction()}() not exists", E_USER_ERROR); } else { Tiny::msg('', '无法找到请求的页面', 404); } } }
/** * Get the contents of this file * * @return string File contents */ public function getContents() { return $this->originalFile->getContents(); }