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

Commit 94cc9d3

Browse files
committed
implement job generator command
lucid make:job [domain] [job]
1 parent 8148ad8 commit 94cc9d3

File tree

5 files changed

+188
-3
lines changed

5 files changed

+188
-3
lines changed

TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- [ ] Generator
1+
- [x] Generator
22
- [x] Generate Services
3-
- [ ] Generate Features
4-
- [ ] Generate Jobs
3+
- [x] Generate Features
4+
- [x] Generate Jobs
55
- [x] Replace modules with custom-built services
66
- [ ] Specifying Laravel version on installation
77
- [ ] Documentation

src/Commands/JobMakeCommand.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 Lucid\Console\Finder;
15+
use Lucid\Console\Filesystem;
16+
use Illuminate\Console\GeneratorCommand;
17+
use Symfony\Component\Console\Input\InputArgument;
18+
19+
/**
20+
* @author Abed Halawi <[email protected]>
21+
*/
22+
class JobMakeCommand extends GeneratorCommand
23+
{
24+
use Finder;
25+
use Filesystem;
26+
27+
/**
28+
* The console command name.
29+
*
30+
* @var string
31+
*/
32+
protected $name = 'make:job';
33+
34+
/**
35+
* The console command description.
36+
*
37+
* @var string
38+
*/
39+
protected $description = 'Create a new Job in a domain';
40+
41+
/**
42+
* The type of class being generated.
43+
*
44+
* @var string
45+
*/
46+
protected $type = 'Job';
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return bool|null
52+
*/
53+
public function fire()
54+
{
55+
$domain = studly_case($this->argument('domain'));
56+
$job = $this->parseName($this->argument('job'));
57+
58+
$path = $this->findJobPath($domain, $job);
59+
60+
if ($this->files->exists($path)) {
61+
$this->error('Job already exists');
62+
63+
return false;
64+
}
65+
66+
// Make sure the domain directory exists
67+
$this->createDirectory($this->findDomainPath($domain).'/Jobs');
68+
69+
// Create the job
70+
$namespace = $this->findDomainJobsNamespace($domain);
71+
72+
$content = file_get_contents($this->getStub());
73+
$content = str_replace(
74+
['{{job}}', '{{namespace}}', '{{foundation_namespace}}'],
75+
[$job, $namespace, $this->findFoundationNamespace()],
76+
$content
77+
);
78+
79+
$this->createFile($path, $content);
80+
81+
$this->info('Job class '.$job.' created successfully.'.
82+
"\n".
83+
"\n".
84+
'Find it at <comment>'.strstr($path, 'src/').'</comment>'."\n");
85+
}
86+
87+
/**
88+
* Get the stub file for the generator.
89+
*
90+
* @return string
91+
*/
92+
public function getStub()
93+
{
94+
return __DIR__.'/stubs/job.stub';
95+
}
96+
97+
public function getArguments()
98+
{
99+
return [
100+
['domain', InputArgument::REQUIRED, 'The domain to be responsible for the job.'],
101+
['job', InputArgument::REQUIRED, 'The job\'s name.'],
102+
];
103+
}
104+
105+
/**
106+
* Parse the job name.
107+
* remove the Job.php suffix if found
108+
* we're adding it ourselves.
109+
*
110+
* @param string $name
111+
*
112+
* @return string
113+
*/
114+
protected function parseName($name)
115+
{
116+
return studly_case(preg_replace('/Job(\.php)?$/', '', $name).'Job');
117+
}
118+
}

src/Commands/stubs/job.stub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace {{namespace}};
3+
4+
use {{foundation_namespace}}\Job;
5+
6+
class {{job}} extends Job
7+
{
8+
public function __construct()
9+
{
10+
11+
}
12+
13+
public function handle()
14+
{
15+
16+
}
17+
}

src/Finder.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,53 @@ public function findFeaturePath($service, $feature)
9898
{
9999
return $this->findServicePath($service).'/Features/'.$feature.'.php';
100100
}
101+
102+
/**
103+
* Find the path for the given domain.
104+
*
105+
* @param string $domain
106+
*
107+
* @return string
108+
*/
109+
public function findDomainPath($domain)
110+
{
111+
return $this->findSourceRoot().'/Domains/'.$domain;
112+
}
113+
114+
/**
115+
* Find the path for the given job name.
116+
*
117+
* @param string$domain
118+
* @param string$job
119+
*
120+
* @return string
121+
*/
122+
public function findJobPath($domain, $job)
123+
{
124+
return $this->findDomainPath($domain).'/Jobs/'.$job.'.php';
125+
}
126+
127+
/**
128+
* Find the namespace for the given domain.
129+
*
130+
* @param string $domain
131+
*
132+
* @return string
133+
*/
134+
public function findDomainNamespace($domain)
135+
{
136+
return $this->findRootNamespace().'\\Domains\\'.$domain;
137+
}
138+
139+
/**
140+
* Find the namespace for the given domain's Jobs.
141+
*
142+
* @param string $domain
143+
*
144+
* @return string
145+
*/
146+
public function findDomainJobsNamespace($domain)
147+
{
148+
return $this->findDomainNamespace($domain).'\\Jobs';
149+
}
101150
}

src/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Kernel extends ConsoleKernel
2727
Commands\NewCommand::class,
2828
Commands\ServiceMakeCommand::class,
2929
Commands\FeatureMakeCommand::class,
30+
Commands\JobMakeCommand::class,
3031
];
3132

3233
}

0 commit comments

Comments
 (0)