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

Commit 1e35262

Browse files
committed
implement Service generator
extracted from the ServiceMakeCommand class
1 parent b30437c commit 1e35262

File tree

2 files changed

+202
-157
lines changed

2 files changed

+202
-157
lines changed

src/Commands/ServiceMakeCommand.php

Lines changed: 11 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Lucid\Console\Filesystem;
1717
use Illuminate\Console\GeneratorCommand;
1818
use Illuminate\Config\Repository as Config;
19+
use Lucid\Console\Generators\ServiceGenerator;
1920
use Symfony\Component\Console\Input\InputOption;
2021
use Symfony\Component\Console\Input\InputInterface;
2122
use Symfony\Component\Console\Output\OutputInterface;
@@ -56,35 +57,14 @@ class ServiceMakeCommand extends GeneratorCommand
5657
*/
5758
protected $description = 'Create a new Service';
5859

59-
/**
60-
* The directories to be created under the service directory.
61-
*
62-
* @var array
63-
*/
64-
protected $directories = [
65-
'Console/',
66-
'Database/',
67-
'Database/Migrations/',
68-
'Database/Seeds/',
69-
'Http/',
70-
'Http/Controllers/',
71-
'Http/Middleware/',
72-
'Http/Requests/',
73-
'Providers/',
74-
'Features/',
75-
'resources/',
76-
'resources/lang/',
77-
'resources/views/',
78-
];
79-
8060
/**
8161
* Get the stub file for the generator.
8262
*
8363
* @return string
8464
*/
8565
protected function getStub()
8666
{
87-
return __DIR__.'/stubs/service.stub';
67+
return __DIR__.'/../Generators/stubs/service.stub';
8868
}
8969

9070
/**
@@ -94,37 +74,19 @@ protected function getStub()
9474
*/
9575
public function fire()
9676
{
97-
$name = Str::service($this->getNameInput());
98-
99-
$slug = snake_case($name);
100-
101-
$path = $this->getPath($name);
102-
103-
if ($this->files->exists($path)) {
104-
$this->error('Service already exists!');
105-
106-
return false;
107-
}
108-
109-
// create service directory
110-
$this->createDirectory($path);
111-
// create .gitkeep file in it
112-
$this->createFile($path.'/.gitkeep');
113-
114-
$this->createServiceDirectories($path);
115-
116-
$this->addServiceProviders($name, $slug, $path);
77+
try {
11778

118-
$this->addRoutesFile($name, $slug, $path);
79+
$name = $this->getNameInput();
11980

120-
$this->addWelcomeViewFile($path);
81+
$generator = app(ServiceGenerator::class);
82+
$service = $generator->generate($name);
12183

122-
$this->info('Service '.$name.' created successfully.'."\n");
84+
$this->info('Service '.$service->name.' created successfully.'."\n");
12385

12486
$rootNamespace = $this->findRootNamespace();
125-
$serviceNamespace = $this->findServiceNamespace($name);
87+
$serviceNamespace = $this->findServiceNamespace($service->name);
12688

127-
$serviceProvider = $serviceNamespace.'\\Providers\\'.$name.'ServiceProvider';
89+
$serviceProvider = $serviceNamespace.'\\Providers\\'.$service->name.'ServiceProvider';
12890

12991
$this->info('Activate it by registering '.
13092
'<comment>'.$serviceProvider.'</comment> '.
@@ -135,117 +97,9 @@ public function fire()
13597
);
13698

13799
$this->info('<comment>$this->app->register(\''.$serviceProvider.'\');</comment>'."\n");
138-
}
139-
140-
/**
141-
* {@inheritdoc}
142-
*/
143-
public function getPath($name)
144-
{
145-
return $this->findServicePath($name);
146-
}
147-
148-
/**
149-
* Create the default directories at the given service path.
150-
*
151-
* @param string $path
152-
*
153-
* @return void
154-
*/
155-
public function createServiceDirectories($path)
156-
{
157-
foreach ($this->directories as $directory) {
158-
$this->createDirectory($path.'/'.$directory);
159-
$this->createFile($path.'/'.$directory.'/.gitkeep');
100+
} catch (\Exception $e) {
101+
dd($e->getMessage(), $e->getFile(), $e->getLine());
160102
}
161103
}
162104

163-
/**
164-
* Add the corresponding service provider for the created service.
165-
*
166-
* @param string $name
167-
* @param string $path
168-
*
169-
* @return bool
170-
*/
171-
public function addServiceProviders($name, $slug, $path)
172-
{
173-
$namespace = $this->findServiceNamespace($name).'\\Providers';
174-
175-
$this->createRegistrationServiceProvider($name, $path, $slug, $namespace);
176-
177-
$this->createRouteServiceProvider($name, $path, $slug, $namespace);
178-
}
179-
180-
/**
181-
* Create the service provider that registers this service.
182-
*
183-
* @param string $name
184-
* @param string $path
185-
*/
186-
public function createRegistrationServiceProvider($name, $path, $slug, $namespace)
187-
{
188-
$content = file_get_contents(__DIR__.'/stubs/serviceprovider.stub');
189-
$content = str_replace(
190-
['{{name}}', '{{slug}}', '{{namespace}}'],
191-
[$name, $slug, $namespace],
192-
$content
193-
);
194-
195-
$this->createFile($path.'/Providers/'.$name.'ServiceProvider.php', $content);
196-
}
197-
198-
/**
199-
* Create the routes service provider file.
200-
*
201-
* @param string $name
202-
* @param string $path
203-
* @param string $slug
204-
* @param string $namespace
205-
*/
206-
public function createRouteServiceProvider($name, $path, $slug, $namespace)
207-
{
208-
$serviceNamespace = $this->findServiceNamespace($name);
209-
$controllers = $serviceNamespace.'\Http\Controllers';
210-
$foundation = $this->findFoundationNamespace();
211-
212-
$content = file_get_contents(__DIR__.'/stubs/routeserviceprovider.stub');
213-
$content = str_replace(
214-
['{{name}}', '{{namespace}}', '{{controllers_namespace}}', '{{foundation_namespace}}'],
215-
[$name, $namespace, $controllers, $foundation],
216-
$content
217-
);
218-
219-
$this->createFile($path.'/Providers/RouteServiceProvider.php', $content);
220-
}
221-
222-
/**
223-
* Add the routes file.
224-
*
225-
* @param string $name
226-
* @param string $slug
227-
* @param string $path
228-
*/
229-
public function addRoutesFile($name, $slug, $path)
230-
{
231-
$controllers = 'src/Services/'.$name.'/Http/Controllers';
232-
233-
$content = file_get_contents(__DIR__.'/stubs/routes.stub');
234-
$content = str_replace(['{{slug}}', '{{controllers_path}}'], [$slug, $controllers], $content);
235-
236-
$this->createFile($path.'/Http/routes.php', $content);
237-
}
238-
239-
/**
240-
* Add the welcome view file.
241-
*
242-
* @param string $path
243-
*/
244-
public function addWelcomeViewFile($path)
245-
{
246-
$this->createFile(
247-
$path.'/resources/views/welcome.blade.php',
248-
file_get_contents(__DIR__.'/stubs/welcome.blade.stub')
249-
);
250-
}
251105
}

0 commit comments

Comments
 (0)