Skip to content

Commit

Permalink
add new methods param
Browse files Browse the repository at this point in the history
  • Loading branch information
timwassenburg committed Apr 21, 2023
1 parent 1cedd99 commit 1c2ce4c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 53 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ To generate a new service use the following artisan command.
php artisan make:service UserService
```

Optionally, you can add multiple method names (seperated by comma) with the ```--methods``` param.

```bash
php artisan make:service UserService --methods=register,login,logout
```


### Generate a service for a model

Add a ```--service``` or ```-S``` param to generate a service for the model.
Expand Down
2 changes: 2 additions & 0 deletions src/Console/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TimWassenburg\ServiceGenerator\Console;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Routing\Console\ControllerMakeCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -10,6 +11,7 @@ class MakeController extends ControllerMakeCommand
{
/**
* @return void
* @throws FileNotFoundException
*/
public function handle()
{
Expand Down
50 changes: 0 additions & 50 deletions src/Console/MakeService.php

This file was deleted.

101 changes: 101 additions & 0 deletions src/Console/MakeServiceCommand.php
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);
}
}
4 changes: 2 additions & 2 deletions src/ServiceGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Support\ServiceProvider;
use TimWassenburg\ServiceGenerator\Console\MakeModel;
use TimWassenburg\ServiceGenerator\Console\MakeService;
use TimWassenburg\ServiceGenerator\Console\MakeServiceCommand;

class ServiceGeneratorServiceProvider extends ServiceProvider
{
Expand All @@ -13,7 +13,7 @@ public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
MakeService::class,
MakeServiceCommand::class,
MakeModel::class
]);
}
Expand Down
4 changes: 4 additions & 0 deletions stubs/service-method.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function {{ method_name }}()
{
//
}
2 changes: 1 addition & 1 deletion stubs/service.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ namespace {{ namespace }};
*/
class DummyClass
{

{{ methods }}
}

0 comments on commit 1c2ce4c

Please sign in to comment.