Skip to content

Commit 168af1b

Browse files
author
Mingeun Kim
authored
Merge pull request #5 from v1
v1
2 parents 6bc2d63 + 0626424 commit 168af1b

11 files changed

+230
-137
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea/
1+
.idea/
2+
.DS_Store
3+
composer.lock

README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,15 @@
44
<a href="https://packagist.org/packages/getsolaris/laravel-make-service"><img src="https://poser.pugx.org/getsolaris/laravel-make-service/license.svg" alt="License"></a>
55
</p>
66

7-
# Introduction
8-
Laravel Make Service Class
7+
# A MVCS pattern create a service command for Laravel 5+
8+
Create a new service class and service contract
99

1010
# Install
1111
```bash
1212
composer require getsolaris/laravel-make-service
1313
```
1414

1515
# Usage
16-
```php
17-
// config/app.php
18-
'providers' => [
19-
getsolaris\LaravelCreateService\CreateServiceProvider::class,
20-
];
21-
```
22-
2316
```bash
24-
php artisan make:service {name}
17+
php artisan make:service {name : Create a service class} {--c : Create a service contract}
2518
```

composer.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
{
22
"name": "getsolaris/laravel-make-service",
3-
"description": "Laravel MVCS Pattern Create a new Service class",
3+
"description": "A MVCS pattern create a service command for Laravel 5+",
44
"keywords": [
55
"laravel",
66
"mvcs",
7-
"service"
7+
"service",
8+
"contract",
9+
"pattern"
810
],
911
"type": "library",
1012
"require": {
1113
"php": "^7.1|^8.0",
1214
"illuminate/support": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
1315
"illuminate/console": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0"
1416
},
15-
"require-dev": {
16-
"phpunit/phpunit": "^7.0|^8.0|^9.0"
17-
},
1817
"license": "MIT",
1918
"authors": [
2019
{
2120
"name": "Mingeun Kim",
2221
"email": "[email protected]"
2322
}
2423
],
24+
"extra": {
25+
"laravel": {
26+
"providers": [
27+
"Getsolaris\\LaravelMakeService\\LaravelMakeServiceProvider"
28+
]
29+
}
30+
},
2531
"autoload": {
2632
"psr-4": {
27-
"getsolaris\\LaravelCreateService\\": "src/"
28-
}
33+
"Getsolaris\\LaravelMakeService\\": "src/"
34+
},
35+
"classmap": ["src/"]
2936
}
3037
}

src/Commands/MakeServices.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/CreateServiceProvider.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/LaravelMakeServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Getsolaris\LaravelMakeService;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LaravelMakeServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register the application services.
11+
*
12+
* @return void
13+
*/
14+
public function register(): void
15+
{
16+
$this->commands(MakeService::class);
17+
}
18+
}

src/MakeService.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Getsolaris\LaravelMakeService;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
/**
8+
* Class MakeService
9+
* @package Getsolaris\LaravelMakeService
10+
* @author getsolaris (https://github.com/getsolaris)
11+
*/
12+
class MakeService extends GeneratorCommand
13+
{
14+
/**
15+
* STUB_PATH
16+
*/
17+
const STUB_PATH = __DIR__ . ' /Stubs/';
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'make:service {name : Create a service class} {--c : Create a service contract}';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Create a new service class and contract';
32+
33+
/**
34+
* The type of class being generated.
35+
*
36+
* @var string
37+
*/
38+
protected $type = 'Service';
39+
40+
protected function getStub() { }
41+
42+
/**
43+
* @param bool $isContract
44+
* @return string
45+
*/
46+
protected function getServiceStub(bool $isContract): string
47+
{
48+
return self::STUB_PATH .
49+
$isContract ? 'service.origin.stub' : 'service.stub';
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
protected function getServiceContractStub(): string
56+
{
57+
return self::STUB_PATH . 'service.contract.stub';
58+
}
59+
60+
/**
61+
* Execute the console command.
62+
*
63+
* @return bool|null
64+
*
65+
* @see \Illuminate\Console\GeneratorCommand
66+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
67+
*/
68+
public function handle()
69+
{
70+
if ($this->isReservedName($this->getNameInput())) {
71+
$this->error('The name "'.$this->getNameInput().'" is reserved by PHP.');
72+
73+
return false;
74+
}
75+
76+
$name = $this->qualifyClass($this->getNameInput());
77+
78+
$path = $this->getPath($name);
79+
80+
if ((! $this->hasOption('force') ||
81+
! $this->option('force')) &&
82+
$this->alreadyExists($this->getNameInput())) {
83+
$this->error($this->type.' already exists!');
84+
85+
return false;
86+
}
87+
88+
$this->makeDirectory($path);
89+
$isContract = $this->option('c');
90+
91+
$this->files->put($path, $this->sortImports(
92+
$this->buildServiceClass($name, $isContract)
93+
));
94+
$message = $this->type;
95+
96+
// Whether to create contract
97+
if ($isContract) {
98+
$contractName = $this->getNameInput() . 'Contract.php';
99+
$contractPath = str_replace($this->getNameInput() . '.php', 'Contracts/', $path);
100+
101+
$this->makeDirectory($contractPath . $contractName);
102+
103+
$this->files->put($contractPath . $contractName,
104+
$this->sortImports(
105+
$this->buildServiceContractInterface($this->getNameInput())
106+
)
107+
);
108+
109+
$message .= ' and Contract';
110+
}
111+
112+
$this->info($message . ' created successfully.');
113+
}
114+
115+
/**
116+
* Build the class with the given name.
117+
*
118+
* @param string $name
119+
* @param $isContract
120+
* @return string
121+
*
122+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
123+
*/
124+
protected function buildServiceClass($name, $isContract): string
125+
{
126+
$stub = $this->files->get($this->getServiceStub($isContract));
127+
128+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
129+
}
130+
131+
/**
132+
* Build the class with the given name.
133+
*
134+
* @param string $name
135+
* @return string
136+
*
137+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
138+
*/
139+
protected function buildServiceContractInterface($name): string
140+
{
141+
$stub = $this->files->get($this->getServiceContractStub());
142+
143+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
144+
}
145+
146+
/**
147+
* @param $rootNamespace
148+
* @return string
149+
*/
150+
protected function getDefaultNamespace($rootNamespace): string
151+
{
152+
return $rootNamespace . '\Services';
153+
}
154+
}

src/Services/Service.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Stubs/service.contract.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Services\Contracts;
4+
5+
/**
6+
* Interface {{ class }}Contract
7+
* @package App\Services\Contracts
8+
*/
9+
interface {{ class }}Contract
10+
{
11+
12+
}

0 commit comments

Comments
 (0)