-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from inhere/master
some modify
- Loading branch information
Showing
12 changed files
with
153 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,20 +11,16 @@ | |
use Swoft\Exception\InvalidArgumentException; | ||
use Swoft\Log\Logger; | ||
use Swoft\Pool\PoolInterface; | ||
use Swoft\Redis\Pool\RedisPool; | ||
use Swoole\Coroutine as SwCoroutine; | ||
|
||
/** | ||
* 应用简写类 | ||
* | ||
* @uses App | ||
* @version 2017年04月25日 | ||
* @author stelin <[email protected]> | ||
* @copyright Copyright 2010-2016 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
*/ | ||
class App | ||
{ | ||
|
||
/** | ||
* 应用对象 | ||
* | ||
|
@@ -264,20 +260,19 @@ public static function setAliases(array $aliases) | |
*/ | ||
public static function setAlias(string $alias, string $path = null) | ||
{ | ||
if (strncmp($alias, '@', 1)) { | ||
if ($alias[0] !== '@') { | ||
$alias = '@' . $alias; | ||
} | ||
|
||
// 删除别名 | ||
// Delete alias | ||
if (!$path) { | ||
unset(self::$aliases[$alias]); | ||
|
||
return; | ||
} | ||
|
||
// $path不是别名,直接设置 | ||
$isAlias = strpos($path, '@'); | ||
if ($isAlias === false) { | ||
// $path 不是别名,直接设置 | ||
if ($path[0] !== '@') { | ||
self::$aliases[$alias] = $path; | ||
|
||
return; | ||
|
@@ -292,7 +287,7 @@ public static function setAlias(string $alias, string $path = null) | |
|
||
list($root) = explode('/', $path); | ||
if (!isset(self::$aliases[$root])) { | ||
throw new \InvalidArgumentException('设置的根别名不存在,alias=' . $root); | ||
throw new \InvalidArgumentException('The set root alias does not exist,alias=' . $root); | ||
} | ||
|
||
$rootPath = self::$aliases[$root]; | ||
|
@@ -309,28 +304,26 @@ public static function setAlias(string $alias, string $path = null) | |
* @return string | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function getAlias($alias): string | ||
public static function getAlias(string $alias): string | ||
{ | ||
if (isset(self::$aliases[$alias])) { | ||
return self::$aliases[$alias]; | ||
} | ||
|
||
// $path不是别名,直接返回 | ||
$isAlias = strpos($alias, '@'); | ||
if ($isAlias === false) { | ||
if ($alias[0] !== '@') { | ||
return $alias; | ||
} | ||
|
||
list($root) = explode('/', $alias); | ||
if (isset(self::$aliases[$alias])) { | ||
return self::$aliases[$alias]; | ||
} | ||
|
||
list($root) = \explode('/', $alias); | ||
if (!isset(self::$aliases[$root])) { | ||
throw new \InvalidArgumentException('设置的根别名不存在,alias=' . $root); | ||
throw new \InvalidArgumentException('The set root alias does not exist,alias=' . $root); | ||
} | ||
|
||
$rootPath = self::$aliases[$root]; | ||
$aliasPath = str_replace($root, '', $alias); | ||
$path = $rootPath . $aliasPath; | ||
$aliasPath = \str_replace($root, '', $alias); | ||
|
||
return $path; | ||
return $rootPath . $aliasPath; | ||
} | ||
|
||
/** | ||
|
@@ -427,9 +420,10 @@ public static function isWorkerStatus(): bool | |
if (self::$server === null) { | ||
return false; | ||
} | ||
|
||
$server = self::$server->getServer(); | ||
|
||
if ($server !== null && property_exists($server, 'taskworker') && $server->taskworker === false) { | ||
if ($server && property_exists($server, 'taskworker') && ($server->taskworker === false)) { | ||
return true; | ||
} | ||
|
||
|
@@ -444,9 +438,10 @@ public static function getWorkerId(): int | |
if (self::$server === null) { | ||
return 0; | ||
} | ||
|
||
$server = self::$server->getServer(); | ||
|
||
if ($server !== null && property_exists($server, 'worker_id') && $server->worker_id > 0) { | ||
if ($server && \property_exists($server, 'worker_id') && $server->worker_id > 0) { | ||
return $server->worker_id; | ||
} | ||
|
||
|
@@ -460,11 +455,7 @@ public static function getWorkerId(): int | |
*/ | ||
public static function isCoContext(): bool | ||
{ | ||
if (SwCoroutine::getuid() > 0) { | ||
return true; | ||
} | ||
|
||
return false; | ||
return SwCoroutine::getuid() > 0; | ||
} | ||
|
||
/** | ||
|
@@ -478,4 +469,12 @@ public static function counting(string $name, int $hit, $total = null) | |
{ | ||
self::getLogger()->counting($name, $hit, $total); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getAliases(): array | ||
{ | ||
return self::$aliases; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,7 @@ | |
|
||
/** | ||
* request handler | ||
* | ||
* @uses RequestHandler | ||
* @version 2017年11月14日 | ||
* @author huangzhhui <[email protected]> | ||
* @copyright Copyright 2010-2016 swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
*/ | ||
class RequestHandler implements RequestHandlerInterface | ||
{ | ||
|
@@ -42,7 +37,7 @@ class RequestHandler implements RequestHandlerInterface | |
*/ | ||
public function __construct(array $middleware, string $default) | ||
{ | ||
$this->middlewares = array_unique($middleware); | ||
$this->middlewares = \array_unique($middleware); | ||
$this->default = $default; | ||
} | ||
|
||
|
@@ -51,18 +46,21 @@ public function __construct(array $middleware, string $default) | |
* | ||
* @param ServerRequestInterface $request | ||
* @return ResponseInterface | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
if (empty($this->middlewares[$this->offset]) && !empty($this->default)) { | ||
if (!empty($this->default) && empty($this->middlewares[$this->offset])) { | ||
$handler = App::getBean($this->default); | ||
} else { | ||
$handler = $this->middlewares[$this->offset]; | ||
is_string($handler) && $handler = App::getBean($handler); | ||
\is_string($handler) && $handler = App::getBean($handler); | ||
} | ||
if (! $handler instanceof MiddlewareInterface) { | ||
throw new \InvalidArgumentException('Invalid Handler'); | ||
|
||
if (!$handler instanceof MiddlewareInterface) { | ||
throw new \InvalidArgumentException('Invalid Handler. It must be an instance of MiddlewareInterface'); | ||
} | ||
|
||
return $handler->process($request, $this->next()); | ||
} | ||
|
||
|
@@ -87,7 +85,7 @@ private function next() | |
*/ | ||
public function insertMiddlewares(array $middlewares, $offset = null) | ||
{ | ||
is_null($offset) && $offset = $this->offset; | ||
null === $offset && $offset = $this->offset; | ||
$chunkArray = array_chunk($this->middlewares, $offset); | ||
$after = []; | ||
$before = $chunkArray[0]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,7 @@ | |
/** | ||
* Class Event | ||
* @package Swoft\Event | ||
* @version 2017年08月30日 | ||
* @author inhere <[email protected]> | ||
* @copyright Copyright 2010-2016 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
*/ | ||
class Event implements EventInterface, \ArrayAccess, \Serializable | ||
{ | ||
|
@@ -50,7 +47,7 @@ public function __construct($name = null, array $params = []) | |
* @return string | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function checkName(string $name) | ||
public static function checkName(string $name): string | ||
{ | ||
$name = trim($name, '. '); | ||
|
||
|
@@ -64,7 +61,7 @@ public static function checkName(string $name) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
@@ -91,9 +88,9 @@ public function setParams(array $params) | |
* @param array $params | ||
* @return $this | ||
*/ | ||
public function addParams(array $params) | ||
public function addParams(array $params): self | ||
{ | ||
$this->params = array_merge($this->params, $params); | ||
$this->params = \array_merge($this->params, $params); | ||
|
||
return $this; | ||
} | ||
|
@@ -102,7 +99,7 @@ public function addParams(array $params) | |
* get all param | ||
* @return array | ||
*/ | ||
public function getParams() | ||
public function getParams(): array | ||
{ | ||
return $this->params; | ||
} | ||
|
@@ -125,7 +122,7 @@ public function clearParams() | |
* @return $this | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function addParam($name, $value) | ||
public function addParam($name, $value): self | ||
{ | ||
if (!isset($this->params[$name])) { | ||
$this->setParam($name, $value); | ||
|
@@ -141,7 +138,7 @@ public function addParam($name, $value) | |
* @throws \InvalidArgumentException If the argument name is null. | ||
* @return $this | ||
*/ | ||
public function setParam($name, $value) | ||
public function setParam($name, $value): self | ||
{ | ||
if (null === $name) { | ||
throw new \InvalidArgumentException('The argument name cannot be null.'); | ||
|
@@ -166,7 +163,7 @@ public function getParam($name, $default = null) | |
* @param $name | ||
* @return bool | ||
*/ | ||
public function hasParam($name) | ||
public function hasParam($name): bool | ||
{ | ||
return isset($this->params[$name]); | ||
} | ||
|
@@ -214,17 +211,17 @@ public function stopPropagation($flag) | |
* Has this event indicated event propagation should stop? | ||
* @return bool | ||
*/ | ||
public function isPropagationStopped() | ||
public function isPropagationStopped(): bool | ||
{ | ||
return $this->stopPropagation; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function serialize() | ||
public function serialize(): string | ||
{ | ||
return serialize(array($this->name, $this->params, $this->stopPropagation)); | ||
return \serialize([$this->name, $this->params, $this->stopPropagation]); | ||
} | ||
|
||
/** | ||
|
@@ -234,16 +231,19 @@ public function serialize() | |
*/ | ||
public function unserialize($serialized) | ||
{ | ||
// ['allowed_class' => null] | ||
list($this->name, $this->params, $this->stopPropagation) = unserialize($serialized, []); | ||
list( | ||
$this->name, | ||
$this->params, | ||
$this->stopPropagation | ||
) = \unserialize($serialized, ['allowed_classes' => false]); | ||
} | ||
|
||
/** | ||
* Tell if the given event argument exists. | ||
* @param string $name The argument name. | ||
* @return boolean True if it exists, false otherwise. | ||
*/ | ||
public function offsetExists($name) | ||
public function offsetExists($name): bool | ||
{ | ||
return $this->hasParam($name); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,7 @@ | |
* Interface EventInterface - Representation of an event | ||
* @package Swoft\Event | ||
* @link https://github.com/php-fig/fig-standards/blob/master/proposed/event-manager.md | ||
* @version 2017年08月30日 | ||
* @author inhere <[email protected]> | ||
* @copyright Copyright 2010-2016 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
*/ | ||
interface EventInterface | ||
{ | ||
|
@@ -18,7 +15,7 @@ interface EventInterface | |
* | ||
* @return string | ||
*/ | ||
public function getName(); | ||
public function getName(): string; | ||
|
||
/** | ||
* Get target/context from which event was triggered | ||
|
@@ -32,7 +29,7 @@ public function getTarget(); | |
* | ||
* @return array | ||
*/ | ||
public function getParams(); | ||
public function getParams(): array ; | ||
|
||
/** | ||
* Get a single parameter by name | ||
|
@@ -78,5 +75,5 @@ public function stopPropagation($flag); | |
* | ||
* @return bool | ||
*/ | ||
public function isPropagationStopped(); | ||
public function isPropagationStopped(): bool ; | ||
} |
Oops, something went wrong.