Skip to content

Commit 32f3f95

Browse files
committed
update readme. add method for autoload commands and controllers
1 parent 84ee2a7 commit 32f3f95

File tree

7 files changed

+82
-73
lines changed

7 files changed

+82
-73
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ class HomeController extends Controller
198198

199199
更多请查看 [examples](./examples) 中的示例代码和在目录下运行示例 `php examples/app` 来查看效果
200200

201+
### 自动扫描注册
202+
203+
可以配置命名空间和对应的路径来,自动扫描并注册命令。
204+
205+
```php
206+
$app->registerCommands('App\\Console\\Commands', get_path('app/Console/Commands'));
207+
$app->registerGroups('App\\Console\\Controllers', get_path('app/Console/Controllers'));
208+
```
209+
201210
## 输入
202211

203212
> 输入对象是 `Inhere\Console\IO\Input` 的实例

src/Application.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console;
1010

1111
use Inhere\Console\Base\AbstractApplication;
12+
use Inhere\Console\Utils\Helper;
1213

1314
/**
1415
* Class App
@@ -157,6 +158,69 @@ public function addGroup(string $name, string $controller = null)
157158
return $this->controller($name, $controller);
158159
}
159160

161+
/**
162+
* auto register commands from a dir.
163+
* @param string $namespace
164+
* @param string $basePath
165+
* @return $this
166+
* @throws \InvalidArgumentException
167+
*/
168+
public function registerCommands(string $namespace, string $basePath)
169+
{
170+
$length = \strlen($basePath) + 1;
171+
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
172+
173+
foreach ($iterator as $file) {
174+
$class = $namespace . '\\' . \substr($file, $length, -4);
175+
$this->addCommand($class);
176+
}
177+
178+
return $this;
179+
}
180+
181+
/**
182+
* auto register controllers from a dir.
183+
* @param string $namespace
184+
* @param string $basePath
185+
* @return $this
186+
* @throws \InvalidArgumentException
187+
*/
188+
public function registerGroups(string $namespace, string $basePath)
189+
{
190+
$length = \strlen($basePath) + 1;
191+
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
192+
193+
foreach ($iterator as $file) {
194+
$class = $namespace . '\\' . \substr($file, $length, -4);
195+
$this->addController($class);
196+
}
197+
198+
return $this;
199+
}
200+
201+
/**
202+
* @return \Closure
203+
*/
204+
protected function getFileFilter()
205+
{
206+
return function (\SplFileInfo $f) {
207+
$name = $f->getFilename();
208+
209+
// Skip hidden files and directories.
210+
if ($name[0] === '.') {
211+
return false;
212+
}
213+
214+
// go on read sub-dir
215+
if ($f->isDir()) {
216+
return true;
217+
}
218+
219+
// php file
220+
return $f->isFile() && \substr($name, -4) === '.php';
221+
};
222+
}
223+
160224
/****************************************************************************
161225
* dispatch and run console controller/command
162226
****************************************************************************/

src/BuiltIn/PharController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Inhere\Console\BuiltIn;
1010

1111
use Inhere\Console\Controller;
12-
use Inhere\Console\Utils\PharCompiler;
12+
use Inhere\Console\Components\PharCompiler;
1313

1414
/**
1515
* Class PharController
@@ -49,7 +49,7 @@ public function packCommand()
4949
'dirExclude' => '#[\.git|tests]#',
5050

5151
'fileInclude' => ['LICENSE', 'app', 'liteApp'],
52-
'fileMatch' => '#\.php#',
52+
'fileMatch' => '#\.php$#',
5353
]);
5454

5555
$pharFile = BASE_PATH . '/test.phar';
@@ -60,4 +60,4 @@ public function packCommand()
6060
'size' => round(filesize($pharFile) / 1000, 2) . ' kb',
6161
]);
6262
}
63-
}
63+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* Time: 17:56
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Components;
1010

1111
/**
1212
* Class AutoCompletion - a simple command auto-completion tool
1313
*
1414
* @todo not available
15-
* @package Inhere\Console\Utils
15+
* @package Inhere\Console\Components
1616
*/
1717
class AutoCompletion
1818
{

src/Components/AutoLoader.php

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* Time: 17:57
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Components;
1010

1111
use Phar;
1212

1313
/**
1414
* Class PharCompiler
15-
* @package Inhere\Console\Utils
15+
* @package Inhere\Console\Components
1616
* @ref Psy\Compiler (package psy/psysh)
1717
*/
1818
class PharCompiler

src/Utils/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public static function init($object, array $options)
9292
* @param string $srcDir
9393
* @param callable $filter
9494
* @return \RecursiveIteratorIterator
95-
* @throws \LogicException
95+
* @throws \InvalidArgumentException
9696
*/
9797
public static function recursiveDirectoryIterator(string $srcDir, callable $filter)
9898
{
9999
if (!$srcDir || !file_exists($srcDir)) {
100-
throw new \LogicException('Please provide a exists source directory.');
100+
throw new \InvalidArgumentException('Please provide a exists source directory.');
101101
}
102102

103103
$directory = new \RecursiveDirectoryIterator($srcDir);

0 commit comments

Comments
 (0)