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

Commit 3642de5

Browse files
committed
implement controllers generator
lucid make:controller [service] [controller]
1 parent f4b1c3c commit 3642de5

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [x] Generate Services
33
- [x] Generate Features
44
- [x] Generate Jobs
5+
- [x] Generate Controllers
56
- [x] Replace modules with custom-built services
67
- [ ] Specifying Laravel version on installation
78
- [ ] Documentation
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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\InputOption;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
20+
/**
21+
* @author Abed Halawi <[email protected]>
22+
*/
23+
class ControllerMakeCommand extends GeneratorCommand
24+
{
25+
use Finder;
26+
use Filesystem;
27+
28+
/**
29+
* The console command name.
30+
*
31+
* @var string
32+
*/
33+
protected $name = 'make:controller';
34+
35+
/**
36+
* The console command description.
37+
*
38+
* @var string
39+
*/
40+
protected $description = 'Create a new resource Controller class in a service';
41+
42+
/**
43+
* The type of class being generated.
44+
*
45+
* @var string
46+
*/
47+
protected $type = 'Controller';
48+
49+
/**
50+
* Execute the console command.
51+
*
52+
* @return bool|null
53+
*/
54+
public function fire()
55+
{
56+
$service = studly_case($this->argument('service'));
57+
$controller = $this->parseName($this->argument('controller'));
58+
59+
$path = $this->findControllerPath($service, $controller);
60+
61+
if ($this->files->exists($path)) {
62+
$this->error('Controller already exists');
63+
64+
return false;
65+
}
66+
67+
$namespace = $this->findControllerNamespace($service);
68+
69+
$content = file_get_contents($this->getStub());
70+
$content = str_replace(
71+
['{{controller}}', '{{namespace}}', '{{foundation_namespace}}'],
72+
[$controller, $namespace, $this->findFoundationNamespace()],
73+
$content
74+
);
75+
76+
$this->createFile($path, $content);
77+
$this->info('Controller class '.$controller.' created successfully.'.
78+
"\n".
79+
"\n".
80+
'Find it at <comment>'.strstr($path, 'src/').'</comment>'."\n");
81+
}
82+
83+
/**
84+
* Get the console command arguments.
85+
*
86+
* @return array
87+
*/
88+
protected function getArguments()
89+
{
90+
return [
91+
['service', InputArgument::REQUIRED, 'The service in which the controller should be generated.'],
92+
['controller', InputArgument::REQUIRED, 'The controller\'s name.'],
93+
];
94+
}
95+
96+
/**
97+
* Get the console command options.
98+
*
99+
* @return array
100+
*/
101+
protected function getOptions()
102+
{
103+
return [
104+
['plain', null, InputOption::VALUE_NONE, 'Generate an empty controller class.'],
105+
];
106+
}
107+
108+
/**
109+
* Parse the feature name.
110+
* remove the Controller.php suffix if found
111+
* we're adding it ourselves.
112+
*
113+
* @param string $name
114+
*
115+
* @return string
116+
*/
117+
protected function parseName($name)
118+
{
119+
return studly_case(preg_replace('/Controller(\.php)?$/', '', $name).'Controller');
120+
}
121+
122+
/**
123+
* Get the stub file for the generator.
124+
*
125+
* @return string
126+
*/
127+
protected function getStub()
128+
{
129+
if ($this->option('plain')) {
130+
return __DIR__.'/stubs/controller.plain.stub';
131+
}
132+
133+
return __DIR__.'/stubs/controller.stub';
134+
}
135+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace {{namespace}};
4+
5+
use Illuminate\Http\Request;
6+
use {{foundation_namespace}}\Http\Controller;
7+
8+
class {{controller}} extends Controller
9+
{
10+
11+
}

src/Commands/stubs/controller.stub

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace {{namespace}};
4+
5+
use Illuminate\Http\Request;
6+
use {{foundation_namespace}}\Http\Controllers\Controller;
7+
8+
class {{controller}} extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
26+
{
27+
//
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @return \Illuminate\Http\Response
35+
*/
36+
public function store(Request $request)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Display the specified resource.
43+
*
44+
* @param int $id
45+
* @return \Illuminate\Http\Response
46+
*/
47+
public function show($id)
48+
{
49+
//
50+
}
51+
52+
/**
53+
* Show the form for editing the specified resource.
54+
*
55+
* @param int $id
56+
* @return \Illuminate\Http\Response
57+
*/
58+
public function edit($id)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Update the specified resource in storage.
65+
*
66+
* @param \Illuminate\Http\Request $request
67+
* @param int $id
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function update(Request $request, $id)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Remove the specified resource from storage.
77+
*
78+
* @param int $id
79+
* @return \Illuminate\Http\Response
80+
*/
81+
public function destroy($id)
82+
{
83+
//
84+
}
85+
}

src/Finder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,29 @@ public function findDomainJobsNamespace($domain)
159159
{
160160
return $this->findDomainNamespace($domain).'\\Jobs';
161161
}
162+
163+
/**
164+
* Find the path for the give controller class.
165+
*
166+
* @param string $service
167+
* @param string $controller
168+
*
169+
* @return string
170+
*/
171+
public function findControllerPath($service, $controller)
172+
{
173+
return $this->findServicePath($service).'/Http/Controllers/'.$controller.'.php';
174+
}
175+
176+
/**
177+
* Find the namespace of controllers in the given service.
178+
*
179+
* @param string $service
180+
*
181+
* @return string
182+
*/
183+
public function findControllerNamespace($service)
184+
{
185+
return $this->findServiceNamespace($service).'\\Http\\Controllers';
186+
}
162187
}

src/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel
2828
Commands\ServiceMakeCommand::class,
2929
Commands\FeatureMakeCommand::class,
3030
Commands\JobMakeCommand::class,
31+
Commands\ControllerMakeCommand::class,
3132
];
3233

3334
}

0 commit comments

Comments
 (0)