Skip to content

Commit 79f10cf

Browse files
committed
add json
1 parent ab6caa1 commit 79f10cf

File tree

15 files changed

+858
-0
lines changed

15 files changed

+858
-0
lines changed

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "pharaonic/laravel-jsonable",
3+
"description": "Laravel Jsonable Responses & Exceptions.",
4+
"keywords": [
5+
"laravel jsonable",
6+
"laravel json",
7+
"laravel",
8+
"php"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Moamen Eltouny (Raggi)",
14+
"email": "support@raggitech.com"
15+
}
16+
],
17+
"require": {
18+
"php": ">=8.0",
19+
"laravel/framework": ">=8.0"
20+
},
21+
"config": {
22+
"sort-packages": true
23+
},
24+
"extra": {
25+
"laravel": {
26+
"providers": [
27+
"Pharaonic\\Laravel\\Jsonable\\JsonableServiceProvider"
28+
]
29+
},
30+
"aliases": {
31+
"Json": "Pharaonic\\Laravel\\Jsonable\\Facades\\Json"
32+
}
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"Pharaonic\\Laravel\\Jsonable\\": "src"
37+
},
38+
"files": [
39+
"src/helpers.php"
40+
]
41+
},
42+
"minimum-stability": "dev",
43+
"prefer-stable": true
44+
}

src/Commands/MakeRequest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Jsonable\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class MakeRequest extends GeneratorCommand
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'jsonable:request {name : The name of the class}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new jsonable request class.';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Request';
29+
30+
/**
31+
* Get the stub file for the generator.
32+
*
33+
* @return string
34+
*/
35+
protected function getStub()
36+
{
37+
return $this->resolveStubPath('/stubs/request.stub');
38+
}
39+
40+
41+
/**
42+
* Build the class with the given name.
43+
*
44+
* @param string $name
45+
* @return string
46+
*
47+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
48+
*/
49+
protected function buildClass($name)
50+
{
51+
$stub = $this->files->get($this->getStub());
52+
53+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
54+
}
55+
56+
/**
57+
* Resolve the fully-qualified path to the stub.
58+
*
59+
* @param string $stub
60+
* @return string
61+
*/
62+
protected function resolveStubPath($stub)
63+
{
64+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
65+
? $customPath
66+
: __DIR__ . $stub;
67+
}
68+
69+
/**
70+
* Get the default namespace for the class.
71+
*
72+
* @param string $rootNamespace
73+
* @return string
74+
*/
75+
protected function getDefaultNamespace($rootNamespace)
76+
{
77+
return $rootNamespace . '\Http\Requests';
78+
}
79+
}

src/Commands/MakeResource.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Jsonable\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class MakeResource extends GeneratorCommand
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'jsonable:resource {name : The name of the class}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new jsonable resource class.';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Resource';
29+
30+
/**
31+
* Get the stub file for the generator.
32+
*
33+
* @return string
34+
*/
35+
protected function getStub()
36+
{
37+
return $this->resolveStubPath('/stubs/resource.stub');
38+
}
39+
40+
/**
41+
* Build the class with the given name.
42+
*
43+
* @param string $name
44+
* @return string
45+
*
46+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
47+
*/
48+
protected function buildClass($name)
49+
{
50+
$stub = $this->files->get($this->getStub());
51+
52+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
53+
}
54+
55+
/**
56+
* Resolve the fully-qualified path to the stub.
57+
*
58+
* @param string $stub
59+
* @return string
60+
*/
61+
protected function resolveStubPath($stub)
62+
{
63+
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
64+
? $customPath
65+
: __DIR__ . $stub;
66+
}
67+
68+
/**
69+
* Get the default namespace for the class.
70+
*
71+
* @param string $rootNamespace
72+
* @return string
73+
*/
74+
protected function getDefaultNamespace($rootNamespace)
75+
{
76+
return $rootNamespace . '\Http\Resources';
77+
}
78+
}

src/Commands/stubs/request.stub

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Pharaonic\Laravel\Jsonable\Requests\JsonableFormRequest;
6+
7+
class {{ class }} extends JsonableFormRequest
8+
{
9+
/**
10+
* Error code (authorization & validation)
11+
*
12+
* @var string
13+
*/
14+
protected $errorCode = '{{ class }}';
15+
16+
/**
17+
* Error message (validation)
18+
*
19+
* @var string
20+
*/
21+
protected $errorMessage = '{{ class }} message';
22+
23+
/**
24+
* Determine if the user is authorized to make this request.
25+
*
26+
* @return bool
27+
*/
28+
public function authorize()
29+
{
30+
return false;
31+
}
32+
33+
/**
34+
* Get the validation rules that apply to the request.
35+
*
36+
* @return array
37+
*/
38+
public function rules()
39+
{
40+
return [
41+
//
42+
];
43+
}
44+
}

src/Commands/stubs/resource.stub

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Pharaonic\Laravel\Jsonable\Resources\Json\Resource;
6+
7+
class {{ class }} extends Resource
8+
{
9+
/**
10+
* Response message
11+
*
12+
* @var string
13+
*/
14+
public static $message = "{{ class }} message";
15+
16+
/**
17+
* Transform the resource or resource collection into an array.
18+
*
19+
* @param \Illuminate\Http\Request $request
20+
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
21+
*/
22+
public function toArray($request)
23+
{
24+
return parent::toArray($request);
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Jsonable\Exceptions\Auth;
4+
5+
use Pharaonic\Laravel\Jsonable\Exceptions\Exception as Exception;
6+
7+
class AuthorizationException extends Exception
8+
{
9+
/**
10+
* Render the exception as an HTTP response.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function render($request)
16+
{
17+
if ($response = parent::render($request))
18+
return $response;
19+
20+
return abort(403, $this->getMessage());
21+
}
22+
}

src/Exceptions/Exception.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Jsonable\Exceptions;
4+
5+
use Exception as GlobalException;
6+
7+
class Exception extends GlobalException
8+
{
9+
protected ?string $exCode = null;
10+
11+
public function __construct(string $message = "", ?string $code = null, ?Throwable $previous = null)
12+
{
13+
parent::__construct($message, 0, $previous);
14+
$this->exCode = $code;
15+
}
16+
17+
/**
18+
* Render the exception as an HTTP response.
19+
*
20+
* @param \Illuminate\Http\Request $request
21+
* @return \Illuminate\Http\Response
22+
*/
23+
public function render($request)
24+
{
25+
if ($request->wantsJson())
26+
return $request->header('accept') == 'application/vnd.api+json' ?
27+
$this->renderJsonApi() :
28+
$this->renderJson();
29+
30+
if (!empty($this->exCode))
31+
$this->message = $this->exCode . ' :: ' . $this->message;
32+
33+
return;
34+
}
35+
36+
/**
37+
* Render the json response
38+
*
39+
* @return \Illuminate\Http\JsonResponse
40+
*/
41+
private function renderJson()
42+
{
43+
return response()->json([
44+
'success' => false,
45+
'code' => $this->exCode ?? $this->getCode() ?? null,
46+
'message' => $this->getMessage(),
47+
'data' => (object) (app()->environment('local', 'staging') ?
48+
[
49+
'line' => $this->getLine(),
50+
'file' => $this->getFile(),
51+
] + (config('Pharaonic.jsonable.tracing', false) ? ['trace' => $this->getTrace()] : [])
52+
: []
53+
)
54+
]);
55+
}
56+
57+
/**
58+
* Render the JSON:API response
59+
*
60+
* @return \Illuminate\Http\JsonResponse
61+
*/
62+
private function renderJsonApi()
63+
{
64+
return response()->json([
65+
// 'success' => false,
66+
// 'code' => $this->exCode ?? $this->getCode() ?? null,
67+
// 'message' => $this->getMessage(),
68+
// 'data' => (object) (app()->environment('local', 'staging') ?
69+
// [
70+
// 'line' => $this->getLine(),
71+
// 'file' => $this->getFile(),
72+
// ] + (config('Pharaonic.jsonable.tracing', false) ? ['trace' => $this->getTrace()] : [])
73+
// : []
74+
// )
75+
]);
76+
}
77+
}

0 commit comments

Comments
 (0)