/** * 应用程序初始化 * @access public * @return void */ public static function start() { // 设定错误和异常处理 register_shutdown_function('Think\\Think::fatalError'); set_error_handler('Think\\Think::appError'); set_exception_handler('Think\\Think::appException'); // 注册AUTOLOAD方法 spl_autoload_register('Think\\Think::autoload'); // 初始化文件存储方式 Storage::connect(STORAGE_TYPE); $runtimefile = RUNTIME_PATH . APP_MODE . '~runtime.php'; if (!APP_DEBUG && Storage::has($runtimefile, 'runtime')) { Storage::load($runtimefile, null, 'runtime'); } else { if (Storage::has($runtimefile, 'runtime')) { Storage::unlink($runtimefile, 'runtime'); } $content = ''; // 读取应用模式 $mode = (include is_file(COMMON_PATH . 'Conf/core.php') ? COMMON_PATH . 'Conf/core.php' : THINK_PATH . 'Conf/Mode/' . APP_MODE . '.php'); // 加载核心文件 foreach ($mode['core'] as $file) { if (is_file($file)) { include $file; if (!APP_DEBUG) { $content .= compile($file); } } } // 加载配置文件 foreach ($mode['config'] as $key => $file) { is_numeric($key) ? C(include $file) : C($key, include $file); } // 加载别名定义 foreach ($mode['alias'] as $alias) { self::addMap(is_array($alias) ? $alias : (file_exists($alias) ? include $alias : array())); } // 加载模式系统行为定义 if (isset($mode['extends'])) { Hook::import(is_array($mode['extends']) ? $mode['extends'] : (include $mode['extends'])); } // 加载应用行为定义 if (isset($mode['tags'])) { if (is_array($mode['tags'])) { $tags = $mode['tags']; } else { $tags = file_exists($mode['tags']) ? include $mode['tags'] : array(); } Hook::import($tags); } // 加载框架底层语言包 L(include THINK_PATH . 'Lang/' . strtolower(C('DEFAULT_LANG')) . '.php'); if (!APP_DEBUG) { $content .= "\nnamespace { Think\\Think::addMap(" . var_export(self::$_map, true) . ");"; $content .= "\nL(" . var_export(L(), true) . ");\nC(" . var_export(C(), true) . ');Think\\Hook::import(' . var_export(Hook::get(), true) . ');}'; Storage::put($runtimefile, strip_whitespace('<?php ' . $content), 'runtime'); } else { // 调试模式加载系统默认的配置文件 C(include THINK_PATH . 'Conf/debug.php'); // 读取调试模式的应用状态 $status = C('APP_STATUS'); // 加载对应的项目配置文件 if (is_file(COMMON_PATH . 'Conf/' . $status . '.php')) { // 允许项目增加开发模式配置定义 C(include COMMON_PATH . 'Conf/' . $status . '.php'); } } } // 设置系统时区 date_default_timezone_set(C('DEFAULT_TIMEZONE')); // 检查项目目录结构 如果不存在则自动创建 if (C('CHECK_APP_DIR') && !is_dir(LOG_PATH)) { // 创建项目目录结构 require THINK_PATH . 'Common/build.php'; } // 记录加载文件时间 G('loadTime'); if (!defined('IsInterface')) { // 运行应用 App::run(); } else { App::init(); } }
/** * 应用程序初始化 * @access public * @return void */ public static function start() { // 注册AUTOLOAD方法 spl_autoload_register('Think\\Think::autoload'); // 设定错误和异常处理 register_shutdown_function('Think\\Think::fatalError'); set_error_handler('Think\\Think::appError'); set_exception_handler('Think\\Think::appException'); // 初始化文件存储方式 Storage::connect(STORAGE_TYPE); $runtimefile = RUNTIME_PATH . APP_MODE . '~runtime.php'; if (!APP_DEBUG && Storage::has($runtimefile)) { Storage::load($runtimefile); } else { if (Storage::has($runtimefile)) { Storage::unlink($runtimefile); } $content = ''; // 读取应用模式 $mode = (include is_file(CONF_PATH . 'core.php') ? CONF_PATH . 'core.php' : MODE_PATH . APP_MODE . '.php'); // 加载核心文件 foreach ($mode['core'] as $file) { if (is_file($file)) { include $file; if (!APP_DEBUG) { $content .= compile($file); } } } // 加载应用模式配置文件 foreach ($mode['config'] as $key => $file) { is_numeric($key) ? C(include $file) : C($key, include $file); } // 读取当前应用模式对应的配置文件 if ('common' != APP_MODE && is_file(CONF_PATH . 'config_' . APP_MODE . '.php')) { C(include CONF_PATH . 'config_' . APP_MODE . '.php'); } // 加载模式别名定义 if (isset($mode['alias'])) { self::addMap(is_array($mode['alias']) ? $mode['alias'] : (include $mode['alias'])); } // 加载应用别名定义文件 if (is_file(CONF_PATH . 'alias.php')) { self::addMap(include CONF_PATH . 'alias.php'); } // 加载模式行为定义 if (isset($mode['tags'])) { Hook::import(is_array($mode['tags']) ? $mode['tags'] : (include $mode['tags'])); } // 加载应用行为定义 if (is_file(CONF_PATH . 'tags.php')) { // 允许应用增加开发模式配置定义 Hook::import(include CONF_PATH . 'tags.php'); } // 加载框架底层语言包 L(include THINK_PATH . 'Lang/' . strtolower(C('DEFAULT_LANG')) . '.php'); if (!APP_DEBUG) { $content .= "\nnamespace { Think\\Think::addMap(" . var_export(self::$_map, true) . ");"; $content .= "\nL(" . var_export(L(), true) . ");\nC(" . var_export(C(), true) . ');Think\\Hook::import(' . var_export(Hook::get(), true) . ');}'; Storage::put($runtimefile, strip_whitespace('<?php ' . $content)); } else { // 调试模式加载系统默认的配置文件 C(include THINK_PATH . 'Conf/debug.php'); // 读取应用调试配置文件 if (is_file(CONF_PATH . 'debug.php')) { C(include CONF_PATH . 'debug.php'); } } } // 读取当前应用状态对应的配置文件 if (APP_STATUS && is_file(CONF_PATH . APP_STATUS . '.php')) { C(include CONF_PATH . APP_STATUS . '.php'); } // 设置系统时区 date_default_timezone_set(C('DEFAULT_TIMEZONE')); // 检查应用目录结构 如果不存在则自动创建 if (C('CHECK_APP_DIR') && !is_dir(LOG_PATH)) { // 创建应用目录结构 require THINK_PATH . 'Common/build.php'; } // 记录加载文件时间 G('loadTime'); // 运行应用 App::run(); }
function testStorage() { // test Storage - public API // store test.txt echo "saving test.txt with a crazy name\n"; $file_id = Storage::save('test.txt', 'O*Bc3wukygfsT@#($0876)$!@#*+_][.txt'); echo "resulting file_id = {$file_id}\n"; $file = Dal::query_one_object("SELECT * FROM files WHERE file_id=?", array($file_id)); $this->assertEquals($file->link_count, 0); $this->assertEquals($file->last_linked, NULL); $file_path = Storage::getPath($file_id); $file_url = Storage::getURL($file_id); echo "getPath({$file_id}) -> {$file_path}\n"; echo "getURL({$file_id}) -> {$file_url}\n"; $this->assertTrue(strpos($file_path, PA::$path . "/web/files/") === 0); $this->assertTrue(strpos($file_url, PA::$url) === 0); // link it in somewhere $link_id = Storage::link($file_id, array('role' => 'avatar', 'user' => 1)); echo "linked it in as avatar for user 1; link_id = {$link_id}\n"; $link = Dal::query_one_object("SELECT * FROM file_links WHERE link_id=?", array($link_id)); $this->assertEquals($link->file_id, $file_id); $file = Dal::query_one_object("SELECT * FROM files WHERE file_id=?", array($file_id)); $this->assertEquals($file->link_count, 1); $this->assertNotEquals($file->last_linked, NULL); // another file $child_file_id = Storage::save('test2.txt', 'this is the child file.jpg', 'throwaway', 'image/jpeg'); echo "child file: {$child_file_id}\n"; $child_file = Dal::query_one_object("SELECT * FROM files WHERE file_id=?", array($child_file_id)); $child_file_path = Storage::getPath($child_file_id); $child_file_url = Storage::getURL($child_file_id); echo "getPath({$child_file_id}) -> {$child_file_path}\n"; echo "getURL({$child_file_id}) -> {$child_file_url}\n"; $this->assertTrue(strpos($child_file_path, PA::$path . "/web/files/") === 0); $this->assertTrue(strpos($child_file_url, PA::$url) === 0); // link child file in as a thumbnail of first file $child_link_id = Storage::link($child_file_id, array('role' => 'thumb', 'file' => $file_id, 'dim' => '123x123')); echo "child link id: {$child_link_id}\n"; $child_link = Dal::query_one_object("SELECT * FROM file_links WHERE link_id=?", array($child_link_id)); $this->assertEquals($child_link->file_id, $child_file_id); $this->assertEquals($child_link->parent_file_id, $file_id); $child_file = Dal::query_one_object("SELECT * FROM files WHERE file_id=?", array($child_file_id)); $this->assertEquals($child_file->link_count, 1); $this->assertNotEquals($child_file->last_linked, NULL); // this should fail (missing role) try { Storage::link($file_id, array("user" => 1)); $this->fail("Expected exception"); } catch (PAException $e) { $this->assertEquals($e->getCode(), BAD_PARAMETER); } // this should fail (missing network) try { Storage::link($file_id, array("role" => "header", "group" => 42)); $this->fail("Expected exception"); } catch (PAException $e) { $this->assertEquals($e->getCode(), BAD_PARAMETER); } // this should fail (network not valid) try { Storage::link($file_id, array("role" => "thumb", "network" => 1, "file" => $file_id, "dim" => "123x123")); $this->fail("Expected exception"); } catch (PAException $e) { $this->assertEquals($e->getCode(), BAD_PARAMETER); } // this should fail (parent_file_id == file_id) try { $link_id = Storage::link($file_id, array("role" => "thumb", "file" => $file_id, "dim" => "123x123")); $this->fail("Expected exception"); } catch (PAException $e) { $this->assertEquals($e->getCode(), BAD_PARAMETER); } // Now unlink the two files we just created ... // unlink the first - but don't delete it Storage::unlink($file_id, $link_id, FALSE); // make sure it's gone $this->assertEquals(Dal::query_one("SELECT * FROM file_links WHERE link_id=?", array($link_id)), NULL); // the file should still be there, with zero links, though $file = Dal::query_one("SELECT * FROM files WHERE file_id=?", array($file_id)); $this->assertNotEquals($file, NULL); $this->assertEquals($file->link_count, 0); // try a bad unlink operation try { Storage::unlink($file_id, $child_link_id); $this->fail("Expected exception"); } catch (PAException $e) { $this->assertEquals($e->getCode(), FILE_NOT_FOUND); } // unlink and delete the second Storage::unlink($child_file_id, $child_link_id); // make sure it's gone $this->assertEquals(Dal::query_one("SELECT * FROM file_links WHERE link_id=?", array($child_link_id)), NULL); // and make sure the file is gone too $this->assertEquals(Dal::query_one("SELECT * FROM files WHERE file_id=?", array($child_file)), NULL); // reap unlinked files (immediately - no grace period) Storage::cleanupFiles(-1, -1); // make sure the first file is now gone $this->assertEquals(Dal::query_one("SELECT * FROM files WHERE file_id=?", array($file_id)), NULL); }