Skip to content

Commit cfa00db

Browse files
author
getsolaris
committed
update v1.1
1 parent 3846a92 commit cfa00db

File tree

6 files changed

+85
-35
lines changed

6 files changed

+85
-35
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"illuminate/support": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0",
1515
"illuminate/console": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0"
1616
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^9.5"
19+
},
1720
"license": "MIT",
1821
"authors": [
1922
{

src/MakeService.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MakeService extends GeneratorCommand
2121
*
2222
* @var string
2323
*/
24-
protected $signature = 'make:service {name : Create a service class} {--c : Create a service contract}';
24+
protected $signature = 'make:service {name : Create a service class} {--i : Create a service interface}';
2525

2626
/**
2727
* The console command description.
@@ -42,21 +42,24 @@ protected function getStub()
4242
}
4343

4444
/**
45-
* @param bool $isContract
4645
* @return string
4746
*/
48-
protected function getServiceStub(bool $isContract): string
47+
protected function getServiceStub(): string
4948
{
50-
return self::STUB_PATH .
51-
($isContract ? 'service.stub' : 'service.origin.stub');
49+
return self::STUB_PATH . 'service.stub';
50+
}
51+
52+
protected function getInterfaceStub(): string
53+
{
54+
return self::STUB_PATH . 'interface.stub';
5255
}
5356

5457
/**
5558
* @return string
5659
*/
57-
protected function getServiceContractStub(): string
60+
protected function getServiceInterfaceStub(): string
5861
{
59-
return self::STUB_PATH . 'service.contract.stub';
62+
return self::STUB_PATH . 'service.interface.stub';
6063
}
6164

6265
/**
@@ -89,31 +92,31 @@ public function handle()
8992
}
9093

9194
$this->makeDirectory($path);
92-
$isContract = $this->option('c');
95+
$isInterface = $this->option('i');
9396

9497
$this->files->put(
9598
$path,
9699
$this->sortImports(
97-
$this->buildServiceClass($name, $isContract)
100+
$this->buildServiceClass($name, $isInterface)
98101
)
99102
);
100103
$message = $this->type;
101104

102105
// Whether to create contract
103-
if ($isContract) {
104-
$contractName = $this->getNameInput() . 'Contract.php';
105-
$contractPath = str_replace($this->getNameInput() . '.php', 'Contracts/', $path);
106+
if ($isInterface) {
107+
$interfaceName = $this->getNameInput() . 'Interface.php';
108+
$interfacePath = str_replace($this->getNameInput() . '.php', 'Interfaces/', $path);
106109

107-
$this->makeDirectory($contractPath . $contractName);
110+
$this->makeDirectory($interfacePath . $interfaceName);
108111

109112
$this->files->put(
110-
$contractPath . $contractName,
113+
$interfacePath . $interfaceName,
111114
$this->sortImports(
112-
$this->buildServiceContractInterface($this->getNameInput())
115+
$this->buildServiceInterface($this->getNameInput())
113116
)
114117
);
115118

116-
$message .= ' and Contract';
119+
$message .= ' and Interface';
117120
}
118121

119122
$this->info($message . ' created successfully.');
@@ -123,14 +126,16 @@ public function handle()
123126
* Build the class with the given name.
124127
*
125128
* @param string $name
126-
* @param $isContract
129+
* @param $isInterface
127130
* @return string
128131
*
129132
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
130133
*/
131-
protected function buildServiceClass($name, $isContract): string
134+
protected function buildServiceClass(string $name, $isInterface): string
132135
{
133-
$stub = $this->files->get($this->getServiceStub($isContract));
136+
$stub = $this->files->get(
137+
$isInterface ? $this->getServiceInterfaceStub() : $this->getServiceStub()
138+
);
134139

135140
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
136141
}
@@ -143,9 +148,9 @@ protected function buildServiceClass($name, $isContract): string
143148
*
144149
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
145150
*/
146-
protected function buildServiceContractInterface($name): string
151+
protected function buildServiceInterface(string $name): string
147152
{
148-
$stub = $this->files->get($this->getServiceContractStub());
153+
$stub = $this->files->get($this->getInterfaceStub());
149154

150155
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
151156
}

src/Stubs/interface.stub

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace App\Services\Contracts;
3+
namespace App\Services\Interfaces;
44

55
/**
6-
* Interface {{ class }}Contract
7-
* @package App\Services\Contracts
6+
* Interface {{ class }}Interface
7+
* @package App\Services\Interfaces
88
*/
9-
interface {{ class }}Contract
9+
interface {{ class }}Interface
1010
{
1111

1212
}

src/Stubs/service.interface.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace {{ namespace }};
44

5-
use {{ namespace }}\Contracts\{{ class }}Contract;
5+
use {{ namespace }}\Interfaces\{{ class }}Interface;
66

77
/**
88
* Class {{ class }}
99
* @package App\Services
1010
*/
11-
class {{ class }} implements {{ class }}Contract
11+
class {{ class }} implements {{ class }}Interface
1212
{
1313

1414
}

tests/GenerateTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
<?php
22

33
use PHPUnit\Framework\TestCase;
4+
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
5+
use Tests\CreatesApplication;
46

5-
class MakeFileExistTest extends TestCase
7+
/**
8+
* @property \Illuminate\Contracts\Container\Container $app
9+
*/
10+
class GenerateTest extends TestCase
611
{
7-
public function test_console_command(): void
12+
use CreatesApplication, InteractsWithConsole;
13+
14+
protected $app;
15+
16+
public function setUp(): void
17+
{
18+
parent::setUp();
19+
$this->app = require __DIR__ . '/../../bootstrap/app.php';
20+
}
21+
22+
/**
23+
* @return void
24+
*/
25+
public function test_make_service_command(): void
26+
{
27+
$this->artisan('make:service TestService')->assertSuccessful();
28+
}
29+
30+
/**
31+
* @return void
32+
*/
33+
public function test_make_service_to_interface_command(): void
834
{
9-
$this->artisan('make:service');
35+
$this->artisan('make:service TestService --i')->assertSuccessful();
1036
}
1137
}

tests/StubExistTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
use PHPUnit\Framework\TestCase;
44

5-
class MakeTest extends TestCase
5+
class StubExistTest extends TestCase
66
{
7+
/**
8+
* @return void
9+
*/
710
public function test_exist_service_stub(): void
811
{
9-
$this->assertFileExists(
10-
'service.stub',
11-
"given filename doesn't exists"
12-
);
12+
$this->assertFileExists(__DIR__ . '/../src/Stubs/service.stub');
13+
}
14+
15+
/**
16+
* @return void
17+
*/
18+
public function test_exist_interface_stub(): void
19+
{
20+
$this->assertFileExists(__DIR__ . '/../src/Stubs/interface.stub');
21+
}
22+
23+
/**
24+
* @return void
25+
*/
26+
public function test_exist_service_interface_stub(): void
27+
{
28+
$this->assertFileExists(__DIR__ . '/../src/Stubs/service.interface.stub');
1329
}
1430
}

0 commit comments

Comments
 (0)