Skip to content

Commit 40162fe

Browse files
authored
Merge pull request #4 from inhere/master
some update, add more ui page
2 parents 00f944a + aedf4ba commit 40162fe

116 files changed

Lines changed: 793 additions & 12616 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# swoft-devtool
1+
# swoft devtool
22

33
Dev tool for swoft
44

@@ -10,13 +10,9 @@ Dev tool for swoft
1010
composer require swoft/devtool
1111
```
1212

13-
## Document
14-
15-
Please see [document site](https://doc.swoft.org)
16-
1713
## Quick start
1814

19-
1. add http middleware in `config/beans/base.php`
15+
1. Add http middleware in `config/beans/base.php`
2016

2117
```php
2218
'serverDispatcher' => [
@@ -27,7 +23,7 @@ Please see [document site](https://doc.swoft.org)
2723
],
2824
```
2925

30-
2. some optional config for devtool(in `config/properties/app.php`).
26+
2. Some optional config for devtool(in `config/properties/app.php`).
3127

3228
```php
3329
'devtool' => [
@@ -36,28 +32,39 @@ Please see [document site](https://doc.swoft.org)
3632
],
3733
```
3834

39-
3. publish devtool's assets to `public` dir.
35+
3. Publish static assets to `public` dir.
4036

4137
```bash
4238
php bin/swoft dev:publish swoft/devtool
39+
// -f will delete old assets.
40+
php bin/swoft dev:publish swoft/devtool -f
4341
```
4442

45-
4. now, you can access your server address by browser. like `http://127.0.0.1:9088/__devtool/`
46-
47-
> NOTICE: ensure your static assets is can accessed.
43+
4. Now, you can access `HOST:PORT/__devtool` by browser. e.g `http://127.0.0.1:9088/__devtool`
4844

4945
5. If you see the following screen, you have successfully installed
5046

5147
![image](./res/images/devtool.jpg)
5248

49+
## Question
50+
51+
If you cannot access the address `HOST:PORT/__devtool`
52+
53+
- Ensure your static assets is can accessed and assets is publish successful.
54+
- Ensure that the server was restarted after updating the package
55+
5356
## Notice
5457

5558
Opening devTool will have some impact on server operation, please turn it off during stress test.
5659

60+
## Document
61+
62+
Please see [document site](https://doc.swoft.org)
63+
5764
## Unit testing
5865

5966
```bash
60-
phpunit test/unit
67+
phpunit
6168
```
6269

6370
## LICENSE

src/Bootstrap/Listener/EventFireListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Swoft\Devtool\Bootstrap\Listener;
44

5+
use Swoft\App;
56
use Swoft\Bean\Annotation\Listener;
67
use Swoft\Bean\Annotation\Value;
78
use Swoft\Console\Helper\ConsoleUtil;
@@ -37,10 +38,11 @@ public function handle(EventInterface $event)
3738

3839
ConsoleUtil::log(
3940
\sprintf('Trigger the event <cyan>%s</cyan>', $event->getName()),
40-
$event->getParams(),
41+
[],
4142
'debug',
4243
[
43-
'Application'
44+
'Application',
45+
'WorkerId' => App::getWorkerId()
4446
]
4547
);
4648
}

src/Bootstrap/Listener/ServerStartListener.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Swoft\Devtool\Bootstrap\Listener;
44

5+
use Swoft\App;
56
use Swoft\Bean\Annotation\ServerListener;
7+
use Swoft\Bean\BeanFactory;
68
use Swoft\Bootstrap\Listeners\Interfaces\BeforeStartInterface;
79
use Swoft\Bootstrap\Listeners\Interfaces\WorkerStartInterface;
810
use Swoft\Bootstrap\SwooleEvent;
911
use Swoft\Bootstrap\Server\AbstractServer;
1012
use Swoft\Devtool\DevTool;
13+
use Swoft\Devtool\WebSocket\DevToolController;
1114
use Swoft\Memory\Table;
1215
use Swoole\Server;
1316

@@ -39,10 +42,16 @@ public function onWorkerStart(Server $server, int $workerId, bool $isWorker)
3942
{
4043
\output()->writeln(\sprintf(
4144
'Children process start successful. ' .
42-
'PID <magenta>%s</magenta>, Worker Id <magenta>%s</magenta>, Role <info>%s</info> process',
45+
'PID <magenta>%s</magenta>, Worker Id <magenta>%s</magenta>, Role <info>%s</info>',
4346
$server->worker_pid,
4447
$workerId,
45-
$isWorker ? 'Work' : 'Task'
48+
$isWorker ? 'Worker' : 'Task'
4649
));
50+
51+
// if websocket is enabled. register a ws route
52+
if ($isWorker && App::hasBean('wsRouter')) {
53+
/* @see \Swoft\WebSocket\Server\Router\HandlerMapping::add() */
54+
App::getBean('wsRouter')->add(DevTool::ROUTE_PREFIX, DevToolController::class);
55+
}
4756
}
4857
}

src/Command/DevCommand.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Swoft\Console\Bean\Annotation\Command;
77
use Swoft\Console\Helper\ConsoleUtil;
88
use Swoft\Console\Input\Input;
9+
use Swoft\Helper\DirHelper;
910
use Swoft\Helper\ProcessHelper;
1011

1112
/**
@@ -18,12 +19,12 @@ class DevCommand
1819
/**
1920
* @return array
2021
*/
21-
public function internalConfig(): array
22+
public static function internalConfig(): array
2223
{
2324
return [
2425
'swoft/devtool' => [
25-
'@devtool/web/dist/devtool',
26-
'@root/public'
26+
'@devtool/web/dist/devtool/static',
27+
'@root/public/devtool'
2728
],
2829
];
2930
}
@@ -34,7 +35,7 @@ public function internalConfig(): array
3435
* srcDir The source assets directory path. eg. `@vendor/some/lib/assets`
3536
* dstDir The defined component name.(default is `@root/public`)
3637
* @Options
37-
* -y, --yes BOOL Whether to ask when writing a file. default is: <info>True</info>
38+
* -y, --yes BOOL Do not confirm when execute publish. default is: <info>False</info>
3839
* -f, --force BOOL Force override all exists file.(default: <info>False</info>)
3940
* @Example
4041
* {fullCommand} swoft/devtool
@@ -56,7 +57,7 @@ public function publish(Input $input): int
5657

5758
// first arg is internal component name
5859
if ($assetDir && !$targetDir) {
59-
$config = $this->internalConfig();
60+
$config = static::internalConfig();
6061

6162
if (!isset($config[$assetDir])) {
6263
\output()->colored('missing arguments!', 'warning');
@@ -68,6 +69,21 @@ public function publish(Input $input): int
6869
$assetDir = App::getAlias($assetDir);
6970
$targetDir = App::getAlias($targetDir);
7071

72+
$force = \input()->sameOpt(['f', 'force'], false);
73+
74+
if ($force && \is_dir($targetDir)) {
75+
\output()->writeln("Will delete the old assets: $targetDir");
76+
77+
list($code, , $error) = ProcessHelper::run("rm -rf $targetDir");
78+
79+
if ($code !== 0) {
80+
\output()->colored("Delete dir $targetDir is failed!", 'error');
81+
\output()->writeln($error);
82+
83+
return -2;
84+
}
85+
}
86+
7187
$yes = \input()->sameOpt(['y', 'yes'], false);
7288
$command = "cp -Rf $assetDir $targetDir";
7389

@@ -79,6 +95,8 @@ public function publish(Input $input): int
7995
return 0;
8096
}
8197

98+
DirHelper::mkdir($targetDir);
99+
82100
list($code, , $error) = ProcessHelper::run($command, App::getAlias('@root'));
83101

84102
if ($code !== 0) {

src/Controller/AppController.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
namespace Swoft\Devtool\Controller;
1212

1313
use Swoft\App;
14+
use Swoft\Bean\BeanFactory;
15+
use Swoft\Bean\Collector\PoolCollector;
1416
use Swoft\Core\Config;
1517
use Swoft\Http\Message\Server\Request;
1618
use Swoft\Http\Server\Bean\Annotation\Controller;
1719
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
1820
use Swoft\Http\Server\Bean\Annotation\RequestMethod;
1921
use Swoft\Http\Server\Payload;
22+
use Swoft\Pool\PoolConfigInterface;
2023

2124
/**
2225
* Class AppController
@@ -27,16 +30,17 @@ class AppController
2730
{
2831
/**
2932
* get app info
30-
* @RequestMapping(route="info", method=RequestMethod::GET)
33+
* @RequestMapping(route="env", method=RequestMethod::GET)
3134
* @return array
3235
*/
3336
public function index(): array
3437
{
3538
return [
3639
'os' => \PHP_OS,
3740
'phpVersion' => \PHP_VERSION,
38-
'swoVersion' => \SWOOLE_VERSION,
39-
'swfVersion' => App::version(),
41+
'swooleVersion' => \SWOOLE_VERSION,
42+
'swoftVersion' => App::version(),
43+
'appName' => \APP_NAME,
4044
'basePath' => \BASE_PATH,
4145
];
4246
}
@@ -58,6 +62,59 @@ public function config(Request $request)
5862
return \bean('config')->toArray();
5963
}
6064

65+
/**
66+
* get app pools
67+
* @RequestMapping(route="pools", method=RequestMethod::GET)
68+
* @param Request $request
69+
* @return array
70+
* @throws \Swoft\Exception\InvalidArgumentException
71+
*/
72+
public function pools(Request $request): array
73+
{
74+
if ($name = $request->query('name')) {
75+
if (!App::hasPool($name)) {
76+
return [];
77+
}
78+
79+
/** @var PoolConfigInterface $poolConfig */
80+
$poolConfig = App::getPool($name)->getPoolConfig();
81+
82+
return $poolConfig->toArray();
83+
}
84+
85+
return PoolCollector::getCollector();
86+
}
87+
88+
/**
89+
* get app beans
90+
* @RequestMapping(route="beans", method=RequestMethod::GET)
91+
* @param Request $request
92+
* @return array
93+
*/
94+
public function beans(Request $request): array
95+
{
96+
if ($name = $request->query('name')) {
97+
return [];
98+
}
99+
100+
return BeanFactory::getContainer()->getBeanNames();
101+
}
102+
103+
/**
104+
* get app beans config
105+
* @RequestMapping(route="beans-config", method=RequestMethod::GET)
106+
* @param Request $request
107+
* @return array
108+
*/
109+
public function beansConfig(Request $request): array
110+
{
111+
if ($name = $request->query('name')) {
112+
return [];
113+
}
114+
115+
return [];
116+
}
117+
61118
/**
62119
* get app path aliases
63120
* @RequestMapping(route="aliases", method=RequestMethod::GET)

src/Controller/GenController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* Class GenController
20-
* @Controller(prefix="/__devtool/gen")
20+
* @Controller(prefix="/__devtool/gen/")
2121
* @package Swoft\Devtool\Controller
2222
*/
2323
class GenController
@@ -35,6 +35,19 @@ public function index(Request $request): array
3535
return ['item0', 'item1'];
3636
}
3737

38+
/**
39+
* Generate class file preview
40+
* @RequestMapping(route="preview", method=RequestMethod::POST)
41+
* @param Request $request
42+
* @return array
43+
*/
44+
public function preview(Request $request): array
45+
{
46+
// $data = $request->json();
47+
48+
return ['item0', 'item1'];
49+
}
50+
3851
/**
3952
* Generate class file
4053
* @RequestMapping(route="file", method=RequestMethod::POST)

src/Controller/RouteController.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ public function rpcRoutes(): array
7777
}
7878

7979
/** @var \Swoft\Rpc\Server\Router\HandlerMapping $router */
80-
// $router = \bean('serviceRouter');
80+
$router = \bean('serviceRouter');
81+
$rawList = $router->getRoutes();
82+
$routes = [];
8183

82-
// return $router->getRoutes();
83-
return [];
84+
foreach ($rawList as $key => $route) {
85+
$routes[] = [
86+
'serviceKey' => $key,
87+
'class' => $route[0],
88+
'method' => $route[1],
89+
];
90+
}
91+
92+
return $routes;
8493
}
8594
}

src/FileGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace Swoft\Devtool;
4+
45
use Leuffen\TextTemplate\TextTemplate;
56
use Swoft\App;
67

src/Middleware/DevToolMiddleware.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Http\Message\ServerRequestInterface;
1616
use Swoft\App;
1717
use Swoft\Bean\Annotation\Bean;
18+
use Swoft\Bean\Annotation\Value;
1819
use Swoft\Console\Helper\ConsoleUtil;
1920
use Swoft\Core\Coroutine;
2021
use Swoft\Devtool\DevTool;
@@ -28,12 +29,16 @@
2829
*/
2930
class DevToolMiddleware implements MiddlewareInterface
3031
{
32+
/**
33+
* @Value("{$config.devtool.logHttpRequestToConsole}")
34+
* @var bool
35+
*/
3136
public $logHttpRequestToConsole = true;
3237

33-
public function __construct()
34-
{
35-
$this->logHttpRequestToConsole = \bean('config')->get('devtool.logHttpRequestToConsole', true);
36-
}
38+
// public function __construct()
39+
// {
40+
// $this->logHttpRequestToConsole = \bean('config')->get('devtool.logHttpRequestToConsole', true);
41+
// }
3742

3843
/**
3944
* @param \Psr\Http\Message\ServerRequestInterface|Request $request
@@ -49,7 +54,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4954
if ($this->logHttpRequestToConsole) {
5055
ConsoleUtil::log(\sprintf('%s %s', $request->getMethod(), $path), [], 'debug', [
5156
'HttpServer',
52-
'coID' => Coroutine::tid()
57+
'WorkerId' => App::getWorkerId(),
58+
'CoId' => Coroutine::tid()
5359
]);
5460
}
5561

0 commit comments

Comments
 (0)