From 47904f8a5070eb13377a0a23a5545fbfec345902 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sat, 22 May 2021 10:11:45 +0900
Subject: [PATCH 01/21] chore: change package name and remove require-dev
---
composer.json | 20 ++++++++++++-------
src/Commands/stubs/service.php | 12 +++++++++++
src/Services/{Service.php => BaseService.php} | 0
3 files changed, 25 insertions(+), 7 deletions(-)
create mode 100644 src/Commands/stubs/service.php
rename src/Services/{Service.php => BaseService.php} (100%)
diff --git a/composer.json b/composer.json
index 16c6660..fe9ee59 100644
--- a/composer.json
+++ b/composer.json
@@ -1,10 +1,11 @@
{
"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",
+ "pattern"
],
"type": "library",
"require": {
@@ -12,9 +13,6 @@
"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": [
{
@@ -22,9 +20,17 @@
"email": "mingeun.k.k@gmail.com"
}
],
+ "extra": {
+ "laravel": {
+ "providers": [
+ "Getsolaris\\LaravelMakeService\\LaravelMakeServiceProvider"
+ ]
+ }
+ },
"autoload": {
"psr-4": {
- "getsolaris\\LaravelCreateService\\": "src/"
- }
+ "getsolaris\\LaravelMakeService\\": "src/"
+ },
+ "classmap": ["src/"]
}
}
diff --git a/src/Commands/stubs/service.php b/src/Commands/stubs/service.php
new file mode 100644
index 0000000..700c98c
--- /dev/null
+++ b/src/Commands/stubs/service.php
@@ -0,0 +1,12 @@
+
Date: Sat, 22 May 2021 10:12:06 +0900
Subject: [PATCH 02/21] chore: added git ignore
---
.gitignore | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore
index 62c8935..15b594e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
-.idea/
\ No newline at end of file
+.idea/
+.DS_Store
+composer.lock
From bd0bb2859999a143b2fd019dec12a4e3f75c8615 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 02:17:43 +0900
Subject: [PATCH 03/21] feat: contract base service (Eloquent ORM)
---
src/Services/BaseService.php | 464 ++++++++++++++++++++++++++++++-
src/Services/ServiceContract.php | 46 +++
2 files changed, 506 insertions(+), 4 deletions(-)
create mode 100644 src/Services/ServiceContract.php
diff --git a/src/Services/BaseService.php b/src/Services/BaseService.php
index 30143a3..bdcfd71 100644
--- a/src/Services/BaseService.php
+++ b/src/Services/BaseService.php
@@ -1,7 +1,463 @@
makeModel();
+ }
+
+ /**
+ * Specify Model class name.
+ *
+ * @return mixed
+ */
+ abstract public function model();
+
+ /**
+ * @return Model|mixed
+ * @throws GeneralException
+ */
+ public function makeModel()
+ {
+ $model = app()->make($this->model());
+
+ if (! $model instanceof Model) {
+ throw new GeneralException("Class {$this->model()} must be an instance of ".Model::class);
+ }
+
+ return $this->model = $model;
+ }
+
+ /**
+ * Get all the model records in the database.
+ *
+ * @param array $columns
+ *
+ * @return Collection|static[]
+ */
+ public function all(array $columns = ['*'])
+ {
+ $this->newQuery()->eagerLoad();
+
+ $models = $this->query->get($columns);
+
+ $this->unsetClauses();
+
+ return $models;
+ }
+
+ /**
+ * Count the number of specified model records in the database.
+ *
+ * @return int
+ */
+ public function count() : int
+ {
+ return $this->get()->count();
+ }
+
+ /**
+ * Create a new model record in the database.
+ *
+ * @param array $data
+ *
+ * @return \Illuminate\Database\Eloquent\Model
+ */
+ public function create(array $data)
+ {
+ $this->unsetClauses();
+
+ return $this->model->create($data);
+ }
+
+ /**
+ * Create one or more new model records in the database.
+ *
+ * @param array $data
+ *
+ * @return \Illuminate\Database\Eloquent\Collection
+ */
+ public function createMultiple(array $data)
+ {
+ $models = new Collection();
+
+ foreach ($data as $d) {
+ $models->push($this->create($d));
+ }
+
+ return $models;
+ }
+
+ /**
+ * Delete one or more model records from the database.
+ *
+ * @return mixed
+ */
+ public function delete()
+ {
+ $this->newQuery()->setClauses()->setScopes();
+
+ $result = $this->query->delete();
+
+ $this->unsetClauses();
+
+ return $result;
+ }
+
+ /**
+ * Delete the specified model record from the database.
+ *
+ * @param $id
+ *
+ * @return bool|null
+ * @throws \Exception
+ */
+ public function deleteById($id) : bool
+ {
+ $this->unsetClauses();
+
+ return $this->getById($id)->delete();
+ }
+
+ /**
+ * Delete multiple records.
+ *
+ * @param array $ids
+ *
+ * @return int
+ */
+ public function deleteMultipleById(array $ids) : int
+ {
+ return $this->model->destroy($ids);
+ }
+
+ /**
+ * Get the first specified model record from the database.
+ *
+ * @param array $columns
+ *
+ * @return Model|static
+ */
+ public function first(array $columns = ['*'])
+ {
+ $this->newQuery()->eagerLoad()->setClauses()->setScopes();
+
+ $model = $this->query->firstOrFail($columns);
+
+ $this->unsetClauses();
+
+ return $model;
+ }
+
+ /**
+ * Get all the specified model records in the database.
+ *
+ * @param array $columns
+ *
+ * @return Collection|static[]
+ */
+ public function get(array $columns = ['*'])
+ {
+ $this->newQuery()->eagerLoad()->setClauses()->setScopes();
+
+ $models = $this->query->get($columns);
+
+ $this->unsetClauses();
+
+ return $models;
+ }
+
+ /**
+ * Get the specified model record from the database.
+ *
+ * @param $id
+ * @param array $columns
+ *
+ * @return Collection|Model
+ */
+ public function getById($id, array $columns = ['*'])
+ {
+ $this->unsetClauses();
+
+ $this->newQuery()->eagerLoad();
+
+ return $this->query->findOrFail($id, $columns);
+ }
+
+ /**
+ * @param $item
+ * @param $column
+ * @param array $columns
+ *
+ * @return Model|null|static
+ */
+ public function getByColumn($item, $column, array $columns = ['*'])
+ {
+ $this->unsetClauses();
+
+ $this->newQuery()->eagerLoad();
+
+ return $this->query->where($column, $item)->first($columns);
+ }
+
+ /**
+ * @param int $limit
+ * @param array $columns
+ * @param string $pageName
+ * @param null $page
+ *
+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
+ */
+ public function paginate($limit = 25, array $columns = ['*'], $pageName = 'page', $page = null)
+ {
+ $this->newQuery()->eagerLoad()->setClauses()->setScopes();
+
+ $models = $this->query->paginate($limit, $columns, $pageName, $page);
+
+ $this->unsetClauses();
+
+ return $models;
+ }
+
+ /**
+ * Update the specified model record in the database.
+ *
+ * @param $id
+ * @param array $data
+ * @param array $options
+ *
+ * @return Collection|Model
+ */
+ public function updateById($id, array $data, array $options = [])
+ {
+ $this->unsetClauses();
+
+ $model = $this->getById($id);
+
+ $model->update($data, $options);
+
+ return $model;
+ }
+
+ /**
+ * Set the query limit.
+ *
+ * @param int $limit
+ *
+ * @return $this
+ */
+ public function limit($limit)
+ {
+ $this->take = $limit;
+
+ return $this;
+ }
+
+ /**
+ * Set an ORDER BY clause.
+ *
+ * @param string $column
+ * @param string $direction
+ * @return $this
+ */
+ public function orderBy($column, $direction = 'asc')
+ {
+ $this->orderBys[] = compact('column', 'direction');
+
+ return $this;
+ }
+
+ /**
+ * Add a simple where clause to the query.
+ *
+ * @param string $column
+ * @param string $value
+ * @param string $operator
+ *
+ * @return $this
+ */
+ public function where($column, $value, $operator = '=')
+ {
+ $this->wheres[] = compact('column', 'value', 'operator');
+
+ return $this;
+ }
+
+ /**
+ * Add a simple where in clause to the query.
+ *
+ * @param string $column
+ * @param mixed $values
+ *
+ * @return $this
+ */
+ public function whereIn($column, $values)
+ {
+ $values = is_array($values) ? $values : [$values];
+
+ $this->whereIns[] = compact('column', 'values');
+
+ return $this;
+ }
+
+ /**
+ * Set Eloquent relationships to eager load.
+ *
+ * @param $relations
+ *
+ * @return $this
+ */
+ public function with($relations)
+ {
+ if (is_string($relations)) {
+ $relations = func_get_args();
+ }
+
+ $this->with = $relations;
+
+ return $this;
+ }
+
+ /**
+ * Create a new instance of the model's query builder.
+ *
+ * @return $this
+ */
+ protected function newQuery()
+ {
+ $this->query = $this->model->newQuery();
+
+ return $this;
+ }
+
+ /**
+ * Add relationships to the query builder to eager load.
+ *
+ * @return $this
+ */
+ protected function eagerLoad()
+ {
+ foreach ($this->with as $relation) {
+ $this->query->with($relation);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Set clauses on the query builder.
+ *
+ * @return $this
+ */
+ protected function setClauses()
+ {
+ foreach ($this->wheres as $where) {
+ $this->query->where($where['column'], $where['operator'], $where['value']);
+ }
+
+ foreach ($this->whereIns as $whereIn) {
+ $this->query->whereIn($whereIn['column'], $whereIn['values']);
+ }
+
+ foreach ($this->orderBys as $orders) {
+ $this->query->orderBy($orders['column'], $orders['direction']);
+ }
+
+ if (isset($this->take) and ! is_null($this->take)) {
+ $this->query->take($this->take);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Set query scopes.
+ *
+ * @return $this
+ */
+ protected function setScopes()
+ {
+ foreach ($this->scopes as $method => $args) {
+ $this->query->$method(implode(', ', $args));
+ }
+
+ return $this;
+ }
+
+ /**
+ * Reset the query clause parameter arrays.
+ *
+ * @return $this
+ */
+ protected function unsetClauses()
+ {
+ $this->wheres = [];
+ $this->whereIns = [];
+ $this->scopes = [];
+ $this->take = null;
+
+ return $this;
+ }
+}
diff --git a/src/Services/ServiceContract.php b/src/Services/ServiceContract.php
new file mode 100644
index 0000000..2963310
--- /dev/null
+++ b/src/Services/ServiceContract.php
@@ -0,0 +1,46 @@
+
Date: Sun, 23 May 2021 02:21:51 +0900
Subject: [PATCH 04/21] feat: create service stub file
TODO: $ parse
---
src/stubs/service.stub | 10 ++++++++++
1 file changed, 10 insertions(+)
create mode 100644 src/stubs/service.stub
diff --git a/src/stubs/service.stub b/src/stubs/service.stub
new file mode 100644
index 0000000..6143eca
--- /dev/null
+++ b/src/stubs/service.stub
@@ -0,0 +1,10 @@
+
Date: Sun, 23 May 2021 17:29:16 +0900
Subject: [PATCH 05/21] docs: update readme
---
README.md | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git a/README.md b/README.md
index 0ec0100..7b2601b 100644
--- a/README.md
+++ b/README.md
@@ -4,8 +4,8 @@
-# Introduction
-Laravel Make Service Class
+# A MVCS pattern create a service command for Laravel 5+
+Create a new service class
# Install
```bash
@@ -16,14 +16,10 @@ composer require getsolaris/laravel-make-service
```php
// config/app.php
'providers' => [
- getsolaris\LaravelCreateService\CreateServiceProvider::class,
+ Getsolaris\LaravelMakeService\LaravelMakeServiceProvider::class,
];
```
```bash
php artisan make:service {name}
```
-
-# Versions
-- `v0.1` app\Http\Services
-- `v0.2` app\Services
\ No newline at end of file
From 8e6986920c950704e07f4ae0c119a7e3595516a2 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 17:31:30 +0900
Subject: [PATCH 06/21] refactor: change psr style namespace
---
src/LaravelMakeServiceProvider.php | 19 +++++++++++++++++++
src/Services/ServiceContract.php | 2 +-
2 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 src/LaravelMakeServiceProvider.php
diff --git a/src/LaravelMakeServiceProvider.php b/src/LaravelMakeServiceProvider.php
new file mode 100644
index 0000000..047a320
--- /dev/null
+++ b/src/LaravelMakeServiceProvider.php
@@ -0,0 +1,19 @@
+commands(MakeService::class);
+ }
+}
diff --git a/src/Services/ServiceContract.php b/src/Services/ServiceContract.php
index 2963310..7c890f1 100644
--- a/src/Services/ServiceContract.php
+++ b/src/Services/ServiceContract.php
@@ -1,6 +1,6 @@
Date: Sun, 23 May 2021 17:33:07 +0900
Subject: [PATCH 07/21] feat: create make service command
---
src/Commands/MakeService.php | 57 +++++++++++++++++++++++
src/Commands/MakeServices.php | 82 ----------------------------------
src/Commands/stubs/service.php | 12 -----
src/CreateServiceProvider.php | 30 -------------
4 files changed, 57 insertions(+), 124 deletions(-)
create mode 100644 src/Commands/MakeService.php
delete mode 100644 src/Commands/MakeServices.php
delete mode 100644 src/Commands/stubs/service.php
delete mode 100644 src/CreateServiceProvider.php
diff --git a/src/Commands/MakeService.php b/src/Commands/MakeService.php
new file mode 100644
index 0000000..4eece96
--- /dev/null
+++ b/src/Commands/MakeService.php
@@ -0,0 +1,57 @@
+files = $files;
- }
-
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $name = trim($this->argument('name'));
-
- if (! file_exists(app_path('Services')))
- mkdir(app_path('Services'), 0777);
-
- if (! $name or is_null($name) or empty($name)) {
- $this->error('Not enough arguments (missing: "name").');
- return false;
- }
-
- $this->createService($name);
- }
-
- private function createService($name) {
- $name = $this->checkServiceName($name);
-
- $file = $name . '.php';
-
- if ($this->files->exists(app_path('Services/' . $file))) {
- $this->error('Service already exists!');
- return false;
- }
-
- $original = $this->files->get(__DIR__ . '/../Services/Service.php');
-
- $original = str_replace('ServiceName', ucfirst($name), $original);
- $this->files->put(app_path('Services/' . $name . '.php'), $original);
-
- $this->info('Service created successfully.');
- return true;
- }
-
- private function checkServiceName($name) {
- if (strpos($name, 'Service'))
- return ucfirst($name);
- else if (strpos($name, 'service'))
- return ucfirst(str_replace('service', 'Service', $name));
- else
- return ucfirst($name) . 'Service';
- }
-}
diff --git a/src/Commands/stubs/service.php b/src/Commands/stubs/service.php
deleted file mode 100644
index 700c98c..0000000
--- a/src/Commands/stubs/service.php
+++ /dev/null
@@ -1,12 +0,0 @@
-app->singleton('command.getsolaris.makeservice', function ($app) {
- return $app['getsolaris\LaravelCreateService\Commands\MakeServices'];
- });
- $this->commands('command.getsolaris.makeservice');
- }
-}
\ No newline at end of file
From dfbbc5d1c61af46e4c99d070cc8f097c6cb7c7e2 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 17:39:48 +0900
Subject: [PATCH 08/21] style: alreadyExists method return type
---
src/Commands/MakeService.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Commands/MakeService.php b/src/Commands/MakeService.php
index 4eece96..8a8671e 100644
--- a/src/Commands/MakeService.php
+++ b/src/Commands/MakeService.php
@@ -33,7 +33,7 @@ class MakeService extends GeneratorCommand
* @param string $rawName
* @return bool
*/
- protected function alreadyExists($rawName)
+ protected function alreadyExists($rawName): bool
{
return class_exists($rawName);
}
From 953aaef433b3cd8cdd3b64e6acce4095dd9822ef Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 17:43:58 +0900
Subject: [PATCH 09/21] style: stubs dir rename (stubs -> Stubs)
---
src/Commands/MakeService.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Commands/MakeService.php b/src/Commands/MakeService.php
index 8a8671e..89e5980 100644
--- a/src/Commands/MakeService.php
+++ b/src/Commands/MakeService.php
@@ -43,7 +43,7 @@ protected function alreadyExists($rawName): bool
*/
private function getStub(): string
{
- return __DIR__ . '/src/stubs/service.stub';
+ return __DIR__ . '/src/Stubs/service.stub';
}
/**
From cba7dadd608fd11944777b7e8d034a1d3b4793c8 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 17:44:27 +0900
Subject: [PATCH 10/21] style: stubs dir rename (stubs -> Stubs)
---
src/{stubs => Stubs}/service.stub | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename src/{stubs => Stubs}/service.stub (100%)
diff --git a/src/stubs/service.stub b/src/Stubs/service.stub
similarity index 100%
rename from src/stubs/service.stub
rename to src/Stubs/service.stub
From 27b1fc69b6dd6ad67fff3d5db4edcfa6719fbb81 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 17:45:43 +0900
Subject: [PATCH 11/21] style: composer autoload psr namespace
---
composer.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/composer.json b/composer.json
index fe9ee59..9b95628 100644
--- a/composer.json
+++ b/composer.json
@@ -29,7 +29,7 @@
},
"autoload": {
"psr-4": {
- "getsolaris\\LaravelMakeService\\": "src/"
+ "Getsolaris\\LaravelMakeService\\": "src/"
},
"classmap": ["src/"]
}
From eba7b62c265d15b4baa223418b0eb52249988e9a Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 17:52:08 +0900
Subject: [PATCH 12/21] fix: getStub abstract modifier
---
src/Commands/MakeService.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Commands/MakeService.php b/src/Commands/MakeService.php
index 89e5980..a5c7a37 100644
--- a/src/Commands/MakeService.php
+++ b/src/Commands/MakeService.php
@@ -1,6 +1,6 @@
Date: Sun, 23 May 2021 17:52:29 +0900
Subject: [PATCH 13/21] fix: target class not exist
---
src/LaravelMakeServiceProvider.php | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/LaravelMakeServiceProvider.php b/src/LaravelMakeServiceProvider.php
index 047a320..a023916 100644
--- a/src/LaravelMakeServiceProvider.php
+++ b/src/LaravelMakeServiceProvider.php
@@ -2,7 +2,6 @@
namespace Getsolaris\LaravelMakeService;
-use Getsolaris\LaravelMakeService\Commands\MakeService;
use Illuminate\Support\ServiceProvider;
class LaravelMakeServiceProvider extends ServiceProvider
@@ -12,7 +11,7 @@ class LaravelMakeServiceProvider extends ServiceProvider
*
* @return void
*/
- public function register()
+ public function register(): void
{
$this->commands(MakeService::class);
}
From 27c995a2fa422e74a813dd4d43a5231b5eb1fa2d Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Sun, 23 May 2021 18:51:30 +0900
Subject: [PATCH 14/21] fix: already exist abstract method
---
src/{Commands => }/MakeService.php | 15 ++-------------
1 file changed, 2 insertions(+), 13 deletions(-)
rename src/{Commands => }/MakeService.php (74%)
diff --git a/src/Commands/MakeService.php b/src/MakeService.php
similarity index 74%
rename from src/Commands/MakeService.php
rename to src/MakeService.php
index a5c7a37..5672105 100644
--- a/src/Commands/MakeService.php
+++ b/src/MakeService.php
@@ -26,24 +26,13 @@ class MakeService extends GeneratorCommand
* @var string
*/
protected $type = 'Service';
-
- /**
- * Determine if the class already exists.
- *
- * @param string $rawName
- * @return bool
- */
- protected function alreadyExists($rawName): bool
- {
- return class_exists($rawName);
- }
-
+
/**
* @return string
*/
protected function getStub(): string
{
- return __DIR__ . '/src/Stubs/service.stub';
+ return __DIR__ . '/Stubs/service.stub';
}
/**
From b80f2201d4fcfa4e34c6bd5c71b17713beed581c Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Mon, 24 May 2021 21:01:48 +0900
Subject: [PATCH 15/21] feat: service and contract stubs
---
src/Services/BaseService.php | 463 -------------------------------
src/Services/ServiceContract.php | 46 ---
src/Stubs/service.contract.stub | 12 +
src/Stubs/service.stub | 12 +-
4 files changed, 20 insertions(+), 513 deletions(-)
delete mode 100644 src/Services/BaseService.php
delete mode 100644 src/Services/ServiceContract.php
create mode 100644 src/Stubs/service.contract.stub
diff --git a/src/Services/BaseService.php b/src/Services/BaseService.php
deleted file mode 100644
index bdcfd71..0000000
--- a/src/Services/BaseService.php
+++ /dev/null
@@ -1,463 +0,0 @@
-makeModel();
- }
-
- /**
- * Specify Model class name.
- *
- * @return mixed
- */
- abstract public function model();
-
- /**
- * @return Model|mixed
- * @throws GeneralException
- */
- public function makeModel()
- {
- $model = app()->make($this->model());
-
- if (! $model instanceof Model) {
- throw new GeneralException("Class {$this->model()} must be an instance of ".Model::class);
- }
-
- return $this->model = $model;
- }
-
- /**
- * Get all the model records in the database.
- *
- * @param array $columns
- *
- * @return Collection|static[]
- */
- public function all(array $columns = ['*'])
- {
- $this->newQuery()->eagerLoad();
-
- $models = $this->query->get($columns);
-
- $this->unsetClauses();
-
- return $models;
- }
-
- /**
- * Count the number of specified model records in the database.
- *
- * @return int
- */
- public function count() : int
- {
- return $this->get()->count();
- }
-
- /**
- * Create a new model record in the database.
- *
- * @param array $data
- *
- * @return \Illuminate\Database\Eloquent\Model
- */
- public function create(array $data)
- {
- $this->unsetClauses();
-
- return $this->model->create($data);
- }
-
- /**
- * Create one or more new model records in the database.
- *
- * @param array $data
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function createMultiple(array $data)
- {
- $models = new Collection();
-
- foreach ($data as $d) {
- $models->push($this->create($d));
- }
-
- return $models;
- }
-
- /**
- * Delete one or more model records from the database.
- *
- * @return mixed
- */
- public function delete()
- {
- $this->newQuery()->setClauses()->setScopes();
-
- $result = $this->query->delete();
-
- $this->unsetClauses();
-
- return $result;
- }
-
- /**
- * Delete the specified model record from the database.
- *
- * @param $id
- *
- * @return bool|null
- * @throws \Exception
- */
- public function deleteById($id) : bool
- {
- $this->unsetClauses();
-
- return $this->getById($id)->delete();
- }
-
- /**
- * Delete multiple records.
- *
- * @param array $ids
- *
- * @return int
- */
- public function deleteMultipleById(array $ids) : int
- {
- return $this->model->destroy($ids);
- }
-
- /**
- * Get the first specified model record from the database.
- *
- * @param array $columns
- *
- * @return Model|static
- */
- public function first(array $columns = ['*'])
- {
- $this->newQuery()->eagerLoad()->setClauses()->setScopes();
-
- $model = $this->query->firstOrFail($columns);
-
- $this->unsetClauses();
-
- return $model;
- }
-
- /**
- * Get all the specified model records in the database.
- *
- * @param array $columns
- *
- * @return Collection|static[]
- */
- public function get(array $columns = ['*'])
- {
- $this->newQuery()->eagerLoad()->setClauses()->setScopes();
-
- $models = $this->query->get($columns);
-
- $this->unsetClauses();
-
- return $models;
- }
-
- /**
- * Get the specified model record from the database.
- *
- * @param $id
- * @param array $columns
- *
- * @return Collection|Model
- */
- public function getById($id, array $columns = ['*'])
- {
- $this->unsetClauses();
-
- $this->newQuery()->eagerLoad();
-
- return $this->query->findOrFail($id, $columns);
- }
-
- /**
- * @param $item
- * @param $column
- * @param array $columns
- *
- * @return Model|null|static
- */
- public function getByColumn($item, $column, array $columns = ['*'])
- {
- $this->unsetClauses();
-
- $this->newQuery()->eagerLoad();
-
- return $this->query->where($column, $item)->first($columns);
- }
-
- /**
- * @param int $limit
- * @param array $columns
- * @param string $pageName
- * @param null $page
- *
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public function paginate($limit = 25, array $columns = ['*'], $pageName = 'page', $page = null)
- {
- $this->newQuery()->eagerLoad()->setClauses()->setScopes();
-
- $models = $this->query->paginate($limit, $columns, $pageName, $page);
-
- $this->unsetClauses();
-
- return $models;
- }
-
- /**
- * Update the specified model record in the database.
- *
- * @param $id
- * @param array $data
- * @param array $options
- *
- * @return Collection|Model
- */
- public function updateById($id, array $data, array $options = [])
- {
- $this->unsetClauses();
-
- $model = $this->getById($id);
-
- $model->update($data, $options);
-
- return $model;
- }
-
- /**
- * Set the query limit.
- *
- * @param int $limit
- *
- * @return $this
- */
- public function limit($limit)
- {
- $this->take = $limit;
-
- return $this;
- }
-
- /**
- * Set an ORDER BY clause.
- *
- * @param string $column
- * @param string $direction
- * @return $this
- */
- public function orderBy($column, $direction = 'asc')
- {
- $this->orderBys[] = compact('column', 'direction');
-
- return $this;
- }
-
- /**
- * Add a simple where clause to the query.
- *
- * @param string $column
- * @param string $value
- * @param string $operator
- *
- * @return $this
- */
- public function where($column, $value, $operator = '=')
- {
- $this->wheres[] = compact('column', 'value', 'operator');
-
- return $this;
- }
-
- /**
- * Add a simple where in clause to the query.
- *
- * @param string $column
- * @param mixed $values
- *
- * @return $this
- */
- public function whereIn($column, $values)
- {
- $values = is_array($values) ? $values : [$values];
-
- $this->whereIns[] = compact('column', 'values');
-
- return $this;
- }
-
- /**
- * Set Eloquent relationships to eager load.
- *
- * @param $relations
- *
- * @return $this
- */
- public function with($relations)
- {
- if (is_string($relations)) {
- $relations = func_get_args();
- }
-
- $this->with = $relations;
-
- return $this;
- }
-
- /**
- * Create a new instance of the model's query builder.
- *
- * @return $this
- */
- protected function newQuery()
- {
- $this->query = $this->model->newQuery();
-
- return $this;
- }
-
- /**
- * Add relationships to the query builder to eager load.
- *
- * @return $this
- */
- protected function eagerLoad()
- {
- foreach ($this->with as $relation) {
- $this->query->with($relation);
- }
-
- return $this;
- }
-
- /**
- * Set clauses on the query builder.
- *
- * @return $this
- */
- protected function setClauses()
- {
- foreach ($this->wheres as $where) {
- $this->query->where($where['column'], $where['operator'], $where['value']);
- }
-
- foreach ($this->whereIns as $whereIn) {
- $this->query->whereIn($whereIn['column'], $whereIn['values']);
- }
-
- foreach ($this->orderBys as $orders) {
- $this->query->orderBy($orders['column'], $orders['direction']);
- }
-
- if (isset($this->take) and ! is_null($this->take)) {
- $this->query->take($this->take);
- }
-
- return $this;
- }
-
- /**
- * Set query scopes.
- *
- * @return $this
- */
- protected function setScopes()
- {
- foreach ($this->scopes as $method => $args) {
- $this->query->$method(implode(', ', $args));
- }
-
- return $this;
- }
-
- /**
- * Reset the query clause parameter arrays.
- *
- * @return $this
- */
- protected function unsetClauses()
- {
- $this->wheres = [];
- $this->whereIns = [];
- $this->scopes = [];
- $this->take = null;
-
- return $this;
- }
-}
diff --git a/src/Services/ServiceContract.php b/src/Services/ServiceContract.php
deleted file mode 100644
index 7c890f1..0000000
--- a/src/Services/ServiceContract.php
+++ /dev/null
@@ -1,46 +0,0 @@
-
Date: Mon, 24 May 2021 21:28:04 +0900
Subject: [PATCH 16/21] docs: update usage command
---
README.md | 11 ++---------
src/Stubs/service.origin.stub | 0
src/Stubs/service.stub | 2 +-
3 files changed, 3 insertions(+), 10 deletions(-)
create mode 100644 src/Stubs/service.origin.stub
diff --git a/README.md b/README.md
index 7b2601b..7ec1e1c 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@
# A MVCS pattern create a service command for Laravel 5+
-Create a new service class
+Create a new service class and service contract
# Install
```bash
@@ -13,13 +13,6 @@ composer require getsolaris/laravel-make-service
```
# Usage
-```php
-// config/app.php
-'providers' => [
- Getsolaris\LaravelMakeService\LaravelMakeServiceProvider::class,
-];
-```
-
```bash
-php artisan make:service {name}
+php artisan make:service {name : Create a service class} {--c : Create a service contract}
```
diff --git a/src/Stubs/service.origin.stub b/src/Stubs/service.origin.stub
new file mode 100644
index 0000000..e69de29
diff --git a/src/Stubs/service.stub b/src/Stubs/service.stub
index bf20b4d..2a5b05f 100644
--- a/src/Stubs/service.stub
+++ b/src/Stubs/service.stub
@@ -8,7 +8,7 @@ use {{ namespace }}\Contracts\{{ class }}Contract;
* Class {{ class }}
* @package App\Services
*/
-class {{ class }} implements {{ class }}Contract
+class {{ class }} {{ implements {{ class }}Contract }}
{
}
From eabdb291534de56f57ddde3bbe6faadc30b1227a Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Mon, 24 May 2021 21:28:31 +0900
Subject: [PATCH 17/21] feat: split service original stub
---
src/Stubs/service.contract.stub | 2 +-
src/Stubs/service.origin.stub | 12 ++++++++++++
src/Stubs/service.stub | 2 +-
3 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/src/Stubs/service.contract.stub b/src/Stubs/service.contract.stub
index daae2c4..b6dc82a 100644
--- a/src/Stubs/service.contract.stub
+++ b/src/Stubs/service.contract.stub
@@ -1,6 +1,6 @@
Date: Mon, 24 May 2021 21:29:38 +0900
Subject: [PATCH 18/21] feat: command option contract
---
src/MakeService.php | 109 ++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 104 insertions(+), 5 deletions(-)
diff --git a/src/MakeService.php b/src/MakeService.php
index 5672105..6b86056 100644
--- a/src/MakeService.php
+++ b/src/MakeService.php
@@ -4,6 +4,11 @@
use Illuminate\Console\GeneratorCommand;
+/**
+ * Class MakeService
+ * @package Getsolaris\LaravelMakeService
+ * @author getsolaris (https://github.com/getsolaris)
+ */
class MakeService extends GeneratorCommand
{
/**
@@ -11,14 +16,14 @@ class MakeService extends GeneratorCommand
*
* @var string
*/
- protected $signature = 'make:service {name}';
+ 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';
+ protected $description = 'Create a new service class and contract';
/**
* The type of class being generated.
@@ -26,13 +31,107 @@ class MakeService extends GeneratorCommand
* @var string
*/
protected $type = 'Service';
-
+
+ protected function getStub() { }
+
/**
+ * @param bool $isContract
* @return string
*/
- protected function getStub(): string
+ protected function getServiceStub(bool $isContract): string
+ {
+ if ($isContract) {
+ return __DIR__ . '/Stubs/service.stub';
+ }
+
+ return __DIR__ . '/Stubs/service.origin.stub';
+ }
+
+ /**
+ * @return string
+ */
+ protected function getServiceContractStub(): string
+ {
+ return __DIR__ . '/Stubs/service.contract.stub';
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return bool|null
+ *
+ * @see \Illuminate\Console\GeneratorCommand
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
+ */
+ public function handle()
{
- return __DIR__ . '/Stubs/service.stub';
+ 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 = str_replace($this->getNameInput(), $this->getNameInput() . 'Contract.php', $this->getNameInput());
+ $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);
}
/**
From bcfb46ddd498ccb94f55d9e8db607c5921881a7a Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Mon, 24 May 2021 21:34:21 +0900
Subject: [PATCH 19/21] refactor: easy contract name
---
src/MakeService.php | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/src/MakeService.php b/src/MakeService.php
index 6b86056..8cba47a 100644
--- a/src/MakeService.php
+++ b/src/MakeService.php
@@ -93,10 +93,17 @@ public function handle()
// Whether to create contract
if ($isContract) {
- $contractName = str_replace($this->getNameInput(), $this->getNameInput() . 'Contract.php', $this->getNameInput());
+ $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())));
+
+ $this->files->put($contractPath . $contractName,
+ $this->sortImports(
+ $this->buildServiceContractInterface($this->getNameInput())
+ )
+ );
+
$message .= ' and Contract';
}
From de36c45e80fce0c3395c4e787b21cd23a7a4ec1e Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Mon, 24 May 2021 21:47:05 +0900
Subject: [PATCH 20/21] refactor: constant stub path
---
src/MakeService.php | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/src/MakeService.php b/src/MakeService.php
index 8cba47a..ccd8943 100644
--- a/src/MakeService.php
+++ b/src/MakeService.php
@@ -11,6 +11,11 @@
*/
class MakeService extends GeneratorCommand
{
+ /**
+ * STUB_PATH
+ */
+ const STUB_PATH = __DIR__ . ' /Stubs/';
+
/**
* The name and signature of the console command.
*
@@ -40,11 +45,8 @@ protected function getStub() { }
*/
protected function getServiceStub(bool $isContract): string
{
- if ($isContract) {
- return __DIR__ . '/Stubs/service.stub';
- }
-
- return __DIR__ . '/Stubs/service.origin.stub';
+ return self::STUB_PATH .
+ $isContract ? 'service.origin.stub' : 'service.stub';
}
/**
@@ -52,7 +54,7 @@ protected function getServiceStub(bool $isContract): string
*/
protected function getServiceContractStub(): string
{
- return __DIR__ . '/Stubs/service.contract.stub';
+ return self::STUB_PATH . 'service.contract.stub';
}
/**
@@ -95,7 +97,7 @@ public function handle()
if ($isContract) {
$contractName = $this->getNameInput() . 'Contract.php';
$contractPath = str_replace($this->getNameInput() . '.php', 'Contracts/', $path);
-
+
$this->makeDirectory($contractPath . $contractName);
$this->files->put($contractPath . $contractName,
From 062642446dbcbcb7835fb6223a19d4e4bd100f97 Mon Sep 17 00:00:00 2001
From: Mingeun Kim
Date: Mon, 24 May 2021 21:51:20 +0900
Subject: [PATCH 21/21] feat: add composer 'contract' keywords
---
composer.json | 1 +
1 file changed, 1 insertion(+)
diff --git a/composer.json b/composer.json
index 9b95628..39f9c59 100644
--- a/composer.json
+++ b/composer.json
@@ -5,6 +5,7 @@
"laravel",
"mvcs",
"service",
+ "contract",
"pattern"
],
"type": "library",