From d41ec42650292a78fc02d1cac01c654c1e1e89f4 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年3月22日 19:00:14 +0800 Subject: [PATCH 01/15] update curl and route --- src/helpers/Curl.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/helpers/Curl.php b/src/helpers/Curl.php index aace67e..c69da30 100644 --- a/src/helpers/Curl.php +++ b/src/helpers/Curl.php @@ -2,6 +2,8 @@ namespace BlankQwq\Helpers; +use BlankPhp\Exception\Exception; + class Curl { @@ -19,15 +21,19 @@ private static function init() public static function get($url, $https = false, $data = [], $option = []) { - if (!empty($data)) { - $url = rtrim($url, '?') . '?' . self::parseGetData($data); + try { + if (!empty($data)) { + $url = rtrim($url, '?') . '?' . self::parseGetData($data); + } + $curl = self::init(); + curl_setopt($curl, CURLOPT_URL, $url); + $error = curl_error($curl); + $info = curl_getinfo($curl); + $result = curl_exec($curl); + return empty($error) ? $result : $info; + } catch (Exception $exception) { + return $exception; } - $curl = self::init(); - curl_setopt($curl, CURLOPT_URL, $url); - $error = curl_error($curl); - $info = curl_getinfo($curl); - $result = curl_exec($curl); - return empty($error) ? $result : $info; } private static function parseGetData($data): string @@ -35,7 +41,7 @@ private static function parseGetData($data): string if (is_array($data)) { $temp = ''; foreach ($data as $k => $v) { - $temp .= '=' . $v; + $temp .= $k . '=' . $v . '&'; } $data = rtrim($temp, '&'); } From 8c3d8ffe26b41109a9300e270ce1c99cab8aa690 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年3月22日 23:35:53 +0800 Subject: [PATCH 02/15] create gd --- src/blankphp/Kernel/HttpKernel.php | 2 +- src/blankphp/Response/Response.php | 1 + src/blankphp/Response/Traits/ResponseType.php | 14 +++- src/blankphp/Route/Router.php | 5 +- src/helpers/GD.php | 67 +++++++++++++++++++ src/helpers/Str.php | 12 ++-- 6 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 src/helpers/GD.php diff --git a/src/blankphp/Kernel/HttpKernel.php b/src/blankphp/Kernel/HttpKernel.php index b2fe3e8..0f01481 100644 --- a/src/blankphp/Kernel/HttpKernel.php +++ b/src/blankphp/Kernel/HttpKernel.php @@ -31,7 +31,7 @@ public function startConfig($config) public function __construct(Application $app) { $this->app = $app; - $this->route = $app->make("router"); + $this->route = $app->make('router'); } public function registerRequest($request) diff --git a/src/blankphp/Response/Response.php b/src/blankphp/Response/Response.php index 4fe295c..3937236 100644 --- a/src/blankphp/Response/Response.php +++ b/src/blankphp/Response/Response.php @@ -29,6 +29,7 @@ public function __construct($result) } else { $this->setType(self::$header['html']); } + dump($this->headerStack); } diff --git a/src/blankphp/Response/Traits/ResponseType.php b/src/blankphp/Response/Traits/ResponseType.php index 6be73f7..c87dda3 100644 --- a/src/blankphp/Response/Traits/ResponseType.php +++ b/src/blankphp/Response/Traits/ResponseType.php @@ -6,10 +6,11 @@ trait ResponseType { - protected static $header = [ + public static $header = [ 'html' => 'Content-Type: text/html; charset=utf-8', 'json' => 'Content-type: application/json', 'text' => 'Content-Type: text/plain', + 'image'=>'Content-Type: image/png', ]; protected static $httpStatus = [ @@ -54,15 +55,16 @@ trait ResponseType 504 => 'HTTP/1.1 504 Gateway Time-out' ]; - public function setType($value): void + public function setType($value) { $this->setHeaderStack($value, 'type'); } - public function json($value = null): void + public function json($value = null) { $this->setType(self::$header['json']); $this->setContent($value); + return $this; } public function file($value = null): void @@ -70,4 +72,10 @@ public function file($value = null): void } + public function image($value = null) + { + $this->setType(self::$header['image']); + $this->setContent($value); + return $this; + } } \ No newline at end of file diff --git a/src/blankphp/Route/Router.php b/src/blankphp/Route/Router.php index 924b68c..d9d2b49 100644 --- a/src/blankphp/Route/Router.php +++ b/src/blankphp/Route/Router.php @@ -26,7 +26,7 @@ public function __construct(Route $route) $this->app = Application::getInstance(); } - public function getMiddleware($group = "web") + public function getMiddleware($group = 'web'): void { $middleware = $this->app->getSignal('GroupMiddleware', $this->route->getGroupMidlleware()); $temp = $this->app->getSignal('AliceMiddleware', $this->route->getMiddleWare()); @@ -53,6 +53,9 @@ public function prepareResponse($response) public static function toResponse($response) { + if ($response instanceof Response) { + return $response->prepare(); + } $response = new Response($response); return $response->prepare(); } diff --git a/src/helpers/GD.php b/src/helpers/GD.php new file mode 100644 index 0000000..f9c6b22 --- /dev/null +++ b/src/helpers/GD.php @@ -0,0 +1,67 @@ + Date: 2020年4月25日 16:54:25 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- src/blankphp/Application.php | 6 +- src/blankphp/Cache/Cache.php | 171 ++++++++++++++++-- src/blankphp/Cache/CacheAbstract.php | 130 ------------- src/blankphp/Cache/CacheManager.php | 24 +++ src/blankphp/Collection/Collection.php | 30 ++- src/blankphp/Config/Config.php | 21 +-- src/blankphp/Config/LoadConfig.php | 13 +- src/blankphp/Container.php | 5 + src/blankphp/Contract/ProviderContract.php | 2 - src/blankphp/Contract/Request.php | 2 +- .../Traits/MiddlewareController.php | 23 ++- src/blankphp/Cookie/Cookie.php | 33 ++-- src/blankphp/Driver/Contract/Driver.php | 4 +- src/blankphp/Driver/Driver.php | 24 ++- .../Driver/Traits/JsonDriverHelper.php | 10 + .../Driver/Traits/SessionHandlerTrait.php | 11 +- src/blankphp/Facade.php | 12 +- src/blankphp/Facade/Cookie.php | 4 + src/blankphp/Facade/Driver.php | 14 ++ .../FileSystem/Driver/DefaultFile.php | 10 + src/blankphp/FileSystem/FileSystem.php | 7 +- src/blankphp/Kernel/ConsoleKernel.php | 4 +- src/blankphp/Kernel/HttpKernel.php | 9 +- src/blankphp/Log/Driver/File.php | 12 +- src/blankphp/Log/Log.php | 5 +- src/blankphp/Manager/ManagerBase.php | 23 +++ src/blankphp/PipeLine/PipeLine.php | 11 +- .../Provider/EventServiceProvider.php | 2 +- src/blankphp/Provider/MiddleWareProvider.php | 29 ++- src/blankphp/Provider/Provider.php | 21 +-- src/blankphp/Provider/RegisterProvider.php | 6 +- src/blankphp/Request/Request.php | 116 +++++------- src/blankphp/Response/Traits/ResponseType.php | 4 +- 34 files changed, 453 insertions(+), 348 deletions(-) delete mode 100644 src/blankphp/Cache/CacheAbstract.php create mode 100644 src/blankphp/Cache/CacheManager.php create mode 100644 src/blankphp/Driver/Traits/JsonDriverHelper.php create mode 100644 src/blankphp/FileSystem/Driver/DefaultFile.php diff --git a/composer.json b/composer.json index db1a5ba..98a0433 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "require": { "psr/log": "^1.1", "predis/predis": "^1.1", - "ext-curl": "*" + "ext-curl": "*", + "ext-json": "*" } } diff --git a/src/blankphp/Application.php b/src/blankphp/Application.php index 9e4b602..f3cb2d0 100644 --- a/src/blankphp/Application.php +++ b/src/blankphp/Application.php @@ -9,7 +9,7 @@ namespace BlankPhp; -use BlankPhp\Cache\Cache; +use BlankPhp\Cache\CacheManager; use BlankPhp\Config\Config; use BlankPhp\Config\LoadConfig; use BlankPhp\Contract\CookieContract; @@ -93,8 +93,8 @@ public function registerService() 'session' => [\BlankPhp\Contract\Session::class, Session::class], 'scheme' => Scheme::class, 'response' => Response::class, - 'cache' => [Cache::class], - 'cache.drive' => [Cache::class], + 'cache' => [CacheManager::class], + 'cache.drive' => [CacheManager::class], 'redis' => [Redis::class], 'log' => Log::class ]; diff --git a/src/blankphp/Cache/Cache.php b/src/blankphp/Cache/Cache.php index 29e0b93..024f4bb 100644 --- a/src/blankphp/Cache/Cache.php +++ b/src/blankphp/Cache/Cache.php @@ -3,30 +3,177 @@ namespace BlankPhp\Cache; - use BlankPhp\Application; -use BlankPhp\Contract\Container; -use BlankPhp\Facade\Driver; -use BlankQwq\Helpers\Str; +use \BlankPhp\Driver\Contract\Driver; -class Cache extends CacheAbstract +class Cache { + /** + * @var Driver + */ + private $handler; + /** + * @var + */ + private $file; + /** + * @var array + */ + private $data = []; + /** + * @var int + */ + private $writeCount = 0; + /** + * @var int + */ + private $getCount = 0; + /** + * @var + */ + protected static $dir; + /** + * @var + */ protected $tag; + /** + * @var string[] + */ + protected $config = [ + 'prefix' => '', + ]; - - public function __construct() + /** + * @return array + */ + public function getData(): array { - $this->setOption(config('cache')); - $driver = $this->config['driver']; - $handler = Driver::factory($driver, "cache"); - $this->setHandler($handler); + return $this->data; } - public function setOption($config) + public function setOption($config): void { $this->config = array_merge($this->config, $config); } + /** + * @param array $data + */ + public function setData($data): void + { + $this->data = $data; + } + + /** + * @return int + */ + public function getWriteCount(): int + { + return $this->writeCount; + } + + /** + * @param int $writeCount + */ + public function setWriteCount($writeCount): void + { + $this->writeCount = $writeCount; + } + + /** + * @return int + */ + public function getGetCount(): int + { + return $this->getCount; + } + + /** + * @param int $getCount + */ + public function setGetCount($getCount): void + { + $this->getCount = $getCount; + } + + + /** + * @return Driver + */ + public function getHandler(): Driver + { + return $this->handler; + } + + /** + * @param Driver $handler + */ + public function setHandler(Driver $handler): void + { + $this->handler = $handler; + } + + /** + * @return mixed + */ + public function getFile() + { + return $this->file; + } + + /** + * @param mixed $file + */ + public function setFile($file): void + { + $this->file = $file; + } + + /*** + * @param $key + * @param $value + * @param null $ttl + * @return mixed + */ + public function set($key, $value, $ttl = null) + { + return $this->handler->set($key, $value, $ttl); + } + + /** + * @param $key + * @param null $default + * @return mixed + */ + public function get($key, $default = null) + { + return $this->handler->get($key, $default); + } + + /** + * @param $key + * @param \Closure $closure + * @return mixed + */ + public function remember(string $key, \Closure $closure) + { + return $this->handler->remember($key, $closure()); + } + + /** + * @param $key + * @return mixed + */ + public function has($key) + { + return $this->handler->has($key); + } + + /** + * @param $name + * @param $arguments + * @return mixed + */ public function __call($name, $arguments) { return $this->getHandler()->$name(...$arguments); diff --git a/src/blankphp/Cache/CacheAbstract.php b/src/blankphp/Cache/CacheAbstract.php deleted file mode 100644 index 2a1aced..0000000 --- a/src/blankphp/Cache/CacheAbstract.php +++ /dev/null @@ -1,130 +0,0 @@ -data; - } - - /** - * @param array $data - */ - public function setData($data) - { - $this->data = $data; - } - - /** - * @return int - */ - public function getWriteCount() - { - return $this->writeCount; - } - - /** - * @param int $writeCount - */ - public function setWriteCount($writeCount) - { - $this->writeCount = $writeCount; - } - - /** - * @return int - */ - public function getGetCount() - { - return $this->getCount; - } - - /** - * @param int $getCount - */ - public function setGetCount($getCount) - { - $this->getCount = $getCount; - } - - - /** - * @return null - */ - public function getHandler() - { - return $this->handler; - } - - /** - * @param Driver $handler - */ - public function setHandler(Driver $handler) - { - $this->handler = $handler; - } - - /** - * @return mixed - */ - public function getFile() - { - return $this->file; - } - - /** - * @param mixed $file - */ - public function setFile($file) - { - $this->file = $file; - } - - - public function set($key, $value, $ttl = null) - { - return $this->handler->set($key, $value, $ttl); - } - - public function get($key, $default = null) - { - return $this->handler->get($key, $default); - } - - public function remember($array, \Closure $closure) - { - return $this->handler->remember($array, $closure()); - } - - public function has($key) - { - return $this->handler->has($key); - } - -} \ No newline at end of file diff --git a/src/blankphp/Cache/CacheManager.php b/src/blankphp/Cache/CacheManager.php new file mode 100644 index 0000000..62feee9 --- /dev/null +++ b/src/blankphp/Cache/CacheManager.php @@ -0,0 +1,24 @@ +tag); + } + +} \ No newline at end of file diff --git a/src/blankphp/Collection/Collection.php b/src/blankphp/Collection/Collection.php index b5fbcd7..9243393 100644 --- a/src/blankphp/Collection/Collection.php +++ b/src/blankphp/Collection/Collection.php @@ -8,11 +8,10 @@ class Collection implements \ArrayAccess, \Iterator, \Countable { //数据的存储 protected $item = []; - private $position = 0; private $keys = []; private $max = 0; + private $position = 0; - //初始化函数 public function __construct() { @@ -23,27 +22,27 @@ public function item($obj) return $this->item[] = empty($obj) ? null : $obj; } - public function merg($value) + public function mere($value) { - $this->item = array_merge($this->item, $value); + return $this->item = array_merge($this->item, $value); } // //转换为数组输出 public function toArray() { - return array_map(function () { + return array_map(static function () { }, $this->item); } //统计$this->>item - public function count() + public function count():int { return count($this->item); } - public function offsetExists($offset) + public function offsetExists($offset): bool { return isset($this->item[$offset]); } @@ -54,11 +53,18 @@ public function offsetGet($offset) } + /** + * @param mixed $offset + * @param mixed $value + */ public function offsetSet($offset, $value) { $this->item[$offset] = $value; } + /** + * @param mixed $offset + */ public function offsetUnset($offset) { unset($this->item[$offset]); @@ -89,7 +95,10 @@ public function next() ++$this->position; } - public function valid() + /** + * @return bool + */ + public function valid(): bool { return isset($this->item[$this->keys[$this->position]]) && $this->position < $this->max; } @@ -100,7 +109,10 @@ public function __call($name, $arguments) } - public function __toArray() + /** + * @return array + */ + private function __toArray() { $data = []; foreach ($this->item as $item) { diff --git a/src/blankphp/Config/Config.php b/src/blankphp/Config/Config.php index 8949bdc..bc93bec 100644 --- a/src/blankphp/Config/Config.php +++ b/src/blankphp/Config/Config.php @@ -13,7 +13,7 @@ class Config implements \ArrayAccess, \Iterator, \Countable protected $current; - public function setConfig($config) + public function setConfig($config): Config { $this->config = $config; return $this; @@ -30,18 +30,14 @@ public function get($descNames, $default = '') foreach ($descNames as $descName) { $config = $config[$descName]; } - unset($descNames, $default); return $config; } catch (\Exception $exception) { return $default; } } - public function set($key, $value) + public function set($key, $value): void { - //获取driver - - //利用driver保存并刷新对应文件 } @@ -82,25 +78,28 @@ public function rewind() } - public function offsetExists($offset) + public function offsetExists($offset): bool { - + return isset($this->config[$offset]); } public function offsetGet($offset) { - + return $this->config[$offset]; } public function offsetSet($offset, $value) { - + $this->config[$offset] = $value; } + /** + * @param mixed $offset + */ public function offsetUnset($offset) { - + unset($this->config[$offset]); } } \ No newline at end of file diff --git a/src/blankphp/Config/LoadConfig.php b/src/blankphp/Config/LoadConfig.php index f745c3c..22a249b 100644 --- a/src/blankphp/Config/LoadConfig.php +++ b/src/blankphp/Config/LoadConfig.php @@ -9,7 +9,11 @@ class LoadConfig { private $configPath = APP_PATH . 'config/'; + private $handler; + /** + * @param Application $app + */ public function load(Application $app) { if (!is_file(APP_PATH . 'cache/framework/config.php')) { @@ -24,14 +28,15 @@ public function load(Application $app) mb_internal_encoding('UTF-8'); } - public function loadConfigFile() + /** + * @return array + */ + public function loadConfigFile(): array { - //暂时耦合 -- 等会使用load解开耦合 $config = []; - foreach (glob($this->configPath . "*.php") as $file) { + foreach (glob($this->configPath . '*.php') as $file) { $fileName = substr($file, strlen($this->configPath), strlen($file)); $position = strpos($fileName, '.'); - //是否截取其中的代码 $fileName = substr($fileName, 0, $position); $config[$fileName] = require $file; } diff --git a/src/blankphp/Container.php b/src/blankphp/Container.php index 9d1804a..705679d 100644 --- a/src/blankphp/Container.php +++ b/src/blankphp/Container.php @@ -252,6 +252,11 @@ public function call($instance, $method = null, $construct = null, array $param } + public function factory($name,\Closure $closure){ + + } + + public function flush(): void { $this->classes = []; diff --git a/src/blankphp/Contract/ProviderContract.php b/src/blankphp/Contract/ProviderContract.php index d5575dc..bd4dbab 100644 --- a/src/blankphp/Contract/ProviderContract.php +++ b/src/blankphp/Contract/ProviderContract.php @@ -14,6 +14,4 @@ interface ProviderContract { public function __construct(); - public function boot(); - public function register(); } \ No newline at end of file diff --git a/src/blankphp/Contract/Request.php b/src/blankphp/Contract/Request.php index 4b8e045..795fbfe 100644 --- a/src/blankphp/Contract/Request.php +++ b/src/blankphp/Contract/Request.php @@ -11,7 +11,7 @@ interface Request { - public function get($name = '', array $optionm = []); + public function get($name, $default = ''); public function getUri(); diff --git a/src/blankphp/Controller/Traits/MiddlewareController.php b/src/blankphp/Controller/Traits/MiddlewareController.php index 904b794..b0dee36 100644 --- a/src/blankphp/Controller/Traits/MiddlewareController.php +++ b/src/blankphp/Controller/Traits/MiddlewareController.php @@ -11,19 +11,22 @@ trait MiddlewareController { - //这里中间件加入到 - protected $middleware=[]; - public function getMiddleware(){ + protected $middleware = []; + + public function getMiddleware(): array + { return $this->middleware; } - public function middleware(){ - //添加中间件 + public function middleware(): void + { $middleware = func_get_args(); - if (is_array($middleware)) - foreach ($middleware as $m) - $this->middleware[]=$m; - else - $this->middleware[]=$middleware; + if (is_array($middleware)) { + foreach ($middleware as $m) { + $this->middleware[] = $m; + } + } else { + $this->middleware[] = $middleware; + } } } \ No newline at end of file diff --git a/src/blankphp/Cookie/Cookie.php b/src/blankphp/Cookie/Cookie.php index ef17969..91b4abb 100644 --- a/src/blankphp/Cookie/Cookie.php +++ b/src/blankphp/Cookie/Cookie.php @@ -19,7 +19,7 @@ class Cookie implements CookieContract protected $path = null; protected $domain = null; protected $secure = false; - protected $httponly = false; + protected $httpOnly = false; protected $cookie = []; protected $queue = []; @@ -28,12 +28,15 @@ public function __construct() $this->getCookie(); } - public function getCookie() + private function getCookie(): void { $this->cookie = $_COOKIE; unset($_COOKIE); } + /** + * @param array $option + */ public function setOption($option = []) { foreach ($option as $k => $v) { @@ -45,44 +48,50 @@ public function setOption($option = []) } } - public function set($key, $value, $expires = null, $option = null) { if (is_array($value) || is_object($value)) { $value = json_encode($value); } - if (!is_null($option)) { + if ($option !== null) { array_shift($option); return setcookie($key, $value, time() + $expires, ...array_values($option) ); } return setcookie($key, $value, time() + $expires, $this->path, $this->domain, $this->secure - , $this->httponly + , $this->httpOnly ); } - public function get($name = "", $default = "") + public function get($name = '', $default = '') { if (empty($name)) { return $this->cookie; } if (!empty($this->cookie)) { - return substr($this->cookie[$name], 0, 1) == '{' ? - json_decode($this->cookie[$name]) : - $this->cookie[$name]; + if ((strpos($this->cookie[$name], '{') === 0)) { + return json_decode($this->cookie[$name], true); + } + return $this->cookie[$name]; } return $default; } - //添加一个队列操作,从队列中加入想要的Cookie - public function putQueue() + public function flush() { + foreach ($this->queue as $k => $v) { + $this->set($k, $v); + } + } + public function putQueue($value): void + { + $this->queue[] = $value; } public function popQueue() { - + return array_pop($this->queue); } } \ No newline at end of file diff --git a/src/blankphp/Driver/Contract/Driver.php b/src/blankphp/Driver/Contract/Driver.php index 8c182f7..d7c8422 100644 --- a/src/blankphp/Driver/Contract/Driver.php +++ b/src/blankphp/Driver/Contract/Driver.php @@ -6,11 +6,11 @@ interface Driver { - public function __construct($name = "default", $option = []); + public function __construct($name = 'default', $option = []); public function parseValue($value); - public function valueParse($value); + public function valueParse($value, $option = []); public function set($key, $value, $ttl = null); diff --git a/src/blankphp/Driver/Driver.php b/src/blankphp/Driver/Driver.php index 8577063..b6816a2 100644 --- a/src/blankphp/Driver/Driver.php +++ b/src/blankphp/Driver/Driver.php @@ -11,26 +11,34 @@ abstract class Driver implements DriverContract, \SessionHandlerInterface { use SessionHandlerTrait, OtherHelpTrait; - public function __construct($name = 'default', $option = []){ - - } - + /** + * @param $value + * @return string + */ public function parseValue($value) { return serialize($value); } - public function valueParse($value) + /** + * @param $value + * @param array $option + * @return mixed + */ + public function valueParse($value, $option = []) { - return unserialize($value); + return unserialize($value, $option); } - public function clearExpireData($max_live_time) + /** + * @param $max_live_time + * @return bool + */ + public function clearExpireData($max_live_time): bool { return true; } - abstract public function set($key, $value, $ttl = null); abstract public function delete($key); diff --git a/src/blankphp/Driver/Traits/JsonDriverHelper.php b/src/blankphp/Driver/Traits/JsonDriverHelper.php new file mode 100644 index 0000000..ef27522 --- /dev/null +++ b/src/blankphp/Driver/Traits/JsonDriverHelper.php @@ -0,0 +1,10 @@ +delete($session_id); } - public function gc($maxlifetime) + public function gc($maxLifeTime): bool { - return $this->clearExpireData(); + return $this->clearExpireData($maxLifeTime); } - public function open($save_path, $name) + public function open($save_path, $name): bool { return true; } @@ -35,6 +35,7 @@ public function read($session_id) public function write($session_id, $session_data) { - $this->set($session_id, $this->parseValue($session_data), $this->gc); + return $this->set($session_id, $this->parseValue($session_data), $this->gc); } + } \ No newline at end of file diff --git a/src/blankphp/Facade.php b/src/blankphp/Facade.php index 41727f0..151da15 100644 --- a/src/blankphp/Facade.php +++ b/src/blankphp/Facade.php @@ -13,11 +13,12 @@ abstract class Facade { protected static $resolveFacadeInstances = []; - protected static function getFacadeAccessor() + public function __invoke() { - throw new \RuntimeException('你没有指定的代理类', 1); + // TODO: Implement __invoke() method. } + protected function clearResolveInstance($instance): void { unset(static::$resolveFacadeInstances[$instance]); @@ -29,6 +30,11 @@ protected function clearResolveInstances($instance): void } public static function resolveFacadeInstance() + { + return self::getFromApp(); + } + + public static function getFromApp() { $className = static::getFacadeAccessor(); if (is_object($className)) { @@ -42,7 +48,7 @@ public static function resolveFacadeInstance() return $obj; } - public static function __CallStatic($method, $args) + public static function __callStatic($method, $args) { $obj = static::resolveFacadeInstance(); //通过反射解决依赖 diff --git a/src/blankphp/Facade/Cookie.php b/src/blankphp/Facade/Cookie.php index f41020a..c1a9cee 100644 --- a/src/blankphp/Facade/Cookie.php +++ b/src/blankphp/Facade/Cookie.php @@ -6,6 +6,10 @@ use BlankPhp\Facade; +/** + * Class Cookie + * @package BlankPhp\Facade + */ class Cookie extends Facade { protected static function getFacadeAccessor() diff --git a/src/blankphp/Facade/Driver.php b/src/blankphp/Facade/Driver.php index 8ee08ab..4400c80 100644 --- a/src/blankphp/Facade/Driver.php +++ b/src/blankphp/Facade/Driver.php @@ -6,8 +6,22 @@ use BlankPhp\Facade; +/** + * Class Driver + * @package BlankPhp\Facade + * @method setConfig($config) + * @method factory($name, $nickName = 'default', $register = false) + * @method parseName($name) + * @method getDrivers($key) + * @method setDrivers($project) + * @method getConfig($key, $default = '') + */ class Driver extends Facade { + + /** + * @return string + */ protected static function getFacadeAccessor():string { return 'driver.factory'; diff --git a/src/blankphp/FileSystem/Driver/DefaultFile.php b/src/blankphp/FileSystem/Driver/DefaultFile.php new file mode 100644 index 0000000..1bae65b --- /dev/null +++ b/src/blankphp/FileSystem/Driver/DefaultFile.php @@ -0,0 +1,10 @@ +args = $args; } - public function flush() + public function flush(): void { $this->route->flush(); $this->app->flush(); diff --git a/src/blankphp/Kernel/HttpKernel.php b/src/blankphp/Kernel/HttpKernel.php index 0f01481..1cb2bb2 100644 --- a/src/blankphp/Kernel/HttpKernel.php +++ b/src/blankphp/Kernel/HttpKernel.php @@ -23,7 +23,7 @@ class HttpKernel protected $app; protected $route; - public function startConfig($config) + public function startConfig($config): void { //处理设置 } @@ -34,12 +34,11 @@ public function __construct(Application $app) $this->route = $app->make('router'); } - public function registerRequest($request) + public function registerRequest($request): void { $this->app->instance('request', $request); } - //处理请求===》返回一个response,这里交给route组件 public function handle($request) { $this->startConfig($this->config); @@ -48,13 +47,13 @@ public function handle($request) } - public function registerService($bootstrap) + public function registerService($bootstrap): void { $this->app->make($bootstrap); } - public function flush() + public function flush(): void { $this->app->flush(); } diff --git a/src/blankphp/Log/Driver/File.php b/src/blankphp/Log/Driver/File.php index b1d281f..38b38a3 100644 --- a/src/blankphp/Log/Driver/File.php +++ b/src/blankphp/Log/Driver/File.php @@ -12,7 +12,7 @@ class File implements LoggerInterface protected $options = [ 'dir' => APP_PATH . '/cache/log/', 'max' => 2 * 1024 * 1024, - 'time_format' => "Y - M - D H:S:", + 'time_format' => 'Y - M - D H:S:', 'extension' => 'log', ]; @@ -85,11 +85,11 @@ public function log($level, $message, array $context = array()) $this->put($res); } - public function put(string $data) + public function put(string $data): void { //判断目录是否存在 - if (!is_dir($this->options['dir'])) { - mkdir($this->options['dir']); + if (!is_dir($this->options['dir']) && !mkdir($concurrentDirectory = $this->options['dir']) && !is_dir($concurrentDirectory)) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory)); } //文件名 if (empty($this->fileName)) { @@ -103,11 +103,9 @@ public function put(string $data) //重命名 rename($this->options['dir'] . $this->fileName, $this->options['dir'] . time() . '.log'); } - $file = fopen($this->options['dir'] . $this->fileName, 'a'); + $file = fopen($this->options['dir'] . $this->fileName, 'ab'); fwrite($file, $data); fclose($file); } - - } } \ No newline at end of file diff --git a/src/blankphp/Log/Log.php b/src/blankphp/Log/Log.php index a8102e6..23f26f3 100644 --- a/src/blankphp/Log/Log.php +++ b/src/blankphp/Log/Log.php @@ -5,18 +5,19 @@ use BlankPhp\Application; +use BlankQwq\Helpers\Str; use Psr\Log\AbstractLogger; -use Psr\Log\LogLevel; class Log extends AbstractLogger { protected $handler; + protected $nameSpace = "BlankPhp\Log\Driver"; public function __construct(Application $app) { $handler = $app['config']->get('app.log_driver', 'File'); - $handler = $this->nameSpace . '\\' . ucfirst($handler);; + $handler = Str::makeClassName($handler, $this->nameSpace); $this->handler = new $handler; } diff --git a/src/blankphp/Manager/ManagerBase.php b/src/blankphp/Manager/ManagerBase.php index ac44fa6..b236c8f 100644 --- a/src/blankphp/Manager/ManagerBase.php +++ b/src/blankphp/Manager/ManagerBase.php @@ -10,5 +10,28 @@ */ class ManagerBase { + protected $drivers = []; + protected $default = null; + + public function __construct() + { + + } + + public function createDefaultDriver() + { + + } + + public function driver() + { + + } + + + public function clear(): void + { + $this->drivers = []; + } } \ No newline at end of file diff --git a/src/blankphp/PipeLine/PipeLine.php b/src/blankphp/PipeLine/PipeLine.php index 2a94697..7cdcf2d 100644 --- a/src/blankphp/PipeLine/PipeLine.php +++ b/src/blankphp/PipeLine/PipeLine.php @@ -21,15 +21,15 @@ public function __construct() { } - public function send($data) + public function send($data): PipeLine { $this->data = $data; return $this; } - public function getAlice() + public function getAlice(): callable { - return function ($stack, $pipe, $method = "handle") { + return function ($stack, $pipe, $method = 'handle') { return function () use ($stack, $pipe, $method) { if (!is_object($pipe)) { $pipe = new $pipe(); @@ -42,12 +42,11 @@ public function getAlice() }; } - public function process() + public function process(): void { - //执行 } - public function through($middleware) + public function through($middleware): PipeLine { //管道模式运行 $this->middleware = is_array($middleware) ? $middleware : func_get_args(); diff --git a/src/blankphp/Provider/EventServiceProvider.php b/src/blankphp/Provider/EventServiceProvider.php index 7691038..b9ea0ec 100644 --- a/src/blankphp/Provider/EventServiceProvider.php +++ b/src/blankphp/Provider/EventServiceProvider.php @@ -11,5 +11,5 @@ class EventServiceProvider extends Provider { - //观察者等方式 + } \ No newline at end of file diff --git a/src/blankphp/Provider/MiddleWareProvider.php b/src/blankphp/Provider/MiddleWareProvider.php index 63c50d3..207b475 100644 --- a/src/blankphp/Provider/MiddleWareProvider.php +++ b/src/blankphp/Provider/MiddleWareProvider.php @@ -10,37 +10,34 @@ class MiddleWareProvider extends Provider { - protected $namespace = 'App\Middleware'; protected $middleware = [ ]; protected $registerMiddleware; - protected $groupMiddleware=[]; - public function getMiddleware($group){ - //写出= =要加载的文件或者config中的中间件 + protected $groupMiddleware = []; + + public function getMiddleware($group) + { if (isset($this->groupMiddleware[$group])) return $this->groupMiddleware[$group]; } - public function boot() + public function boot(): void { -// $this->app->bind('middleware', $this); $this->app->signal('GroupMiddleware', $this->groupMiddleware); $this->app->signal('AliceMiddleware', $this->registerMiddleware); } - public function register() - { - parent::register(); // TODO: Change the autogenerated stub - } - - public function getAliceMiddleWare($alice){ - $temp=[]; - foreach ($alice as $item) - if (isset($this->registerMiddleware[$item])) - $temp[]=$this->registerMiddleware[$item]; + public function getAliceMiddleWare($alice): array + { + $temp = []; + foreach ($alice as $item) { + if (isset($this->registerMiddleware[$item])) { + $temp[] = $this->registerMiddleware[$item]; + } + } return $temp; } diff --git a/src/blankphp/Provider/Provider.php b/src/blankphp/Provider/Provider.php index 8c6af54..6bdfac3 100644 --- a/src/blankphp/Provider/Provider.php +++ b/src/blankphp/Provider/Provider.php @@ -17,10 +17,11 @@ class Provider implements ProviderContract protected $app; /** * @var array - * 命令行注册 */ protected $command = []; - //关联配置文件映射 + /** + * @var array + */ protected $config = []; public function __construct() @@ -34,20 +35,4 @@ public function __construct() } } - /** - * @return void - */ - public function boot() - { - - } - - /** - * @return void - */ - public function register() - { - - } - } \ No newline at end of file diff --git a/src/blankphp/Provider/RegisterProvider.php b/src/blankphp/Provider/RegisterProvider.php index 523641a..ec14c4e 100644 --- a/src/blankphp/Provider/RegisterProvider.php +++ b/src/blankphp/Provider/RegisterProvider.php @@ -11,12 +11,12 @@ class RegisterProvider protected $providers = [ ]; - public function getProviders() + public function getProviders(): void { $this->providers = array_merge($this->providers, config('app.providers')); } - public function register(Application $app) + public function register(Application $app): void { $this->boot($app); $this->getProviders(); @@ -25,7 +25,7 @@ public function register(Application $app) } } - public function boot(Application $app) + public function boot(Application $app): void { foreach (config('app.alice') as $name => $class) { $app->alice($name, $class); diff --git a/src/blankphp/Request/Request.php b/src/blankphp/Request/Request.php index 91ddc40..658a0d3 100644 --- a/src/blankphp/Request/Request.php +++ b/src/blankphp/Request/Request.php @@ -9,39 +9,28 @@ namespace BlankPhp\Request; use BlankPhp\Contract\Request as RequestContract; +use BlankPhp\Facade\Cache; use BlankPhp\Facade\Cookie; use BlankPhp\Facade\Session; class Request implements RequestContract { - //端口存储以及其他信息的存储!!--》 表单验证功能的实现 public $uri; - //方法 public $method; - //请求数组 public $request = [ 'get' => '', 'post' => '', 'files' => '', 'input' => '', ]; - public $session = []; - public $cookie = []; - //php://input + public $session; + public $cookie; public $input = []; - //用户ip; - public $user_ip; - //访问所需的ip - public $server_ip; - //http协议 + public $userIp; + public $serverIp; public $http; - //443 public $https; - //用户user public $userAgent; - //用户ip - public $userIp; - //用户语言 public $language; public function __construct() @@ -50,9 +39,16 @@ public function __construct() $this->getMethod(); $this->getRequest(); $this->getFromServer(); + $this->getOther(); + } + + public function getOther(): void + { + $this->cookie = Cache::getFromApp(); + $this->session = Session::getFromApp(); } - public function getFromServer() + public function getFromServer(): void { $this->getUserAgent(); $this->getLanguage(); @@ -66,43 +62,40 @@ public function stripSlashesDeep($value) return $value; } - public function get($name = '', array $optionm = []) + public function get($name, $default = ''): string { $this->{'_' . strtolower($this->method)}(); if (isset($this->request[strtolower($this->method)][$name])) { return $this->request[strtolower($this->method)][$name]; - } else { - $this->getRequest(); - foreach ($this->request as $item) { - if (isset($item[$name])) - return $item[$name]; + } + + $this->getRequest(); + foreach ($this->request as $item) { + if (isset($item[$name])) { + return $item[$name]; } - return null; } + return $default; } - public function capture() + public function capture(): Request { return $this; } - public function getUri() + public function getUri(): string { - if (is_null($this->uri)) { + if ($this->uri === null) { $url = $_SERVER['REQUEST_URI']; - // 清除?之后的内容,计算?出现的位置position(定位) $position = strpos($url, '?'); - //是否截取其中的代码 $url = $position === false ? $url : substr($url, 0, $position); $url = ltrim($url, '/'); $urlArray = explode('/', $url); $urlArray = array_filter($urlArray); - //获取路径 $file = explode('/', str_replace(DS, '/', PUBLIC_PATH . 'index.php')); $urlArray = array_diff($urlArray, $file); - //去除两边的东西 if ($urlArray) { $this->uri = '/' . implode('/', $urlArray); } else { @@ -125,17 +118,14 @@ public function getMethod() public function file($name = '') { - if (empty($this->request['files'])) { - $this->request['files'] = !is_null($_FILES) ? $_FILES : ''; + if (empty($this->request['files']) && !empty($_FILES)) { + $this->request['files'] = $_FILES; unset($_FILES); } - if (isset($this->request['files'][$name])) - return $this->request['files'][$name]; - else - return ''; + return $this->request['files'][$name] ?? ''; } - public function __get($name) + public function __get($name): ?string { if (!isset($this->$name)) { return $this->get($name); @@ -143,7 +133,7 @@ public function __get($name) return $this->$name; } - private function getRequest() + private function getRequest(): void { $this->_get(); $this->_post(); @@ -162,15 +152,17 @@ public function getUserAgent() public function userIp() { - if (empty($this->user_ip)) - $this->user_ip = $_SERVER['REMOTE_ADDR']; - return $this->user_ip; + if (empty($this->userIp)) { + $this->userIp = $_SERVER['REMOTE_ADDR']; + } + return $this->userIp; } public function getLanguage() { - if (empty($this->language)) + if (empty($this->language)) { $this->language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; + } return $this->language; } @@ -190,36 +182,30 @@ private function _cookie($name = '', array $optionm = []) } - private function _get($name = '', array $optionm = []) + private function _get($name = '', array $options = []): string { if (empty($this->request['get'])) { //是否进行过滤?递归的效率很成问题 - $this->request['get'] = !is_null($_GET) ? $this->stripSlashesDeep($_GET) : ''; + $this->request['get'] = $_GET !== null ? $this->stripSlashesDeep($_GET) : ''; unset($_GET); } - if (isset($this->request['get'][$name])) - return $this->request['get'][$name]; - else - return ''; + return $this->request['get'][$name] ?? ''; } - private function _post($name = '', array $optionm = []) + private function _post($name = '', array $options = []): string { if (empty($this->request['post'])) { - $this->request['post'] = !is_null($_POST) ? $this->stripSlashesDeep($_POST) : ''; + $this->request['post'] = $_POST !== null ? $this->stripSlashesDeep($_POST) : ''; unset($_POST); } - if (isset($this->request['post'][$name])) - return $this->request['post'][$name]; - else - return ''; + return $this->request['post'][$name] ?? ''; } private function _input($name = '', array $args = []) { if (empty($this->request['input'])) { $this->input = file_get_contents('php://input'); - if (strstr($this->input, '{')) { + if (strpos($this->input, '{') !== false) { $this->request['input'] = json_decode($this->input, true); } else { parse_str($this->input, $this->request['input']); @@ -228,20 +214,11 @@ private function _input($name = '', array $args = []) if (empty($name)) { return $this->input; } - if (isset($this->request['input'][$name])) - return $this->request['input'][$name]; - else - return ''; - } - - public function flush() - { - //清理 - + return $this->request['input'][$name] ?? ''; } - public function __toArray() + public function __toArray(): array { return [ 'uri' => $this->uri, @@ -249,12 +226,11 @@ public function __toArray() 'request' => $this->request, 'session' => $this->session, 'input' => $this->input, - 'user_ip' => $this->user_ip, - 'server_ip' => $this->server_ip, + 'userIp' => $this->userIp, + 'serverIp' => $this->serverIp, 'http' => $this->http, 'https' => $this->https, 'userAgent' => $this->userAgent, - 'userIp' => $this->userIp, 'language' => $this->language, ]; } diff --git a/src/blankphp/Response/Traits/ResponseType.php b/src/blankphp/Response/Traits/ResponseType.php index c87dda3..19ae215 100644 --- a/src/blankphp/Response/Traits/ResponseType.php +++ b/src/blankphp/Response/Traits/ResponseType.php @@ -55,12 +55,12 @@ trait ResponseType 504 => 'HTTP/1.1 504 Gateway Time-out' ]; - public function setType($value) + public function setType($value): void { $this->setHeaderStack($value, 'type'); } - public function json($value = null) + public function json($value = null): ResponseType { $this->setType(self::$header['json']); $this->setContent($value); From 8a56ad5cf2b5d311771a4c254a6bb6400f685a96 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: Mon, 4 May 2020 22:17:18 +0800 Subject: [PATCH 04/15] create base traits and manager --- README.md | 8 +++++ src/blankphp/Application.php | 4 +-- .../Base/Traits/FactoryClientTrait.php | 20 ++++++++++++ src/blankphp/Cache/CacheManager.php | 10 +++++- src/blankphp/Container.php | 12 ++++--- .../FileSystem/Driver/DefaultFile.php | 11 +++++++ src/blankphp/Manager/ManagerBase.php | 31 ++++++++++++++----- src/blankphp/Provider/Provider.php | 4 +++ src/blankphp/Response/Response.php | 6 ++-- src/blankphp/Route/Route.php | 2 +- src/blankphp/Session/Session.php | 4 +-- 11 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 src/blankphp/Base/Traits/FactoryClientTrait.php diff --git a/README.md b/README.md index 29a6771..2e8cffd 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,14 @@ $ composer require blankqwq/framework -vvv - [ ] Console - [x] ConsoleKernel +``` +# proxy代理 + +# manage管理者 + +# event事件 +``` + ## Contributing diff --git a/src/blankphp/Application.php b/src/blankphp/Application.php index f3cb2d0..2543988 100644 --- a/src/blankphp/Application.php +++ b/src/blankphp/Application.php @@ -124,10 +124,10 @@ public function registerBase() public function getSignal($abstract, $name = '') { if (empty($name)) { - return isset($this->signal[$abstract]) ? $this->signal[$abstract] : []; + return $this->signal[$abstract] ?? []; } - return isset($this->signal[$abstract][$name]) ? $this->signal[$abstract][$name] : []; + return $this->signal[$abstract][$name] ?? []; } public function unsetSignal($abstract) diff --git a/src/blankphp/Base/Traits/FactoryClientTrait.php b/src/blankphp/Base/Traits/FactoryClientTrait.php new file mode 100644 index 0000000..4f65481 --- /dev/null +++ b/src/blankphp/Base/Traits/FactoryClientTrait.php @@ -0,0 +1,20 @@ +tag); + $handler = Driver::factory(config('cache.driver'), $this->tag); } + + public function createDefaultDriver() + { + // TODO: Implement createDefaultDriver() method. + } } \ No newline at end of file diff --git a/src/blankphp/Container.php b/src/blankphp/Container.php index 705679d..1286ab7 100644 --- a/src/blankphp/Container.php +++ b/src/blankphp/Container.php @@ -217,10 +217,11 @@ public function resolveDepends($params = []): array if (isset($this->classes[$paramClassName])) { $args = $this->classes[$paramClassName]; } else - if ($this->has($paramClassName)) + if ($this->has($paramClassName)) { $args = $this->make($paramClassName); - else + } else { $args = $this->build($paramClassName); + } $paramArr[] = $args; } } @@ -252,7 +253,8 @@ public function call($instance, $method = null, $construct = null, array $param } - public function factory($name,\Closure $closure){ + public function factory($name, \Closure $closure) + { } @@ -288,8 +290,8 @@ public function offsetGet($offset) /** * * 为一个元素的赋值 - * * @param offset - * * @param value + * * @param $offset + * * @param $value * */ public function offsetSet($offset, $value): void { diff --git a/src/blankphp/FileSystem/Driver/DefaultFile.php b/src/blankphp/FileSystem/Driver/DefaultFile.php index 1bae65b..a8d24c4 100644 --- a/src/blankphp/FileSystem/Driver/DefaultFile.php +++ b/src/blankphp/FileSystem/Driver/DefaultFile.php @@ -6,5 +6,16 @@ class DefaultFile { + public function set(){ + + } + + public function write(){ + + } + + public function delete(){ + + } } \ No newline at end of file diff --git a/src/blankphp/Manager/ManagerBase.php b/src/blankphp/Manager/ManagerBase.php index b236c8f..88c33e2 100644 --- a/src/blankphp/Manager/ManagerBase.php +++ b/src/blankphp/Manager/ManagerBase.php @@ -3,32 +3,49 @@ namespace BlankPhp\Manager; +use BlankPhp\Application; + /** * Class ManagerBase * @package BlankPhp\Manager * 管理模式基类 */ -class ManagerBase +abstract class ManagerBase { protected $drivers = []; protected $default = null; - public function __construct() - { + protected $app; + public function __construct(Application $app) + { + $this->app = $app; } - public function createDefaultDriver() - { + public function driver($name = '', $config = []) + { + if (empty($name)) { + return $this->drivers['default'] = $this->createDefaultDriver(); + } + //开始创建对于handler + $method = $this->formatCreateMethod($name); + if (method_exists($this, $method)) { + $driver = $this->{$method}(...$config); + } else { + $name = 'default'; + $driver = $this->createDefaultDriver(); + } + return $this->drivers[$name] = $driver; } - public function driver() + protected function formatCreateMethod($name): string { - + return sprintf('create%dDriver', ucfirst($name)); } + abstract public function createDefaultDriver(); public function clear(): void { diff --git a/src/blankphp/Provider/Provider.php b/src/blankphp/Provider/Provider.php index 6bdfac3..d6d16e6 100644 --- a/src/blankphp/Provider/Provider.php +++ b/src/blankphp/Provider/Provider.php @@ -35,4 +35,8 @@ public function __construct() } } + public function boot(){ + + } + } \ No newline at end of file diff --git a/src/blankphp/Response/Response.php b/src/blankphp/Response/Response.php index 3937236..3267bdd 100644 --- a/src/blankphp/Response/Response.php +++ b/src/blankphp/Response/Response.php @@ -87,9 +87,12 @@ public function setContent($value): void public function send(): void { $this->setHeader(); + ob_start(); echo $this->result; if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); + }else if(1){ + ob_end_flush(); } } @@ -100,8 +103,7 @@ public function prepare(): Response public function isJson($string): bool { - json_decode($string); - return (json_last_error() === JSON_ERROR_NONE); + return strpos($string, '{') === 0; } diff --git a/src/blankphp/Route/Route.php b/src/blankphp/Route/Route.php index 85e25ad..c39c688 100644 --- a/src/blankphp/Route/Route.php +++ b/src/blankphp/Route/Route.php @@ -169,7 +169,7 @@ public function match($request) throw new HttpException("This Route is not allowed [{$method}]", 405); } } - if (count($this->files) === 0) { + if (count($this->files) <= 0) { break; } $this->loadNextFile(); diff --git a/src/blankphp/Session/Session.php b/src/blankphp/Session/Session.php index 9c0c076..0fcac90 100644 --- a/src/blankphp/Session/Session.php +++ b/src/blankphp/Session/Session.php @@ -108,13 +108,13 @@ public function flash($key, $value): void } - public function reFlash(): void + private function reFlash(): void { //重新加入current $this->set('b__next__p', $this->forget('b__current__p')); } - public function clearFlash(): void + private function clearFlash(): void { //清楚flash $this->delete('b__current__p'); From ec8373acc8da1e81badf2e380ddf00d9e5b396d7 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: Sat, 9 May 2020 21:59:50 +0800 Subject: [PATCH 05/15] update some code and create Base Trait and add Manager --- src/blankphp/Application.php | 33 ++++++++---- src/blankphp/Cache/Cache.php | 26 +-------- src/blankphp/Cache/CacheManager.php | 10 +--- src/blankphp/Console/Args.php | 9 ++-- src/blankphp/Container.php | 20 ++++--- src/blankphp/Contract/Container.php | 4 +- src/blankphp/Event/EventAbstract.php | 4 +- src/blankphp/Facade/Cache.php | 2 +- src/blankphp/Manager/ManagerBase.php | 80 +++++++++++++++++++++++----- 9 files changed, 116 insertions(+), 72 deletions(-) diff --git a/src/blankphp/Application.php b/src/blankphp/Application.php index 2543988..6a46b9e 100644 --- a/src/blankphp/Application.php +++ b/src/blankphp/Application.php @@ -59,7 +59,7 @@ protected function __construct() $this->bootstrap(); } - public function bootstrap() + public function bootstrap(): void { if ($this->boot) { return; @@ -70,12 +70,12 @@ public function bootstrap() $this->boot = true; } - public function registerDirName() + public function registerDirName(): void { define('PUBLIC_PATH', APP_PATH . DIRECTORY_SEPARATOR . 'public/'); } - public function registerService() + public function registerService(): void { $temp = [ 'kernel' => [\BlankPhp\Contract\Kernel::class, HttpKernel::class], @@ -102,25 +102,35 @@ public function registerService() unset($temp); } - - public function make($abstract, $parameters = []) + /** + * @param string $abstract + * @param array $parameters + * @return mixed|void|null + */ + public function make(string $abstract, $parameters = []) { if (!$this->has($abstract)) { - if (class_exists($abstract) && !empty($parameters)) { + if (!empty($parameters) && class_exists($abstract)) { return $this->instance($abstract, new $abstract(...$parameters)); } } return parent::make($abstract, $parameters); } - - public function registerBase() + /** + * @return void + */ + public function registerBase(): void { $this->instance('app', $this); static::$instance = $this; } - + /** + * @param $abstract + * @param string $name + * @return array|mixed + */ public function getSignal($abstract, $name = '') { if (empty($name)) { @@ -130,7 +140,10 @@ public function getSignal($abstract, $name = '') return $this->signal[$abstract][$name] ?? []; } - public function unsetSignal($abstract) + /** + * @param $abstract + */ + public function unsetSignal($abstract): void { unset($this->signal[$abstract]); } diff --git a/src/blankphp/Cache/Cache.php b/src/blankphp/Cache/Cache.php index 024f4bb..40b78aa 100644 --- a/src/blankphp/Cache/Cache.php +++ b/src/blankphp/Cache/Cache.php @@ -12,10 +12,6 @@ class Cache * @var Driver */ private $handler; - /** - * @var - */ - private $file; /** * @var array */ @@ -29,11 +25,7 @@ class Cache */ private $getCount = 0; /** - * @var - */ - protected static $dir; - /** - * @var + * @var string */ protected $tag; /** @@ -113,22 +105,6 @@ public function setHandler(Driver $handler): void $this->handler = $handler; } - /** - * @return mixed - */ - public function getFile() - { - return $this->file; - } - - /** - * @param mixed $file - */ - public function setFile($file): void - { - $this->file = $file; - } - /*** * @param $key * @param $value diff --git a/src/blankphp/Cache/CacheManager.php b/src/blankphp/Cache/CacheManager.php index b28050c..e122160 100644 --- a/src/blankphp/Cache/CacheManager.php +++ b/src/blankphp/Cache/CacheManager.php @@ -13,20 +13,14 @@ class CacheManager extends ManagerBase { - use FactoryClientTrait; + private $config; private $tag = 'cache'; - public function __construct() - { - parent::__construct(); - $handler = Driver::factory(config('cache.driver'), $this->tag); - } - public function createDefaultDriver() { - // TODO: Implement createDefaultDriver() method. + return Driver::factory(config('cache.driver'), $this->tag); } } \ No newline at end of file diff --git a/src/blankphp/Console/Args.php b/src/blankphp/Console/Args.php index 798b4a3..66fe906 100644 --- a/src/blankphp/Console/Args.php +++ b/src/blankphp/Console/Args.php @@ -10,7 +10,7 @@ */ class Args { - public static function capture($argv) + public static function capture($argv): array { if (empty($argv)) { return []; @@ -18,15 +18,16 @@ public static function capture($argv) array_shift($argv); $mod = []; foreach ($argv as $item) { - if (strstr($item, ":")) { + if (strpos($item, ':') !== false) { $command = explode(':', $item); - foreach ($command as $value) + foreach ($command as $value) { $mod[] = $value; + } } else { $mod[] = $item; } } unset($argv); - return array_map("trim", array_filter($mod)); + return array_map('trim', array_filter($mod)); } } \ No newline at end of file diff --git a/src/blankphp/Container.php b/src/blankphp/Container.php index 1286ab7..9b04a22 100644 --- a/src/blankphp/Container.php +++ b/src/blankphp/Container.php @@ -79,13 +79,14 @@ protected function getShareObj($abstract) } - public function make($abstract, $parameters = []) + public function make(string $abstract, $parameters = []) { if ($res = $this->getShareObj($abstract)) { return $res; } if (!empty($res = $this->getAlice($abstract))) { + /** @var string $res */ $abstract = $res; } @@ -125,14 +126,21 @@ public function bind($instance, $abstract, $share = false) $this->bindAlice(is_array($instance) ? $instance : [$instance], $abstract); } - - public function bindAlice(array $class, $abstract) + /** + * @param array $class + * @param $abstract + */ + private function bindAlice(array $class, $abstract): void { foreach ($class as $item) { $this->alice[$item] = $abstract; } } + /** + * @param $abstract + * @return mixed|null + */ public function getAlice($abstract) { return $this->alice[$abstract] ?? null; @@ -171,7 +179,7 @@ public function instance($abstract, $instance, $share = false) } - public function notInstantiable($concrete) + public function notInstantiable($concrete): void { throw new \RuntimeException("[$concrete] no Instantiable", 3); } @@ -253,7 +261,7 @@ public function call($instance, $method = null, $construct = null, array $param } - public function factory($name, \Closure $closure) + public function factory($name, \Closure $closure): void { } @@ -271,7 +279,7 @@ public function flush(): void * @param mixed $offset * @return bool */ - public function offsetExists($offset) + public function offsetExists($offset): bool { if ($this->has($offset)) { return true; diff --git a/src/blankphp/Contract/Container.php b/src/blankphp/Contract/Container.php index e618449..50c19e9 100644 --- a/src/blankphp/Contract/Container.php +++ b/src/blankphp/Contract/Container.php @@ -44,11 +44,11 @@ public function signal($abstract, $instance); public function instance($abstract, $instance, $share = false); /** - * @param $abstract + * @param string $abstract * @param array $parameters * @return mixed */ - public function make($abstract, $parameters = []); + public function make(string $abstract, $parameters = []); /** * 通过反射创建某个类 diff --git a/src/blankphp/Event/EventAbstract.php b/src/blankphp/Event/EventAbstract.php index 28f957e..03802a9 100644 --- a/src/blankphp/Event/EventAbstract.php +++ b/src/blankphp/Event/EventAbstract.php @@ -20,8 +20,8 @@ public static function observe(Observer $observer): void self::$observes = $observer; } - /* - * 解除绑定 + /** + * @param $name */ public function deobserve($name): void { diff --git a/src/blankphp/Facade/Cache.php b/src/blankphp/Facade/Cache.php index 03ee69e..4a92f94 100644 --- a/src/blankphp/Facade/Cache.php +++ b/src/blankphp/Facade/Cache.php @@ -8,7 +8,7 @@ class Cache extends Facade { - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { return 'cache'; } diff --git a/src/blankphp/Manager/ManagerBase.php b/src/blankphp/Manager/ManagerBase.php index 88c33e2..f6d8313 100644 --- a/src/blankphp/Manager/ManagerBase.php +++ b/src/blankphp/Manager/ManagerBase.php @@ -4,6 +4,7 @@ namespace BlankPhp\Manager; use BlankPhp\Application; +use BlankPhp\Exception\Method\NotFoundMethodException; /** * Class ManagerBase @@ -14,41 +15,92 @@ abstract class ManagerBase { protected $drivers = []; - protected $default = null; + protected $initCreateDefaultDriver = false; + + protected const FORMAT_METHOD = 'create%sDriver'; protected $app; + /** + * ManagerBase constructor. + * @param Application $app + */ public function __construct(Application $app) { $this->app = $app; + if ($this->initCreateDefaultDriver) { + $this->default = $this->createDefaultDriver(); + } } - - public function driver($name = '', $config = []) + /** + * @param string $name + * @return mixed|void|null + */ + public function driver(string $name = null) { - if (empty($name)) { - return $this->drivers['default'] = $this->createDefaultDriver(); + $name = $name ?? $this->getDefaultName(); + + if (isset($this->drivers[$name])) { + return $this->drivers[$name]; } - //开始创建对于handler $method = $this->formatCreateMethod($name); if (method_exists($this, $method)) { - $driver = $this->{$method}(...$config); - } else { - $name = 'default'; - $driver = $this->createDefaultDriver(); + $driver = $this->{$method}(); + return $this->drivers[$name] = $driver; } - return $this->drivers[$name] = $driver; + + return $this->drivers[$name] = $this->app->make($name); } - protected function formatCreateMethod($name): string + /** + * @param string $name + */ + public function forgetDriver($name = ''): void { - return sprintf('create%dDriver', ucfirst($name)); + unset($this->drivers[$name]); } - abstract public function createDefaultDriver(); + /** + * @param $name + * @return string + */ + protected function formatCreateMethod($name): string + { + return sprintf(self::FORMAT_METHOD, ucfirst($name)); + } + /** + * @return void + */ public function clear(): void { $this->drivers = []; + $this->default = null; } + + /** + * @param $name + * @param $arguments + */ + public function __call($name, $arguments) + { + if (empty($this->default)) { + $this->createDefaultDriver(); + } + return $this->driver()->{$name}(...$arguments); + } + + /** + * @return string + */ + protected function getDefaultName(): string + { + return 'default'; + } + + /** + * @return mixed + */ + abstract public function createDefaultDriver(); } \ No newline at end of file From c1027008fc5ac008e779f601b5d5e2dece5f5c1c Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年5月10日 14:15:38 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84=E5=92=8C?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=BB=84=E4=BB=B6Traits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 ++- .../Base/Traits/FactoryClientTrait.php | 15 +++++++-- src/blankphp/Cache/CacheManager.php | 4 +-- src/blankphp/Driver/FileDriver.php | 33 ++++++++++++------- src/blankphp/Driver/RedisDriver.php | 6 ++-- src/blankphp/Factory/FactoryBase.php | 6 +--- src/helpers/Arr.php | 7 +++- src/helpers/File.php | 19 ++++------- src/helpers/GD.php | 8 ++--- src/helpers/Re.php | 9 +++++ src/helpers/Str.php | 4 +++ src/helpers/helper.php | 10 +++--- 12 files changed, 75 insertions(+), 50 deletions(-) diff --git a/composer.json b/composer.json index 98a0433..b216ff7 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,8 @@ "psr/log": "^1.1", "predis/predis": "^1.1", "ext-curl": "*", - "ext-json": "*" + "ext-json": "*", + "ext-gd": "*", + "ext-openssl": "*" } } diff --git a/src/blankphp/Base/Traits/FactoryClientTrait.php b/src/blankphp/Base/Traits/FactoryClientTrait.php index 4f65481..f0e0ab9 100644 --- a/src/blankphp/Base/Traits/FactoryClientTrait.php +++ b/src/blankphp/Base/Traits/FactoryClientTrait.php @@ -4,17 +4,26 @@ namespace BlankPhp\Base\Traits; +use BlankPhp\Driver\DriverFactory; +use BlankPhp\Facade\Driver; + trait FactoryClientTrait { + /** + * @var DriverFactory + */ private $factory; - private function createFromFactory() + private function createFromFactory(string $name, string $tag = 'default') { - + return $this->getFactory()->factory($name, $tag); } private function getFactory() { - + if (empty($this->factory)) { + $this->factory = Driver::getFromApp(); + } + return $this->factory; } } \ No newline at end of file diff --git a/src/blankphp/Cache/CacheManager.php b/src/blankphp/Cache/CacheManager.php index e122160..eecae62 100644 --- a/src/blankphp/Cache/CacheManager.php +++ b/src/blankphp/Cache/CacheManager.php @@ -15,12 +15,10 @@ class CacheManager extends ManagerBase { use FactoryClientTrait; - private $config; private $tag = 'cache'; - public function createDefaultDriver() { - return Driver::factory(config('cache.driver'), $this->tag); + return $this->createFromFactory(config('cache.driver'), $this->tag); } } \ No newline at end of file diff --git a/src/blankphp/Driver/FileDriver.php b/src/blankphp/Driver/FileDriver.php index 9b8bbf0..d85d06b 100644 --- a/src/blankphp/Driver/FileDriver.php +++ b/src/blankphp/Driver/FileDriver.php @@ -10,9 +10,12 @@ class FileDriver extends Driver public static $key; protected static $cacheTime = 0; + private static $dir; protected $config = []; + protected $data; + protected $option; - public function __construct($name = "default", $option = []) + public function __construct($name = 'default', $option = []) { $this->config = array_merge($this->config, $option); } @@ -28,13 +31,13 @@ public function getData() /** * @param mixed $data */ - public function setData($data) + public function setData($data): void { $this->data = $data; } - public function getFromFile($file) + public function getFromFile($file): void { //加载 $data = []; @@ -44,23 +47,28 @@ public function getFromFile($file) } - public function canRebuild($file, $descFile) + public function canRebuild($file, $descFile): bool { return filemtime($file) - filemtime(self::$dir . $descFile) < self::$cacheTime; } - public function build($key) + public function build($key): string { return $this->option['tag'] . $key; } + /** + * @param $key + * @param $value + * @param null $ttl + */ public function set($key, $value, $ttl = null) { $value = ["value" => $value, "ttl" => time() + $ttl]; - \BlankQwq\Helpers\File::put($this->build($key), $value); + return \BlankQwq\Helpers\File::put($this->build($key), $value); } - public function remember($key, \Closure $closure, $ttl = null) + public function remember($key, \Closure $closure, $ttl = null): string { $this->build($key); if ($this->has($key)) @@ -71,16 +79,17 @@ public function remember($key, \Closure $closure, $ttl = null) public function has($key) { - if (isset($this->data[$key])) + if (isset($this->data[$key])) { return true; - else - return false; + } + return false; } - public function get($key, $default = '') + public function get($key, $default = ''): string { - if ($this->has($key)) + if ($this->has($key)) { return $this->data[$key]; + } return $default; } diff --git a/src/blankphp/Driver/RedisDriver.php b/src/blankphp/Driver/RedisDriver.php index 37c3f06..33a288e 100644 --- a/src/blankphp/Driver/RedisDriver.php +++ b/src/blankphp/Driver/RedisDriver.php @@ -17,7 +17,7 @@ class RedisDriver extends Driver private $redis; protected static $instance; - public function __construct($name = "default", $option = []) + public function __construct($name = 'default', $option = []) { $this->option = empty($option) ? $this->option : $option; $app = Application::getInstance(); @@ -35,9 +35,9 @@ public function set($key, $value, $ttl = null) { if ($ttl !== null) { return $this->redis->set($key, $this->parseValue($value), 'EX', $ttl); - } else { - return $this->redis->set($key, $this->parseValue($value)); } + + return $this->redis->set($key, $this->parseValue($value)); } public function delete($key) diff --git a/src/blankphp/Factory/FactoryBase.php b/src/blankphp/Factory/FactoryBase.php index ef81cc1..1672dc5 100644 --- a/src/blankphp/Factory/FactoryBase.php +++ b/src/blankphp/Factory/FactoryBase.php @@ -17,8 +17,6 @@ class FactoryBase //驱动实例 protected $instance = []; - //绑定此时driver - public function __construct($config = [], $project = []) { $this->setDrivers($project); @@ -46,6 +44,7 @@ public function factory($name, $nickName = 'default', $register = false) $realName = $name; //解析名称 [$name, $config] = $this->parseName($name); + //如果app中有,那么直接返回app中的 if (isset($this->instance[$realName]) && !empty($res = $this->instance[$realName])) { return $res; @@ -104,9 +103,6 @@ public function getConfig($key, $default = '') foreach ($key as $k) { if (isset($value[$k])) { $value = $value[$k]; - } else { - $value = []; - break; } } return $value; diff --git a/src/helpers/Arr.php b/src/helpers/Arr.php index df7a281..66a2f4d 100644 --- a/src/helpers/Arr.php +++ b/src/helpers/Arr.php @@ -4,7 +4,12 @@ class Arr { - public static function merge($first, $second) + /** + * @param $first + * @param $second + * @return array + */ + public static function merge($first, $second): array { return array_merge($first, $second); } diff --git a/src/helpers/File.php b/src/helpers/File.php index 67e65e1..e9aea3a 100644 --- a/src/helpers/File.php +++ b/src/helpers/File.php @@ -10,10 +10,8 @@ public static function put($fileName, $data) //写入文件 $dir = dirname($fileName); //判断目录是否存在 - if (!is_dir($dir)) { - if (!mkdir($dir, 0777, true) && !is_dir($dir)) { - throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir)); - } + if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir)); } //写入文件 return file_put_contents($fileName, $data); @@ -24,28 +22,25 @@ public static function append($fileName, $data) //写入文件 $dir = dirname($fileName); //判断目录是否存在 - if (!is_dir($dir)) { - if (!mkdir($dir, 0777, true) && !is_dir($dir)) { - throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir)); - } + if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir)); } //写入文件 return file_put_contents($fileName, $data, FILE_APPEND); } //读取文件 - public static function get($name, $type = 'text') + public static function get($name, $type = 'text'): void { } - public static function delete($fileName) + public static function delete($fileName): bool { - //删除 return unlink($fileName); } - public static function putCache(\BlankPhp\Route\RuleCollection $routes, string $string) + public static function putCache(\BlankPhp\Route\RuleCollection $routes, string $string): void { } diff --git a/src/helpers/GD.php b/src/helpers/GD.php index f9c6b22..f511463 100644 --- a/src/helpers/GD.php +++ b/src/helpers/GD.php @@ -43,22 +43,18 @@ public static function captcha($stringLen = 4, $pointNum = 50, $linNum = 4, $fil $colorString = imagecolorallocate($img, random_int(10, 100), random_int(10, 100), random_int(10, 100));//文本 imagestring($img, 5, random_int(0, $width - 36), random_int(0, $height - 15), $string, $colorString); -//输出图片到浏览器 header(Response::$header['image']); imagejpeg($img, $fileName); -//销毁,释放资源 imagedestroy($img); return $string; } - public - static function image() + public static function image(): void { } - public - static function watermark() + public static function watermark(): void { } diff --git a/src/helpers/Re.php b/src/helpers/Re.php index 3d09992..0671e70 100644 --- a/src/helpers/Re.php +++ b/src/helpers/Re.php @@ -9,4 +9,13 @@ class Re { + public function find($pattern, $subject): array + { + return []; + } + + public function findAll($pattern, $subject): array + { + return []; + } } \ No newline at end of file diff --git a/src/helpers/Str.php b/src/helpers/Str.php index 3d74731..95da5c6 100644 --- a/src/helpers/Str.php +++ b/src/helpers/Str.php @@ -22,6 +22,10 @@ public static function random($length, $str = null) $str = empty($str) ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.+?' : $str; return substr(str_shuffle($str), random_int(0, strlen($str) - $length), $length); + } + public function studly(string $str): string + { + return $str; } } \ No newline at end of file diff --git a/src/helpers/helper.php b/src/helpers/helper.php index 36da691..c6b7a58 100644 --- a/src/helpers/helper.php +++ b/src/helpers/helper.php @@ -3,10 +3,11 @@ function app($abstract) { $a = \BlankPhp\Application::getInstance(); - if ($a->has($abstract)) + if ($a->has($abstract)) { return $a->make($abstract); - else - return $a->getSignal($abstract); + } + + return $a->getSignal($abstract); } } @@ -51,8 +52,9 @@ function real_path($path, $ff = null) { $static = config('app.static'); $url = APP_PATH . '/' . $static . '/' . $path; - if ($ff) + if ($ff) { $url = str_replace("\\", "/", $url); + } return $url; } } From 278a751ea4c3cd4f6a2c9b1e8a2e2fc23cad904d Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年5月10日 14:39:05 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E3=80=90=E5=BC=80=E5=8F=91=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DSession=E7=BB=84=E4=BB=B6=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- .../Base/Traits/FactoryClientTrait.php | 4 +- src/blankphp/Cache/Cache.php | 8 +- .../FileSystem/Driver/DefaultFile.php | 9 ++- src/blankphp/Session/Session.php | 76 ++++++++++++++----- src/blankphp/Validate/ValidateBase.php | 16 ++-- src/helpers/Arr.php | 5 +- src/helpers/Curl.php | 4 +- 8 files changed, 84 insertions(+), 41 deletions(-) diff --git a/composer.json b/composer.json index b216ff7..a3cdc27 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "ext-curl": "*", "ext-json": "*", "ext-gd": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "ext-mbstring": "*" } } diff --git a/src/blankphp/Base/Traits/FactoryClientTrait.php b/src/blankphp/Base/Traits/FactoryClientTrait.php index f0e0ab9..d7d59d2 100644 --- a/src/blankphp/Base/Traits/FactoryClientTrait.php +++ b/src/blankphp/Base/Traits/FactoryClientTrait.php @@ -14,9 +14,9 @@ trait FactoryClientTrait */ private $factory; - private function createFromFactory(string $name, string $tag = 'default') + private function createFromFactory($name, $nickName = 'default', $register = false) { - return $this->getFactory()->factory($name, $tag); + return $this->getFactory()->factory($name, $nickName, $register); } private function getFactory() diff --git a/src/blankphp/Cache/Cache.php b/src/blankphp/Cache/Cache.php index 40b78aa..9eb44bd 100644 --- a/src/blankphp/Cache/Cache.php +++ b/src/blankphp/Cache/Cache.php @@ -113,7 +113,7 @@ public function setHandler(Driver $handler): void */ public function set($key, $value, $ttl = null) { - return $this->handler->set($key, $value, $ttl); + return $this->getHandler()->set($key, $value, $ttl); } /** @@ -123,7 +123,7 @@ public function set($key, $value, $ttl = null) */ public function get($key, $default = null) { - return $this->handler->get($key, $default); + return $this->getHandler()->get($key, $default); } /** @@ -133,7 +133,7 @@ public function get($key, $default = null) */ public function remember(string $key, \Closure $closure) { - return $this->handler->remember($key, $closure()); + return $this->getHandler()->remember($key, $closure()); } /** @@ -142,7 +142,7 @@ public function remember(string $key, \Closure $closure) */ public function has($key) { - return $this->handler->has($key); + return $this->getHandler()->has($key); } /** diff --git a/src/blankphp/FileSystem/Driver/DefaultFile.php b/src/blankphp/FileSystem/Driver/DefaultFile.php index a8d24c4..7d66e1d 100644 --- a/src/blankphp/FileSystem/Driver/DefaultFile.php +++ b/src/blankphp/FileSystem/Driver/DefaultFile.php @@ -6,15 +6,18 @@ class DefaultFile { - public function set(){ + public function set(): void + { } - public function write(){ + public function write(): void + { } - public function delete(){ + public function delete(): void + { } diff --git a/src/blankphp/Session/Session.php b/src/blankphp/Session/Session.php index 0fcac90..e1f68bc 100644 --- a/src/blankphp/Session/Session.php +++ b/src/blankphp/Session/Session.php @@ -10,13 +10,17 @@ use BlankPhp\Application; +use BlankPhp\Base\Traits\FactoryClientTrait; use BlankPhp\Contract\Session as SessionContract; use BlankPhp\Facade\Cookie; use BlankPhp\Facade\Driver; +use BlankQwq\Helpers\Arr; use BlankQwq\Helpers\Str; class Session implements SessionContract { + use FactoryClientTrait; + protected static $sessionName = '_session'; protected $handler; @@ -29,21 +33,27 @@ class Session implements SessionContract //过期时间 protected $expire = 35000; + /** + * Session constructor. + */ public function __construct() { $config = config('app.session'); static::$sessionName = $config['name']; $this->expire = $config['expire']; - $this->handler = Driver::factory($config['driver'], 'session', true); + $this->handler = $this->createFromFactory($config['driver'], 'session', true); session_set_save_handler($this->handler, true); } + /** + * @return false|string + */ public function generate() { return Str::random(40); } - public function start() + public function start(): void { $this->isLegal(); if ($this->generate) { @@ -51,13 +61,20 @@ public function start() } } - public function reGenerate() + + /** + * @return void + */ + private function reGenerate(): void { $this->setId($this->generate()); $this->setCookie(); } - public function isLegal() + /** + * @return void + */ + private function isLegal(): void { $id = Cookie::get(static::$sessionName); if (!empty($id) && ($data = $this->handler->read($id)) !== null) { @@ -67,41 +84,60 @@ public function isLegal() } } - public function get($key, $default = "") + /** + * @param $key + * @param string $default + * @return mixed|string + */ + public function get($key, $default = '') { return $this->data[$key] ?? $default; } - public function getFlash($key, $default = "") + /** + * @param $key + * @param string $default + * @return mixed|string + */ + public function getFlash($key, $default = '') { return $this->data['b__current__p'][$key] ?? $default; } - public function setId($id): void + /** + * @param $id + */ + private function setId($id): void { $this->id = $id; } + /** + * @param $data + */ public function setData($data): void { - if (!empty($this->data)) { - $this->data = array_merge($this->data, $data); - } else { - $this->data = $data; - } + $this->data = !empty($this->data) ? Arr::merge($data, $this->data, false) : $data; } - public function setCookie(): void + private function setCookie(): void { Cookie::set(static::$sessionName, $this->id, $this->expire); } + /** + * @param $key + * @param $value + */ public function set($key, $value): void { - $this->setData([$key => $value]); + $this->setData([(string)$key => $value]); } - //下一次请求有效 + /** + * @param $key + * @param $value + */ public function flash($key, $value): void { $this->push('b__next__p', [$key => $value]); @@ -140,7 +176,7 @@ public function forget($key) return $value; } - public function push($key, $value = []) + public function push($key, $value = []): void { if (!isset($this->data[$key]) || $this->data[$key] === null) { $this->data[$key] = []; @@ -151,22 +187,20 @@ public function push($key, $value = []) private function save() { - $this->handler->write($this->id, $this->data); + return $this->handler->write($this->id, $this->data); } - public function end() + public function end(): void { $this->clearFlash(); $this->save(); } //清理内容 - public function flush() + public function flush(): void { $this->data = []; - //清理内容 $this->handler->destroy($this->id); - //重新生成 $this->reGenerate(); } diff --git a/src/blankphp/Validate/ValidateBase.php b/src/blankphp/Validate/ValidateBase.php index 014d5c2..70a8386 100644 --- a/src/blankphp/Validate/ValidateBase.php +++ b/src/blankphp/Validate/ValidateBase.php @@ -4,27 +4,31 @@ namespace BlankPhp\Validate; +use BlankPhp\Contract\Request; + class ValidateBase { - protected $validataRule = []; - public static $rule=[ + protected $validateRule = []; + public static $rule = [ ]; - public function validate(Request $request,array $array,array $message=[]){ + public function validate(Request $request, array $array, array $message = []): void + { } - public function add(){ + public function add(): void + { } - public function replace(){ + public function replace(): void + { } - } \ No newline at end of file diff --git a/src/helpers/Arr.php b/src/helpers/Arr.php index 66a2f4d..26a2a42 100644 --- a/src/helpers/Arr.php +++ b/src/helpers/Arr.php @@ -7,11 +7,12 @@ class Arr /** * @param $first * @param $second + * @param bool $func * @return array */ - public static function merge($first, $second): array + public static function merge($first, $second, $func = true): array { - return array_merge($first, $second); + return $func?array_merge($first, $second): $first+ $second; } } \ No newline at end of file diff --git a/src/helpers/Curl.php b/src/helpers/Curl.php index c69da30..4b499ad 100644 --- a/src/helpers/Curl.php +++ b/src/helpers/Curl.php @@ -57,13 +57,13 @@ public static function post($url, $https = false, $data = [], $option = []) } - public static function request() + public static function request(): void { } - public static function getFile() + public static function getFile(): void { } From 2b0dee5c7989daccfa36a1bb74e1e98309e35583 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年5月11日 16:57:11 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5=E5=92=8C=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- composer.json | 3 +- src/blankphp/Application.php | 25 +++- src/blankphp/Connect/Connect.php | 14 +++ src/blankphp/Container.php | 22 +++- src/blankphp/Database/Database.php | 109 +++++++++++------- src/blankphp/Database/DbConnect.php | 19 ++- .../Exception/ConnectErrException.php | 12 ++ src/blankphp/Driver/RedisDriver.php | 43 +++++-- src/blankphp/Kernel/HttpKernel.php | 9 ++ src/blankphp/Response/Response.php | 15 +-- 11 files changed, 203 insertions(+), 70 deletions(-) create mode 100644 src/blankphp/Connect/Connect.php create mode 100644 src/blankphp/Database/Exception/ConnectErrException.php diff --git a/README.md b/README.md index 2e8cffd..22dfcfc 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ $ composer require blankqwq/framework -vvv - [ ] 国际化 - [ ] 国际化支持 - [ ] 安全 - - [ ] 验证码 + - [x] 验证码 - [ ] 加密 - [ ] xss - [x] 防注入 diff --git a/composer.json b/composer.json index a3cdc27..2a33e44 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "ext-json": "*", "ext-gd": "*", "ext-openssl": "*", - "ext-mbstring": "*" + "ext-mbstring": "*", + "ext-pdo": "*" } } diff --git a/src/blankphp/Application.php b/src/blankphp/Application.php index 6a46b9e..ef2333a 100644 --- a/src/blankphp/Application.php +++ b/src/blankphp/Application.php @@ -12,6 +12,7 @@ use BlankPhp\Cache\CacheManager; use BlankPhp\Config\Config; use BlankPhp\Config\LoadConfig; +use BlankPhp\Connect\Connect; use BlankPhp\Contract\CookieContract; use BlankPhp\Cookie\Cookie; use BlankPhp\Database\Database; @@ -38,7 +39,9 @@ class Application extends Container private $version = '0.1.3-dev'; - protected $boot = false; + private $boot = false; + + private $connect = []; protected $bootstraps = [ LoadConfig::class => 'load', @@ -109,6 +112,7 @@ public function registerService(): void */ public function make(string $abstract, $parameters = []) { + //需要比较一下hash if (!$this->has($abstract)) { if (!empty($parameters) && class_exists($abstract)) { return $this->instance($abstract, new $abstract(...$parameters)); @@ -117,6 +121,19 @@ public function make(string $abstract, $parameters = []) return parent::make($abstract, $parameters); } + public function pushConnect($conn): void + { + $this->connects[] = $conn; + } + + protected function disconnects(): void + { + foreach ($this->connects as $item) { + /** @var Connect $item */ + $item->disconnect(); + } + } + /** * @return void */ @@ -148,6 +165,12 @@ public function unsetSignal($abstract): void unset($this->signal[$abstract]); } + public function flush(): void + { + parent::flush(); // TODO: Change the autogenerated stub + $this->disconnects(); + } + } diff --git a/src/blankphp/Connect/Connect.php b/src/blankphp/Connect/Connect.php new file mode 100644 index 0000000..6f58e84 --- /dev/null +++ b/src/blankphp/Connect/Connect.php @@ -0,0 +1,14 @@ +beforeBuild($concrete); try { $reflector = new \ReflectionClass($concrete); } catch (\ReflectionException $e) { throw new \RuntimeException("reflection [$concrete] error"); } if (!$reflector->isInstantiable()) { - return $this->notInstantiable($concrete); + $this->notInstantiable($concrete); } $constructor = $reflector->getConstructor(); if ($constructor === null) { @@ -205,10 +212,23 @@ public function build($concrete) return new $concrete(); } $paramsArray = $this->resolveDepends($constructor->getParameters()); + $this->afterBuild($paramsArray); return $reflector->newInstanceArgs($paramsArray); } } + + + protected function beforeBuild($concrete): void + { + + } + + protected function afterBuild($array): void + { + + } + /** * @param array $params * @return array diff --git a/src/blankphp/Database/Database.php b/src/blankphp/Database/Database.php index 267b752..32ebdda 100644 --- a/src/blankphp/Database/Database.php +++ b/src/blankphp/Database/Database.php @@ -10,6 +10,7 @@ use BlankPhp\Application; +use BlankPhp\Connect\Connect; use BlankPhp\Database\Query\Builder; use BlankPhp\Database\Query\Raw; use BlankPhp\Database\Traits\DBFunction; @@ -18,22 +19,42 @@ use BlankPhp\Facade\Log; use BlankQwq\Helpers\Str; -class Database +class Database implements Connect { use DBFunction, DBJoin; - private static $pdo = null; + + /** + * @var \PDO + */ + private $pdo = null; + /** + * @var Builder + */ protected $sql; + /** + * @var string + */ protected $id = 'default'; + /** + * @var Collection + */ protected $collection; + + /** + * @var \PDOStatement + */ protected $PDOsmt; - protected $driver; + + /** + * @var array + */ protected $config; public function __construct(Builder $sql, $config = []) { $this->sql = $sql; - self::$pdo = $this->connectFactory(); + $this->pdo = $this->connectFactory(); $driver = config('db.default'); $this->sql->engine($driver); if (!empty($config)) { @@ -43,7 +64,7 @@ public function __construct(Builder $sql, $config = []) } } - public function connectFactory() + private function connectFactory(): ?\Generator { yield; yield DbConnect::getPdo($this->config); @@ -63,7 +84,7 @@ public function errorInfo() * @param string $sql * 执行查询语句 */ - public function query($sql = '') + public function query($sql = ''): void { //执行sql //返回集合 @@ -73,7 +94,7 @@ public function query($sql = '') * @param string $sql * 返回行数目 */ - public function execute($sql = '') + public function execute($sql = ''): void { } @@ -83,7 +104,7 @@ public function execute($sql = '') * @param $table * @return $this */ - public function table($table) + public function table($table): self { $this->sql->from($table); return $this; @@ -95,25 +116,26 @@ public function update(array $values = []) return $this->commit()->rowCount(); } - protected function beginTransaction() + protected function beginTransaction(): void { $this->connect(); - self::$pdo->beginTransaction(); + $this->pdo->beginTransaction(); } - protected function commitTransaction() + protected function commitTransaction(): void { $this->connect(); - self::$pdo->commit(); + $this->pdo->commit(); } - protected function rollBack() + + protected function rollBack(): void { $this->connect(); - self::$pdo->rollBack(); + $this->pdo->rollBack(); } - final function transaction(\Closure $closure) + final public function transaction(\Closure $closure): void { try { $this->beginTransaction(); @@ -124,21 +146,23 @@ final function transaction(\Closure $closure) } } - public function raw($string) + public function raw($string): Raw { - $raw = new Raw($string); - return $raw; + return new Raw($string); } - public function connect() + /** + * @return void + */ + public function connect(): void { - if (self::$pdo instanceof \Generator) { - self::$pdo->next(); - self::$pdo = self::$pdo->current(); + if ($this->pdo instanceof \Generator) { + $this->pdo->next(); + $this->pdo = $this->pdo->current(); } } - public function all() + public function all(): void { @@ -153,7 +177,7 @@ public function commit() public function _commit() { - $smt = self::$pdo->prepare($this->sql->toSql()); + $smt = $this->pdo->prepare($this->sql->toSql()); $this->PDOsmt = $smt; $procedure = in_array(substr($smt->queryString, 0, 4), ['exec', 'call']); if ($procedure) { @@ -166,7 +190,10 @@ public function _commit() return $smt; } - public function get() + /** + * @return Collection + */ + public function get(): Collection { $result = $this->commit(); //这样只有单一的数据,需要重复的创建然后保存到一个大collection @@ -190,7 +217,7 @@ public function delete() $args = func_get_args(); foreach ($args as $arg) { if (is_array($arg)) { - $this->sql->deleteSome($id = null, $arg); + $this->sql->deleteSome($arg); } elseif (is_numeric($arg)) { $this->sql->deleteSome($arg); } @@ -213,30 +240,25 @@ public function first() { $result = $this->commit(); //这样只有单一的数据,需要重复的创建然后保存到一个大collection - $data = $result->fetchObject(Collection::class); - return $data; + return $result->fetchObject(Collection::class); } - public function limit() + public function limit(): Database { $args = func_get_args(); $count = count($args); - if ($count> 2) + if ($count> 2) { throw new \Exception('错误的范围'); - elseif ($count == 1) + } + if ($count === 1) { $this->sql->limit([0, $args[0]]); - elseif ($count == 2) + } elseif ($count === 2) { $this->sql->limit($args); + } return $this; } - public function __set($name, $value) - { - //修改或者创建某个表中的元素..得判断有没有获取到目标id - - } - public function __call($name, $arguments) { @@ -257,7 +279,7 @@ public function bindValues(array $values = []): void $b = is_numeric($k) ? ++$i : $k; if (is_int($item)) { $this->PDOsmt->bindValue($b, $item, \PDO::PARAM_INT); - } elseif (is_null($item)) { + } elseif ($item === null) { $this->PDOsmt->bindValue($b, $item, \PDO::PARAM_NULL); } else { $this->PDOsmt->bindValue($b, (string)$item, \PDO::PARAM_STR); @@ -267,7 +289,7 @@ public function bindValues(array $values = []): void } } - public function bindCall(array $values) + public function bindCall(array $values): void { if ($this->PDOsmt === null) { throw new Exception('异常错误'); @@ -282,4 +304,11 @@ public function bindCall(array $values) } } + public function disconnect(): void + { + } + + public function reconnect(): void + { + } } \ No newline at end of file diff --git a/src/blankphp/Database/DbConnect.php b/src/blankphp/Database/DbConnect.php index 633c3c6..9cc7904 100644 --- a/src/blankphp/Database/DbConnect.php +++ b/src/blankphp/Database/DbConnect.php @@ -4,6 +4,8 @@ namespace BlankPhp\Database; +use BlankPhp\Database\Exception\ConnectErrException; + class DbConnect { private static $pdo; @@ -17,6 +19,7 @@ public static function pdo(array $db = []) try { $dsn = sprintf('%s:host=%s;dbname=%s;charset=%s', $db['driver'], $db['host'], $db['database'], $db['charset']); + $dbEngine = ''; $option = array(\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC); self::$pdo = new \PDO($dsn, $db['username'], $db['password'], $option); self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); @@ -25,26 +28,22 @@ public static function pdo(array $db = []) false ); if (empty($dbEngine)) { - $dbEngine = (string) self::$pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $dbEngine = (string)self::$pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); } return self::$pdo; } catch (\PDOException $e) { - exit($e->getMessage()); + throw new ConnectErrException($e->getMessage()); } } - public static function getPdo($array = []) + public static function getPdo($array = []): \PDO { - if (empty($array)) + if (empty($array)) { return self::$pdo; - else - return self::pdo($array); + } + return self::pdo($array); } - public function setConfig() - { - - } } \ No newline at end of file diff --git a/src/blankphp/Database/Exception/ConnectErrException.php b/src/blankphp/Database/Exception/ConnectErrException.php new file mode 100644 index 0000000..127f78b --- /dev/null +++ b/src/blankphp/Database/Exception/ConnectErrException.php @@ -0,0 +1,12 @@ +option = empty($option) ? $this->option : $option; - $app = Application::getInstance(); try { //初始化连接 - $this->redis = $app->make(Client::class, [$this->option]); - $app->instance('redis.connect.' . $name, $this->redis); + $this->connect($name); } catch (ConnectionException $exception) { throw new RedisConnectException($exception->getMessage()); } @@ -40,7 +43,7 @@ public function set($key, $value, $ttl = null) return $this->redis->set($key, $this->parseValue($value)); } - public function delete($key) + public function delete($key): int { return $this->redis->del($key); } @@ -63,9 +66,9 @@ public function remember($array, \Closure $closure, $ttl = 0) return $value; } - public function has($key) + public function has($key): int { - return $this->redis->exist($key); + return $this->redis->exists($key); } public function flush() @@ -81,4 +84,26 @@ public function forget($key) } + public function disconnect(): void + { + $this->redis->disconnect(); + } + + public function connect($name = 'default'): Client + { + /** @var \BlankPhp\Application $app */ + $app = Application::getInstance(); + $this->redis = $app->make(Client::class, [$this->option]); + $app->instance('redis.connect.' . $name, $this->redis); + $app->pushConnect($this->redis); + return $this->redis; + } + + public function reconnect(): void + { + if (!empty($this->redis)) { + $this->redis->connect(); + } + $this->connect(); + } } \ No newline at end of file diff --git a/src/blankphp/Kernel/HttpKernel.php b/src/blankphp/Kernel/HttpKernel.php index 1cb2bb2..d08995c 100644 --- a/src/blankphp/Kernel/HttpKernel.php +++ b/src/blankphp/Kernel/HttpKernel.php @@ -19,8 +19,17 @@ class HttpKernel { + /** + * @var array + */ protected $config = []; + /** + * @var Application + */ protected $app; + /** + * @var mixed|void|null + */ protected $route; public function startConfig($config): void diff --git a/src/blankphp/Response/Response.php b/src/blankphp/Response/Response.php index 3267bdd..8b45b44 100644 --- a/src/blankphp/Response/Response.php +++ b/src/blankphp/Response/Response.php @@ -15,12 +15,16 @@ class Response { use ResponseType; + /** + * @var string + */ protected $result = ''; - + /** + * @var array + */ protected $headerStack = []; - public function __construct($result) { $this->result = (string)$result; @@ -29,14 +33,12 @@ public function __construct($result) } else { $this->setType(self::$header['html']); } - dump($this->headerStack); } - /** * @return array */ - public function getHeaderStack() + public function getHeaderStack(): array { return $this->headerStack; } @@ -91,9 +93,8 @@ public function send(): void echo $this->result; if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); - }else if(1){ - ob_end_flush(); } + ob_end_flush(); } public function prepare(): Response From f06c06f73494156ec396cc0959adfa91a10d9442 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年5月11日 21:40:24 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9F=BA=E6=9C=AC=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/blankphp/Container.php | 33 +++++++-- src/blankphp/Event/Observer.php | 15 ++-- src/blankphp/Exception/Error.php | 2 +- src/blankphp/Exception/Handler.php | 16 +++-- src/blankphp/Kernel/HttpKernel.php | 14 ++-- src/blankphp/Model/Model.php | 32 ++++++--- src/blankphp/PipeLine/PipeLine.php | 12 ++++ src/blankphp/Request/Request.php | 70 ++++++++++++++++--- src/blankphp/Request/TestRequest.php | 3 +- src/blankphp/Route/Route.php | 14 ++-- src/blankphp/Route/Router.php | 2 +- .../Route/Traits/ResolveSomeDepends.php | 11 +-- src/blankphp/Route/Traits/SetMiddleWare.php | 34 +++++---- src/blankphp/Scheme/Scheme.php | 4 +- src/blankphp/Session/Session.php | 25 +++++-- src/helpers/helper.php | 2 +- 16 files changed, 213 insertions(+), 76 deletions(-) diff --git a/src/blankphp/Container.php b/src/blankphp/Container.php index 1c08d8d..7792d17 100644 --- a/src/blankphp/Container.php +++ b/src/blankphp/Container.php @@ -66,6 +66,11 @@ class Container implements \ArrayAccess, ContainerContract */ protected $connects = []; + /** + * @var array + */ + protected $cache = []; + /** * 单例模式 @@ -95,11 +100,10 @@ public function make(string $abstract, $parameters = []) /** @var string $res */ $abstract = $res; } - if (isset($this->instances[$abstract])) { return $this->instances[$abstract]; } - $class = $this->binds[$abstract]['concert']; + $class = !empty($this->binds[$abstract]) ? $this->binds[$abstract]['concert'] : $abstract; return (empty($parameters)) ? $this->instance($abstract, $this->build($class)) : $this->instance($abstract, new $class(...$parameters)); } @@ -218,7 +222,6 @@ public function build($concrete) } - protected function beforeBuild($concrete): void { @@ -244,12 +247,18 @@ public function resolveDepends($params = []): array // 获得参数类型 if (isset($this->classes[$paramClassName])) { $args = $this->classes[$paramClassName]; - } else + } else { + if ($args = $this->getCache($paramClassName)) { + $paramArr[] = $args; + continue; + } if ($this->has($paramClassName)) { $args = $this->make($paramClassName); } else { $args = $this->build($paramClassName); } + } + $this->putCache($paramClassName, $args); $paramArr[] = $args; } } @@ -257,6 +266,22 @@ public function resolveDepends($params = []): array } + public function putCache($key, $value): void + { + $this->cache[$key] = $value; + } + + public function getCache($key) + { + if (isset($this->cache[$key])) { + $res = $this->cache[$key]; + unset($this->cache[$key]); + return $res; + } + return null; + } + + /** * @param $instance * @param null $method diff --git a/src/blankphp/Event/Observer.php b/src/blankphp/Event/Observer.php index 19e7677..8d42b3c 100644 --- a/src/blankphp/Event/Observer.php +++ b/src/blankphp/Event/Observer.php @@ -7,23 +7,28 @@ class Observer extends ObserveAbstract { //创建好模型的监听者的模板 - public function doListen(){ + public function doListen() + { } - public function creating(){ + public function creating() + { } - public function created(){ + public function created() + { } - public function updated(){ + public function updated() + { } - public function updating(){ + public function updating() + { } } \ No newline at end of file diff --git a/src/blankphp/Exception/Error.php b/src/blankphp/Exception/Error.php index 8d7dca0..3f34f34 100644 --- a/src/blankphp/Exception/Error.php +++ b/src/blankphp/Exception/Error.php @@ -22,7 +22,7 @@ public function register(Application $app) public static function Exception($e){ $handler =self::getHandler(); $sapi_type = php_sapi_name(); - if (substr($sapi_type, 0, 3) == 'cli') { + if (strpos($sapi_type, 'cli') === 0) { $handler->handToConsole($e); } else { $handler->handToRender($e); diff --git a/src/blankphp/Exception/Handler.php b/src/blankphp/Exception/Handler.php index 10cc9f6..ddb1d8a 100644 --- a/src/blankphp/Exception/Handler.php +++ b/src/blankphp/Exception/Handler.php @@ -9,17 +9,19 @@ class Handler { - public function handToConsole( $e){ + public function handToConsole($e) + { } - public function handToRender( $e){ - if ($e instanceof Exception) - $e->render(); - else{ - $e = new HttpException($e->getMessage(),500); + public function handToRender($e): void + { + if ($e instanceof Exception) { + $e->render(); + } else { + $e = new HttpException($e->getMessage(), 500); $e->render(); } - Log::error('error_code: '.$e->getCode()." error_message: ".$e->getMessage(),$e->getTrace()); + Log::error('error_code: ' . $e->getCode() . " error_message: " . $e->getMessage(), $e->getTrace()); } } \ No newline at end of file diff --git a/src/blankphp/Kernel/HttpKernel.php b/src/blankphp/Kernel/HttpKernel.php index d08995c..051f8a7 100644 --- a/src/blankphp/Kernel/HttpKernel.php +++ b/src/blankphp/Kernel/HttpKernel.php @@ -32,17 +32,23 @@ class HttpKernel */ protected $route; - public function startConfig($config): void - { - //处理设置 - } + + /** + * HttpKernel constructor. + * @param Application $app + */ public function __construct(Application $app) { $this->app = $app; $this->route = $app->make('router'); } + public function startConfig($config): void + { + //处理设置 + } + public function registerRequest($request): void { $this->app->instance('request', $request); diff --git a/src/blankphp/Model/Model.php b/src/blankphp/Model/Model.php index 3ca860a..684be81 100644 --- a/src/blankphp/Model/Model.php +++ b/src/blankphp/Model/Model.php @@ -21,16 +21,24 @@ class Model extends EventAbstract { use belongsTo, belongsToMany, hasMany, hasOne; + protected $database; protected $tableName; protected $primaryKey; - protected $fillable = []; - //原来的数据 + /** + * @var array + */ + protected $fillAble = []; + /** + * @var array + */ protected $origin = []; - //真实数据 + /** + * @var array + */ protected $data = []; + protected $collection; - //sql protected $sql; protected $status; @@ -42,8 +50,9 @@ public function __construct($id = 0) $this->makeQuery(); $this->database->table($this->tableName); $this->collection = new Collection(); - if ($id> 0) + if ($id> 0) { $this->collection = $this->find($id); + } //设定好对应关系以及 } @@ -56,9 +65,9 @@ public function makeQuery() { if (empty($this->database)) { $driver = config('db.default'); - $grammer_class = 'BlankPhp\\Database\\Grammar\\' . ucwords(strtolower($driver)) . 'Grammar'; - $grammer = new $grammer_class; - $builder = new Builder($grammer); + $grammar_class = 'BlankPhp\\Database\\Grammar\\' . ucwords(strtolower($driver)) . 'Grammar'; + $grammar = new $grammar_class; + $builder = new Builder($grammar); return $this->database = new Database($builder); } } @@ -72,9 +81,9 @@ public function save() $result = $this->database->create($this->data); $this->event('saved'); return $this->data = $this->collection; - } else { - return $this->updateOne($this->data); } + + return $this->updateOne(); } @@ -105,7 +114,8 @@ public function __call($name, $arguments) } //预加载 - public function with(){ + public function with(): void + { $args = func_get_args(); } diff --git a/src/blankphp/PipeLine/PipeLine.php b/src/blankphp/PipeLine/PipeLine.php index 7cdcf2d..16a2b2f 100644 --- a/src/blankphp/PipeLine/PipeLine.php +++ b/src/blankphp/PipeLine/PipeLine.php @@ -14,7 +14,14 @@ class PipeLine { + /** + * @var array + */ protected $middleware; + + /** + * @var mixed + */ protected $data; public function __construct() @@ -46,6 +53,11 @@ public function process(): void { } + public function wait(): void + { + + } + public function through($middleware): PipeLine { //管道模式运行 diff --git a/src/blankphp/Request/Request.php b/src/blankphp/Request/Request.php index 658a0d3..2aed644 100644 --- a/src/blankphp/Request/Request.php +++ b/src/blankphp/Request/Request.php @@ -15,24 +15,59 @@ class Request implements RequestContract { + /** + * @var string + */ public $uri; + /** + * @var string + */ public $method; - public $request = [ + /** + * @var string[] + */ + protected $request = [ 'get' => '', 'post' => '', 'files' => '', 'input' => '', ]; + /** + * @var \BlankPhp\Session\Session + */ public $session; + /** + * @var \BlankPhp\Cookie\Cookie + */ public $cookie; + /** + * @var array + */ public $input = []; + /** + * @var string + */ public $userIp; + /** + * @var string + */ public $serverIp; + public $http; + public $https; + /** + * @var string + */ public $userAgent; + /** + * @var string + */ public $language; + /** + * Request constructor. + */ public function __construct() { $this->getUri(); @@ -55,13 +90,21 @@ public function getFromServer(): void $this->getServicePort(); } + /*** + * @param $value + * @return array|string + */ public function stripSlashesDeep($value) { - //递归方式解决不安全字符 $value = is_array($value) ? array_map([$this, 'stripSlashesDeep'], $value) : stripslashes($value); return $value; } + /** + * @param $name + * @param string $default + * @return string + */ public function get($name, $default = ''): string { $this->{'_' . strtolower($this->method)}(); @@ -78,13 +121,17 @@ public function get($name, $default = ''): string return $default; } - + /** + * @return $this + */ public function capture(): Request { return $this; } - + /** + * @return string + */ public function getUri(): string { if ($this->uri === null) { @@ -106,16 +153,23 @@ public function getUri(): string } + /*** + * @return mixed|string + */ public function getMethod() { $method = $_SERVER['REQUEST_METHOD']; - if ($method === 'POST') + if ($method === 'POST') { $method = isset($this->request['post']['_method']) ? strtoupper($this->request['post']['_method']) : 'POST'; + } $this->method = $method; return $this->method; } - + /** + * @param string $name + * @return string| mixed + */ public function file($name = '') { if (empty($this->request['files']) && !empty($_FILES)) { @@ -166,12 +220,12 @@ public function getLanguage() return $this->language; } - public function getHttp() + public function getHttp(): void { } - public function getServicePort() + public function getServicePort(): void { } diff --git a/src/blankphp/Request/TestRequest.php b/src/blankphp/Request/TestRequest.php index ba40bff..20d87db 100644 --- a/src/blankphp/Request/TestRequest.php +++ b/src/blankphp/Request/TestRequest.php @@ -10,11 +10,10 @@ class TestRequest extends Request public function __construct() { - return $this; } public static function create($method, $uri, $parameters, $cookies, - $files, $server, $content) + $files, $server, $content): TestRequest { self::$instance = new self(); self::$instance->uri = $uri; diff --git a/src/blankphp/Route/Route.php b/src/blankphp/Route/Route.php index c39c688..af7d05a 100644 --- a/src/blankphp/Route/Route.php +++ b/src/blankphp/Route/Route.php @@ -34,7 +34,9 @@ class Route implements Contract protected $routes; //当前路由 protected $currentRoute; - //application + /** + * @var Application + */ private $app; //控制器命名空间 protected $controllerNamespace; @@ -139,7 +141,7 @@ public function prefix($prefix = '') return $this; } - public function group($group) + public function group($group): Route { $this->group[] = $group; $this->groupMiddleware[] = $group; @@ -147,7 +149,7 @@ public function group($group) } - public function setNamespace($namespace, $parent = '') + public function setNamespace($namespace, $parent = ''): Route { $this->controllerNamespace = empty($namespace) ? $namespace : $parent . '\\' . $namespace; return $this; @@ -193,7 +195,7 @@ public function getRoute($match, $method, &$route) } - public function loadNextFile($shift = true) + public function loadNextFile($shift = true): void { $file = $this->loadedFile(array_shift($this->files)); $this->routes = new RuleCollection(); @@ -213,7 +215,7 @@ public function loadedFile($file) public function findRoute($request) { $current = $this->match($request); - if (!empty($current)) { + if ($current !== null) { //获取控制器 $controller = $this->getController($current->action); $current->setAction($controller); @@ -264,7 +266,7 @@ public function putCache() File::putCache($this->routes, 'route.php'); } - public function cache() + public function cache(): array { $cache = []; while (True) { diff --git a/src/blankphp/Route/Router.php b/src/blankphp/Route/Router.php index d9d2b49..87064f4 100644 --- a/src/blankphp/Route/Router.php +++ b/src/blankphp/Route/Router.php @@ -28,7 +28,7 @@ public function __construct(Route $route) public function getMiddleware($group = 'web'): void { - $middleware = $this->app->getSignal('GroupMiddleware', $this->route->getGroupMidlleware()); + $middleware = $this->app->getSignal('GroupMiddleware', $this->route->getGroupMiddleware()); $temp = $this->app->getSignal('AliceMiddleware', $this->route->getMiddleWare()); $this->middleware = array_filter(array_merge($middleware[$group], $temp)); } diff --git a/src/blankphp/Route/Traits/ResolveSomeDepends.php b/src/blankphp/Route/Traits/ResolveSomeDepends.php index 258aa4f..fd49f6e 100644 --- a/src/blankphp/Route/Traits/ResolveSomeDepends.php +++ b/src/blankphp/Route/Traits/ResolveSomeDepends.php @@ -9,6 +9,7 @@ namespace BlankPhp\Route\Traits; +use BlankPhp\Application; use BlankPhp\Contract\RequestContract; use BlankPhp\Facade; use BlankPhp\Model\Model; @@ -20,7 +21,7 @@ trait ResolveSomeDepends { - public function resolveClassMethodDependencies($parameters, $instance, $method) + public function resolveClassMethodDependencies($parameters, $instance, $method): array { //解决类方法的依赖-->反射解决 if (!empty($parameters)){ @@ -39,21 +40,21 @@ public function resolveClassMethodDependencies($parameters, $instance, $method) } - public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector) + public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector): array { $instanceCount = 0; $values = array_values($parameters); foreach ($reflector->getParameters() as $key => $parameter) { $instance = $parameter->getClass(); - if (!is_null($instance)) { + if ($instance !== null) { array_splice($parameters, $key, $instanceCount, - [$this->app->make($instance->getName(),[$values[$instanceCount]])] + [$this->app->make($instance->getName(),$values[$instanceCount]?[$values[$instanceCount]]:null)] ); } elseif (!isset($values[$key - $instanceCount]) && $parameter->isDefaultValueAvailable()) { array_splice($parameters, $key, $instanceCount, [$parameter->getDefaultValue()]); - $instanceCount++; } + $instanceCount++; } return $parameters; } diff --git a/src/blankphp/Route/Traits/SetMiddleWare.php b/src/blankphp/Route/Traits/SetMiddleWare.php index f50026b..b97506c 100644 --- a/src/blankphp/Route/Traits/SetMiddleWare.php +++ b/src/blankphp/Route/Traits/SetMiddleWare.php @@ -15,17 +15,26 @@ trait SetMiddleWare protected $currentController; public $middleware; + /** + * @return string| mixed + */ public function getMiddleWare() { - return isset($this->middleware['alice'])?$this->middleware['alice']:''; + return $this->middleware['alice'] ?? ''; } - public function getGroupMidlleware() + /** + * @return string| mixed + */ + public function getGroupMiddleware() { - return isset($this->middleware['group'])?$this->middleware['group']:''; + return $this->middleware['group'] ?? ''; } - public function setMiddleWare($middleware) + /** + * @param $middleware + */ + public function setMiddleWare($middleware): void { $this->middleware = $middleware; } @@ -34,21 +43,22 @@ public function setMiddleWare($middleware) public function getOneMiddleWare($array) { //引用中间件别名[然后获取]. - if (isset($array['middleware'])) + if (isset($array['middleware'])) { return $array['middleware']; - else - return []; + } + + return []; } - public function setOneMiddleWare( $uri, $method) + public function setOneMiddleWare($uri, $method): void { if (!empty($this->tempMiddleware)) { - $this->route[$uri][$method]['middleware']['alice']=[]; - array_push($this->route[$uri][$method]['middleware']['alice'], $this->tempMiddleware); + $this->route[$uri][$method]['middleware']['alice'] = []; + $this->route[$uri][$method]['middleware']['alice'][] = $this->tempMiddleware; } } - public function setCurrentController($uri, $method) + public function setCurrentController($uri, $method): void { $this->currentController = array($uri, $method); } @@ -58,7 +68,7 @@ public function getCurrentController() return $this->currentController; } - public function init() + public function init(): void { $this->currentController = ''; } diff --git a/src/blankphp/Scheme/Scheme.php b/src/blankphp/Scheme/Scheme.php index 887a267..88c930b 100644 --- a/src/blankphp/Scheme/Scheme.php +++ b/src/blankphp/Scheme/Scheme.php @@ -69,10 +69,10 @@ public function timestamps() public function end(){ self::$pdo->exec($this->sql->toSql()); - echo "do ok~~~".PHP_EOL; + echo 'do ok~~~' .PHP_EOL; } - public function bind() + public function bind(): void { } diff --git a/src/blankphp/Session/Session.php b/src/blankphp/Session/Session.php index e1f68bc..47293c2 100644 --- a/src/blankphp/Session/Session.php +++ b/src/blankphp/Session/Session.php @@ -21,16 +21,29 @@ class Session implements SessionContract { use FactoryClientTrait; + /** + * @var string + */ protected static $sessionName = '_session'; - + /** + * @var mixed + */ protected $handler; - //session_id + /** + * @var string + */ protected $id; - //数据 + /** + * @var array + */ protected $data = []; - //是否生成 + /** + * @var bool + */ protected $generate = true; - //过期时间 + /** + * @var int + */ protected $expire = 35000; /** @@ -196,7 +209,6 @@ public function end(): void $this->save(); } - //清理内容 public function flush(): void { $this->data = []; @@ -204,5 +216,4 @@ public function flush(): void $this->reGenerate(); } - } \ No newline at end of file diff --git a/src/helpers/helper.php b/src/helpers/helper.php index c6b7a58..210b0ea 100644 --- a/src/helpers/helper.php +++ b/src/helpers/helper.php @@ -12,7 +12,7 @@ function app($abstract) } if (!function_exists('config')) { - function config($name = "", $default = null) + function config($name = '', $default = null) { if (empty($name)) { return app('config')->all(); From 6623b37389f9a7be556ef8bbc320000bc56c7859 Mon Sep 17 00:00:00 2001 From: blankqwq <1136589038@qq.com> Date: 2020年5月31日 19:46:28 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E3=80=90=E5=BC=80=E5=8F=91=E3=80=91?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=9E=84=E5=BB=BAProxy=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 ++++++- .../Base/Traits/GetFromFileCacheTrait.php | 10 +++++ src/blankphp/Config/LoadConfig.php | 2 +- src/blankphp/Event/EventAbstract.php | 1 - src/blankphp/Event/ObserveAbstract.php | 1 - src/blankphp/Exception/Error.php | 37 ++++++++++--------- src/blankphp/Exception/Exception.php | 11 +++--- src/blankphp/Proxy/ProxyBridge.php | 10 +++++ src/blankphp/Proxy/ProxyClient.php | 10 +++++ src/blankphp/Proxy/ProxyService.php | 10 +++++ src/blankphp/Proxy/ProxyServiceProvider.php | 10 +++++ src/blankphp/Response/Response.php | 3 +- src/blankphp/Route/Route.php | 2 +- src/blankphp/Route/Router.php | 6 +-- .../Route/Traits/ResolveSomeDepends.php | 1 - src/blankphp/View/View.php | 9 ++--- src/helpers/Exec.php | 14 +++++++ 17 files changed, 113 insertions(+), 38 deletions(-) create mode 100644 src/blankphp/Base/Traits/GetFromFileCacheTrait.php create mode 100644 src/blankphp/Proxy/ProxyBridge.php create mode 100644 src/blankphp/Proxy/ProxyClient.php create mode 100644 src/blankphp/Proxy/ProxyService.php create mode 100644 src/blankphp/Proxy/ProxyServiceProvider.php create mode 100644 src/helpers/Exec.php diff --git a/README.md b/README.md index 22dfcfc..f45ad8f 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,7 @@ ```shell $ composer require blankqwq/framework -vvv ``` - - +- [ ] 可测试性&& 可监控性 - [ ] 文档 - [x] 容器 - [x] 依赖注入 @@ -51,6 +50,13 @@ $ composer require blankqwq/framework -vvv - [ ] RPC - [ ] ... - [ ] FormValidate +- [ ] Helper + - [ ] Exec + - [ ] Array + - [ ] Curl + - [ ] Rsa + - [ ] Str + ... - [ ] Console - [x] ConsoleKernel @@ -59,7 +65,11 @@ $ composer require blankqwq/framework -vvv # manage管理者 +# driverFactory 驱动管理 + # event事件 + +# Application容器 ``` diff --git a/src/blankphp/Base/Traits/GetFromFileCacheTrait.php b/src/blankphp/Base/Traits/GetFromFileCacheTrait.php new file mode 100644 index 0000000..042b75f --- /dev/null +++ b/src/blankphp/Base/Traits/GetFromFileCacheTrait.php @@ -0,0 +1,10 @@ +loadConfigFile(); diff --git a/src/blankphp/Event/EventAbstract.php b/src/blankphp/Event/EventAbstract.php index 03802a9..cd4b027 100644 --- a/src/blankphp/Event/EventAbstract.php +++ b/src/blankphp/Event/EventAbstract.php @@ -16,7 +16,6 @@ abstract class EventAbstract public static function observe(Observer $observer): void { - //根据信号进行指定更新 self::$observes = $observer; } diff --git a/src/blankphp/Event/ObserveAbstract.php b/src/blankphp/Event/ObserveAbstract.php index 1cfa982..4f2fdb4 100644 --- a/src/blankphp/Event/ObserveAbstract.php +++ b/src/blankphp/Event/ObserveAbstract.php @@ -11,7 +11,6 @@ abstract class ObserveAbstract { - // abstract public function doListen(); } \ No newline at end of file diff --git a/src/blankphp/Exception/Error.php b/src/blankphp/Exception/Error.php index 3f34f34..0f3626a 100644 --- a/src/blankphp/Exception/Error.php +++ b/src/blankphp/Exception/Error.php @@ -10,17 +10,19 @@ class Error { protected static $handler; protected $app; - public function register(Application $app) + + public function register(Application $app): void { - $this->app= $app; + $this->app = $app; error_reporting(E_ALL); - set_error_handler([__CLASS__, 'Error']); - set_exception_handler([__CLASS__, 'Exception']); - register_shutdown_function([__CLASS__, 'Shutdown']); + register_shutdown_function([__CLASS__, 'shutdown']); + set_error_handler([__CLASS__, 'error']); + set_exception_handler([__CLASS__, 'exception']); } - public static function Exception($e){ - $handler =self::getHandler(); + public static function exception($e): void + { + $handler = self::getHandler(); $sapi_type = php_sapi_name(); if (strpos($sapi_type, 'cli') === 0) { $handler->handToConsole($e); @@ -29,20 +31,21 @@ public static function Exception($e){ } } - public static function Error(){ - + public static function error(): void + { } - public static function Shutdown(){ - + public static function shutdown(): void + { + $hah = error_get_last(); + var_dump($hah); } - public static function getHandler(){ - if (empty(self::$handler)){ - //创建一个新的handler - //获取一个handler - $class=config('app.exception_handler'); - self::$handler=$handler = new $class(); + public static function getHandler() + { + if (empty(self::$handler)) { + $class = config('app.exception_handler'); + self::$handler = new $class(); } return self::$handler; } diff --git a/src/blankphp/Exception/Exception.php b/src/blankphp/Exception/Exception.php index 95782d5..daaf2cd 100644 --- a/src/blankphp/Exception/Exception.php +++ b/src/blankphp/Exception/Exception.php @@ -13,17 +13,17 @@ abstract class Exception extends \Exception protected $code; protected $httpCode = 500; - public function __construct($message = "", $code = 0, Throwable $previous = null) + public function __construct($message = '', $code = 0, Throwable $previous = null) { parent::__construct($message, $code, $previous); } - public function bootstrap() + public function bootstrap(): void { } - public function httpCode() + public function httpCode(): int { return $this->httpCode; } @@ -33,9 +33,10 @@ public function render() //返回模板 $this->httpCode = $this->code; //判断是否是json - $response = new Response(view(__DIR__ . "/stub/error.php", ["message" => $this->getMessage(), "file" => $this->getFile(), 'line' => $this->getLine(),"trace"=>$this->getTrace()], false)); + $trace = json_encode($this->getTrace()); + $response = new Response(view(__DIR__ . '/stub/error.php', ['message' => $this->getMessage(), 'file' => $this->getFile(), 'line' => $this->getLine(), 'trace' => $trace],false)); $response->header($this->httpCode()); - $response->prepare()->send(); + $response->send(); } public function handle() diff --git a/src/blankphp/Proxy/ProxyBridge.php b/src/blankphp/Proxy/ProxyBridge.php new file mode 100644 index 0000000..4f80291 --- /dev/null +++ b/src/blankphp/Proxy/ProxyBridge.php @@ -0,0 +1,10 @@ +result = (string)$result; if ($this->isJson($this->result)) { $this->setType(self::$header['json']); @@ -104,7 +105,7 @@ public function prepare(): Response public function isJson($string): bool { - return strpos($string, '{') === 0; + return strpos($string, '{') === 0 ; } diff --git a/src/blankphp/Route/Route.php b/src/blankphp/Route/Route.php index af7d05a..fd42c18 100644 --- a/src/blankphp/Route/Route.php +++ b/src/blankphp/Route/Route.php @@ -224,7 +224,7 @@ public function findRoute($request) throw new HttpException("Not Found this route controller [{$current->action}]", 404); } - public function getController($controller) + public function getController($controller): array { //如过传递的是闭包 if ($controller instanceof \Closure) diff --git a/src/blankphp/Route/Router.php b/src/blankphp/Route/Router.php index 87064f4..3f33c51 100644 --- a/src/blankphp/Route/Router.php +++ b/src/blankphp/Route/Router.php @@ -46,12 +46,12 @@ public function dispatcher($request) }); } - public function prepareResponse($response) + public function prepareResponse($response): Response { return self::toResponse($response); } - public static function toResponse($response) + public static function toResponse($response): Response { if ($response instanceof Response) { return $response->prepare(); @@ -60,7 +60,7 @@ public static function toResponse($response) return $response->prepare(); } - public function flush() + public function flush(): void { $this->middleware = []; } diff --git a/src/blankphp/Route/Traits/ResolveSomeDepends.php b/src/blankphp/Route/Traits/ResolveSomeDepends.php index fd49f6e..65ca135 100644 --- a/src/blankphp/Route/Traits/ResolveSomeDepends.php +++ b/src/blankphp/Route/Traits/ResolveSomeDepends.php @@ -10,7 +10,6 @@ use BlankPhp\Application; -use BlankPhp\Contract\RequestContract; use BlankPhp\Facade; use BlankPhp\Model\Model; use BlankPhp\Request\Request; diff --git a/src/blankphp/View/View.php b/src/blankphp/View/View.php index c4e1674..fffdba8 100644 --- a/src/blankphp/View/View.php +++ b/src/blankphp/View/View.php @@ -9,7 +9,7 @@ namespace BlankPhp\View; //工厂模式 -class View +class View implements \BlankPhp\Contract\View { //缓存的目录 protected $cacheDir = APP_PATH . 'cache/view/'; @@ -57,10 +57,10 @@ public function __construct() public function setDescFile($fileName) { - $this->descFile = md5($this->getFile()) . '.php'; + $this->descFile = $fileName?? md5($this->getFile()) . '.php'; } - public function getDescFile() + public function getDescFile(): string { return $this->cacheDir . $this->descFile; } @@ -136,8 +136,7 @@ public function view($filename, $data = [], $makeFileName = true) ob_start(); ob_clean(); include($file); - $content = ob_get_contents(); - ob_end_clean(); + $content = ob_get_clean(); return $content; } diff --git a/src/helpers/Exec.php b/src/helpers/Exec.php new file mode 100644 index 0000000..ab7d2cc --- /dev/null +++ b/src/helpers/Exec.php @@ -0,0 +1,14 @@ + Date: 2020年6月23日 23:17:23 +0800 Subject: [PATCH 11/15] start proxy --- src/blankphp/Proxy/Proxy.php | 47 +++++++++++++++++++++ src/blankphp/Proxy/ProxyBridge.php | 3 ++ src/blankphp/Proxy/ProxyClient.php | 29 +++++++++++++ src/blankphp/Proxy/ProxyServiceProvider.php | 10 ++++- src/blankphp/Proxy/Traits/GetAllMethod.php | 15 +++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/blankphp/Proxy/Proxy.php create mode 100644 src/blankphp/Proxy/Traits/GetAllMethod.php diff --git a/src/blankphp/Proxy/Proxy.php b/src/blankphp/Proxy/Proxy.php new file mode 100644 index 0000000..f31a069 --- /dev/null +++ b/src/blankphp/Proxy/Proxy.php @@ -0,0 +1,47 @@ +bridge){ + + } + $bridge = new ProxyBridge(); + return ; + } + + //链接的代理 + public function client(){ + $this->client = new ProxyClient(); + + } + + /** + * @param $name + * @param $arguments + * 调用真正的方法 + */ + public function __call($name, $arguments) + { + + } + + private function getMethod(){ + //反射获取方法列表 + } + +} \ No newline at end of file diff --git a/src/blankphp/Proxy/ProxyBridge.php b/src/blankphp/Proxy/ProxyBridge.php index 4f80291..7fde925 100644 --- a/src/blankphp/Proxy/ProxyBridge.php +++ b/src/blankphp/Proxy/ProxyBridge.php @@ -4,7 +4,10 @@ namespace BlankPhp\Proxy; +use BlankPhp\Proxy\Traits\GetAllMethod; + class ProxyBridge { + use GetAllMethod; } \ No newline at end of file diff --git a/src/blankphp/Proxy/ProxyClient.php b/src/blankphp/Proxy/ProxyClient.php index 46025dd..52339f5 100644 --- a/src/blankphp/Proxy/ProxyClient.php +++ b/src/blankphp/Proxy/ProxyClient.php @@ -4,7 +4,36 @@ namespace BlankPhp\Proxy; +use BlankPhp\Proxy\Traits\GetAllMethod; + class ProxyClient { + use GetAllMethod; + + private $method; + private $origin; + private $proxy; + + public function __construct($originClass, $proxyClass) + { + $this->origin = $originClass; + $this->proxy = $proxyClass; + } + + /** + * 关闭链接 + */ + public function close(): void + { + + } + + public function __call($name, $arguments) + { + if (!in_array($name, $this->method, true)) { + return $this->origin->{$name}(...$arguments); + } + return $this->proxy->{$name}(...$arguments); + } } \ No newline at end of file diff --git a/src/blankphp/Proxy/ProxyServiceProvider.php b/src/blankphp/Proxy/ProxyServiceProvider.php index c6238d4..fcf2659 100644 --- a/src/blankphp/Proxy/ProxyServiceProvider.php +++ b/src/blankphp/Proxy/ProxyServiceProvider.php @@ -4,7 +4,15 @@ namespace BlankPhp\Proxy; -class ProxyServiceProvider +use BlankPhp\Provider\Provider; + +//动态代理应该原生支持 +class ProxyServiceProvider extends Provider { + public function boot() + { + parent::boot(); // TODO: Change the autogenerated stub + } + } \ No newline at end of file diff --git a/src/blankphp/Proxy/Traits/GetAllMethod.php b/src/blankphp/Proxy/Traits/GetAllMethod.php new file mode 100644 index 0000000..fc87564 --- /dev/null +++ b/src/blankphp/Proxy/Traits/GetAllMethod.php @@ -0,0 +1,15 @@ + Date: Thu, 9 Jul 2020 23:16:05 +0800 Subject: [PATCH 12/15] update proxy --- src/blankphp/Cookie/Cookie.php | 2 +- .../Factory/CanNotCreateObjectException.php | 10 +++++ src/blankphp/Proxy/Proxy.php | 40 +++---------------- src/blankphp/Proxy/ProxyBridge.php | 7 +++- src/blankphp/Proxy/ProxyClient.php | 3 +- src/blankphp/Proxy/Traits/Macro.php | 24 +++++++++++ 6 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 src/blankphp/Exception/Factory/CanNotCreateObjectException.php create mode 100644 src/blankphp/Proxy/Traits/Macro.php diff --git a/src/blankphp/Cookie/Cookie.php b/src/blankphp/Cookie/Cookie.php index 91b4abb..efb9050 100644 --- a/src/blankphp/Cookie/Cookie.php +++ b/src/blankphp/Cookie/Cookie.php @@ -37,7 +37,7 @@ private function getCookie(): void /** * @param array $option */ - public function setOption($option = []) + public function setOption($option = []): void { foreach ($option as $k => $v) { if (isset($this->{$k})) { diff --git a/src/blankphp/Exception/Factory/CanNotCreateObjectException.php b/src/blankphp/Exception/Factory/CanNotCreateObjectException.php new file mode 100644 index 0000000..fbc6f4f --- /dev/null +++ b/src/blankphp/Exception/Factory/CanNotCreateObjectException.php @@ -0,0 +1,10 @@ +bridge){ - - } - $bridge = new ProxyBridge(); - return ; - } - - //链接的代理 - public function client(){ - $this->client = new ProxyClient(); - - } + use GetAllMethod; + public $name; + protected $methods = []; - /** - * @param $name - * @param $arguments - * 调用真正的方法 - */ public function __call($name, $arguments) { - - } - - private function getMethod(){ - //反射获取方法列表 + // TODO: Implement __call() method. } - } \ No newline at end of file diff --git a/src/blankphp/Proxy/ProxyBridge.php b/src/blankphp/Proxy/ProxyBridge.php index 7fde925..ed378fd 100644 --- a/src/blankphp/Proxy/ProxyBridge.php +++ b/src/blankphp/Proxy/ProxyBridge.php @@ -6,8 +6,11 @@ use BlankPhp\Proxy\Traits\GetAllMethod; -class ProxyBridge +class ProxyBridge extends Proxy { - use GetAllMethod; + + public function bridge(){ + + } } \ No newline at end of file diff --git a/src/blankphp/Proxy/ProxyClient.php b/src/blankphp/Proxy/ProxyClient.php index 52339f5..2268f03 100644 --- a/src/blankphp/Proxy/ProxyClient.php +++ b/src/blankphp/Proxy/ProxyClient.php @@ -6,7 +6,7 @@ use BlankPhp\Proxy\Traits\GetAllMethod; -class ProxyClient +class ProxyClient extends Proxy { use GetAllMethod; @@ -33,7 +33,6 @@ public function __call($name, $arguments) if (!in_array($name, $this->method, true)) { return $this->origin->{$name}(...$arguments); } - return $this->proxy->{$name}(...$arguments); } } \ No newline at end of file diff --git a/src/blankphp/Proxy/Traits/Macro.php b/src/blankphp/Proxy/Traits/Macro.php new file mode 100644 index 0000000..2d6bb2b --- /dev/null +++ b/src/blankphp/Proxy/Traits/Macro.php @@ -0,0 +1,24 @@ + Date: Thu, 9 Jul 2020 23:41:43 +0800 Subject: [PATCH 13/15] replace log to monolog --- composer.json | 3 ++- src/blankphp/Application.php | 4 +-- src/blankphp/Log/Log.php | 23 ++--------------- src/blankphp/Log/Logger.php | 41 +++++++++++++++++++++++++++++++ src/blankphp/PipeLine/Process.php | 10 ++++++++ 5 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 src/blankphp/Log/Logger.php create mode 100644 src/blankphp/PipeLine/Process.php diff --git a/composer.json b/composer.json index 2a33e44..ce92a12 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "ext-gd": "*", "ext-openssl": "*", "ext-mbstring": "*", - "ext-pdo": "*" + "ext-pdo": "*", + "monolog/monolog": "1.25.*|2.1.*" } } diff --git a/src/blankphp/Application.php b/src/blankphp/Application.php index ef2333a..b0bf4ab 100644 --- a/src/blankphp/Application.php +++ b/src/blankphp/Application.php @@ -23,7 +23,7 @@ use BlankPhp\Factory\FactoryBase; use BlankPhp\Kernel\ConsoleKernel; use BlankPhp\Kernel\HttpKernel; -use BlankPhp\Log\Log; +use BlankPhp\Log\Logger; use BlankPhp\Provider\RegisterProvider; use BlankPhp\Request\Request; use BlankPhp\Response\Response; @@ -99,7 +99,7 @@ public function registerService(): void 'cache' => [CacheManager::class], 'cache.drive' => [CacheManager::class], 'redis' => [Redis::class], - 'log' => Log::class + 'log' => Logger::class ]; array_walk($temp, array($this, 'bind')); unset($temp); diff --git a/src/blankphp/Log/Log.php b/src/blankphp/Log/Log.php index 23f26f3..b4eddf4 100644 --- a/src/blankphp/Log/Log.php +++ b/src/blankphp/Log/Log.php @@ -3,28 +3,9 @@ namespace BlankPhp\Log; +use \Monolog\Logger; -use BlankPhp\Application; -use BlankQwq\Helpers\Str; -use Psr\Log\AbstractLogger; - -class Log extends AbstractLogger +class Log extends Logger { - protected $handler; - - protected $nameSpace = "BlankPhp\Log\Driver"; - - public function __construct(Application $app) - { - $handler = $app['config']->get('app.log_driver', 'File'); - $handler = Str::makeClassName($handler, $this->nameSpace); - $this->handler = new $handler; - } - - public function log($level, $message, array $context = array()) - { - //存储到对应的message 和content - $this->handler->log($level, $message, $context); - } } \ No newline at end of file diff --git a/src/blankphp/Log/Logger.php b/src/blankphp/Log/Logger.php new file mode 100644 index 0000000..15a710a --- /dev/null +++ b/src/blankphp/Log/Logger.php @@ -0,0 +1,41 @@ + 'debug', + ]; + + public function __construct(Application $app) + { + $this->config = array_merge($this->config, $app['config']->get('log')); + $this->handler = $this->getHandler(); + } + + private function getHandler(): Log + { + $level = strtoupper($this->config['level']); + return new Log(new StreamHandler($this->config['path'], $level)); + } + + private function formatFileName(){ + + } + + public function log($level, $message, array $context = array()): void + { + $this->handler->log($level, $message, $context); + } + +} \ No newline at end of file diff --git a/src/blankphp/PipeLine/Process.php b/src/blankphp/PipeLine/Process.php new file mode 100644 index 0000000..937f68a --- /dev/null +++ b/src/blankphp/PipeLine/Process.php @@ -0,0 +1,10 @@ + Date: 2020年7月14日 07:37:25 +0800 Subject: [PATCH 14/15] add logger driver --- src/blankphp/Log/Driver/Daily.php | 24 +++++++ src/blankphp/Log/Driver/File.php | 111 ------------------------------ src/blankphp/Log/Logger.php | 37 ++++++++-- src/blankphp/Proxy/Proxy.php | 3 +- 4 files changed, 56 insertions(+), 119 deletions(-) create mode 100644 src/blankphp/Log/Driver/Daily.php delete mode 100644 src/blankphp/Log/Driver/File.php diff --git a/src/blankphp/Log/Driver/Daily.php b/src/blankphp/Log/Driver/Daily.php new file mode 100644 index 0000000..b962851 --- /dev/null +++ b/src/blankphp/Log/Driver/Daily.php @@ -0,0 +1,24 @@ +pushHandler(new StreamHandler($config['path'], $config['level'])); + } + + public function setFormatter($formatter) + { + return $this->getHandlers()[0]->setFormatter($formatter); + } +} \ No newline at end of file diff --git a/src/blankphp/Log/Driver/File.php b/src/blankphp/Log/Driver/File.php deleted file mode 100644 index 38b38a3..0000000 --- a/src/blankphp/Log/Driver/File.php +++ /dev/null @@ -1,111 +0,0 @@ - APP_PATH . '/cache/log/', - 'max' => 2 * 1024 * 1024, - 'time_format' => 'Y - M - D H:S:', - 'extension' => 'log', - ]; - - private $fileName = null; - - public function parseLog($type, $message, $context) - { - //将数组等转换未string类型 - $time = time(); - $content = "_____________【 $type 】 start time:[$time]_____________" . PHP_EOL; - $content .= $message . PHP_EOL; - $content .= var_export($context, true); - $content .= "_____________ 【 $type 】 stop _____________" . PHP_EOL; - return $content; - } - - public function emergency($message, array $context = array()) - { - // TODO: Implement emergency() method. - return $this->parseLog('emergency', $message, $context); - - } - - public function alert($message, array $context = array()) - { - // TODO: Implement alert() method. - return $this->parseLog('alert', $message, $context); - - } - - public function critical($message, array $context = array()) - { - // TODO: Implement critical() method. - return $this->parseLog('critical', $message, $context); - } - - public function error($message, array $context = array()) - { - // TODO: Implement error() method. - return $this->parseLog('error', $message, $context); - } - - public function warning($message, array $context = array()) - { - // TODO: Implement warning() method. - return $this->parseLog('warning', $message, $context); - } - - public function notice($message, array $context = array()) - { - // TODO: Implement notice() method. - return $this->parseLog('notice', $message, $context); - } - - public function info($message, array $context = array()) - { - return $this->parseLog('info', $message, $context); - } - - public function debug($message, array $context = array()) - { - // TODO: Implement debug() method. - return $this->parseLog('debug', $message, $context); - } - - public function log($level, $message, array $context = array()) - { - //代理方法调用子方法 - $res = $this->{$level}($message, $context); - $this->put($res); - } - - public function put(string $data): void - { - //判断目录是否存在 - if (!is_dir($this->options['dir']) && !mkdir($concurrentDirectory = $this->options['dir']) && !is_dir($concurrentDirectory)) { - throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory)); - } - //文件名 - if (empty($this->fileName)) { - //数据格式 -// $this->fileName = date('Y-m-d-H i s') . '-log.' . $this->options['extension']; - $this->fileName = 'day.log'; - } - //写入文件 - if (is_dir($this->options['dir'])) { - if (filesize($this->options['dir'] . $this->fileName)>= $this->options['max']) { - //重命名 - rename($this->options['dir'] . $this->fileName, $this->options['dir'] . time() . '.log'); - } - $file = fopen($this->options['dir'] . $this->fileName, 'ab'); - fwrite($file, $data); - fclose($file); - } - } -} \ No newline at end of file diff --git a/src/blankphp/Log/Logger.php b/src/blankphp/Log/Logger.php index 15a710a..1738dd7 100644 --- a/src/blankphp/Log/Logger.php +++ b/src/blankphp/Log/Logger.php @@ -5,17 +5,18 @@ use BlankPhp\Application; +use BlankPhp\Exception\NotFoundClassException; use BlankQwq\Helpers\Str; -use Monolog\Handler\StreamHandler; -use Monolog\Logger as LoggerBase; -use Psr\Log\AbstractLogger; +use Monolog\Formatter\LineFormatter; class Logger { protected $handler; protected $config = [ - 'level' => 'debug', + 'format'=>'[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n', + 'time_format'=>'Y-m-d H:i:s' ]; + private const NAME_SPACE = __NAMESPACE__ . '/Driver'; public function __construct(Application $app) { @@ -25,11 +26,33 @@ public function __construct(Application $app) private function getHandler(): Log { - $level = strtoupper($this->config['level']); - return new Log(new StreamHandler($this->config['path'], $level)); + $channel = $this->config['default']; + $config = $this->config[$channel]; + $driver = $this->config['driver']; + $formatter = new LineFormatter($this->config['format'], $this->config['time_format']); + if (class_exists($driver)) { + $driver = new $driver($config); + }else{ + $driver = $this->getDriver($config); + } + $driver->setFormatter($formatter); + return $this->handler = $driver ; } - private function formatFileName(){ + + private function getDriver($name) + { + $className = Str::makeClassName($name, self::NAME_SPACE); + if (class_exists($className)) { + // 生产对象 +// 需要设置值 + + } + throw new NotFoundClassException($className); + } + + private function formatFileName() + { } diff --git a/src/blankphp/Proxy/Proxy.php b/src/blankphp/Proxy/Proxy.php index 16e2c15..3b6ba88 100644 --- a/src/blankphp/Proxy/Proxy.php +++ b/src/blankphp/Proxy/Proxy.php @@ -9,11 +9,12 @@ class Proxy { use GetAllMethod; + public $name; + protected $origin; protected $methods = []; public function __call($name, $arguments) { - // TODO: Implement __call() method. } } \ No newline at end of file From 528ab9ec16eb6348c63bf0d6288bb58b0a08bac6 Mon Sep 17 00:00:00 2001 From: blank <1136589038@qq.com> Date: 2022年1月13日 17:39:39 +0800 Subject: [PATCH 15/15] update request --- src/blankphp/Request/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blankphp/Request/Request.php b/src/blankphp/Request/Request.php index 36a0db6..46078d7 100644 --- a/src/blankphp/Request/Request.php +++ b/src/blankphp/Request/Request.php @@ -68,7 +68,7 @@ public function stripSlashesDeep($value) return $value; } - public function get($name = '', array $optionm = []) + public function get($name = '', $default = null) { $this->{'_' . strtolower($this->method)}(); if (isset($this->request[strtolower($this->method)][$name])) {

AltStyle によって変換されたページ (->オリジナル) /