目的
图示
<?php
declare(strict_types=1);
namespace AppCommand;
use HyperfCommandAnnotationCommand;
use HyperfCommandCommand as HyperfCommand;
use PsrContainerContainerInterface;
class ModelRefreshCommand extends HyperfCommand
{
protected $container;
protected $name = 'model:refresh';
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct();
}
public function configure()
{
parent::configure();
$this->setDescription('项目model初始化');
}
public function handle()
{
$this->handleDirectoryFile(function ($pathName) {
$pathInfo = pathinfo($pathName);
$entity = str_replace('/', '', sprintf('%s%s%s', $pathInfo['dirname'], '/', $pathInfo['filename']));
$entity = str_replace('src', '', $entity);
if (class_exists($entity)) {
$model = new $entity();
$this->info($model->getModel()->getTable() . 'model开始刷新');
$this->call('gen:model', [
'table' => $model->getModel()->getTable(),
'--path' => $pathInfo['dirname'],
]);
$this->info($model->getModel()->getTable() . 'model刷新成功');
}
}, 'src', 'Model');
}
public function handleDirectoryFile(callable $callback, string $baseDir = 'src', string $needle = ''): void{
$model = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir));
foreach ($model as $key => $val) {
if (! is_file($val->getPathName())) {
continue;
}
if (((! $needle) || (strpos($val->getPathName(), $needle) !== false)) && $callback) {
$callback($val->getPathName());
}
}
}
}