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

Commit 2db400a

Browse files
committed
implement service generator
1 parent 5cf4b2b commit 2db400a

File tree

9 files changed

+535
-9
lines changed

9 files changed

+535
-9
lines changed

lucid

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

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

10-
$app = new Symfony\Component\Console\Application('Lucid CLI', '0.1.0');
11-
$app->add(new Lucid\Console\Commands\NewCommand());
9+
require __DIR__.'/../../bootstrap/autoload.php';
10+
$app = require __DIR__.'/../../bootstrap/app.php';
1211

13-
$app->run();
12+
$kernel = $app->make(Lucid\Console\Kernel::class);
13+
14+
$status = $kernel->handle(
15+
$input = new Symfony\Component\Console\Input\ArgvInput,
16+
new Symfony\Component\Console\Output\ConsoleOutput
17+
);
18+
19+
$kernel->terminate($input, $status);
20+
21+
exit($status);

src/Commands/NewCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* This file is part of the lucid-cli project.
4+
* This file is part of the lucid-console project.
55
*
66
* (c) Vinelab <[email protected]>
77
*

src/Commands/ServiceMakeCommand.php

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
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 Illuminate\Filesystem\Filesystem;
16+
use Illuminate\Console\GeneratorCommand;
17+
use Illuminate\Config\Repository as Config;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
22+
/**
23+
* @author Abed Halawi <[email protected]>
24+
*/
25+
class ServiceMakeCommand extends GeneratorCommand
26+
{
27+
use Finder;
28+
29+
/**
30+
* The base namespace for this command.
31+
*
32+
* @var string
33+
*/
34+
private $namespace;
35+
36+
/**
37+
* The Services path
38+
*
39+
* @var string
40+
*/
41+
private $path;
42+
43+
/**
44+
* The console command name.
45+
*
46+
* @var string
47+
*/
48+
protected $name = 'make:service';
49+
50+
/**
51+
* The console command description.
52+
*
53+
* @var string
54+
*/
55+
protected $description = 'Create a new Service';
56+
57+
/**
58+
* The type of class being generated.
59+
*
60+
* @var string
61+
*/
62+
protected $type = 'Service';
63+
64+
/**
65+
* The directories to be created under the service directory.
66+
*
67+
* @var array
68+
*/
69+
protected $directories = [
70+
'Console/',
71+
'Database/',
72+
'Database/Migrations/',
73+
'Database/Seeds/',
74+
'Http/',
75+
'Http/Controllers/',
76+
'Http/Middleware/',
77+
'Http/Requests/',
78+
'Providers/',
79+
'resources/',
80+
'resources/lang/',
81+
'resources/views/',
82+
];
83+
84+
/**
85+
* Get the stub file for the generator.
86+
*
87+
* @return string
88+
*/
89+
protected function getStub()
90+
{
91+
return __DIR__.'/stubs/service.stub';
92+
}
93+
94+
/**
95+
* Execute the console command.
96+
*
97+
* @return bool|null
98+
*/
99+
public function fire()
100+
{
101+
$name = studly_case($this->getNameInput());
102+
103+
$slug = snake_case($name);
104+
105+
$path = $this->getPath($name);
106+
107+
if ($this->files->exists($path)) {
108+
$this->error($this->type.' already exists!');
109+
110+
return false;
111+
}
112+
113+
// create service directory
114+
$this->createDirectory($path);
115+
// create .gitkeep file in it
116+
$this->createFile($path.'/.gitkeep');
117+
118+
$this->createServiceDirectories($path);
119+
120+
$this->addServiceProviders($name, $slug, $path);
121+
122+
$this->addRoutesFile($name, $slug, $path);
123+
124+
$this->addWelcomeViewFile($path);
125+
126+
$this->info('Service '.$name.' created successfully.'."\n");
127+
128+
$rootNamespace = $this->findRootNamespace();
129+
$serviceNamespace = $this->findServiceNamespace($name);
130+
131+
$serviceProvider = $serviceNamespace.'\\Providers\\'.$name.'ServiceProvider';
132+
133+
$this->info('Activate it by registering '.
134+
'<comment>'.$serviceProvider.'</comment> '.
135+
"\n".
136+
'in <comment>'.$rootNamespace.'\Foundation\Providers\ServiceProvider@register</comment> '.
137+
'with the following:'.
138+
"\n"
139+
);
140+
141+
$this->info('<comment>$this->app->register(\''.$serviceProvider.'\');</comment>'."\n");
142+
}
143+
144+
/**
145+
* {@inheritdoc}
146+
*/
147+
public function getPath($name)
148+
{
149+
return $this->findServicePath($name);
150+
}
151+
152+
/**
153+
* Create an empty directory at the given path.
154+
*
155+
* @param string $path
156+
*
157+
* @return bool
158+
*/
159+
public function createDirectory($path)
160+
{
161+
return $this->files->makeDirectory($path, 0755, true, true);
162+
}
163+
164+
/**
165+
* Create a file at the given path with the given contents.
166+
*
167+
* @param string $path
168+
* @param string $contents
169+
*
170+
* @return bool
171+
*/
172+
public function createFile($path, $contents = '')
173+
{
174+
return $this->files->put($path, $contents);
175+
}
176+
177+
/**
178+
* Create the default directories at the given service path.
179+
*
180+
* @param string $path
181+
*
182+
* @return void
183+
*/
184+
public function createServiceDirectories($path)
185+
{
186+
foreach ($this->directories as $directory) {
187+
$this->createDirectory($path.'/'.$directory);
188+
$this->createFile($path.'/'.$directory.'/.gitkeep');
189+
}
190+
}
191+
192+
/**
193+
* Add the corresponding service provider for the created service.
194+
*
195+
* @param string $name
196+
* @param string $path
197+
*
198+
* @return bool
199+
*/
200+
public function addServiceProviders($name, $slug, $path)
201+
{
202+
$namespace = $this->findServiceNamespace($name).'\\Providers';
203+
204+
$this->createRegistrationServiceProvider($name, $path, $slug, $namespace);
205+
206+
$this->createRouteServiceProvider($name, $path, $slug, $namespace);
207+
}
208+
209+
/**
210+
* Create the service provider that registers this service.
211+
*
212+
* @param string $name
213+
* @param string $path
214+
*/
215+
public function createRegistrationServiceProvider($name, $path, $slug, $namespace)
216+
{
217+
$content = file_get_contents(__DIR__.'/stubs/serviceprovider.stub');
218+
$content = str_replace(
219+
['{{name}}', '{{slug}}', '{{namespace}}'],
220+
[$name, $slug, $namespace],
221+
$content
222+
);
223+
224+
$this->createFile($path.'/Providers/'.$name.'ServiceProvider.php', $content);
225+
}
226+
227+
/**
228+
* Create the routes service provider file.
229+
*
230+
* @param string $name
231+
* @param string $path
232+
* @param string $slug
233+
* @param string $namespace
234+
*/
235+
public function createRouteServiceProvider($name, $path, $slug, $namespace)
236+
{
237+
$serviceNamespace = $this->findServiceNamespace($name);
238+
$controllers = $serviceNamespace.'\Http\Controllers';
239+
$foundation = $this->findRootNamespace().'\Foundation';
240+
241+
$content = file_get_contents(__DIR__.'/stubs/routeserviceprovider.stub');
242+
$content = str_replace(
243+
['{{name}}', '{{namespace}}', '{{controllers_namespace}}', '{{foundation_namespace}}'],
244+
[$name, $namespace, $controllers, $foundation],
245+
$content
246+
);
247+
248+
$this->createFile($path.'/Providers/RouteServiceProvider.php', $content);
249+
}
250+
251+
/**
252+
* Add the routes file.
253+
*
254+
* @param string $name
255+
* @param string $slug
256+
* @param string $path
257+
*/
258+
public function addRoutesFile($name, $slug, $path)
259+
{
260+
$controllers = 'src/Services/'.$name.'/Http/Controllers';
261+
262+
$content = file_get_contents(__DIR__.'/stubs/routes.stub');
263+
$content = str_replace(['{{slug}}', '{{controllers_path}}'], [$slug, $controllers], $content);
264+
265+
$this->createFile($path.'/Http/routes.php', $content);
266+
}
267+
268+
/**
269+
* Add the welcome view file.
270+
*
271+
* @param string $path
272+
*/
273+
public function addWelcomeViewFile($path)
274+
{
275+
$this->createFile(
276+
$path.'/resources/views/welcome.blade.php',
277+
file_get_contents(__DIR__.'/stubs/welcome.blade.stub')
278+
);
279+
}
280+
}

src/Commands/stubs/routes.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Service Routes
6+
|--------------------------------------------------------------------------
7+
|
8+
| Here is where you can register all of the routes for this service.
9+
| It's a breeze. Simply tell Laravel the URIs it should respond to
10+
| and give it the controller to call when that URI is requested.
11+
|
12+
*/
13+
14+
Route::group(['prefix' => '{{slug}}'], function() {
15+
16+
// The controllers live in {{controllers_path}}
17+
// Route::get('/', 'UserController@index');
18+
19+
Route::get('/', function() {
20+
return view('{{slug}}::welcome');
21+
});
22+
23+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace {{namespace}};
3+
4+
use Illuminate\Routing\Router;
5+
use {{foundation_namespace}}\Providers\RouteServiceProvider as ServiceProvider;
6+
7+
class RouteServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Read the routes from the "routes.php" file of this service
11+
*
12+
* @param \Illuminate\Routing\Router $router
13+
*/
14+
public function map(Router $router)
15+
{
16+
$namespace = '{{controllers_namespace}}';
17+
$routesPath = __DIR__.'/../Http/routes.php';
18+
19+
$this->loadRoutesFile($router, $namespace, $routesPath);
20+
}
21+
}

0 commit comments

Comments
 (0)