1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- declare (strict_types = 1);
- namespace app\common\command;
- use DirectoryIterator;
- use SplFileInfo;
- // 引入框架内置类
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\facade\Config;
- use think\File;
- use app\common\model\FileManager;
- class FileConsole extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('fileconsole')
- ->addArgument('name', Argument::OPTIONAL, "console name")
- ->addOption('dir', null, Option::VALUE_REQUIRED, 'directory name')
- ->setDescription('the fileconsole command');
- }
- protected function execute(Input $input, Output $output)
- {
- $name = trim($input->getArgument('name'));
- $name = $name ?: 'thinkphp';
- if ($input->hasOption('dir')) {
- $dir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('dir'));
- } else {
- $dir = str_replace("\\", '/', Config::get('filesystem.disks.public.root')) .'/';
- }
- // 指令输出
- $output->writeln('fileconsole :' . $name . "--dir: " . $dir);
-
- $this->$name($dir);
- $output->writeln('..ok');
- }
- /**
- * 建立文件管理目录索引(递归遍历目录)
- */
- public function scan($dir)
- {
- if (is_file($dir)) {
- $file = new File($dir);
- FileManager::saveFile($file);
- } else {
- $dirs = new DirectoryIterator($dir);
-
- foreach ($dirs as $fileInfo) {
- if($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
- continue;
- }
-
- $this->scan($dirs->getPath() . '/' . $fileInfo->getFilename());
- }
- }
- }
- /**
- * 清除无效目录索引
- */
- public function clear($dir = '')
- {
- $list = FileManager::select();
-
- foreach ($list as $value) {
- $file = app()->getRootPath() . 'public/' . $value->filepath;
- if (!is_file($file)) {
- FileManager::destroy($value->fileid);
- }
- }
- }
- }
|