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

Commit 2ba2fc6

Browse files
committed
implement feature generator command
lucid make:feature [service] [feature]
1 parent 119be59 commit 2ba2fc6

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

src/Commands/FeatureMakeCommand.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 FeatureMakeCommand 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:feature';
33+
34+
/**
35+
* The console command description.
36+
*
37+
* @var string
38+
*/
39+
protected $description = 'Create a new Feature in a service';
40+
41+
/**
42+
* The type of class being generated.
43+
*
44+
* @var string
45+
*/
46+
protected $type = 'Feature';
47+
48+
public function fire()
49+
{
50+
$service = studly_case($this->argument('service'));
51+
$feature = $this->parseName($this->argument('feature'));
52+
53+
$path = $this->findFeaturePath($service, $feature);
54+
55+
if ($this->files->exists($path)) {
56+
$this->error('Feature already exists!');
57+
58+
return false;
59+
}
60+
61+
$namespace = $this->findServiceNamespace($service).'\\Features';
62+
63+
$content = file_get_contents($this->getStub());
64+
$content = str_replace(
65+
['{{feature}}', '{{namespace}}', '{{foundation_namespace}}'],
66+
[$feature, $namespace, $this->findFoundationNamespace()],
67+
$content
68+
);
69+
70+
$this->createFile($path, $content);
71+
72+
$this->info('Class '.$feature.' created successfully.'.
73+
"\n".
74+
"\n".
75+
'Find it at <comment>'.strstr($path, 'src/').'</comment>'."\n");
76+
}
77+
78+
/**
79+
* Get the console command arguments.
80+
*
81+
* @return array
82+
*/
83+
protected function getArguments()
84+
{
85+
return [
86+
['service', InputArgument::REQUIRED, 'The service in which the feature should be implemented.'],
87+
['feature', InputArgument::REQUIRED, 'The feature name'],
88+
];
89+
}
90+
91+
/**
92+
* Get the stub file for the generator.
93+
*
94+
* @return string
95+
*/
96+
protected function getStub()
97+
{
98+
return __DIR__.'/stubs/feature.stub';
99+
}
100+
101+
/**
102+
* Parse the feature name.
103+
* remove the Feature.php suffix if found
104+
* we're adding it ourselves.
105+
*
106+
* @param string $name
107+
*
108+
* @return string
109+
*/
110+
protected function parseName($name)
111+
{
112+
return studly_case(preg_replace('/Feature(\.php)?$/', '', $name).'Feature');
113+
}
114+
}

src/Commands/stubs/feature.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}}\Feature;
5+
6+
class {{feature}} extends Feature
7+
{
8+
public function __construct()
9+
{
10+
11+
}
12+
13+
public function handle()
14+
{
15+
16+
}
17+
}

src/Finder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,17 @@ public function findServicePath($service)
8585
{
8686
return $this->findSourceRoot().'/Services/'.$service;
8787
}
88+
89+
/**
90+
* Find the file path for the given feature.
91+
*
92+
* @param string $service
93+
* @param string $feature
94+
*
95+
* @return string
96+
*/
97+
public function findFeaturePath($service, $feature)
98+
{
99+
return $this->findServicePath($service).'/Features/'.$feature.'.php';
100+
}
88101
}

src/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Kernel extends ConsoleKernel
2626
protected $commands = [
2727
Commands\NewCommand::class,
2828
Commands\ServiceMakeCommand::class,
29-
Commands\ControllerMakeCommand::class,
29+
Commands\FeatureMakeCommand::class,
3030
];
3131

3232
}

0 commit comments

Comments
 (0)