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

Commit 4ed92c4

Browse files
committed
add delete feature and delete job commands
1 parent 30263e2 commit 4ed92c4

File tree

5 files changed

+244
-1
lines changed

5 files changed

+244
-1
lines changed

src/Commands/FeatureDeleteCommand.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the lucid-console project.
5+
*
6+
* (c) Vinelab <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lucid\Console\Commands;
13+
14+
use Illuminate\Console\GeneratorCommand;
15+
use Lucid\Console\Filesystem;
16+
use Lucid\Console\Finder;
17+
use Lucid\Console\Str;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
20+
/**
21+
* @author Charalampos Raftopoulos <[email protected]>
22+
*/
23+
class FeatureDeleteCommand extends GeneratorCommand
24+
{
25+
use Finder;
26+
use Filesystem;
27+
28+
/**
29+
* The console command name.
30+
*
31+
* @var string
32+
*/
33+
protected $name = 'delete:feature';
34+
35+
/**
36+
* The console command description.
37+
*
38+
* @var string
39+
*/
40+
protected $description = 'Delete an existing Feature in a service';
41+
42+
/**
43+
* The type of class being deleted.
44+
*
45+
* @var string
46+
*/
47+
protected $type = 'Feature';
48+
49+
/**
50+
* Execute the console command.
51+
*
52+
* @return bool|null
53+
*/
54+
public function fire()
55+
{
56+
try {
57+
$service = studly_case($this->argument('service'));
58+
$title = $this->parseName($this->argument('feature'));
59+
60+
if (!file_exists($feature = $this->findFeaturePath($service, $title))) {
61+
$this->error('Feature class '.$title.' cannot be found.');
62+
} else {
63+
$this->deleteFile($feature);
64+
65+
$this->info('Feature class <comment>'.$title.'</comment> deleted successfully.');
66+
}
67+
} catch (Exception $e) {
68+
$this->error($e->getMessage());
69+
}
70+
}
71+
72+
/**
73+
* Get the console command arguments.
74+
*
75+
* @return array
76+
*/
77+
protected function getArguments()
78+
{
79+
return [
80+
['service', InputArgument::REQUIRED, 'The service in which the feature should be deleted from.'],
81+
['feature', InputArgument::REQUIRED, 'The feature\'s name.'],
82+
];
83+
}
84+
85+
/**
86+
* Get the stub file for the generator.
87+
*
88+
* @return string
89+
*/
90+
protected function getStub()
91+
{
92+
return __DIR__.'/../Generators/stubs/feature.stub';
93+
}
94+
95+
/**
96+
* Parse the feature name.
97+
* remove the Feature.php suffix if found
98+
* we're adding it ourselves.
99+
*
100+
* @param string $name
101+
*
102+
* @return string
103+
*/
104+
protected function parseName($name)
105+
{
106+
return Str::feature($name);
107+
}
108+
}

src/Commands/JobDeleteCommand.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the lucid-console project.
5+
*
6+
* (c) Vinelab <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lucid\Console\Commands;
13+
14+
use Illuminate\Console\GeneratorCommand;
15+
use Lucid\Console\Filesystem;
16+
use Lucid\Console\Finder;
17+
use Lucid\Console\Str;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
20+
/**
21+
* @author Charalampos Raftopoulos <[email protected]>
22+
*/
23+
class JobDeleteCommand extends GeneratorCommand
24+
{
25+
use Finder;
26+
use Filesystem;
27+
28+
/**
29+
* The console command name.
30+
*
31+
* @var string
32+
*/
33+
protected $name = 'delete:job';
34+
35+
/**
36+
* The console command description.
37+
*
38+
* @var string
39+
*/
40+
protected $description = 'Delete an existing Job in a domain';
41+
42+
/**
43+
* The type of class being deleted.
44+
*
45+
* @var string
46+
*/
47+
protected $type = 'Job';
48+
49+
/**
50+
* Execute the console command.
51+
*
52+
* @return bool|null
53+
*/
54+
public function fire()
55+
{
56+
try {
57+
$domain = studly_case($this->argument('domain'));
58+
$title = $this->parseName($this->argument('job'));
59+
$domainPath = $this->findDomainPath($domain).'/Jobs';
60+
61+
if (!file_exists($job = $this->findJobPath($domain, $title))) {
62+
$this->error('Job class '.$title.' cannot be found.');
63+
} else {
64+
$this->deleteFile($job);
65+
66+
if (count($this->checkDirectories($domainPath)) === 0) {
67+
$this->deleteDirectory($domainPath);
68+
}
69+
70+
$this->info('Job class <comment>'.$title.'</comment> deleted successfully.');
71+
}
72+
} catch (Exception $e) {
73+
$this->error($e->getMessage());
74+
}
75+
}
76+
77+
public function getArguments()
78+
{
79+
return [
80+
['domain', InputArgument::REQUIRED, 'The domain from which the job will be deleted.'],
81+
['job', InputArgument::REQUIRED, 'The job\'s name.'],
82+
];
83+
}
84+
85+
/**
86+
* Get the stub file for the generator.
87+
*
88+
* @return string
89+
*/
90+
public function getStub()
91+
{
92+
return __DIR__.'/../Generators/stubs/job.stub';
93+
}
94+
95+
/**
96+
* Parse the job name.
97+
* remove the Job.php suffix if found
98+
* we're adding it ourselves.
99+
*
100+
* @param string $name
101+
*
102+
* @return string
103+
*/
104+
protected function parseName($name)
105+
{
106+
return Str::job($name);
107+
}
108+
}

src/Commands/ServiceDeleteCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function fire()
7373

7474
$this->deleteDirectory($this->findServicePath($name));
7575

76-
$this->info('Service '.$name.' deleted successfully.'."\n");
76+
$this->info('Service <comment>'.$name.'</comment> deleted successfully.'."\n");
7777

7878
$this->info('Please remove your registered service providers, if any.');
7979
} catch (\Exception $e) {

src/Filesystem.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ public function deleteDirectory($path)
5353
{
5454
return $this->files->deleteDirectory($path, false);
5555
}
56+
57+
/**
58+
* Delete an existing file at the given path.
59+
*
60+
* @param string $path
61+
*
62+
* @return bool
63+
*/
64+
public function deleteFile($path)
65+
{
66+
return $this->files->delete($path);
67+
}
68+
69+
/**
70+
* Check if a directory is empty.
71+
*
72+
* @param string $directory
73+
*
74+
* @return array
75+
*/
76+
public function checkDirectories($directory)
77+
{
78+
return $this->files->allFiles($directory);
79+
}
5680
}

src/Kernel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* @author Abed Halawi <[email protected]>
18+
* @author Charalampos Raftopoulos <[email protected]>
1819
*/
1920
class Kernel extends ConsoleKernel
2021
{
@@ -26,9 +27,11 @@ class Kernel extends ConsoleKernel
2627
protected $commands = [
2728
Commands\NewCommand::class,
2829
Commands\JobMakeCommand::class,
30+
Commands\JobDeleteCommand::class,
2931
Commands\ServiceMakeCommand::class,
3032
Commands\ServiceDeleteCommand::class,
3133
Commands\FeatureMakeCommand::class,
34+
Commands\FeatureDeleteCommand::class,
3235
Commands\ServicesListCommand::class,
3336
Commands\FeaturesListCommand::class,
3437
Commands\ControllerMakeCommand::class,

0 commit comments

Comments
 (0)