Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit 459ce2e

Browse files
committed
refactor delete commands
1 parent 6377b3b commit 459ce2e

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

lucid

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ $laravel = require __DIR__.'/../../../bootstrap/app.php';
1111

1212
$commands = [
1313
new Lucid\Console\Commands\JobMakeCommand(),
14+
new Lucid\Console\Commands\JobDeleteCommand(),
1415
new Lucid\Console\Commands\ServiceMakeCommand(),
16+
new Lucid\Console\Commands\ServiceDeleteCommand(),
1517
new Lucid\Console\Commands\FeatureMakeCommand(),
18+
new Lucid\Console\Commands\FeatureDeleteCommand(),
1619
new Lucid\Console\Commands\ControllerMakeCommand(),
1720
new Lucid\Console\Commands\ServicesListCommand(),
1821
new Lucid\Console\Commands\FeaturesListCommand(),

src/Commands/FeatureDeleteCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111

1212
namespace Lucid\Console\Commands;
1313

14-
use Illuminate\Console\GeneratorCommand;
15-
use Lucid\Console\Filesystem;
16-
use Lucid\Console\Finder;
1714
use Lucid\Console\Str;
15+
use Lucid\Console\Finder;
16+
use Lucid\Console\Command;
17+
use Lucid\Console\Filesystem;
1818
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
1920

2021
/**
2122
* @author Charalampos Raftopoulos <[email protected]>
2223
*/
23-
class FeatureDeleteCommand extends GeneratorCommand
24+
class FeatureDeleteCommand extends SymfonyCommand
2425
{
2526
use Finder;
27+
use Command;
2628
use Filesystem;
2729

2830
/**

src/Commands/JobDeleteCommand.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111

1212
namespace Lucid\Console\Commands;
1313

14-
use Illuminate\Console\GeneratorCommand;
15-
use Lucid\Console\Filesystem;
16-
use Lucid\Console\Finder;
1714
use Lucid\Console\Str;
15+
use Lucid\Console\Finder;
16+
use Lucid\Console\Command;
17+
use Lucid\Console\Filesystem;
1818
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
1920

2021
/**
2122
* @author Charalampos Raftopoulos <[email protected]>
2223
*/
23-
class JobDeleteCommand extends GeneratorCommand
24+
class JobDeleteCommand extends SymfonyCommand
2425
{
2526
use Finder;
27+
use Command;
2628
use Filesystem;
2729

2830
/**
@@ -56,15 +58,14 @@ public function fire()
5658
try {
5759
$domain = studly_case($this->argument('domain'));
5860
$title = $this->parseName($this->argument('job'));
59-
$domainPath = $this->findDomainPath($domain).'/Jobs';
6061

6162
if (!file_exists($job = $this->findJobPath($domain, $title))) {
6263
$this->error('Job class '.$title.' cannot be found.');
6364
} else {
6465
$this->deleteFile($job);
6566

66-
if (count($this->checkDirectories($domainPath)) === 0) {
67-
$this->deleteDirectory($domainPath);
67+
if (count($this->listJobs($domain)->first()) === 0) {
68+
$this->deleteDirectory($this->findDomainPath($domain));
6869
}
6970

7071
$this->info('Job class <comment>'.$title.'</comment> deleted successfully.');

src/Commands/ServiceDeleteCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
namespace Lucid\Console\Commands;
1313

1414
use Lucid\Console\Finder;
15+
use Lucid\Console\Command;
1516
use Lucid\Console\Filesystem;
16-
use Illuminate\Console\GeneratorCommand;
17+
use Symfony\Component\Console\Input\InputArgument;
18+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
1719

1820
/**
1921
* @author Charalampos Raftopoulos <[email protected]>
2022
*/
21-
class ServiceDeleteCommand extends GeneratorCommand
23+
class ServiceDeleteCommand extends SymfonyCommand
2224
{
2325
use Finder;
26+
use Command;
2427
use Filesystem;
2528

2629
/**
@@ -69,7 +72,7 @@ protected function getStub()
6972
public function fire()
7073
{
7174
try {
72-
$name = $this->getNameInput();
75+
$name = ucfirst($this->argument('name'));
7376

7477
$this->deleteDirectory($this->findServicePath($name));
7578

@@ -80,4 +83,11 @@ public function fire()
8083
dd($e->getMessage(), $e->getFile(), $e->getLine());
8184
}
8285
}
86+
87+
public function getArguments()
88+
{
89+
return [
90+
['name', InputArgument::REQUIRED, 'The service name.'],
91+
];
92+
}
8393
}

src/Filesystem.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,22 @@ public function createDirectory($path, $mode = 0755, $recursive = true, $force =
7070
*/
7171
public function deleteDirectory($path)
7272
{
73-
return $this->files->deleteDirectory($path, false);
73+
if ($handle = opendir($path)) {
74+
while (false !== ($file = readdir($handle))) {
75+
if ($file != '.' && $file != '..') {
76+
if (is_dir($path.'/'.$file)) {
77+
if (!@rmdir($path.'/'.$file)) {
78+
$this->deleteDirectory($path.'/'.$file.'/');
79+
}
80+
} else {
81+
@unlink($path.'/'.$file);
82+
}
83+
}
84+
}
85+
closedir($handle);
86+
87+
return @rmdir($path);
88+
}
7489
}
7590

7691
/**
@@ -82,18 +97,6 @@ public function deleteDirectory($path)
8297
*/
8398
public function deleteFile($path)
8499
{
85-
return $this->files->delete($path);
86-
}
87-
88-
/**
89-
* Check if a directory is empty.
90-
*
91-
* @param string $directory
92-
*
93-
* @return array
94-
*/
95-
public function checkDirectories($directory)
96-
{
97-
return $this->files->allFiles($directory);
100+
return @unlink($path);
98101
}
99102
}

0 commit comments

Comments
 (0)