diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8be29a7..eb4aa35 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,7 +22,7 @@ jobs: swoole-version: 'v4.6.7' - php-version: '8.1' swoole-version: 'v4.7.1' - max-parallel: 12 + max-parallel: 16 steps: - name: Checkout uses: actions/checkout@v2 diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index e94fa0e..f10c2a6 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -31,6 +31,11 @@ 'concat_space' => [ 'spacing' => 'one' ], + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => null, + ], 'blank_line_before_statement' => [ 'statements' => [ 'declare', diff --git a/composer.json b/composer.json index 09f852c..567eeaa 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,14 @@ "php": ">=8.0", "hyperf/command": "^3.0.0", "hyperf/config": "^3.0.0", + "hyperf/context": "^3.0.16", + "hyperf/contract": "^3.0.0", "hyperf/di": "^3.0.0", "hyperf/framework": "^3.0.0", "hyperf/http-server": "^3.0.0", - "hyperf/utils": "^3.0.0" + "hyperf/stringable": "^3.0.0", + "hyperf/support": "^3.0.0", + "hyperf/tappable": "^3.0.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", diff --git a/example/index.php b/example/index.php index 5a350a2..e21e8eb 100644 --- a/example/index.php +++ b/example/index.php @@ -11,6 +11,7 @@ */ namespace Hyperf\Nano; +use Exception; use Hyperf\Command\Command; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\DB\DB; @@ -18,6 +19,8 @@ use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\Nano\Factory\AppFactory; +use function Hyperf\Support\env; + require_once __DIR__ . '/../vendor/autoload.php'; interface FooInterface @@ -83,7 +86,7 @@ public function bar(): string }); $app->get('/exception', function () { - throw new \Exception(); + throw new Exception(); }); $app->addExceptionHandler(function ($throwable, $response) { diff --git a/src/App.php b/src/App.php index 3a4a882..ce680f5 100644 --- a/src/App.php +++ b/src/App.php @@ -28,6 +28,9 @@ use Psr\EventDispatcher\ListenerProviderInterface; use Psr\Http\Server\MiddlewareInterface; +use function Hyperf\Support\call; +use function Hyperf\Tappable\tap; + /** * @method get($route, $handler, array $options = []) * @method post($route, $handler, array $options = []) @@ -58,7 +61,7 @@ public function __construct(protected ContainerInterface $container) public function __call($name, $arguments) { $router = $this->dispatcherFactory->getRouter($this->serverName); - if ($arguments[1] instanceof \Closure) { + if ($arguments[1] instanceof Closure) { $arguments[1] = $arguments[1]->bindTo($this->bound, $this->bound); } return $router->{$name}(...$arguments); @@ -209,7 +212,7 @@ public function addCrontab(string $rule, callable|string $crontab): Crontab return $crontab; } - $callback = \Closure::fromCallable($crontab); + $callback = Closure::fromCallable($crontab); $callback = $callback->bindTo($this->bound, $this->bound); $callbackId = spl_object_hash($callback); $this->container->set($callbackId, $callback); @@ -240,7 +243,7 @@ public function addProcess(callable|string $process): AbstractProcess|ClosurePro return $this->container->get($process); } - $callback = \Closure::fromCallable($process); + $callback = Closure::fromCallable($process); $callback = $callback->bindTo($this->bound, $this->bound); $processFactory = $this->container->get(ProcessFactory::class); @@ -265,7 +268,7 @@ public function addRoute($httpMethod, string $route, $handler, array $options = if (isset($options['middleware'])) { $this->convertClosureToMiddleware($options['middleware']); } - if ($handler instanceof \Closure) { + if ($handler instanceof Closure) { $handler = $handler->bindTo($this->bound, $this->bound); } $router->addRoute($httpMethod, $route, $handler, $options); @@ -323,7 +326,7 @@ private function convertClosureToMiddleware(array &$middlewares): void { $middlewareFactory = $this->container->get(MiddlewareFactory::class); foreach ($middlewares as &$middleware) { - if ($middleware instanceof \Closure) { + if ($middleware instanceof Closure) { $middleware = $middleware->bindTo($this->bound, $this->bound); $middleware = $middlewareFactory->create($middleware); } diff --git a/src/Factory/AppFactory.php b/src/Factory/AppFactory.php index a03b235..07132a6 100644 --- a/src/Factory/AppFactory.php +++ b/src/Factory/AppFactory.php @@ -16,6 +16,7 @@ use Dotenv\Repository\RepositoryBuilder; use Hyperf\Config\Config; use Hyperf\Config\ProviderConfig; +use Hyperf\Context\ApplicationContext; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\ContainerInterface; use Hyperf\Contract\StdoutLoggerInterface; @@ -27,8 +28,10 @@ use Hyperf\Nano\BoundInterface; use Hyperf\Nano\ContainerProxy; use Hyperf\Nano\Preset\Preset; -use Hyperf\Utils\ApplicationContext; use Psr\Log\LogLevel; +use ReflectionClass; + +use function Hyperf\Support\env; class AppFactory { @@ -138,7 +141,7 @@ protected static function prepareFlags(int $hookFlags = SWOOLE_HOOK_ALL): void ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); error_reporting(E_ALL); - $reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class); + $reflection = new ReflectionClass(\Composer\Autoload\ClassLoader::class); $projectRootPath = dirname($reflection->getFileName(), 3); ! defined('BASE_PATH') && define('BASE_PATH', $projectRootPath); ! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', $hookFlags); diff --git a/src/Factory/ClosureCommand.php b/src/Factory/ClosureCommand.php index f958ae0..f66d02c 100644 --- a/src/Factory/ClosureCommand.php +++ b/src/Factory/ClosureCommand.php @@ -11,14 +11,17 @@ */ namespace Hyperf\Nano\Factory; +use Closure; use Hyperf\Command\Command; use Psr\Container\ContainerInterface; +use function Hyperf\Support\call; + class ClosureCommand extends Command { protected ParameterParser $parameterParser; - public function __construct(protected ContainerInterface $container, protected ?string $signature, private \Closure $closure) + public function __construct(protected ContainerInterface $container, protected ?string $signature, private Closure $closure) { $this->parameterParser = $container->get(ParameterParser::class); parent::__construct(); diff --git a/src/Factory/ClosureProcess.php b/src/Factory/ClosureProcess.php index b7840d6..f384315 100644 --- a/src/Factory/ClosureProcess.php +++ b/src/Factory/ClosureProcess.php @@ -11,9 +11,12 @@ */ namespace Hyperf\Nano\Factory; +use Closure; use Hyperf\Process\AbstractProcess; use Psr\Container\ContainerInterface; +use function Hyperf\Support\call; + class ClosureProcess extends AbstractProcess { /** @@ -21,7 +24,7 @@ class ClosureProcess extends AbstractProcess */ private $enable; - public function __construct(ContainerInterface $container, private \Closure $closure) + public function __construct(ContainerInterface $container, private Closure $closure) { parent::__construct($container); } diff --git a/src/Factory/CommandFactory.php b/src/Factory/CommandFactory.php index debded8..f70af9f 100644 --- a/src/Factory/CommandFactory.php +++ b/src/Factory/CommandFactory.php @@ -11,12 +11,13 @@ */ namespace Hyperf\Nano\Factory; +use Closure; use Hyperf\Command\Command; -use Hyperf\Utils\ApplicationContext; +use Hyperf\Context\ApplicationContext; class CommandFactory { - public function create(string $signature, \Closure $closure): Command + public function create(string $signature, Closure $closure): Command { $container = ApplicationContext::getContainer(); diff --git a/src/Factory/ExceptionHandlerFactory.php b/src/Factory/ExceptionHandlerFactory.php index 96096fc..87691f3 100644 --- a/src/Factory/ExceptionHandlerFactory.php +++ b/src/Factory/ExceptionHandlerFactory.php @@ -11,21 +11,24 @@ */ namespace Hyperf\Nano\Factory; +use Closure; use Hyperf\ExceptionHandler\ExceptionHandler; use Psr\Http\Message\ResponseInterface; use Throwable; +use function Hyperf\Support\call; + class ExceptionHandlerFactory { - public function create(\Closure $closure): ExceptionHandler + public function create(Closure $closure): ExceptionHandler { return new class($closure) extends ExceptionHandler { /** - * @var \Closure + * @var Closure */ private $closure; - public function __construct(\Closure $closure) + public function __construct(Closure $closure) { $this->closure = $closure; } diff --git a/src/Factory/MiddlewareFactory.php b/src/Factory/MiddlewareFactory.php index 4b8bd91..3b27805 100644 --- a/src/Factory/MiddlewareFactory.php +++ b/src/Factory/MiddlewareFactory.php @@ -11,22 +11,25 @@ */ namespace Hyperf\Nano\Factory; +use Closure; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use function Hyperf\Support\call; + class MiddlewareFactory { - public function create(\Closure $closure): MiddlewareInterface + public function create(Closure $closure): MiddlewareInterface { return new class($closure) implements MiddlewareInterface { /** - * @var \Closure + * @var Closure */ private $closure; - public function __construct(\Closure $closure) + public function __construct(Closure $closure) { $this->closure = $closure; } diff --git a/src/Factory/ParameterParser.php b/src/Factory/ParameterParser.php index f7747f2..bba6176 100644 --- a/src/Factory/ParameterParser.php +++ b/src/Factory/ParameterParser.php @@ -15,7 +15,8 @@ use Hyperf\Contract\NormalizerInterface; use Hyperf\Di\ClosureDefinitionCollectorInterface; use Hyperf\Di\MethodDefinitionCollectorInterface; -use Hyperf\Utils\Str; +use Hyperf\Stringable\Str; +use InvalidArgumentException; use Psr\Container\ContainerInterface; class ParameterParser @@ -40,7 +41,7 @@ public function __construct(private ContainerInterface $container) } /** - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function parseClosureParameters(Closure $closure, array $arguments): array { @@ -64,7 +65,7 @@ public function parseMethodParameters(string $class, string $method, array $argu } /** - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ private function getInjections(array $definitions, string $callableName, array $arguments): array { @@ -80,7 +81,7 @@ private function getInjections(array $definitions, string $callableName, array $ } elseif ($definition->allowsNull()) { $injections[] = null; } else { - throw new \InvalidArgumentException("Parameter '{$definition->getMeta('name')}' " + throw new InvalidArgumentException("Parameter '{$definition->getMeta('name')}' " . "of {$callableName} should not be null"); } } else { diff --git a/src/Factory/ProcessFactory.php b/src/Factory/ProcessFactory.php index e439839..909e8a4 100644 --- a/src/Factory/ProcessFactory.php +++ b/src/Factory/ProcessFactory.php @@ -11,11 +11,12 @@ */ namespace Hyperf\Nano\Factory; -use Hyperf\Utils\ApplicationContext; +use Closure; +use Hyperf\Context\ApplicationContext; class ProcessFactory { - public function create(\Closure $closure): ClosureProcess + public function create(Closure $closure): ClosureProcess { $container = ApplicationContext::getContainer();