Skip to content

Commit

Permalink
some format update ...
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 19, 2018
1 parent cebd7a7 commit 337f827
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 51 deletions.
7 changes: 3 additions & 4 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ class App
*
* @var array
*/
private static $aliases
= [
'@swoft' => __DIR__,
];
private static $aliases = [
'@swoft' => __DIR__,
];

/**
* 获取mysqlBean对象
Expand Down
35 changes: 7 additions & 28 deletions src/Bootstrap/Server/ServerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait ServerTrait
*/
protected function beforeServerStart()
{
$this->fireServerListener(SwooleEvent::ON_BEFORE_START, [$this]);
$this->fireServerEvent(SwooleEvent::ON_BEFORE_START, [$this]);
}

/**
Expand All @@ -41,7 +41,7 @@ public function onStart(Server $server)

ProcessHelper::setProcessTitle($this->serverSetting['pname'] . ' master process (' . $this->scriptFile . ')');

$this->fireServerListener(SwooleEvent::ON_START, [$server]);
$this->fireServerEvent(SwooleEvent::ON_START, [$server]);
}

/**
Expand All @@ -52,7 +52,7 @@ public function onStart(Server $server)
*/
public function onManagerStart(Server $server)
{
$this->fireServerListener(SwooleEvent::ON_MANAGER_START, [$server]);
$this->fireServerEvent(SwooleEvent::ON_MANAGER_START, [$server]);

ProcessHelper::setProcessTitle($this->serverSetting['pname'] . ' manager process');
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public function onWorkerStart(Server $server, int $workerId)
ProcessHelper::setProcessTitle($this->serverSetting['pname'] . ' worker process');
}

$this->fireServerListener(SwooleEvent::ON_WORKER_START, [$server, $workerId, $isWorker]);
$this->fireServerEvent(SwooleEvent::ON_WORKER_START, [$server, $workerId, $isWorker]);
$this->beforeWorkerStart($server, $workerId, $isWorker);
}

Expand Down Expand Up @@ -144,7 +144,7 @@ protected function reloadBean(bool $isWorker)
* @param string $event
* @param array $params
*/
protected function fireServerListener(string $event, array $params)
protected function fireServerEvent(string $event, array $params)
{
/** @var array[] $collector */
$collector = ServerListenerCollector::getCollector();
Expand All @@ -153,31 +153,10 @@ protected function fireServerListener(string $event, array $params)
return;
}

foreach ($collector[$event] as $listenerBeanName) {
$listener = App::getBean($listenerBeanName);
foreach ($collector[$event] as $beanClass) {
$listener = App::getBean($beanClass);
$method = SwooleEvent::getHandlerFunction($event);
$listener->$method(...$params);
}
}

/**
* fire swoole event listeners
*
* @param string $event
* @param array $params
* @param string $type
*/
protected function fireSwooleListener(string $event, array $params, string $type = SwooleEvent::TYPE_SERVER)
{
$collector = SwooleListenerCollector::getCollector();

if (!isset($collector[$type][$event]) || empty($collector[$type][$event])) {
return;
}

$beanClass = $collector[$type][$event];
$listener = App::getBean($beanClass);
$method = SwooleEvent::getHandlerFunction($event);
$listener->$method(...$params);
}
}
11 changes: 6 additions & 5 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public static function merge($a, $b)
* getting value from an object.
*
* @return mixed the value of the element if found, default value otherwise
* @throws InvalidParamException if $array is neither an array nor an object.
*/
public static function getValue($array, $key, $default = null)
{
Expand All @@ -156,18 +155,20 @@ public static function getValue($array, $key, $default = null)

if (($pos = strrpos($key, '.')) !== false) {
$array = static::getValue($array, substr($key, 0, $pos), $default);
$key = substr($key, $pos + 1);
$key = (string)substr($key, $pos + 1);
}

if (\is_object($array)) {
// this is expected to fail if the property does not exist, or __get() is not implemented
// it is not reliably possible to check whether a property is accessable beforehand
return $array->$key;
} elseif (\is_array($array)) {
}

if (\is_array($array)) {
return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default;
} else {
return $default;
}

return $default;
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/Helper/ComponentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function getComponentNamespace(string $component, string $path):st
*/
public static function getComponentNs(string $component): string
{
if ($component == 'framework') {
if ($component === 'framework') {
return '';
}

Expand All @@ -48,11 +48,14 @@ public static function getComponentNs(string $component): string
private static function parseAutoloadFromComposerFile($filename): array
{
$json = file_get_contents($filename);
$mapping = [];

try {
$content = JsonHelper::decode($json, true);
} catch (\InvalidArgumentException $e) {
$content = [];
}

// only compatible with psr-4 now
//TODO compatible with the another autoload standard
if (isset($content['autoload']['psr-4'])) {
Expand All @@ -63,6 +66,7 @@ private static function parseAutoloadFromComposerFile($filename): array
$mapping[$key] = $value[$valueLength - 1] === '\\' ? substr($value, 0, $valueLength - 1) : $value;
}
}

return \is_array($mapping) ? $mapping : [];
}

Expand All @@ -75,6 +79,7 @@ private static function getDefaultNamespace($component): string
$componentNs = ComponentHelper::getComponentNs($component);
$componentNs = self::handlerFrameworkNamespace($componentNs);
$namespace = "Swoft{$componentNs}";

return $namespace;
}

Expand All @@ -85,9 +90,10 @@ private static function getDefaultNamespace($component): string
*/
private static function handlerFrameworkNamespace(string $componentNs):string
{
if($componentNs == '\Swoft\Framework'){
return "";
if($componentNs === '\Swoft\Framework'){
return '';
}

return $componentNs;
}
}
}
9 changes: 4 additions & 5 deletions src/Helper/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class FileHelper
{

/**
* 获得文件扩展名、后缀名
* @param $filename
Expand All @@ -34,13 +33,13 @@ public static function getSuffix($filename, $clearPoint = false): string
*/
public static function isAbsPath($path): bool
{
if (!$path || !is_string($path)) {
if (!$path || !\is_string($path)) {
return false;
}

if (
$path{0} === '/' || // linux/mac
1 === preg_match('#^[a-z]:[\/|\\\]{1}.+#i', $path) // windows
1 === \preg_match('#^[a-z]:[\/|\\\]{1}.+#i', $path) // windows
) {
return true;
}
Expand All @@ -58,13 +57,13 @@ public static function isAbsPath($path): bool
public static function md5File($dir)
{
if (!is_dir($dir)) {
return "";
return '';
}

$md5File = array();
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if ($entry != '.' && $entry != '..') {
if ($entry !== '.' && $entry !== '..') {
if (is_dir($dir . '/' . $entry)) {
$md5File[] = self::md5File($dir . '/' . $entry);
} elseif (substr($entry, -4) === '.php') {
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/JsonHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function decode($json, $assoc = false, $depth = 512, $options = 0)
* @throws \InvalidArgumentException if the JSON cannot be encoded.
* @link http://www.php.net/manual/en/function.json-encode.php
*/
public static function encode($value, $options = 0, $depth = 512)
public static function encode($value, $options = 0, $depth = 512): string
{
if ($value instanceof Arrayable) {
$value = $value->toArray();
Expand Down
8 changes: 4 additions & 4 deletions src/Helper/PhpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function isCli(): bool
*
* @return bool
*/
public static function isMac()
public static function isMac(): bool
{
return stripos(PHP_OS, 'Darwin') !== false;
}
Expand All @@ -43,11 +43,11 @@ public static function isMac()
*/
public static function call($cb, array $args = [])
{
if (is_object($cb) || (is_string($cb) && function_exists($cb))) {
if (\is_object($cb) || (\is_string($cb) && \function_exists($cb))) {
$ret = $cb(...$args);
} elseif (is_array($cb)) {
} elseif (\is_array($cb)) {
list($obj, $mhd) = $cb;
$ret = is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
$ret = \is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
} else {
$ret = \Swoole\Coroutine::call_user_func_array($cb, $args);
}
Expand Down

0 comments on commit 337f827

Please sign in to comment.