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

Commit b96623d

Browse files
committed
implement controller generator
1 parent 1ea6c18 commit b96623d

File tree

5 files changed

+95
-32
lines changed

5 files changed

+95
-32
lines changed

src/Commands/ControllerMakeCommand.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Lucid\Console\Filesystem;
1616
use Illuminate\Console\GeneratorCommand;
1717
use Symfony\Component\Console\Input\InputOption;
18+
use Lucid\Console\Generators\ControllerGenerator;
1819
use Symfony\Component\Console\Input\InputArgument;
1920

2021
/**
@@ -53,31 +54,22 @@ class ControllerMakeCommand extends GeneratorCommand
5354
*/
5455
public function fire()
5556
{
56-
$service = studly_case($this->argument('service'));
57-
$controller = $this->parseName($this->argument('controller'));
57+
$generator = app(ControllerGenerator::class);
5858

59-
$path = $this->findControllerPath($service, $controller);
59+
$service = $this->argument('service');
60+
$name = $this->argument('controller');
6061

61-
if ($this->files->exists($path)) {
62-
$this->error('Controller already exists');
62+
try {
63+
$controller = $generator->generate($name, $service);
6364

64-
return false;
65+
$this->info('Controller class created successfully.'.
66+
"\n".
67+
"\n".
68+
'Find it at <comment>'.strstr($controller, 'src/').'</comment>'."\n"
69+
);
70+
} catch (Exception $e) {
71+
$this->error($e->getMessage());
6572
}
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");
8173
}
8274

8375
/**
@@ -110,7 +102,7 @@ protected function getOptions()
110102
* remove the Controller.php suffix if found
111103
* we're adding it ourselves.
112104
*
113-
* @param string $name
105+
* @param string $name
114106
*
115107
* @return string
116108
*/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the laravel-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\Generators;
13+
14+
use Lucid\Console\Str;
15+
16+
/**
17+
* @author Abed Halawi <[email protected]>
18+
*/
19+
class ControllerGenerator extends Generator
20+
{
21+
public function generate($name, $service, $plain = false)
22+
{
23+
$name = Str::controller($name);
24+
$service = Str::service($service);
25+
26+
$path = $this->findControllerPath($service, $name);
27+
28+
if ($this->files->exists($path)) {
29+
$this->error('Controller already exists!');
30+
31+
return false;
32+
}
33+
34+
$namespace = $this->findControllerNamespace($service);
35+
36+
$content = file_get_contents($this->getStub($plain));
37+
$content = str_replace(
38+
['{{controller}}', '{{namespace}}', '{{foundation_namespace}}'],
39+
[$name, $namespace, $this->findFoundationNamespace()],
40+
$content
41+
);
42+
43+
$this->createFile($path, $content);
44+
45+
return $this->relativeFromReal($path);
46+
}
47+
48+
/**
49+
* Get the stub file for the generator.
50+
*
51+
* @return string
52+
*/
53+
protected function getStub($plain)
54+
{
55+
if ($plain) {
56+
return __DIR__.'/stubs/controller.plain.stub';
57+
}
58+
59+
return __DIR__.'/stubs/controller.stub';
60+
}
61+
}

src/Generators/stubs/controller.plain.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace {{namespace}};
43

54
use Illuminate\Http\Request;

src/Generators/stubs/controller.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
2-
32
namespace {{namespace}};
43

54
use Illuminate\Http\Request;
6-
use {{foundation_namespace}}\Http\Controllers\Controller;
5+
use {{foundation_namespace}}\Http\Controller;
76

87
class {{controller}} extends Controller
98
{

src/Str.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class Str
2020
* Determine the real name of the given name,
2121
* excluding the given pattern.
2222
* i.e. the name: "CreateArticleFeature.php" with pattern '/Feature.php'
23-
* will result in "Create Article"
23+
* will result in "Create Article".
2424
*
25-
* @param string $name
26-
* @param string $pattern
25+
* @param string $name
26+
* @param string $pattern
2727
*
2828
* @return string
2929
*/
@@ -41,7 +41,7 @@ public static function realName($name, $pattern = '//')
4141
* and many other forms will be transformed to "CreatePostFeature" which is
4242
* the standard feature class name.
4343
*
44-
* @param string $name
44+
* @param string $name
4545
*
4646
* @return string
4747
*/
@@ -57,7 +57,7 @@ public static function feature($name)
5757
* and many other forms will be transformed to "CreatePostJob" which is
5858
* the standard job class name.
5959
*
60-
* @param string $name
60+
* @param string $name
6161
*
6262
* @return string
6363
*/
@@ -71,7 +71,7 @@ public static function job($name)
7171
*
7272
* Domain names are just CamelCase
7373
*
74-
* @param string $name
74+
* @param string $name
7575
*
7676
* @return string
7777
*/
@@ -83,12 +83,24 @@ public static function domain($name)
8383
/**
8484
* Get the given name formatted as a service name.
8585
*
86-
* @param string $name
86+
* @param string $name
8787
*
8888
* @return string
8989
*/
9090
public static function service($name)
9191
{
9292
return studly_case($name);
9393
}
94+
95+
/**
96+
* Get the given name formatted as a controller name.
97+
*
98+
* @param string $name
99+
*
100+
* @return string
101+
*/
102+
public static function controller($name)
103+
{
104+
return studly_case(preg_replace('/Controller(\.php)?$/', '', $name).'Controller');
105+
}
94106
}

0 commit comments

Comments
 (0)