-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cedd99
commit 1c2ce4c
Showing
7 changed files
with
117 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace TimWassenburg\ServiceGenerator\Console; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class MakeServiceCommand extends GeneratorCommand | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'make:service {name} {--methods=}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new service class'; | ||
|
||
/** | ||
* The type of class being generated. | ||
* | ||
* @var string | ||
*/ | ||
protected $type = 'Service'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
*/ | ||
protected function getStub(): string | ||
{ | ||
return __DIR__ . '/../../stubs/service.stub'; | ||
} | ||
|
||
/** | ||
* Get the method stub. | ||
*/ | ||
protected function getMethodStub(): string | ||
{ | ||
return __DIR__ . '/../../stubs/service-method.stub'; | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
* | ||
* @param string $rootNamespace | ||
*/ | ||
protected function getDefaultNamespace($rootNamespace): string | ||
{ | ||
return $rootNamespace . '\Services'; | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments(): array | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the service'], | ||
['methods', InputArgument::OPTIONAL, 'The methods you want to add to the service (separated by a comma)'], | ||
]; | ||
} | ||
|
||
/** | ||
* Build the class with the given name. | ||
* | ||
* Remove the base controller import if we are already in the base namespace. | ||
* | ||
* @param string $name | ||
* @return string | ||
* @throws FileNotFoundException | ||
*/ | ||
protected function buildClass($name): string | ||
{ | ||
$replace = []; | ||
$replace['{{ methods }}'] = $this->option('methods') ? $this->setMethods() : ''; | ||
|
||
return str_replace( | ||
array_keys($replace), array_values($replace), parent::buildClass($name) | ||
); | ||
} | ||
|
||
private function setMethods(): string | ||
{ | ||
$methods = []; | ||
$methodArguments = explode(',', $this->option('methods')); | ||
|
||
foreach ($methodArguments as $methodArgument) { | ||
$methods[] = str_replace('{{ method_name }}', $methodArgument, $this->files->get($this->getMethodStub())); | ||
} | ||
|
||
return implode(PHP_EOL, $methods); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public function {{ method_name }}() | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ namespace {{ namespace }}; | |
*/ | ||
class DummyClass | ||
{ | ||
|
||
{{ methods }} | ||
} |