public function delete($key) { if (DEBUG) { \Lysine\logger('cache')->debug('Eaccelerator delete key ' . (is_array($key) ? implode(',', $key) : $key)); } $key = $this->makeKey($key); return eaccelerator_rm($key); }
function render_view($file, $vars = array()) { static $view; if (!$view) { $view = new MVC\View(); } if (DEBUG) { \Lysine\logger('mvc')->info('Render view file [' . $file . ']'); } return $view->render($file, $vars); }
function __exception_handler($exception, $call = false) { try { \Lysine\logger()->exception($exception); if (PHP_SAPI == 'cli') { echo $exception . PHP_EOL; exit(1); } $response = __exception_response(HTTP::INTERNAL_SERVER_ERROR, $exception); if ($call) { return $response; } $response->execute(); } catch (\Exception $e) { error_log($exception . PHP_EOL . $e); if (ini_get('display_errors')) { echo $exception . PHP_EOL . $e . PHP_EOL; } } exit(1); }
public function execute($uri, $method) { list($class, $params, $path) = $this->dispatch($uri); \Lysine\logger()->debug('Dispatch to controller: ' . $class); $this->fireEvent(self::BEFORE_DISPATCH_EVENT, array($class, $path)); $controller = new $class(); if (method_exists($controller, '__before_run')) { $response = $params ? call_user_func_array(array($controller, '__before_run'), $params) : $controller->__before_run(); if ($response) { return $response; } } $method = $method ?: req()->getMethod(); if ($method == 'HEAD') { $method = 'GET'; } if (!is_callable(array($controller, $method))) { throw HTTP\Exception::factory(HTTP::METHOD_NOT_ALLOWED); } $response = $params ? call_user_func_array(array($controller, $method), $params) : $controller->{$method}(); if (method_exists($controller, '__after_run')) { $result = $controller->__after_run($response); if ($result !== null) { $response = $result; } } $this->fireEvent(self::AFTER_DISPATCH_EVENT, array($class, $response)); return $response; }
/** * 执行sql并返回结果或结果对象 * * @param string $sql * @param mixed $bind * @access public * @return Lysine\Storage\DB\IResult */ public function execute($sql, $bind = null) { if ($bind === null) { $bind = array(); } if (!is_array($bind)) { $bind = array_slice(func_get_args(), 1); } try { $sth = $sql instanceof \PDOStatement ? $sql : $this->connect()->prepare($sql); if ($sth === false) { return false; } if (!$sth->execute($bind)) { return false; } } catch (\PDOException $ex) { $error = new Storage\Error($ex->getMessage(), $ex->errorInfo[1], $ex, array('sql' => (string) $sql, 'bind' => $bind, 'native_code' => $ex->errorInfo[0])); fire_event($this, EXECUTE_EXCEPTION_EVENT, $error); throw $error; } fire_event($this, EXECUTE_EVENT, array($sql, $bind)); if (DEBUG) { $log = 'Execute SQL: ' . $sql; if ($bind) { $log .= ' with ' . json_encode($bind); } \Lysine\logger('storage')->debug($log); } $sth->setFetchMode(\PDO::FETCH_ASSOC); return $sth; }
/** * 执行sql语句 * * @param string $sql * @param mixed... [$params] * @return \Lysine\Service\DB\Statement * * @example * $db->execute('select * from foobar'); * $db->execute('select * from foobar where foo = ? and bar = ?', $foo, $bar); * $db->execute('select * from foobar where foo = ? and bar = ?', array($foo, $bar)); */ public function execute($sql, $params = null) { $params = $params === null ? array() : is_array($params) ? $params : array_slice(func_get_args(), 1); try { $sth = $sql instanceof \PDOStatement ? $sql : $this->connect()->prepare($sql); $sth->execute($params); } catch (\PDOException $ex) { throw new \Lysine\Exception($ex->getMessage(), $ex->errorInfo[1], $ex, array('sql' => (string) $sql, 'params' => $params, 'native_code' => $ex->errorInfo[0])); } $log = 'SQL: ' . $sql; if ($params) { $log .= ' [' . implode(',', $params) . ']'; } \Lysine\logger()->debug($log); $sth->setFetchMode(\PDO::FETCH_ASSOC); return $sth; }
<?php use Lysine\Config; use Lysine\MVC; define('ROOT_DIR', realpath(__DIR__ . '/../')); define('DEBUG', true); require __DIR__ . '/../../../src/loader.php'; require ROOT_DIR . '/lib/mvc.php'; // 加载配置文件 $config = (require __DIR__ . '/_config.php'); Config::import($config); // 初始化外部存储服务管理 Lysine\Service\Manager::getInstance()->importConfig(Config::get('services')); // 系统日志 Lysine\logger()->setLevel(DEBUG ? Lysine\Logging::DEBUG : Lysine\Logging::ERROR)->addHandler(new Lysine\Logging\FileHandler(Config::get('logging'))); // 使用内置SESSION封装 Lysine\Session::initialize(); // MVC环境初始化 $app = new MVC\Application(Config::get('application')); $app->setRouter(new MVC\Router(Config::get('router'))); // 在每次router dispatch之前调用rbac检查 $app->getRouter()->onEvent(MVC\Router::BEFORE_DISPATCH_EVENT, function ($class, $path) { $rules = Config::get('router', 'rbac'); $rbac = new \Model\Rbac($rules); $rbac->check($class, $path); }); return $app;
public function delete($key) { if (DEBUG) { \Lysine\logger('cache')->debug('Memcached delete key ' . (is_array($key) ? implode(',', $key) : $key)); } $key = $this->makeKey($key); return $this->memcached->delete($key); }
/** * 分发请求到对应的controller * 执行并返回结果 * * @param string $url * @param array $params * @access public * @return mixed */ public function dispatch($url, array $params = array()) { if (DEBUG) { $logger = \Lysine\logger('mvc'); } $url = strtolower(rtrim($url, '/')); if (DEBUG) { $logger->info('Request url:' . req()->requestUri()); } list($class, $args) = $this->match($url); if (DEBUG) { $logger->debug('Dispatch url to controller: ' . $class); } if (!$class || !class_exists($class)) { throw HTTP\Error::page_not_found(array('controller' => $class)); } if ($params) { $args = array_merge($args, $params); } fire_event($this, BEFORE_DISPATCH_EVENT, array($url, $class, $args)); $controller = new $class(); if (method_exists($controller, '__before_run')) { // 如果__before_run返回了内容,就直接完成动作 // 可以在这里进行某些阻断操作 // 正常的内容不应该通过这里输出 $resp = $args ? call_user_func_array(array($controller, '__before_run'), $args) : $controller->__before_run(); if ($resp) { return $resp instanceof Response ? $resp : resp()->setBody($resp); } } $request = req(); $method = $request->method(); // head方法除了不输出数据之外,和get方法没有区别 if ($method == 'HEAD') { $method = 'GET'; } if (DEBUG) { $log = 'Call controller [' . $class . '] method [' . $method . ']'; if ($args) { $log .= ' with ' . json_encode($args); } $logger->info($log); } // 执行controller动作并返回结果 // 不检查method是否存在,用is_callable() // 保留__call()重载方法的方式 if (!is_callable(array($controller, $method))) { throw HTTP\Error::method_not_allowed(array('url' => $url, 'class' => $class)); } $resp = $args ? call_user_func_array(array($controller, $method), $args) : $controller->{$method}(); // 这里有机会对输出结果进行进一步处理 if (method_exists($controller, '__after_run')) { $controller->__after_run($resp); } fire_event($this, AFTER_DISPATCH_EVENT, array($url, $class, $args, $resp)); return $resp instanceof Response ? $resp : resp()->setBody($resp); }