Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
xtgxiso committed Nov 14, 2016
1 parent 95050f5 commit 6047400
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 0 deletions.
174 changes: 174 additions & 0 deletions App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
namespace WebWorker;

use Workerman\Connection\TcpConnection;
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Autoloader;
use Workerman\Connection\AsyncTcpConnection;
use Workerman\Protocols\Http;

class App extends Worker
{

/**
* 版本
*
* @var string
*/
const VERSION = '0.1.0';

private $conn = false;
private $map = array();

public $autoload = array();
public $on404 ="";

public $onAppStart = NULL;

public function __construct($socket_name, $context_option = array())
{
parent::__construct($socket_name, $context_option);
}

public function HandleFunc($url,callable $callback){
if ( $url != "/" ){
$url = strtolower(trim($url,"/"));
}
$this->map[] = array($url,$callback,1);
}

public function AddFunc($url,callable $callback){
if ( $url != "/" ){
$url = strtolower(trim($url,"/"));
}
$this->map[] = array($url,$callback,2);
}

private function show_404($connection){
if ( $this->on404 ){
call_user_func($this->on404);
}else{
Http::header("HTTP/1.1 404 Not Found");
$html = '<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>Workerman</center>
</body>
</html>';
$connection->send($html);
}
}

private function auto_close($conn){
if ( strtolower($_SERVER["SERVER_PROTOCOL"]) == "http/1.1" ){
if ( isset($_SERVER["HTTP_CONNECTION"]) ){
if ( strtolower($_SERVER["HTTP_CONNECTION"]) == "close" ){
$conn->close();
}
}
}else{
if ( $_SERVER["HTTP_CONNECTION"] == "keep-alive" ){

}else{
$conn->close();
}
}
}

public function onClientMessage($connection,$data){
if ( empty($this->map) ){
$str = <<<'EOD'
<div style="margin: 200px auto;width:600px;height:800px;text-align:left;">基于<a href="http://www.workerman.net/" target="_blank">Workerman</a>实现的自带http server的web开发框架.没有添加路由,请添加路由!
<pre>$app->HandleFunc("/",function($conn,$data) use($app){
$conn->send("默认页");
});</pre>
</div>
EOD;
$connection->send($str);
return;
}
require_once __DIR__ . '/../Applications/Statistics/Clients/StatisticClient.php';
$statistic_address = 'udp://127.0.0.1:55656';
$this->conn = $connection;
$url= $_SERVER["REQUEST_URI"];
$pos = stripos($url,"?");
if ($pos != false) {
$url = substr($url,0,$pos);
}
if ( $url != "/"){
$url = strtolower(trim($url,"/"));
}
$url_arr = explode("/",$url);
$class = empty($url_arr[0]) ? "_default" : $url_arr[0];
$method = empty($url_arr[1]) ? "_default" : $url_arr[1];
\StatisticClient::tick($class, $method);
$success = false;
foreach($this->map as $route){
if ( $route[2] == 1){//正常路由
if ( $route[0] == $url ){
$callback[] = $route[1];
}
}else if ( $route[2] == 2 ){//中间件
if ( $route[0] == "/" ){
$callback[] = $route[1];
}else if ( stripos($url,$route[0]) === 0 ){
$callback[] = $route[1];
}
}
}
if ( isset($callback) ){
try {
foreach($callback as $cl){
if ( call_user_func($cl) === true){
break;
}
}
\StatisticClient::report($class, $method, 1, 0, '', $statistic_address);
}catch (\Exception $e) {
// Jump_exit?
if ($e->getMessage() != 'jump_exit') {
echo $e;
}
$code = $e->getCode() ? $e->getCode() : 500;
StatisticClient::report($class, $method, $success, $code, $e, $statistic_address);
}
}else{
$this->show_404($connection);
$code = 404;
$msg = "class $class not found";
\StatisticClient::report($class, $method, $success, $code, $msg, $statistic_address);
}
$this->auto_close($connection);
}

public function ServerJson($data){
Http::header("Content-type: application/json");
$this->conn->send(json_encode($data));
}

public function ServerHtml($data){
$this->conn->send($data);
}

public function run()
{
autoload_dir($this->autoload);
$this->reusePort = true;
$this->onWorkerStart = $this->onAppStart;
$this->onMessage = array($this, 'onClientMessage');
parent::run();
}

}

function autoload_dir($dir_arr){
extract($GLOBALS);
foreach($dir_arr as $dir ){
foreach(glob($dir.'*.php') as $start_file)
{
require_once $start_file;
}
}
}
22 changes: 22 additions & 0 deletions Libs/Mdb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace WebWorker\Libs;

class Mdb{

/**
* 静态成品变量 保存全局实例
*/
private static $_instance = array();

/**
* 静态工厂方法,返还此类的唯一实例
*/
public static function getInstance($config=array()) {
$key = md5(implode(":",$config));
if (!isset(self::$_instance[$key])) {
self::$_instance[$key] = new Mmysqli($config);
}
return self::$_instance[$key];
}

}
58 changes: 58 additions & 0 deletions Libs/Mmysqli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace WebWorker\Libs;

$driver = new \mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_STRICT|MYSQLI_REPORT_ERROR;

class Mmysqli extends \mysqli{

private $config = array();

public function __construct($config=array()){
$this->config = $config;
$this->connect_db();
}


private function connect_db(){
$host = isset($this->config["host"]) ? $this->config["host"] : "127.0.0.1";
$user = isset($this->config["user"]) ? $this->config["user"] : "root";
$password = isset($this->config["password"]) ? $this->config["password"] : "123456";
$db = isset($this->config["db"]) ? $this->config["db"] : "test";
$port = isset($this->config["port"]) ? $this->config["port"] : 3306;
$charset = isset($this->config["charset"]) ? $this->config["charset"] : "utf8";
parent::__construct($host,$user,$password,$db,$port);
if ( $this->connect_error ) {
echo ("connect error " . $this->connect_errno ."\r\n");
return false;
}
if ( !$this->set_charset($charset) ) {
echo ("Error loading character set $charset".$this->error."\r\n");
return false;
}
return true;
}

public function reconnect(){
if ( !$this->ping() ){
$this->close();
return $this->connect_db();
}
return true;
}

public function query( $query,$resultmode=MYSQLI_STORE_RESULT ){
try {
return parent::query($query,$resultmode);
} catch (\mysqli_sql_exception $e) {
if ($e->getCode() == 2006 || $e->getCode == 2013) {
$this->close();
$this->connect_db();
return parent::query($query,$resultmode);
}
}
return false;

}

}
33 changes: 33 additions & 0 deletions Libs/Mredis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace WebWorker\Libs;

class Mredis extends \Redis{

/**
* 静态成品变量 保存全局实例
*/
private static $_instance = array();

/**
* 静态工厂方法,返还此类的唯一实例
*/
public static function getInstance($config=array()) {
$key = md5(implode(":",$config));
if (!isset(self::$_instance[$key])) {
self::$_instance[$key] = new self();
$host = isset($config["host"]) ? $config["host"] : "127.0.0.1";
$port = isset($config["port"]) ? $config["port"] : 6379;
self::$_instance[$key]->connect($host,$port);
$password = isset($config["password"]) ? $config["password"] : "";
if ( $password ){
self::$_instance[$key]->auth($password);
}
$db = isset($config["db"]) ? $config["db"] : 0;
if ( !self::$_instance[$key]->select($db) ){
echo "redis can't connect\r\n";
}
}
return self::$_instance[$key];
}

}

0 comments on commit 6047400

Please sign in to comment.