Skip to content

Commit

Permalink
Merge pull request #96 from inhere/master
Browse files Browse the repository at this point in the history
some modify
  • Loading branch information
inhere authored Apr 4, 2018
2 parents 914ad2b + 4a65207 commit b4d5c38
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 124 deletions.
59 changes: 29 additions & 30 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
* 应用对象
*
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Core/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RequestContext
private static $context;

/**
* @return \Psr\Http\Message\RequestInterface|null
* @return \Psr\Http\Message\ServerRequestInterface|null
*/
public static function getRequest()
{
Expand Down
20 changes: 9 additions & 11 deletions src/Core/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}

Expand All @@ -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());
}

Expand All @@ -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];
Expand Down
34 changes: 17 additions & 17 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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, '. ');

Expand All @@ -64,7 +61,7 @@ public static function checkName(string $name)
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return $this->name;
}
Expand All @@ -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;
}
Expand All @@ -102,7 +99,7 @@ public function addParams(array $params)
* get all param
* @return array
*/
public function getParams()
public function getParams(): array
{
return $this->params;
}
Expand All @@ -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);
Expand All @@ -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.');
Expand All @@ -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]);
}
Expand Down Expand Up @@ -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]);
}

/**
Expand All @@ -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);
}
Expand Down
9 changes: 3 additions & 6 deletions src/Event/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,7 +15,7 @@ interface EventInterface
*
* @return string
*/
public function getName();
public function getName(): string;

/**
* Get target/context from which event was triggered
Expand All @@ -32,7 +29,7 @@ public function getTarget();
*
* @return array
*/
public function getParams();
public function getParams(): array ;

/**
* Get a single parameter by name
Expand Down Expand Up @@ -78,5 +75,5 @@ public function stopPropagation($flag);
*
* @return bool
*/
public function isPropagationStopped();
public function isPropagationStopped(): bool ;
}
Loading

0 comments on commit b4d5c38

Please sign in to comment.