diff --git a/src/LaravelApiDevelopServiceProvider.php b/src/LaravelApiDevelopServiceProvider.php index 3cecaef..d4e8588 100644 --- a/src/LaravelApiDevelopServiceProvider.php +++ b/src/LaravelApiDevelopServiceProvider.php @@ -13,13 +13,13 @@ class LaravelApiDevelopServiceProvider extends ServiceProvider */ public function boot() { - if ($this->app->runningInConsole()) { $this->publishes([ __DIR__ . '/resources/stubs/ApiController.stub' => app_path('Http/Controllers/Api/ApiController.php'), __DIR__ . '/resources/stubs/ApiHandlerTrait.stub' => app_path('Exceptions/Traits/ApiHandlerTrait.php'), __DIR__ . '/resources/stubs/ApiJson.stub' => app_path('Http/Resources/Macros/ApiJson.php'), __DIR__ . '/resources/stubs/ApiResourceTrait.stub' => app_path('Http/Resources/Traits/ApiResourceTrait.php'), + __DIR__ . '/resources/stubs/message.stub' => resource_path('lang/en/message.php'), __DIR__.'/../config/config.php' => config_path('laravel-api-develop.php'), ], 'LaravelApiDevelop'); // Registering package commands. @@ -38,6 +38,10 @@ public function register() // Automatically apply the package configuration $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-api-develop'); + if(file_exists(app_path('Http/Resources/Macros/ApiJson.php'))){ + require app_path('Http/Resources/Macros/ApiJson.php'); + } + // Register the main class to use with the facade $this->app->singleton('laravel-api-develop', function () { return new LaravelApiDevelop; diff --git a/src/resources/stubs/ApiController.stub b/src/resources/stubs/ApiController.stub index 0ce72bc..495d563 100644 --- a/src/resources/stubs/ApiController.stub +++ b/src/resources/stubs/ApiController.stub @@ -15,7 +15,7 @@ class ApiController extends BaseController { return response()->apiJson([ 'success' => false, - 'message' => 'Method '.$method.' does not exist', + 'message' => __('message.method_404', compact('method')), ], 404); } } diff --git a/src/resources/stubs/ApiResourceTrait.stub b/src/resources/stubs/ApiResourceTrait.stub index 1e0d981..472e711 100644 --- a/src/resources/stubs/ApiResourceTrait.stub +++ b/src/resources/stubs/ApiResourceTrait.stub @@ -31,7 +31,7 @@ trait ApiResourceTrait public function with($request): array { if (is_null($this->message) && !$this->count()) { - $this->setMessage('There are no data available.'); + $this->setMessage( __('message.no_data') ); } return [ 'success' => true, @@ -44,11 +44,11 @@ trait ApiResourceTrait /** * Retrieve a value when key Exists. * - * @param $key + * @param string $key * @param mixed $default * @return MissingValue|mixed */ - protected function whenExists($key, $default = null) + protected function whenExists(string $key, $default = null) { if (array_key_exists($key,$this->getAttributes())) { return value($this->{$key}); @@ -59,11 +59,11 @@ trait ApiResourceTrait /** * Retrieve a manipulated value when key Exists. * - * @param $key + * @param string $key * @param $value * @return MissingValue|mixed */ - protected function whenExistsThan($key, $value) + protected function whenExistsThan(string $key, $value) { if (array_key_exists($key,$this->getAttributes())) { return value($value); diff --git a/src/resources/stubs/ModelNameController.stub b/src/resources/stubs/ModelNameController.stub index 37bfffe..8de32bb 100644 --- a/src/resources/stubs/ModelNameController.stub +++ b/src/resources/stubs/ModelNameController.stub @@ -35,7 +35,7 @@ class {{modelName}}Controller extends ApiController { $requestData = $request->all(); ${{modelNameCamel}} = {{modelName}}::create($requestData); - return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage('Created!'); + return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage( __('message.create', ['model' => '{{modelName}}' ]) ); } /** @@ -60,7 +60,7 @@ class {{modelName}}Controller extends ApiController { $requestData = $request->all(); ${{modelNameCamel}}->update($requestData); - return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage('Updated!'); + return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage( __('message.update', ['model' => '{{modelName}}' ]) ); } /** @@ -73,7 +73,7 @@ class {{modelName}}Controller extends ApiController { ${{modelNameCamel}}->delete(); return response()->apiJson([ - 'message' => 'Deleted!' + 'message' => __('message.delete', ['model' => '{{modelName}}' ]) ]); } } diff --git a/src/resources/stubs/message.stub b/src/resources/stubs/message.stub new file mode 100644 index 0000000..596b8c4 --- /dev/null +++ b/src/resources/stubs/message.stub @@ -0,0 +1,18 @@ + ':model successfully Created!!', + 'update' => ':model successfully Updated!!', + 'delete' => ':model successfully Deleted!!', + 'no_data' => 'There are no data available!!', + 'method_404' => 'Method :method does not exist!!', +];