| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- <?php
- use Nyholm\Psr7\Factory\Psr17Factory;
- use Spiral\Goridge\RPC\RPC;
- use Spiral\RoadRunner\Http\PSR7Worker;
- use Spiral\RoadRunner\Jobs\Consumer;
- use Spiral\RoadRunner\Jobs\Jobs;
- use Spiral\RoadRunner\Jobs\Task\ReceivedTaskInterface;
- use Spiral\RoadRunner\Worker;
- include "vendor/autoload.php";
- include 'response.class.php';
- /**
- * The zand router class file of ZenTaoPHP framework.
- *
- * The author disclaims copyright to this source code. In place of
- * a legal notice, here is a blessing:
- *
- * May you do good and not evil.
- * May you find forgiveness for yourself and forgive others.
- * May you share freely, never taking more than you give.
- */
- /**
- * router类。
- * The router class.
- *
- * @package framework
- */
- include dirname(__DIR__) . '/router.class.php';
- class zandRouter extends router
- {
- /**
- * 全局变量的快照
- * Snaps for global variables.
- *
- * @static
- * @var array
- * @access public
- */
- static $snaps = array();
- /**
- * 构造方法, 设置路径,类,超级变量等。注意:
- * 1.应该使用createApp()方法实例化router类;
- * 2.如果$appRoot为空,框架会根据$appName计算应用路径。
- *
- * The construct function.
- * Prepare all the paths, classes, super objects and so on.
- * Notice:
- * 1. You should use the createApp() method to get an instance of the router.
- * 2. If the $appRoot is empty, the framework will compute the appRoot according the $appName
- *
- * @param string $appName the name of the app
- * @param string $appRoot the root path of the app
- * @access public
- * @return void
- */
- public function __construct($appName = 'demo', $appRoot = '')
- {
- $_SERVER['HTTP_USER_AGENT'] = '';
- $_SERVER['SCRIPT_NAME'] = '/index.php';
- $_SERVER['SCRIPT_FILENAME'] = dirname(__DIR__, 2) . '/www/index.php';
- $this->worker = new zandWorker();
- $this->consumer = new Consumer();
- parent::__construct($appName, $appRoot);
- /* Snap. */
- global $filter;
- self::$snaps['config'] = clone $this->config;
- self::$snaps['lang'] = clone $this->lang;
- self::$snaps['filter'] = clone $filter;
- }
- /**
- * 初始化全局变量和客户端信息。
- * Init global variables and client.
- *
- * @access public
- * @return void
- */
- public function initRequest()
- {
- global $config, $lang, $filter, $loadedTargets;
- $config = clone self::$snaps['config'];
- $lang = clone self::$snaps['lang'];
- $filter = clone self::$snaps['filter'];
- $loadedTargets = array();
- $this->config = $config;
- $this->lang = $lang;
- $this->moduleName = NULL;
- $this->methodName = NULL;
- $this->rawModule = NULL;
- $this->rawMethod = NULL;
- $this->params = NULL;
- $this->user = NULL;
- self::$loadedConfigs = array();
- self::$loadedLangs = array();
- $this->setClient();
- }
- /**
- * 关闭请求会话
- * Close request.
- *
- * @access public
- * @return void
- */
- public function closeRequest()
- {
- $obLevel = ob_get_level();
- for($i = 0; $i < $obLevel; $i++) ob_end_clean();
- session_write_close();
- }
- /**
- * 开启session。
- * Start session.
- *
- * @access public
- * @return void
- */
- public function startSession()
- {
- $sessionName = $this->config->sessionVar;
- if(!defined('SESSION_STARTED'))
- {
- global $config;
- $driver = $config->db->driver;
- if(!class_exists($driver))
- {
- $classFile = $this->coreLibRoot . 'dao' . DS . $driver . '.class.php';
- include($classFile);
- }
- $dao = new $driver();
- $ztSessionHandler = new zandSession($dao);
- session_set_save_handler(
- \Closure::fromCallable([$ztSessionHandler, 'open']),
- \Closure::fromCallable([$ztSessionHandler, 'close']),
- \Closure::fromCallable([$ztSessionHandler, 'read']),
- \Closure::fromCallable([$ztSessionHandler, 'write']),
- \Closure::fromCallable([$ztSessionHandler, 'destroy']),
- \Closure::fromCallable([$ztSessionHandler, 'gc'])
- );
- session_name($sessionName);
- session_set_cookie_params(0, $this->config->webRoot, '', $this->config->cookieSecure, true);
- define('SESSION_STARTED', true);
- }
- else
- {
- $this->sessionID = isset($_COOKIE[$sessionName]) ? $_COOKIE[$sessionName] : session_create_id();
- session_id($this->sessionID);
- session_start();
- $this->worker->response->setCookie($sessionName, $this->sessionID, 0);
- }
- }
- }
- /**
- * 消息队列的消息类型。
- * Message in queue.
- *
- * @package zand
- */
- class zandMessage
- {
- public $id;
- public $type;
- public $command;
- }
- /**
- * 消息队列。
- * Message queue.
- *
- * @package zand
- */
- class zandQueue
- {
- private $mq;
- public function __construct($queueName)
- {
- $jobs = new Jobs(RPC::create('tcp://127.0.0.1:6001'));
- $this->mq = $jobs->connect('crons');
- }
- public function push($message)
- {
- $task = $this->mq->create(zandMessage::class, $message);
- $this->mq->dispatch($task);
- }
- }
- /**
- * HTTP worker.
- *
- * @package zand
- */
- class zandWorker
- {
- /**
- * RoadRunner PSR7 worker.
- *
- * @var object
- * @access private
- */
- private $psr7;
- /**
- * response.
- *
- * @var object
- * @access public
- */
- public $response;
- /**
- * Constructor.
- *
- * @access public
- * @return void
- */
- public function __construct()
- {
- $worker = Worker::create();
- $factory = new Psr17Factory();
- $this->psr7 = new PSR7Worker($worker, $factory, $factory, $factory);
- $this->response = new zandResponse();
- }
- /**
- * Wait request to run.
- *
- * @access public
- * @return void
- */
- public function waitRequest()
- {
- $request = $this->psr7->waitRequest();
- $this->initGlobal($request);
- $this->response = new zandResponse();
- }
- /**
- * Init global variables.
- *
- * @param object $request
- * @access public
- * @return void
- */
- public function initGlobal($request)
- {
- $_SERVER = $request->getServerParams();
- $_SERVER['REQUEST_TIME'] = time();
- $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
- $_SERVER['SERVER_PROTOCOL'] = $request->getUri()->getScheme();
- $_SERVER['REQUEST_METHOD'] = $request->getMethod();
- $_SERVER['SERVER_NAME'] = $request->getUri()->getHost();
- $_SERVER['SERVER_PORT'] = $request->getUri()->getPort();
- $_SERVER['REQUEST_URI'] = $request->getUri()->getPath();
- $_SERVER['SCRIPT_NAME'] = '/index.php';
- $_SERVER['PHP_SELF'] = 'index.php';
- $_SERVER['PATH_TRANSLATED'] = 'index.php';
- $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] . (in_array($_SERVER['SERVER_PORT'], array(80, 443)) ? '' : ':' . $_SERVER['SERVER_PORT']);
- $query = $request->getUri()->getQuery();
- if(!empty($query))
- {
- $_SERVER['REQUEST_URI'] .= '?' . $query;
- $_SERVER['QUERY_STRING'] = $query;
- }
- $_GET = $request->getQueryParams();
- $_POST = $request->getParsedBody();
- $_COOKIE = $request->getCookieParams();
- $_FILE = $request->getUploadedFiles();
- }
- /**
- * Send response.
- *
- * @param string $body
- * @access public
- * @return void
- */
- public function respond($body)
- {
- $this->response->setBody($body);
- $this->psr7->respond($this->response);
- }
- /**
- * Send error.
- *
- * @param Exception $e
- * @access public
- * @return void
- */
- public function error($e)
- {
- $this->psr7->getWorker()->error((string)$e);
- }
- }
- /**
- * MySQL实现的Session管理.
- * Session handler implements by MySQL.
- *
- * @package zand
- */
- class zandSession
- {
- /**
- * DAO for database.
- *
- * @var object
- * @access private
- */
- private $dao;
- /**
- * Constructor.
- *
- * @param object $dao
- * @access public
- * @return void
- */
- public function __construct($dao)
- {
- $this->dao = $dao;
- }
- /**
- * Open session.
- *
- * @param string $savePath
- * @param string $sessionName
- * @access public
- * @return bool
- */
- public function open($savePath, $sessionName)
- {
- $this->savePath = $savePath;
- $this->sessionName = $sessionName;
- return true;
- }
- /**
- * Close session.
- *
- * @access public
- * @return bool
- */
- public function close()
- {
- return true;
- }
- /**
- * Read session.
- *
- * @param string $id
- * @access public
- * @return string
- */
- public function read($id)
- {
- $result = $this->dao->select('data')->from(TABLE_SESSION)->where('id')->eq($id)->fetch();
- return $result ? $result->data : '';
- }
- /**
- * Write session.
- *
- * @param string $id
- * @param string $data
- * @access public
- * @return bool
- */
- public function write($id, $data)
- {
- $data = array('id' => $id, 'data' => $data, 'timestamp' => time());
- $this->dao->replace(TABLE_SESSION)->data($data)->exec();
- return true;
- }
- /**
- * Destroy session.
- *
- * @param string $id
- * @access public
- * @return bool
- */
- public function destroy($id)
- {
- $this->dao->delete()->from(TABLE_SESSION)->where('id')->eq($id)->exec();
- return true;
- }
- /**
- * GC for session.
- *
- * @param int $maxlifetime
- * @access public
- * @return bool
- */
- public function gc($maxlifetime)
- {
- $this->dao->delete()->from(TABLE_SESSION)->where('timestamp')->lt(time() - intval($maxlifetime))->exec();
- return true;
- }
- }
|