Skip to content

Commit c322c0a

Browse files
authored
Merge pull request #21 from albinvar/dev
Refactoring codes
2 parents ae7db63 + dc5880a commit c322c0a

File tree

4 files changed

+90
-46
lines changed

4 files changed

+90
-46
lines changed

app/Commands/Installer/Symfony.php

+67-40
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@
77
use Illuminate\Console\Scheduling\Schedule;
88
use Laminas\Text\Figlet\Figlet;
99
use LaravelZero\Framework\Commands\Command;
10+
use App\Helpers\Webzone;
11+
use App\Helpers\Downloader;
12+
use Illuminate\Support\Facades\Storage;
1013

1114
class Symfony extends Command
1215
{
13-
protected $symfony;
16+
protected static $symfony;
1417

15-
protected $dir;
18+
protected static $dir;
19+
20+
protected static $cliName = 'symfony';
21+
22+
protected static $disk;
23+
24+
protected static $link;
25+
26+
protected static string $needle = 'Symfony CLI version';
1627

1728
/**
1829
* The signature of the command.
@@ -26,49 +37,61 @@ class Symfony extends Command
2637
*
2738
* @var string
2839
*/
29-
protected $description = 'Install Symfony Installer';
30-
40+
protected $description = 'Install Symfony CLI ';
41+
42+
public function __construct()
43+
{
44+
parent::__construct();
45+
46+
$this->webzone = new Webzone();
47+
}
48+
3149
/**
3250
* Execute the console command.
3351
*/
3452
public function handle(): void
3553
{
3654
$this->callSilently('settings:init');
37-
$this->symfony = config('symfony.PATH');
38-
$this->dir = '/data/data/com.termux/files/usr/bin';
55+
56+
self::setConfigs();
57+
3958
$this->install();
4059
}
60+
61+
public static function setConfigs()
62+
{
63+
static::$symfony = config('symfony.CLI_PATH');
64+
static::$dir = config('symfony.PATH', '/data/data/com.termux/files/usr/bin');
65+
static::$link = config('symfony.CLI_DOWNLOAD_LINK');
66+
}
4167

4268
public function install()
4369
{
4470
if ($this->checkInstallation()) {
4571
$this->error('Symfony CLI is already installed. Use "symfony help" to show all commands.');
46-
return false;
72+
return 1;
4773
}
4874

49-
$this->info(exec('clear'));
50-
$this->logo();
51-
$this->comment("\nInstalling Symfony CLI...\n");
52-
$link = config('symfony.CLI_DOWNLOAD_LINK');
53-
$lines = shell_exec("curl -w '\n%{http_code}\n' {$link} -o {$this->symfony} && chmod +x {$this->symfony}");
54-
$lines = explode("\n", trim($lines));
55-
$status = $lines[count($lines) - 1];
56-
$this->checkDownloadStatus($status, $this->dir);
75+
$this->webzone->clear();
76+
77+
$this->webzone->logo('Symfony', 'comment');
78+
79+
$this->newline();
80+
$this->comment("Installing Symfony CLI...");
81+
$this->newline();
82+
83+
$this->runTasks();
84+
5785
}
5886

5987
public function checkInstallation(): bool
6088
{
61-
if (file_exists($this->symfony)) {
89+
if (file_exists(static::$symfony)) {
6290
return true;
6391
}
6492
return false;
6593
}
6694

67-
public function logo(): void
68-
{
69-
$figlet = new Figlet();
70-
$this->comment($figlet->setFont(config('logo.font'))->render('Symfony'));
71-
}
7295

7396
/**
7497
* Define the command's schedule.
@@ -78,30 +101,34 @@ public function schedule(Schedule $schedule): void
78101
// $schedule->command(static::class)->everyMinute();
79102
}
80103

81-
private function checkDownloadStatus($status, $dir): void
82-
{
83-
switch ($status) {
84-
case 000:
85-
$this->error('Cannot connect to Server');
86-
break;
87-
case 200:
88-
$this->comment("\nDownloaded Successfully...!!!");
89-
$this->runTasks();
90-
break;
91-
case 404:
92-
$this->error('File not found on server..');
93-
break;
94-
default:
95-
$this->error('An Unknown Error occurred...');
96-
}
97-
}
98-
99104
private function runTasks(): void
100105
{
106+
$this->download();
107+
108+
$this->task('setting permissions', function() {
109+
return chmod(static::$symfony, 0775);
110+
});
111+
101112
$this->task('verifying command ', function () {
102-
if (file_exists($this->symfony)) {
113+
return (str_contains(shell_exec('symfony version'), static::$needle) && file_exists(static::$symfony));
114+
});
115+
}
116+
117+
private function download(): void
118+
{
119+
static::$disk = Storage::build([
120+
'driver' => 'local',
121+
'root' => static::$dir,
122+
]);
123+
124+
$downloadTask = $this->task('Downloading resources ', function () {
125+
$this->downloader = new Downloader(static::$link, static::$cliName, static::$disk);
126+
$response = $this->downloader->download();
127+
128+
if ($response['ok']) {
103129
return true;
104130
}
131+
$this->error($response['error']->getMessage());
105132
return false;
106133
});
107134
}

app/Commands/Projects.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Projects extends Command
2828
/**
2929
* Execute the console command.
3030
*/
31-
public function handle(): mixed
31+
public function handle(): void
3232
{
3333
if (file_exists($this->getRoot())) {
3434
$this->listAll();

app/Helpers/Downloader.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use GuzzleHttp\Exception\RequestException;
99
use GuzzleHttp\Psr7\Utils;
1010
use Storage;
11+
use Illuminate\Filesystem\FilesystemAdapter;
1112

1213
class Downloader extends Webzone
1314
{
@@ -21,18 +22,23 @@ class Downloader extends Webzone
2122

2223
protected $saveTo;
2324

24-
public function __construct(string $url, string $file, string $disk = 'tmp')
25+
public function __construct(string $url, string $file, $disk = 'tmp')
2526
{
2627
parent::__construct();
2728

2829
$this->client = new Client(['http_error' => false]);
2930

3031
$this->url = $url;
3132
$this->file = $file;
33+
34+
if(is_object($disk) && $disk instanceof FilesystemAdapter)
35+
{
36+
$this->downloadFile = $disk->getAdapter()->getPathPrefix().$this->file;
37+
} else {
38+
$this->disk = ($disk !== 'tmp') ? $disk : 'tmp';
39+
$this->downloadFile = Storage::disk($this->disk)->getAdapter()->getPathPrefix().'/'.$this->file;
40+
}
3241

33-
$disk !== 'tmp' ? $this->disk = $disk : $this->disk = 'tmp';
34-
35-
$this->downloadFile = Storage::disk($this->disk)->getAdapter()->getPathPrefix().'/'.$this->file;
3642
}
3743

3844
public function download()

config/symfony.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@
1818
| Symfony CLI Path (Do Not Change)
1919
|--------------------------------------------------------------------------
2020
|
21+
| path to symfony cli.
22+
|
23+
*/
24+
25+
'CLI_PATH' => '/data/data/com.termux/files/usr/bin/symfony',
26+
27+
/*
28+
|--------------------------------------------------------------------------
29+
| Path which all commands are stored.
30+
|--------------------------------------------------------------------------
31+
|
2132
| location of symfony cli.
2233
|
2334
*/
2435

25-
'PATH' => '/data/data/com.termux/files/usr/bin/symfony',
36+
'PATH' => '/data/data/com.termux/files/usr/bin',
2637

2738
/*
2839
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)