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

Commit 11974d8

Browse files
authored
Merge pull request #1 from lucid-architecture/master
Controller generator, update foundation namespace etc.
2 parents 1ea6c18 + 49a0671 commit 11974d8

20 files changed

+387
-144
lines changed

lucid

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use Illuminate\Container\Container;
5-
use Lucid\Console\Commands\NewCommand;
6-
use Lucid\Console\Commands\ServiceMakeCommand;
7-
use Lucid\Console\Commands\ControllerMakeCommand;
4+
if (file_exists(__DIR__.'/vendor/autoload.php')) {
5+
require __DIR__.'/vendor/autoload.php';
6+
} else {
7+
require __DIR__.'/../../../bootstrap/autoload.php';
8+
}
89

9-
require __DIR__.'/../../../bootstrap/autoload.php';
10-
$app = require __DIR__.'/../../../bootstrap/app.php';
10+
$laravel = require __DIR__.'/../../../bootstrap/app.php';
1111

12-
$kernel = $app->make(Lucid\Console\Kernel::class);
12+
$commands = [
13+
new Lucid\Console\Commands\JobMakeCommand(),
14+
new Lucid\Console\Commands\ServiceMakeCommand(),
15+
new Lucid\Console\Commands\FeatureMakeCommand(),
16+
new Lucid\Console\Commands\ControllerMakeCommand(),
17+
new Lucid\Console\Commands\ServicesListCommand(),
18+
new Lucid\Console\Commands\FeaturesListCommand(),
19+
];
1320

14-
$status = $kernel->handle(
15-
$input = new Symfony\Component\Console\Input\ArgvInput,
16-
new Symfony\Component\Console\Output\ConsoleOutput
17-
);
21+
$app = new Symfony\Component\Console\Application('Lucid Console', '0.3.0');
22+
array_walk($commands, [$app, 'add']);
1823

19-
$kernel->terminate($input, $status);
20-
21-
exit($status);
24+
$app->run();

src/Command.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
namespace Lucid\Console;
4+
5+
use Symfony\Component\Console\Helper\Table;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
trait Command
10+
{
11+
12+
/**
13+
* Configure the command options.
14+
*
15+
* @return void
16+
*/
17+
protected function configure()
18+
{
19+
$this
20+
->setName($this->name)
21+
->setDescription($this->description);
22+
23+
foreach ($this->getArguments() as $arguments) {
24+
call_user_func_array([$this, 'addArgument'], $arguments);
25+
}
26+
27+
foreach ($this->getOptions() as $options) {
28+
call_user_func_array([$this, 'addOption'], $options);
29+
}
30+
}
31+
32+
/**
33+
* Default implementation to get the arguments of this command.
34+
*
35+
* @return array
36+
*/
37+
public function getArguments()
38+
{
39+
return [];
40+
}
41+
42+
/**
43+
* Default implementation to get the options of this command.
44+
*
45+
* @return array
46+
*/
47+
public function getOptions()
48+
{
49+
return [];
50+
}
51+
52+
/**
53+
* Execute the command.
54+
*
55+
* @param \Symfony\Component\Console\Input\InputInterface $input
56+
* @param \Symfony\Component\Console\Output\OutputInterface $output
57+
*/
58+
public function execute(InputInterface $input, OutputInterface $output)
59+
{
60+
$this->input = $input;
61+
$this->output = $output;
62+
63+
return (int) $this->fire();
64+
}
65+
66+
/**
67+
* Get an argument from the input.
68+
*
69+
* @param string $key
70+
*
71+
* @return string
72+
*/
73+
public function argument($key)
74+
{
75+
return $this->input->getArgument($key);
76+
}
77+
78+
/**
79+
* Get an option from the input.
80+
*
81+
* @param string $key
82+
*
83+
* @return string
84+
*/
85+
public function option($key)
86+
{
87+
return $this->input->getOption($key);
88+
}
89+
90+
/**
91+
* Write a string as information output.
92+
*
93+
* @param string $string
94+
*/
95+
public function info($string)
96+
{
97+
$this->output->writeln("<info>$string</info>");
98+
}
99+
100+
/**
101+
* Write a string as comment output.
102+
*
103+
* @param string $string
104+
* @return void
105+
*/
106+
public function comment($string)
107+
{
108+
$this->output->writeln("<comment>$string</comment>");
109+
}
110+
111+
/**
112+
* Write a string as error output.
113+
*
114+
* @param string $string
115+
* @return void
116+
*/
117+
public function error($string)
118+
{
119+
$this->output->writeln("<error>$string</error>");
120+
}
121+
122+
/**
123+
* Format input to textual table.
124+
*
125+
* @param array $headers
126+
* @param \Illuminate\Contracts\Support\Arrayable|array $rows
127+
* @param string $style
128+
* @return void
129+
*/
130+
public function table(array $headers, $rows, $style = 'default')
131+
{
132+
$table = new Table($this->output);
133+
134+
if ($rows instanceof Arrayable) {
135+
$rows = $rows->toArray();
136+
}
137+
138+
$table->setHeaders($headers)->setRows($rows)->setStyle($style)->render();
139+
}
140+
141+
/**
142+
* Ask the user the given question.
143+
*
144+
* @param string $question
145+
*
146+
* @return string
147+
*/
148+
public function ask($question)
149+
{
150+
$question = '<comment>'.$question.'</comment> ';
151+
152+
return $this->getHelperSet()->get('dialog')->ask($this->output, $question);
153+
}
154+
155+
/**
156+
* Confirm the operation with the user.
157+
*
158+
* @param string $task
159+
* @param string $question
160+
*
161+
* @return bool
162+
*/
163+
public function confirmTaskWithUser($task, $question)
164+
{
165+
$question = $question === true ? 'Are you sure you want to run the ['.$task.'] task?' : (string) $question;
166+
167+
$question = '<comment>'.$question.' [y/N]:</comment> ';
168+
169+
return $this->getHelperSet()->get('dialog')->askConfirmation($this->output, $question, false);
170+
}
171+
172+
/**
173+
* Ask the user the given secret question.
174+
*
175+
* @param string $question
176+
*
177+
* @return string
178+
*/
179+
public function secret($question)
180+
{
181+
$question = '<comment>'.$question.'</comment> ';
182+
183+
return $this->getHelperSet()->get('dialog')->askHiddenResponse($this->output, $question, false);
184+
}
185+
}

src/Commands/ControllerMakeCommand.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
namespace Lucid\Console\Commands;
1313

1414
use Lucid\Console\Finder;
15+
use Lucid\Console\Command;
1516
use Lucid\Console\Filesystem;
16-
use Illuminate\Console\GeneratorCommand;
1717
use Symfony\Component\Console\Input\InputOption;
18+
use Lucid\Console\Generators\ControllerGenerator;
1819
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
1921

2022
/**
2123
* @author Abed Halawi <[email protected]>
2224
*/
23-
class ControllerMakeCommand extends GeneratorCommand
25+
class ControllerMakeCommand extends SymfonyCommand
2426
{
2527
use Finder;
28+
use Command;
2629
use Filesystem;
2730

2831
/**
@@ -53,31 +56,22 @@ class ControllerMakeCommand extends GeneratorCommand
5356
*/
5457
public function fire()
5558
{
56-
$service = studly_case($this->argument('service'));
57-
$controller = $this->parseName($this->argument('controller'));
59+
$generator = new ControllerGenerator();
5860

59-
$path = $this->findControllerPath($service, $controller);
61+
$service = $this->argument('service');
62+
$name = $this->argument('controller');
6063

61-
if ($this->files->exists($path)) {
62-
$this->error('Controller already exists');
64+
try {
65+
$controller = $generator->generate($name, $service, $this->option('plain'));
6366

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

8377
/**
@@ -110,7 +104,7 @@ protected function getOptions()
110104
* remove the Controller.php suffix if found
111105
* we're adding it ourselves.
112106
*
113-
* @param string $name
107+
* @param string $name
114108
*
115109
* @return string
116110
*/

src/Commands/FeatureMakeCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111

1212
namespace Lucid\Console\Commands;
1313

14-
use Illuminate\Console\GeneratorCommand;
15-
use Lucid\Console\Filesystem;
14+
use Lucid\Console\Str;
1615
use Lucid\Console\Finder;
16+
use Lucid\Console\Command;
17+
use Lucid\Console\Filesystem;
1718
use Lucid\Console\Generators\FeatureGenerator;
18-
use Lucid\Console\Str;
1919
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
2021

2122
/**
2223
* @author Abed Halawi <[email protected]>
2324
*/
24-
class FeatureMakeCommand extends GeneratorCommand
25+
class FeatureMakeCommand extends SymfonyCommand
2526
{
2627
use Finder;
28+
use Command;
2729
use Filesystem;
2830

2931
/**
@@ -58,7 +60,7 @@ public function fire()
5860
$service = studly_case($this->argument('service'));
5961
$title = $this->parseName($this->argument('feature'));
6062

61-
$generator = app(FeatureGenerator::class);
63+
$generator = new FeatureGenerator();
6264
$feature = $generator->generate($title, $service);
6365

6466
$this->info(

src/Commands/FeaturesListCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
namespace Lucid\Console\Commands;
1313

1414
use Lucid\Console\Finder;
15-
use Illuminate\Console\Command;
15+
use Lucid\Console\Command;
1616
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
1718

1819
/**
1920
* @author Abed Halawi <[email protected]>
2021
*/
21-
class FeaturesListCommand extends Command
22+
class FeaturesListCommand extends SymfonyCommand
2223
{
2324
use Finder;
25+
use Command;
2426

2527
/**
2628
* The console command name.

src/Commands/JobMakeCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313

1414
use Lucid\Console\Str;
1515
use Lucid\Console\Finder;
16+
use Lucid\Console\Command;
1617
use Lucid\Console\Filesystem;
17-
use Illuminate\Console\GeneratorCommand;
18-
use Symfony\Component\Console\Input\InputArgument;
1918
use Lucid\Console\Generators\JobGenerator;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
2021

2122
/**
2223
* @author Abed Halawi <[email protected]>
2324
*/
24-
class JobMakeCommand extends GeneratorCommand
25+
class JobMakeCommand extends SymfonyCommand
2526
{
2627
use Finder;
28+
use Command;
2729
use Filesystem;
2830

2931
/**
@@ -54,7 +56,7 @@ class JobMakeCommand extends GeneratorCommand
5456
*/
5557
public function fire()
5658
{
57-
$generator = app(JobGenerator::class);
59+
$generator = new JobGenerator();
5860

5961
$domain = studly_case($this->argument('domain'));
6062
$title = $this->parseName($this->argument('job'));

0 commit comments

Comments
 (0)