Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(Framework): Refactor framework from upstream
Browse files Browse the repository at this point in the history
1. Remove `Mix\Base\Facade` class because of removing of facade
2. Add default value for Request object
3. Move \Mix\Base\Env -> \Mix\Config\Env
4. Rename `app()->config()` to `app()->env()`, so that we will not misuse
   the environment or dynamic config.
5. Change some commit words.
  • Loading branch information
Rhilip committed Feb 1, 2019
1 parent 3db0f5e commit 605e111
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 68 deletions.
8 changes: 4 additions & 4 deletions apps/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function actionService()

private function infoRedis()
{
$panel = app()->request->get('panel') ?? 'status';
$panel = app()->request->get('panel', 'status');

if ($panel === 'keys') {
if (app()->request->isPost()) {
Expand All @@ -46,9 +46,9 @@ private function infoRedis()
app()->redis->del(app()->redis->keys($pattern));
}
}
$offset = app()->request->get('offset') ?? null;
$perpage = app()->request->get('perpage') ?? 50;
$pattern = app()->request->get('pattern') ?? '';
$offset = app()->request->get('offset', 0);
$perpage = app()->request->get('perpage', 50);
$pattern = app()->request->get('pattern', '');

$keys = app()->redis->keys($pattern);
sort($keys);
Expand Down
8 changes: 4 additions & 4 deletions apps/controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public function actionIndex()
`last_action_at` = NOW();")->bindParams([
"tid" => $torrentInfo ? $torrentInfo["id"] : 0,
'uid' => $userInfo ? $userInfo["id"] : 0,
'ua' => app()->request->header("user-agent") ?: "",
'peer_id' => app()->request->get("peer_id") ?: "",
'ua' => app()->request->header("user-agent", ""),
'peer_id' => app()->request->get("peer_id", ""),
'req_info' => $req_info,
'msg' => $e->getMessage()
])->execute();
Expand Down Expand Up @@ -300,7 +300,7 @@ private function checkUserAgent(bool $onlyCheckUA = false)

// Start Check Client by `User-Agent` and `peer_id`
$userAgent = app()->request->header("user-agent");
$peer_id = app()->request->get("peer_id") ?? "";
$peer_id = app()->request->get("peer_id", "");

$agentAccepted = null;
$peerIdAccepted = null;
Expand Down Expand Up @@ -456,7 +456,7 @@ private function checkAnnounceFields(&$queries = [])
'numwant' => 50, 'corrupt' => 0, 'key' => '',
'ip' => '', 'ipv4' => '', 'ipv6' => '',
] as $item => $value) {
$queries[$item] = app()->request->get($item) ?? $value;
$queries[$item] = app()->request->get($item, $value);
}

foreach (['numwant', 'corrupt', 'no_peer_id', 'compact'] as $item) {
Expand Down
2 changes: 1 addition & 1 deletion bin/mix-httpd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require __DIR__ . '/../vendor/autoload.php';

Mix\Base\Env::load(__DIR__ . '/../.env');
Mix\Config\Env::load(__DIR__ . '/../.env');

$config = require __DIR__ . '/../apps/config/httpd.php';
$exitCode = (new Mix\Console\Application($config))->run();
Expand Down
4 changes: 2 additions & 2 deletions framework/Base/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public function loadComponent($name, $return = false)
}

// 获取配置
public function config($name)
public function env($name)
{
$message = "Config does not exist: {$name}.";
$message = "Environment key {$name} does not exist.";
// 处理带前缀的名称
preg_match('/(\[[\w.]+\])/', $name, $matches);
$subname = array_pop($matches);
Expand Down
2 changes: 1 addition & 1 deletion framework/Base/ComponentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mix\Base;

/**
* 组件基类Interface
* Interface ComponentInterface
* @author 刘健 <coder.liu@qq.com>
*/
interface ComponentInterface
Expand Down
2 changes: 1 addition & 1 deletion framework/Base/ComponentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mix\Base;

/**
* 组件基类Trait
* Trait ComponentTrait
* @author 刘健 <coder.liu@qq.com>
*/
trait ComponentTrait
Expand Down
26 changes: 0 additions & 26 deletions framework/Base/Facade.php

This file was deleted.

2 changes: 1 addition & 1 deletion framework/Base/StaticInstanceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mix\Base;

/**
* Interface InstanceInterface
* Interface StaticInstanceInterface
* @author 刘健 <coder.liu@qq.com>
*/
interface StaticInstanceInterface
Expand Down
4 changes: 2 additions & 2 deletions framework/Base/StaticInstanceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mix\Base;

/**
* Trait InstanceTrait
* Trait StaticInstanceTrait
* @author 刘健 <coder.liu@qq.com>
*/
trait StaticInstanceTrait
Expand All @@ -27,7 +27,7 @@ public static function new(...$args)
public static function newInstanceByConfig($name)
{
$class = get_called_class();
$config = \Mix::app()->config($name);
$config = \Mix::app()->env($name);
$object = \Mix::createObject($config);
if (get_class($object) != $class) {
throw new \Mix\Exceptions\ConfigException('实例化类型与配置类型不符');
Expand Down
8 changes: 4 additions & 4 deletions framework/Base/Env.php → framework/Config/Env.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Mix\Base;
namespace Mix\Config;

/**
* 环境类
Expand All @@ -23,15 +23,15 @@ public static function load($envFile)
}

// 获取配置
public static function get($name = null)
public static function get($name = null, $default = '')
{
if (is_null($name)) {
return self::$_env;
}

$current = self::$_env;
$current = self::$_env;
if (!isset($current[$name])) {
throw new \Mix\Exceptions\EnvException("Environment config does not exist: {$name}.");
return $default;
}
$current = $current[$name];
return $current;
Expand Down
32 changes: 16 additions & 16 deletions framework/Http/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,51 +85,51 @@ public function setRoute($route)
}

// 提取 GET 值
public function get($name = null)
public function get($name = null, $default = null)
{
return self::fetch($name, $this->_get);
return self::fetch($name, $default, $this->_get);
}

// 提取 POST 值
public function post($name = null)
public function post($name = null, $default = null)
{
return self::fetch($name, $this->_post);
return self::fetch($name, $default, $this->_post);
}

// 提取 FILES 值
public function files($name = null)
public function files($name = null, $default = null)
{
return self::fetch($name, $this->_files);
return self::fetch($name, $default, $this->_files);
}

// 提取 ROUTE 值
public function route($name = null)
public function route($name = null, $default = null)
{
return self::fetch($name, $this->_route);
return self::fetch($name, $default, $this->_route);
}

// 提取 COOKIE 值
public function cookie($name = null)
public function cookie($name = null, $default = null)
{
return self::fetch($name, $this->_cookie);
return self::fetch($name, $default, $this->_cookie);
}

// 提取 SERVER 值
public function server($name = null)
public function server($name = null, $default = null)
{
return self::fetch($name, $this->_server);
return self::fetch($name, $default, $this->_server);
}

// 提取 HEADER 值
public function header($name = null)
public function header($name = null, $default = null)
{
return self::fetch($name, $this->_header);
return self::fetch($name, $default, $this->_header);
}

// 提取数据
protected static function fetch($name, $container)
protected static function fetch($name, $default, $container)
{
return is_null($name) ? $container : (isset($container[$name]) ? $container[$name] : null);
return is_null($name) ? $container : (isset($container[$name]) ? $container[$name] : $default);
}

// 是否为 GET 请求
Expand Down
9 changes: 9 additions & 0 deletions framework/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class HttpServer extends BaseObject
protected $_settings = [
// 开启协程
'enable_coroutine' => false,
// 主进程事件处理线程数
'reactor_num' => 8,
// 工作进程数
'worker_num' => 8,
// 任务进程数
'task_worker_num' => 0,
// 进程的最大任务数
'max_request' => 10000,
// 异步安全重启
Expand Down Expand Up @@ -134,11 +140,14 @@ protected function welcome()
EOL;
app()->output->writeln('Server Name: mix-httpd');
app()->output->writeln('System Name: ' . strtolower(PHP_OS));
app()->output->writeln('Framework Version: ' . \Mix::VERSION);
app()->output->writeln("PHP Version: " . PHP_VERSION);
app()->output->writeln("Swoole Version: " . swoole_version());
app()->output->writeln("Listen Addr: {$this->_host}");
app()->output->writeln("Listen Port: {$this->_port}");
app()->output->writeln('Reactor Num: ' . $this->settings['reactor_num']);
app()->output->writeln('Worker Num: ' . $this->settings['worker_num']);
app()->output->writeln('Hot Update: ' . ($this->settings['max_request'] == 1 ? 'enabled' : 'disabled'));
app()->output->writeln('Coroutine Mode: ' . ($this->settings['enable_coroutine'] ? 'enabled' : 'disabled'));
app()->output->writeln("Config File: {$this->virtualHost['configFile']}");
Expand Down
2 changes: 1 addition & 1 deletion framework/Pool/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ConnectionPool extends Component
// 最大连接数
public $max;

// 队列
/** @var \Swoole\Coroutine\Channel */
protected $_queue;

// 活跃连接数
Expand Down
20 changes: 15 additions & 5 deletions framework/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@
*/

if (!function_exists('app')) {
// 返回当前 App 实例
/** 返回当前 App 实例
* @param null $prefix
* @return \Mix\Console\Application|\Mix\Http\Application
*/
function app($prefix = null)
{
return \Mix::app($prefix);
}
}

if (!function_exists('env')) {
// 获取一个环境变量的值
function env($name = null)
/** 获取一个环境变量的值
* @param null $name
* @param string $default
* @return array|mixed|string
*/
function env($name = null, $default = '')
{
return \Mix\Base\Env::get($name);
return \Mix\Config\Env::get($name, $default);
}
}

if (!function_exists('tgo')) {
// 创建一个带异常捕获的协程

/** 创建一个带异常捕获的协程
* @param $closure
*/
function tgo($closure)
{
go(function () use ($closure) {
Expand Down

0 comments on commit 605e111

Please sign in to comment.