新增zip&composer工具

This commit is contained in:
JaguarJack 2020-07-21 08:01:20 +08:00
parent 30f405f233
commit f0351246fc
4 changed files with 118 additions and 4 deletions

View File

@ -43,10 +43,10 @@ class CatchAdminService extends Service
*/ */
protected function registerCommands(): void protected function registerCommands(): void
{ {
if ($this->app->runningInConsole() && class_exists('Symfony\Component\Finder\Finder')) { if ($this->app->runningInConsole()) {
$catchConsole = new CatchConsole($this->app); $catchConsole = new CatchConsole($this->app);
$this->app->bind('catch_console', $catchConsole); $this->app->bind('catch\console', $catchConsole);
$this->commands($catchConsole->commands()); $this->commands($catchConsole->commands());
} }

View File

@ -55,8 +55,8 @@ abstract class ModuleService extends Service
if (method_exists($this,'loadCommands') && $this->app->runningInConsole()) { if (method_exists($this,'loadCommands') && $this->app->runningInConsole()) {
list($namespace, $path) = $this->loadCommands(); list($namespace, $path) = $this->loadCommands();
if ($this->app->has('catch_console')) { if ($this->app->has('catch\console')) {
$catchConsole = $this->app['catch_console']; $catchConsole = $this->app['catch\console'];
$this->commands($catchConsole->setNamespace($namespace) $this->commands($catchConsole->setNamespace($namespace)
->path($path) ->path($path)

View File

@ -19,6 +19,11 @@ class Composer
return $this->composerContent()['autoload']['psr-4']; return $this->composerContent()['autoload']['psr-4'];
} }
public function requires()
{
return $this->composerContent()['require'];
}
protected function composerContent() protected function composerContent()
{ {
return \json_decode(FileSystem::get($this->composerJsonPath()), true); return \json_decode(FileSystem::get($this->composerJsonPath()), true);

View File

@ -8,3 +8,112 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catcher\library;
use catcher\facade\FileSystem;
class Zip
{
protected $zipArchive;
const EXTENSION = 'zip';
protected $folder;
public function __construct()
{
$this->zipArchive = new \ZipArchive();
}
/**
* zip 文件
*
* @time 2020年07月19日
* @param $zip
* @throws \Exception
* @return $this
*/
public function make($zip)
{
if (FileSystem::extension($zip) != self::EXTENSION) {
throw new \Exception("make zip muse set [zip] extension");
}
$this->zipArchive->open($zip, \ZipArchive::CREATE);
return $this;
}
/**
* 添加文件
*
* @time 2020年07月19日
* @param $files
* @param bool $relative
* @return $this
*/
public function addFiles($files, $relative = true)
{
if ($relative) {
foreach ($files as $file) {
$this->zipArchive->addFile($file->getPathname(), $this->folder . $file->getRelativePathname());
}
} else {
foreach ($files as $file) {
$this->zipArchive->addFile($file->getPathname(), $this->folder . $file->getPathname());
}
}
return $this;
}
/**
* 根目录
*
* @time 2020年07月19日
* @param string $folder
* @return $this
*/
public function folder(string $folder)
{
$this->zipArchive->addEmptyDir($folder);
$this->folder = $folder . DIRECTORY_SEPARATOR;
return $this;
}
/**
* 解压到
*
* @time 2020年07月19日
* @param $path
* @return $this
* @throws \Exception
*/
public function extractTo($path)
{
if (!$this->zipArchive->extractTo($path)) {
throw new \Exception('extract failed');
}
return $this;
}
public function getFiles()
{
$this->zipArchive;
}
/**
* 关闭
*
* @time 2020年07月19日
* @return void
*/
public function close()
{
$this->zipArchive->close();
}
}