Skip to content

Commit

Permalink
Merge pull request #1828 from nWidart/handle-case-where-config-servic…
Browse files Browse the repository at this point in the history
…e-does-not-exist

added invokable and force options into make-service command
  • Loading branch information
dcblogdev authored Apr 25, 2024
2 parents d74e3aa + d40e10f commit addf050
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/Commands/Make/ServiceMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ServiceMakeCommand extends GeneratorCommand
{
Expand All @@ -20,9 +21,9 @@ public function getDestinationFilePath(): string
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());

$servicePath = GenerateConfigReader::read('service');
$servicePath = GenerateConfigReader::read('service')->getPath() ?? config('modules.paths.app_folder').'Services';

return $path . $servicePath->getPath() . '/' . $this->getServiceName() . '.php';
return $path . $servicePath . '/' . $this->getServiceName() . '.php';
}

protected function getTemplateContents(): string
Expand All @@ -43,6 +44,17 @@ protected function getArguments(): array
];
}

/**
* @return array
*/
protected function getOptions()
{
return [
['invokable', 'i', InputOption::VALUE_NONE, 'Generate an invokable class', null],
['force', 'f', InputOption::VALUE_NONE, 'su.'],
];
}

protected function getServiceName(): array|string
{
return Str::studly($this->argument('name'));
Expand All @@ -55,12 +67,11 @@ private function getClassNameWithoutNamespace(): array|string

public function getDefaultNamespace(): string
{
return config('modules.paths.generator.service.namespace')
?? ltrim(config('modules.paths.generator.service.path', 'Services'), config('modules.paths.app_folder'));
return config('modules.paths.generator.service.namespace', 'Services');
}

protected function getStubName(): string
{
return '/service.stub';
return $this->option('invokable') === true ? '/service-invoke.stub' : '/service.stub';
}
}
11 changes: 11 additions & 0 deletions src/Commands/stubs/service-invoke.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace $CLASS_NAMESPACE$;

class $CLASS$
{
public function __invokable()
{
//
}
}
17 changes: 17 additions & 0 deletions tests/Commands/ServiceMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public function test_it_generates_a_new_service_class()
$this->assertSame(0, $code);
}

public function test_it_generates_a_new_service_class_can_override_with_force_option()
{
$this->artisan('module:make-service', ['name' => 'MyService', 'module' => 'Blog']);
$code = $this->artisan('module:make-service', ['name' => 'MyService', 'module' => 'Blog', '--force' => true]);

$this->assertTrue(is_file($this->modulePath . '/Services/MyService.php'));
$this->assertSame(0, $code);
}

public function test_it_generates_a_new_service_class_can_use_invoke_option()
{
$code = $this->artisan('module:make-service', ['name' => 'MyService', 'module' => 'Blog', '--invokable' => true]);

$this->assertTrue(is_file($this->modulePath . '/Services/MyService.php'));
$this->assertSame(0, $code);
}

public function test_it_generated_correct_file_with_content()
{
$code = $this->artisan('module:make-service', ['name' => 'MyService', 'module' => 'Blog']);
Expand Down

0 comments on commit addf050

Please sign in to comment.