added module:download command

This commit is contained in:
Denis Duliçi
2020-12-24 02:16:00 +03:00
parent 5b0a50c758
commit 531dcd8dab
16 changed files with 793 additions and 515 deletions

View File

@ -0,0 +1,77 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class CopyFiles extends Job
{
protected $alias;
protected $path;
/**
* Create a new job instance.
*
* @param $alias
* @param $path
*/
public function __construct($alias, $path)
{
$this->alias = $alias;
$this->path = $path;
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
if (empty($this->path)) {
throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias]));
}
$source = storage_path('app/temp/' . $this->path);
$destination = $this->getDestination($source);
// Move all files/folders from temp path
if (!File::copyDirectory($source, $destination)) {
throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias]));
}
// Delete temp directory
File::deleteDirectory($source);
}
protected function getDestination($source)
{
if ($this->alias == 'core') {
return base_path();
}
if (!is_file($source . '/module.json')) {
throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias]));
}
$modules_path = config('module.paths.modules');
// Create modules directory
if (!File::isDirectory($modules_path)) {
File::makeDirectory($modules_path);
}
$module_path = $modules_path . '/' . Str::studly($this->alias);
// Create module directory
if (!File::isDirectory($module_path)) {
File::makeDirectory($module_path);
}
return $module_path;
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use App\Utilities\Console;
class DisableModule extends Job
{
protected $alias;
protected $company_id;
protected $locale;
/**
* Create a new job instance.
*
* @param $alias
* @param $company_id
* @param $locale
*/
public function __construct($alias, $company_id = null, $locale = null)
{
$this->alias = $alias;
$this->company_id = $company_id ?: session('company_id');
$this->locale = $locale ?: app()->getLocale();
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
$command = "module:disable {$this->alias} {$this->company_id} {$this->locale}";
$result = Console::run($command);
if ($result !== true) {
throw new \Exception($result);
}
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use App\Traits\SiteApi;
use App\Utilities\Info;
use Illuminate\Support\Facades\File;
class DownloadFile extends Job
{
use SiteApi;
protected $alias;
protected $version;
/**
* Create a new job instance.
*
* @param $alias
* @param $version
*/
public function __construct($alias, $version)
{
$this->alias = $alias;
$this->version = $version;
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
if (!$response = static::getResponse('GET', $this->getUrl(), ['timeout' => 50, 'track_redirects' => true])) {
throw new \Exception(trans('modules.errors.download', ['module' => $this->alias]));
}
$file = $response->getBody()->getContents();
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp/' . $path);
$file_path = $temp_path . '/upload.zip';
// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
if (!$uploaded) {
throw new \Exception(trans('modules.errors.download', ['module' => $this->alias]));
}
return $path;
}
protected function getUrl()
{
if ($this->alias == 'core') {
$info = Info::all();
$url = 'core/download/' . $this->version . '/' . $info['php'] . '/' . $info['mysql'];
} else {
$url = 'apps/' . $this->alias . '/download/' . $this->version . '/' . version('short') . '/' . setting('apps.api_key');
}
return $url;
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use App\Utilities\Console;
class EnableModule extends Job
{
protected $alias;
protected $company_id;
protected $locale;
/**
* Create a new job instance.
*
* @param $alias
* @param $company_id
* @param $locale
*/
public function __construct($alias, $company_id = null, $locale = null)
{
$this->alias = $alias;
$this->company_id = $company_id ?: session('company_id');
$this->locale = $locale ?: app()->getLocale();
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
$command = "module:enable {$this->alias} {$this->company_id} {$this->locale}";
$result = Console::run($command);
if ($result !== true) {
throw new \Exception($result);
}
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use App\Models\Module\Module;
use App\Utilities\Console;
class FinishUpdate extends Job
{
protected $alias;
protected $new;
protected $old;
/**
* Create a new job instance.
*
* @param $alias
* @param $new
* @param $old
*/
public function __construct($alias, $new, $old)
{
$this->alias = $alias;
$this->new = $new;
$this->old = $old;
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
if ($this->alias == 'core') {
$companies = [session('company_id')];
} else {
$companies = Module::alias($this->alias)->allCompanies()->cursor();
}
foreach ($companies as $company) {
$company_id = is_object($company) ? $company->id : $company;
$command = "update:finish {$this->alias} {$company_id} {$this->new} {$this->old}";
if (true !== $result = Console::run($command)) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $this->alias]);
throw new \Exception($message);
}
}
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use App\Utilities\Console;
class InstallModule extends Job
{
protected $alias;
protected $company_id;
protected $locale;
/**
* Create a new job instance.
*
* @param $alias
* @param $company_id
* @param $locale
*/
public function __construct($alias, $company_id = null, $locale = null)
{
$this->alias = $alias;
$this->company_id = $company_id ?: session('company_id');
$this->locale = $locale ?: app()->getLocale();
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
$command = "module:install {$this->alias} {$this->company_id} {$this->locale}";
$result = Console::run($command);
if ($result !== true) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $this->alias]);
throw new \Exception($message);
}
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use App\Utilities\Console;
class UninstallModule extends Job
{
protected $alias;
protected $company_id;
protected $locale;
/**
* Create a new job instance.
*
* @param $alias
* @param $company_id
* @param $locale
*/
public function __construct($alias, $company_id = null, $locale = null)
{
$this->alias = $alias;
$this->company_id = $company_id ?: session('company_id');
$this->locale = $locale ?: app()->getLocale();
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
$command = "module:uninstall {$this->alias} {$this->company_id} {$this->locale}";
$result = Console::run($command);
if ($result !== true) {
throw new \Exception($result);
}
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace App\Jobs\Install;
use App\Abstracts\Job;
use Illuminate\Support\Facades\File;
use ZipArchive;
class UnzipFile extends Job
{
protected $alias;
protected $path;
/**
* Create a new job instance.
*
* @param $alias
* @param $path
*/
public function __construct($alias, $path)
{
$this->alias = $alias;
$this->path = $path;
}
/**
* Execute the job.
*
* @return string
*/
public function handle()
{
if (empty($this->path)) {
throw new \Exception(trans('modules.errors.unzip', ['module' => $this->alias]));
}
$temp_path = storage_path('app/temp/' . $this->path);
$file = $temp_path . '/upload.zip';
// Unzip the file
$zip = new ZipArchive();
if (!$zip->open($file) || !$zip->extractTo($temp_path)) {
throw new \Exception(trans('modules.errors.unzip', ['module' => $this->alias]));
}
$zip->close();
// Remove Zip
File::delete($file);
return $this->path;
}
}