mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-03 20:27:59 +08:00
Removed nano and swoole-enterprise.
This commit is contained in:
parent
78d2df0b85
commit
3c4749748e
1
src/nano/.gitattributes
vendored
1
src/nano/.gitattributes
vendored
@ -1 +0,0 @@
|
||||
/tests export-ignore
|
@ -1,21 +0,0 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Hyperf
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -1,252 +0,0 @@
|
||||
# nano
|
||||
|
||||
Scaling Hyperf down to a single file.
|
||||
|
||||
## Example
|
||||
|
||||
```php
|
||||
<?php
|
||||
// index.php
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create('0.0.0.0', 9051);
|
||||
|
||||
$app->get('/', function () {
|
||||
|
||||
$user = $this->request->input('user', 'nano');
|
||||
$method = $this->request->getMethod();
|
||||
|
||||
return [
|
||||
'message' => "hello {$user}",
|
||||
'method' => $method,
|
||||
];
|
||||
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
Run
|
||||
|
||||
```bash
|
||||
php index.php start
|
||||
```
|
||||
|
||||
That's all you need.
|
||||
|
||||
## Feature
|
||||
|
||||
* No skeleton.
|
||||
* Fast startup.
|
||||
* Zero config.
|
||||
* Closure style.
|
||||
* Support all Hyperf features except annotations.
|
||||
* Compatible with all Hyperf components.
|
||||
|
||||
## More Examples
|
||||
|
||||
### Routing
|
||||
|
||||
$app inherits all methods from hyperf router.
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->addGroup('/nano', function () use ($app) {
|
||||
$app->addRoute(['GET', 'POST'], '/{id:\d+}', function($id) {
|
||||
return '/nano/'.$id;
|
||||
});
|
||||
$app->put('/{name:.+}', function($name) {
|
||||
return '/nano/'.$name;
|
||||
});
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
### DI Container
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Nano\ContainerProxy;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
class Foo {
|
||||
public function bar() {
|
||||
return 'bar';
|
||||
}
|
||||
}
|
||||
|
||||
$app = AppFactory::create();
|
||||
$app->getContainer()->set(Foo::class, new Foo());
|
||||
|
||||
$app->get('/', function () {
|
||||
/** @var ContainerProxy $this */
|
||||
$foo = $this->get(Foo::class);
|
||||
return $foo->bar();
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
> As a convention, $this is bind to ContainerProxy in all closures managed by nano, including middleware, exception handler and more.
|
||||
|
||||
### Middleware
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->get('/', function () {
|
||||
return $this->request->getAttribute('key');
|
||||
});
|
||||
|
||||
$app->addMiddleware(function ($request, $handler) {
|
||||
$request = $request->withAttribute('key', 'value');
|
||||
return $handler->handle($request);
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
> In addition to closure, all $app->addXXX() methods also accept class name as argument. You can pass any corresponding hyperf classes.
|
||||
|
||||
### ExceptionHandler
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->get('/', function () {
|
||||
throw new \Exception();
|
||||
});
|
||||
|
||||
$app->addExceptionHandler(function ($throwable, $response) {
|
||||
return $response->withStatus('418')
|
||||
->withBody(new SwooleStream('I\'m a teapot'));
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
### Custom Command
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->addCommand('echo', function(){
|
||||
$this->get(StdoutLoggerInterface::class)->info('A new command called echo!');
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
To run this command, execute
|
||||
```bash
|
||||
php index.php echo
|
||||
```
|
||||
|
||||
### Event Listener
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Framework\Event\BootApplication;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->addListener(BootApplication::class, function($event){
|
||||
$this->get(StdoutLoggerInterface::class)->info('App started');
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
### Custom Process
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->addProcess(function(){
|
||||
while (true) {
|
||||
sleep(1);
|
||||
$this->get(StdoutLoggerInterface::class)->info('Processing...');
|
||||
}
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
### Crontab
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->addCrontab('* * * * * *', function(){
|
||||
$this->get(StdoutLoggerInterface::class)->info('execute every second!');
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
||||
|
||||
### Use Hyperf Component.
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Hyperf\DB\DB;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->config([
|
||||
'db.default' => [
|
||||
'host' => env('DB_HOST', 'localhost'),
|
||||
'port' => env('DB_PORT', 3306),
|
||||
'database' => env('DB_DATABASE', 'hyperf'),
|
||||
'username' => env('DB_USERNAME', 'root'),
|
||||
'password' => env('DB_PASSWORD', ''),
|
||||
]
|
||||
]);
|
||||
|
||||
$app->get('/', function(){
|
||||
return DB::query('SELECT * FROM `user` WHERE gender = ?;', [1]);
|
||||
});
|
||||
|
||||
$app->run();
|
||||
```
|
@ -1,54 +0,0 @@
|
||||
{
|
||||
"name": "hyperf/nano",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"php",
|
||||
"hyperf"
|
||||
],
|
||||
"description": "Scale Hyperf application down to a single file",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Hyperf\\Nano\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.2",
|
||||
"ext-swoole": ">=4.4",
|
||||
"hyperf/command": "^1.1",
|
||||
"hyperf/config": "^1.1",
|
||||
"hyperf/di": "1.1.*",
|
||||
"hyperf/framework": "^1.1",
|
||||
"hyperf/http-server": "^1.1",
|
||||
"hyperf/utils": "^1.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^2.14",
|
||||
"hyperf/crontab": "^1.1",
|
||||
"hyperf/db": "^1.1",
|
||||
"hyperf/process": "^1.1",
|
||||
"hyperf/testing": "1.1.*",
|
||||
"phpstan/phpstan": "^0.10.5",
|
||||
"swoft/swoole-ide-helper": "dev-master"
|
||||
},
|
||||
"suggest": {
|
||||
"hyperf/crontab": "Required to use closure crontab",
|
||||
"hyperf/process": "Required to use closure process"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"scripts": {
|
||||
"test": "co-phpunit -c phpunit.xml --colors=always",
|
||||
"analyse": "phpstan analyse --memory-limit 300M -l 5 ./src",
|
||||
"cs-fix": "php-cs-fixer fix $1"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.1-dev"
|
||||
},
|
||||
"hyperf": {
|
||||
"config": "Hyperf\\Nano\\ConfigProvider"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano;
|
||||
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\DB\DB;
|
||||
use Hyperf\Framework\Event\BootApplication;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\Nano\Factory\AppFactory;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
class Foo
|
||||
{
|
||||
public function bar()
|
||||
{
|
||||
return 'bar';
|
||||
}
|
||||
}
|
||||
|
||||
$app = AppFactory::createBase();
|
||||
|
||||
$app->get('/', function () {
|
||||
$user = $this->request->input('user', 'nano');
|
||||
$method = $this->request->getMethod();
|
||||
return [
|
||||
'message' => "hello {$user}",
|
||||
'method' => $method,
|
||||
];
|
||||
});
|
||||
|
||||
$app->addGroup('/route', function () use ($app) {
|
||||
$app->addRoute(['GET', 'POST'], '/{id:\d+}', function ($id) {
|
||||
return '/route/' . $id;
|
||||
});
|
||||
$app->put('/{name:.+}', function ($name) {
|
||||
return '/route/' . $name;
|
||||
});
|
||||
});
|
||||
|
||||
$app->get('/di', function () {
|
||||
/** @var ContainerProxy $this */
|
||||
$foo = $this->get(Foo::class);
|
||||
return $foo->bar();
|
||||
});
|
||||
|
||||
$app->get('/middleware', function () {
|
||||
return $this->request->getAttribute('key');
|
||||
});
|
||||
|
||||
$app->addMiddleware(function ($request, $handler) {
|
||||
$request = $request->withAttribute('key', 'value');
|
||||
return $handler->handle($request);
|
||||
});
|
||||
|
||||
$app->get('/exception', function () {
|
||||
throw new \Exception();
|
||||
});
|
||||
|
||||
$app->addExceptionHandler(function ($throwable, $response) {
|
||||
return $response->withStatus('418')->withBody(new SwooleStream('I\'m a teapot'));
|
||||
});
|
||||
|
||||
$app->addCommand('echo', function () {
|
||||
$this->get(StdoutLoggerInterface::class)->info('A new command called echo!');
|
||||
});
|
||||
|
||||
$app->addListener(BootApplication::class, function ($event) {
|
||||
$this->get(StdoutLoggerInterface::class)->info('App started');
|
||||
});
|
||||
|
||||
$app->addProcess(function () {
|
||||
while (true) {
|
||||
sleep(1);
|
||||
$this->get(StdoutLoggerInterface::class)->info('Processing...');
|
||||
}
|
||||
});
|
||||
|
||||
$app->addCrontab('* * * * * *', function () {
|
||||
$this->get(StdoutLoggerInterface::class)->info('execute every second!');
|
||||
});
|
||||
|
||||
$app->config([
|
||||
'db.default' => [
|
||||
'host' => env('DB_HOST', 'localhost'),
|
||||
'port' => env('DB_PORT', 3306),
|
||||
'database' => env('DB_DATABASE', 'hyperf'),
|
||||
'username' => env('DB_USERNAME', 'root'),
|
||||
'password' => env('DB_PASSWORD', ''),
|
||||
],
|
||||
]);
|
||||
|
||||
$app->get('/db', function () {
|
||||
return DB::query('SELECT * FROM `user` WHERE gender = ?;', [1]);
|
||||
});
|
||||
|
||||
$app->run();
|
@ -1,331 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano;
|
||||
|
||||
use Closure;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Crontab\Crontab;
|
||||
use Hyperf\Crontab\Process\CrontabDispatcherProcess;
|
||||
use Hyperf\HttpServer\Router\DispatcherFactory;
|
||||
use Hyperf\Nano\Factory\CommandFactory;
|
||||
use Hyperf\Nano\Factory\CronFactory;
|
||||
use Hyperf\Nano\Factory\ExceptionHandlerFactory;
|
||||
use Hyperf\Nano\Factory\MiddlewareFactory;
|
||||
use Hyperf\Nano\Factory\ProcessFactory;
|
||||
use Psr\EventDispatcher\ListenerProviderInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
|
||||
/**
|
||||
* @method get($route, $handler, array $options = [])
|
||||
* @method post($route, $handler, array $options = [])
|
||||
* @method put($route, $handler, array $options = [])
|
||||
* @method delete($route, $handler, array $options = [])
|
||||
* @method patch($route, $handler, array $options = [])
|
||||
* @method head($route, $handler, array $options = [])
|
||||
*/
|
||||
class App
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @var ConfigInterface
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var DispatcherFactory
|
||||
*/
|
||||
protected $dispatcherFactory;
|
||||
|
||||
/**
|
||||
* @var BoundInterface
|
||||
*/
|
||||
protected $bound;
|
||||
|
||||
private $serverName = 'http';
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->config = $this->container->get(ConfigInterface::class);
|
||||
$this->dispatcherFactory = $this->container->get(DispatcherFactory::class);
|
||||
$this->bound = $this->container->has(BoundInterface::class)
|
||||
? $this->container->get(BoundInterface::class)
|
||||
: new ContainerProxy($this->container);
|
||||
}
|
||||
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
$router = $this->dispatcherFactory->getRouter($this->serverName);
|
||||
if ($arguments[1] instanceof \Closure) {
|
||||
$arguments[1] = $arguments[1]->bindTo($this->bound, $this->bound);
|
||||
}
|
||||
return $router->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the application.
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$application = $this->container->get(\Hyperf\Contract\ApplicationInterface::class);
|
||||
$application->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Config the application using arrays.
|
||||
*/
|
||||
public function config(array $configs)
|
||||
{
|
||||
foreach ($configs as $key => $value) {
|
||||
$this->addConfig($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the dependency injection container.
|
||||
*/
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a middleware globally.
|
||||
* @param callable|MiddlewareInterface|string $middleware
|
||||
*/
|
||||
public function addMiddleware($middleware)
|
||||
{
|
||||
if ($middleware instanceof MiddlewareInterface || is_string($middleware)) {
|
||||
$this->appendConfig('middlewares.' . $this->serverName, $middleware);
|
||||
return;
|
||||
}
|
||||
|
||||
$middleware = Closure::fromCallable($middleware);
|
||||
$middlewareFactory = $this->container->get(MiddlewareFactory::class);
|
||||
$this->appendConfig(
|
||||
'middlewares.' . $this->serverName,
|
||||
$middlewareFactory->create($middleware->bindTo($this->bound, $this->bound))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an exception handler globally.
|
||||
* @param callable|string $exceptionHandler
|
||||
*/
|
||||
public function addExceptionHandler($exceptionHandler)
|
||||
{
|
||||
if (is_string($exceptionHandler)) {
|
||||
$this->appendConfig('exceptions.handler.' . $this->serverName, $exceptionHandler);
|
||||
return;
|
||||
}
|
||||
|
||||
$exceptionHandler = Closure::fromCallable($exceptionHandler);
|
||||
$exceptionHandlerFactory = $this->container->get(ExceptionHandlerFactory::class);
|
||||
$handler = $exceptionHandlerFactory->create($exceptionHandler->bindTo($this->bound, $this->bound));
|
||||
$handlerId = spl_object_hash($handler);
|
||||
$this->container->set($handlerId, $handler);
|
||||
$this->appendConfig(
|
||||
'exceptions.handler.' . $this->serverName,
|
||||
$handlerId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an listener globally.
|
||||
* @param null|callable|string $listener
|
||||
*/
|
||||
public function addListener(string $event, $listener = null, int $priority = 1)
|
||||
{
|
||||
if ($listener === null) {
|
||||
$listener = $event;
|
||||
}
|
||||
|
||||
if (is_string($listener)) {
|
||||
$this->appendConfig('listeners', $listener);
|
||||
return;
|
||||
}
|
||||
|
||||
$listener = Closure::fromCallable($listener);
|
||||
$listener = $listener->bindTo($this->bound, $this->bound);
|
||||
$provider = $this->container->get(ListenerProviderInterface::class);
|
||||
$provider->on($event, $listener, $priority);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a route group.
|
||||
* @param array|string $prefix
|
||||
*/
|
||||
public function addGroup($prefix, callable $callback, array $options = [])
|
||||
{
|
||||
$router = $this->dispatcherFactory->getRouter($this->serverName);
|
||||
if (isset($options['middleware'])) {
|
||||
$this->convertClosureToMiddleware($options['middleware']);
|
||||
}
|
||||
return $router->addGroup($prefix, $callback, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new command.
|
||||
* @param null|callable|string $command
|
||||
*/
|
||||
public function addCommand(string $name, $command = null)
|
||||
{
|
||||
if ($command === null) {
|
||||
$command = $name;
|
||||
}
|
||||
|
||||
if (is_string($command)) {
|
||||
$this->appendConfig('command' . $this->serverName, $command);
|
||||
return;
|
||||
}
|
||||
|
||||
$command = Closure::fromCallable($command);
|
||||
$commandFactory = $this->container->get(CommandFactory::class);
|
||||
$handler = $commandFactory->create($name, $command->bindTo($this->bound, $this->bound));
|
||||
$handlerId = spl_object_hash($handler);
|
||||
$this->container->set($handlerId, $handler);
|
||||
$this->appendConfig(
|
||||
'commands',
|
||||
$handlerId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new crontab.
|
||||
* @param callable | string $crontab
|
||||
*/
|
||||
public function addCrontab(string $rule, $crontab)
|
||||
{
|
||||
$this->config->set('crontab.enable', true);
|
||||
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);
|
||||
|
||||
if ($crontab instanceof Crontab) {
|
||||
$this->appendConfig('crontab.crontab', $crontab);
|
||||
return;
|
||||
}
|
||||
|
||||
$callback = \Closure::fromCallable($crontab);
|
||||
$callback = $callback->bindTo($this->bound, $this->bound);
|
||||
$callbackId = spl_object_hash($callback);
|
||||
$this->container->set($callbackId, $callback);
|
||||
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);
|
||||
$this->config->set('crontab.enable', true);
|
||||
|
||||
$this->appendConfig(
|
||||
'crontab.crontab',
|
||||
(new Crontab())
|
||||
->setName(uniqid())
|
||||
->setRule($rule)
|
||||
->setCallback([CronFactory::class, 'execute', [$callbackId]])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new process.
|
||||
* @param callable | string $process
|
||||
*/
|
||||
public function addProcess($process)
|
||||
{
|
||||
if (is_string($process)) {
|
||||
$this->appendConfig('processes', $process);
|
||||
return;
|
||||
}
|
||||
|
||||
$callback = \Closure::fromCallable($process);
|
||||
$callback = $callback->bindTo($this->bound, $this->bound);
|
||||
$processFactory = $this->container->get(ProcessFactory::class);
|
||||
$process = $processFactory->create($callback);
|
||||
$processId = spl_object_hash($process);
|
||||
$this->container->set($processId, $process);
|
||||
$this->appendConfig(
|
||||
'processes',
|
||||
$processId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new route.
|
||||
* @param mixed $httpMethod
|
||||
* @param mixed $handler
|
||||
*/
|
||||
public function addRoute($httpMethod, string $route, $handler, array $options = [])
|
||||
{
|
||||
$router = $this->dispatcherFactory->getRouter($this->serverName);
|
||||
if (isset($options['middleware'])) {
|
||||
$this->convertClosureToMiddleware($options['middleware']);
|
||||
}
|
||||
if ($handler instanceof \Closure) {
|
||||
$handler = $handler->bindTo($this->bound, $this->bound);
|
||||
}
|
||||
return $router->addRoute($httpMethod, $route, $handler, $options = []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a server.
|
||||
*/
|
||||
public function addServer(string $serverName, callable $callback)
|
||||
{
|
||||
$this->serverName = $serverName;
|
||||
call($callback, [$this]);
|
||||
$this->serverName = 'http';
|
||||
}
|
||||
|
||||
private function appendConfig(string $key, $configValues)
|
||||
{
|
||||
$configs = $this->config->get($key, []);
|
||||
array_push($configs, $configValues);
|
||||
$this->config->set($key, $configs);
|
||||
}
|
||||
|
||||
private function ensureConfigHasValue(string $key, $configValues)
|
||||
{
|
||||
$config = $this->config->get($key, []);
|
||||
if (! is_array($config)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (in_array($configValues, $config)) {
|
||||
return;
|
||||
}
|
||||
|
||||
array_push($config, $configValues);
|
||||
$this->config->set($key, $config);
|
||||
}
|
||||
|
||||
private function addConfig(string $key, $configValues)
|
||||
{
|
||||
$config = $this->config->get($key);
|
||||
|
||||
if (! is_array($config)) {
|
||||
$this->config->set($key, $configValues);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->config->set($key, array_merge_recursive($config, $configValues));
|
||||
}
|
||||
|
||||
private function convertClosureToMiddleware(array &$middlewares)
|
||||
{
|
||||
$middlewareFactory = $this->container->get(MiddlewareFactory::class);
|
||||
foreach ($middlewares as &$middleware) {
|
||||
if ($middleware instanceof \Closure) {
|
||||
$middleware = $middleware->bindTo($this->bound, $this->bound);
|
||||
$middleware = $middlewareFactory->create($middleware);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano;
|
||||
|
||||
interface BoundInterface
|
||||
{
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
public function __invoke(): array
|
||||
{
|
||||
return [
|
||||
'dependencies' => [
|
||||
BoundInterface::class => ContainerProxy::class,
|
||||
],
|
||||
'commands' => [
|
||||
],
|
||||
'annotations' => [
|
||||
'scan' => [
|
||||
'paths' => [
|
||||
__DIR__,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano;
|
||||
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
|
||||
class ContainerProxy implements BoundInterface, ContainerInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
private $response;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->request = $container->get(RequestInterface::class);
|
||||
$this->response = $container->get(ResponseInterface::class);
|
||||
}
|
||||
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
return $this->container->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
public function get($id)
|
||||
{
|
||||
return $this->container->get($id);
|
||||
}
|
||||
|
||||
public function define(string $name, $definition)
|
||||
{
|
||||
return $this->container->define($name, $definition);
|
||||
}
|
||||
|
||||
public function has($id)
|
||||
{
|
||||
return $this->container->has($id);
|
||||
}
|
||||
|
||||
public function make(string $name, array $parameters = [])
|
||||
{
|
||||
return $this->container->make($name, $parameters);
|
||||
}
|
||||
|
||||
public function set(string $name, $entry)
|
||||
{
|
||||
return $this->container->set($name, $entry);
|
||||
}
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Factory;
|
||||
|
||||
use Hyperf\Config\Config;
|
||||
use Hyperf\Config\ProviderConfig;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Di\Container;
|
||||
use Hyperf\Di\Definition\DefinitionSource;
|
||||
use Hyperf\Di\Definition\ScanConfig;
|
||||
use Hyperf\HttpServer\Router\DispatcherFactory;
|
||||
use Hyperf\Nano\App;
|
||||
use Hyperf\Nano\BoundInterface;
|
||||
use Hyperf\Nano\ContainerProxy;
|
||||
use Hyperf\Nano\Preset\Preset;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
class AppFactory
|
||||
{
|
||||
/**
|
||||
* Create an application.
|
||||
*/
|
||||
public static function create(string $host = '0.0.0.0', int $port = 9501): App
|
||||
{
|
||||
$app = self::createApp();
|
||||
$app->config([
|
||||
'server' => Preset::default(),
|
||||
'server.servers.0.host' => $host,
|
||||
'server.servers.0.port' => $port,
|
||||
]);
|
||||
return $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single worker application in base mode, with max_requests = 0.
|
||||
*/
|
||||
public static function createBase(string $host = '0.0.0.0', int $port = 9501): App
|
||||
{
|
||||
$app = self::createApp();
|
||||
$app->config([
|
||||
'server' => Preset::base(),
|
||||
'server.servers.0.host' => $host,
|
||||
'server.servers.0.port' => $port,
|
||||
]);
|
||||
return $app;
|
||||
}
|
||||
|
||||
protected static function prepareContainer(): ContainerInterface
|
||||
{
|
||||
$config = new Config(ProviderConfig::load());
|
||||
$config->set(StdoutLoggerInterface::class, [
|
||||
'log_level' => [
|
||||
LogLevel::ALERT,
|
||||
LogLevel::CRITICAL,
|
||||
LogLevel::DEBUG,
|
||||
LogLevel::EMERGENCY,
|
||||
LogLevel::ERROR,
|
||||
LogLevel::INFO,
|
||||
LogLevel::NOTICE,
|
||||
LogLevel::WARNING,
|
||||
],
|
||||
]);
|
||||
$container = new Container(new DefinitionSource($config->get('dependencies'), new ScanConfig()));
|
||||
$container->set(ConfigInterface::class, $config);
|
||||
$container->define(DispatcherFactory::class, DispatcherFactory::class);
|
||||
$container->define(BoundInterface::class, ContainerProxy::class);
|
||||
|
||||
ApplicationContext::setContainer($container);
|
||||
return $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup flags, ini settings and constants.
|
||||
*/
|
||||
protected static function prepareFlags(int $hookFlags = SWOOLE_HOOK_ALL)
|
||||
{
|
||||
ini_set('display_errors', 'on');
|
||||
ini_set('display_startup_errors', 'on');
|
||||
error_reporting(E_ALL);
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an application with a chosen preset.
|
||||
*/
|
||||
protected static function createApp(): App
|
||||
{
|
||||
// Setting ini and flags
|
||||
self::prepareFlags();
|
||||
|
||||
// Prepare container
|
||||
$container = self::prepareContainer();
|
||||
|
||||
return new App($container);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Factory;
|
||||
|
||||
use Hyperf\Command\Command;
|
||||
|
||||
class CommandFactory
|
||||
{
|
||||
public function create(string $name, \Closure $closure): Command
|
||||
{
|
||||
return new class($name, $closure) extends Command {
|
||||
private $closure;
|
||||
|
||||
public function __construct(string $name, \Closure $closure)
|
||||
{
|
||||
parent::__construct($name);
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
call($this->closure);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Factory;
|
||||
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
|
||||
class CronFactory
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function execute($name)
|
||||
{
|
||||
$callback = $this->container->get($name);
|
||||
$callback();
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Factory;
|
||||
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class ExceptionHandlerFactory
|
||||
{
|
||||
public function create(\Closure $closure): ExceptionHandler
|
||||
{
|
||||
return new class($closure) extends ExceptionHandler {
|
||||
/**
|
||||
* @var \Closure
|
||||
*/
|
||||
private $closure;
|
||||
|
||||
public function __construct(\Closure $closure)
|
||||
{
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
return call($this->closure, [$throwable, $response]);
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Factory;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class MiddlewareFactory
|
||||
{
|
||||
public function create(\Closure $closure): MiddlewareInterface
|
||||
{
|
||||
return new class($closure) implements MiddlewareInterface {
|
||||
/**
|
||||
* @var \Closure
|
||||
*/
|
||||
private $closure;
|
||||
|
||||
public function __construct(\Closure $closure)
|
||||
{
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
return call($this->closure, [$request, $handler]);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Factory;
|
||||
|
||||
use Hyperf\Process\AbstractProcess;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class ProcessFactory
|
||||
{
|
||||
public function create(\Closure $closure): AbstractProcess
|
||||
{
|
||||
$container = ApplicationContext::getContainer();
|
||||
return new class($container, $closure) extends AbstractProcess {
|
||||
private $closure;
|
||||
|
||||
public function __construct(ContainerInterface $container, \Closure $closure)
|
||||
{
|
||||
parent::__construct($container);
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
call($this->closure);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
use Hyperf\Server\Server;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
|
||||
return [
|
||||
'mode' => SWOOLE_BASE,
|
||||
'servers' => [
|
||||
[
|
||||
'name' => 'http',
|
||||
'type' => Server::SERVER_HTTP,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9501,
|
||||
'sock_type' => SWOOLE_SOCK_TCP,
|
||||
'callbacks' => [
|
||||
SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
|
||||
],
|
||||
],
|
||||
],
|
||||
'settings' => [
|
||||
'enable_coroutine' => true,
|
||||
'worker_num' => 1,
|
||||
'open_tcp_nodelay' => true,
|
||||
'max_coroutine' => 100000,
|
||||
'open_http2_protocol' => true,
|
||||
'max_request' => 0,
|
||||
'socket_buffer_size' => 2 * 1024 * 1024,
|
||||
'buffer_output_size' => 2 * 1024 * 1024,
|
||||
],
|
||||
'callbacks' => [
|
||||
SwooleEvent::ON_BEFORE_START => [Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
|
||||
SwooleEvent::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
|
||||
SwooleEvent::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
|
||||
SwooleEvent::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
|
||||
],
|
||||
];
|
@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano;
|
||||
|
||||
/*
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
use Hyperf\Server\Server;
|
||||
use Hyperf\Server\SwooleEvent;
|
||||
|
||||
return [
|
||||
'mode' => SWOOLE_PROCESS,
|
||||
'servers' => [
|
||||
[
|
||||
'name' => 'http',
|
||||
'type' => Server::SERVER_HTTP,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9501,
|
||||
'sock_type' => SWOOLE_SOCK_TCP,
|
||||
'callbacks' => [
|
||||
SwooleEvent::ON_REQUEST => [\Hyperf\HttpServer\Server::class, 'onRequest'],
|
||||
],
|
||||
],
|
||||
],
|
||||
'settings' => [
|
||||
'enable_coroutine' => true,
|
||||
'worker_num' => swoole_cpu_num(),
|
||||
'open_tcp_nodelay' => true,
|
||||
'max_coroutine' => 100000,
|
||||
'open_http2_protocol' => true,
|
||||
'max_request' => 100000,
|
||||
'socket_buffer_size' => 2 * 1024 * 1024,
|
||||
'buffer_output_size' => 2 * 1024 * 1024,
|
||||
],
|
||||
'callbacks' => [
|
||||
SwooleEvent::ON_BEFORE_START => [\Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
|
||||
SwooleEvent::ON_WORKER_START => [\Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
|
||||
SwooleEvent::ON_WORKER_EXIT => [\Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
|
||||
SwooleEvent::ON_PIPE_MESSAGE => [\Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
|
||||
],
|
||||
];
|
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nano\Preset;
|
||||
|
||||
/**
|
||||
* The preset class represents a set of configs.
|
||||
* All includes path should be static, ie. no variables.
|
||||
* This is because Phar and static analyzer doesn't work
|
||||
* well with dynamic includes.
|
||||
*/
|
||||
class Preset
|
||||
{
|
||||
public static function default(): array
|
||||
{
|
||||
return include __DIR__ . '/Default.php';
|
||||
}
|
||||
|
||||
public static function base(): array
|
||||
{
|
||||
return include __DIR__ . '/Base.php';
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Hyperf
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -1,60 +0,0 @@
|
||||
{
|
||||
"name": "hyperf/swoole-enterprise",
|
||||
"description": "A swoole dashboard library for Hyperf.",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"php",
|
||||
"swoole",
|
||||
"hyperf",
|
||||
"swoole-enterprise"
|
||||
],
|
||||
"homepage": "https://hyperf.io",
|
||||
"support": {
|
||||
"docs": "https://hyperf.wiki",
|
||||
"issues": "https://github.com/hyperf/hyperf/issues",
|
||||
"pull-request": "https://github.com/hyperf/hyperf/pulls",
|
||||
"source": "https://github.com/hyperf/hyperf"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.2",
|
||||
"hyperf/contract": "~1.1.0",
|
||||
"hyperf/utils": "~1.1.0",
|
||||
"psr/container": "^1.0",
|
||||
"psr/http-server-middleware": "^1.0",
|
||||
"psr/log": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^2.9",
|
||||
"malukenho/docheader": "^0.1.6",
|
||||
"mockery/mockery": "^1.0",
|
||||
"phpunit/phpunit": "^7.0"
|
||||
},
|
||||
"suggest": {
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Hyperf\\SwooleEnterprise\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.1-dev"
|
||||
},
|
||||
"hyperf": {
|
||||
"config": "Hyperf\\SwooleEnterprise\\ConfigProvider"
|
||||
}
|
||||
},
|
||||
"bin": [
|
||||
],
|
||||
"scripts": {
|
||||
"cs-fix": "php-cs-fixer fix $1",
|
||||
"test": "phpunit --colors=always"
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\SwooleEnterprise;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
public function __invoke(): array
|
||||
{
|
||||
return [
|
||||
'annotations' => [
|
||||
'scan' => [
|
||||
'paths' => [
|
||||
__DIR__,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\SwooleEnterprise\Middleware;
|
||||
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use StatsCenter;
|
||||
|
||||
class HttpServerMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
public function __construct(ConfigInterface $config)
|
||||
{
|
||||
$this->name = $config->get('app_name', 'hyperf-skeleton');
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if (class_exists(StatsCenter::class)) {
|
||||
$path = $request->getUri()->getPath();
|
||||
$ip = current(swoole_get_local_ip());
|
||||
|
||||
$tick = StatsCenter::beforeExecRpc($path, $this->name, $ip);
|
||||
try {
|
||||
$response = $handler->handle($request);
|
||||
StatsCenter::afterExecRpc($tick, true, $response->getStatusCode());
|
||||
} catch (\Throwable $exception) {
|
||||
StatsCenter::afterExecRpc($tick, false, $exception->getCode());
|
||||
throw $exception;
|
||||
}
|
||||
} else {
|
||||
$response = $handler->handle($request);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user