Skip to content

Commit

Permalink
Merge pull request #5 from v1
Browse files Browse the repository at this point in the history
v1
  • Loading branch information
Mingeun Kim authored May 24, 2021
2 parents 6bc2d63 + 0626424 commit 168af1b
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 137 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/
.idea/
.DS_Store
composer.lock
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@
<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>
</p>

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

# Install
```bash
composer require getsolaris/laravel-make-service
```

# Usage
```php
// config/app.php
'providers' => [
getsolaris\LaravelCreateService\CreateServiceProvider::class,
];
```

```bash
php artisan make:service {name}
php artisan make:service {name : Create a service class} {--c : Create a service contract}
```
21 changes: 14 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
{
"name": "getsolaris/laravel-make-service",
"description": "Laravel MVCS Pattern Create a new Service class",
"description": "A MVCS pattern create a service command for Laravel 5+",
"keywords": [
"laravel",
"mvcs",
"service"
"service",
"contract",
"pattern"
],
"type": "library",
"require": {
"php": "^7.1|^8.0",
"illuminate/support": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
"illuminate/console": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0|^9.0"
},
"license": "MIT",
"authors": [
{
"name": "Mingeun Kim",
"email": "[email protected]"
}
],
"extra": {
"laravel": {
"providers": [
"Getsolaris\\LaravelMakeService\\LaravelMakeServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"getsolaris\\LaravelCreateService\\": "src/"
}
"Getsolaris\\LaravelMakeService\\": "src/"
},
"classmap": ["src/"]
}
}
82 changes: 0 additions & 82 deletions src/Commands/MakeServices.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/CreateServiceProvider.php

This file was deleted.

18 changes: 18 additions & 0 deletions src/LaravelMakeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Getsolaris\LaravelMakeService;

use Illuminate\Support\ServiceProvider;

class LaravelMakeServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register(): void
{
$this->commands(MakeService::class);
}
}
154 changes: 154 additions & 0 deletions src/MakeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Getsolaris\LaravelMakeService;

use Illuminate\Console\GeneratorCommand;

/**
* Class MakeService
* @package Getsolaris\LaravelMakeService
* @author getsolaris (https:/getsolaris)
*/
class MakeService extends GeneratorCommand
{
/**
* STUB_PATH
*/
const STUB_PATH = __DIR__ . ' /Stubs/';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:service {name : Create a service class} {--c : Create a service contract}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new service class and contract';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Service';

protected function getStub() { }

/**
* @param bool $isContract
* @return string
*/
protected function getServiceStub(bool $isContract): string
{
return self::STUB_PATH .
$isContract ? 'service.origin.stub' : 'service.stub';
}

/**
* @return string
*/
protected function getServiceContractStub(): string
{
return self::STUB_PATH . 'service.contract.stub';
}

/**
* Execute the console command.
*
* @return bool|null
*
* @see \Illuminate\Console\GeneratorCommand
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle()
{
if ($this->isReservedName($this->getNameInput())) {
$this->error('The name "'.$this->getNameInput().'" is reserved by PHP.');

return false;
}

$name = $this->qualifyClass($this->getNameInput());

$path = $this->getPath($name);

if ((! $this->hasOption('force') ||
! $this->option('force')) &&
$this->alreadyExists($this->getNameInput())) {
$this->error($this->type.' already exists!');

return false;
}

$this->makeDirectory($path);
$isContract = $this->option('c');

$this->files->put($path, $this->sortImports(
$this->buildServiceClass($name, $isContract)
));
$message = $this->type;

// Whether to create contract
if ($isContract) {
$contractName = $this->getNameInput() . 'Contract.php';
$contractPath = str_replace($this->getNameInput() . '.php', 'Contracts/', $path);

$this->makeDirectory($contractPath . $contractName);

$this->files->put($contractPath . $contractName,
$this->sortImports(
$this->buildServiceContractInterface($this->getNameInput())
)
);

$message .= ' and Contract';
}

$this->info($message . ' created successfully.');
}

/**
* Build the class with the given name.
*
* @param string $name
* @param $isContract
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildServiceClass($name, $isContract): string
{
$stub = $this->files->get($this->getServiceStub($isContract));

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

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildServiceContractInterface($name): string
{
$stub = $this->files->get($this->getServiceContractStub());

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

/**
* @param $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace . '\Services';
}
}
7 changes: 0 additions & 7 deletions src/Services/Service.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Stubs/service.contract.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Services\Contracts;

/**
* Interface {{ class }}Contract
* @package App\Services\Contracts
*/
interface {{ class }}Contract
{

}
Loading

0 comments on commit 168af1b

Please sign in to comment.