Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Update to Laravel 9,10
Browse files Browse the repository at this point in the history
  • Loading branch information
populov committed Jan 12, 2024
1 parent c6565c0 commit 9182c7d
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '7.1', '7.2', '7.3', '7.4' ]
php-versions: [ '7.3', '7.4', '8.0', '8.1', '8.2' ]
name: PHP ${{ matrix.php-versions }} - PHPUnit
steps:
- name: Checkout
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ package.xml
/vendor
.idea
*.cache
*.bak
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changes History

5.0.0
-----
* Update to Laravel 9, 10
* Throw away dingo/api package
* Use tymon/jwt-auth 2.0
* Get rid of BaseApiController
* Add `Saritasa\LaravelControllers\Responses\ResponsesTrait`
* Accomodate `PagingInfo` class from saritasa/dingo-api-custom

4.1.0
-----
Add ILoginRequest interface to provider customizable login validation rules
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Controllers for common UI and endpoints in Laravel,
like API authentication, password change, login page, etc.

## Laravel 5.7+/6.0
## Laravel 9.x/10.x
Install the ```saritasa/laravel-controllers``` package:

```bash
Expand All @@ -28,9 +28,6 @@ There are 2 types of controllers:

Controllers, described below, exist, but you must register routes for them manually

### BaseApiController Base API controller, utilizing helpers from Dingo/API package.
Recommended to use as base controller for other API controllers.

#### Methods
* function json($data, IDataTransformer $transformer = null): Response

Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
"require": {
"php": ">=7.1",
"ext-json": "*",
"illuminate/http": "^5.7 | 6.*",
"illuminate/support": "^5.7 | 6.*",
"saritasa/dingo-api-custom": "^2.2",
"saritasa/laravel-fluent-validation": "^1.2",
"saritasa/transformers": "^1.0",
"tymon/jwt-auth": "^1.0"
"laravel/framework": "^9.0 | ^10.0",
"laravel/ui": "^4.0",
"saritasa/laravel-fluent-validation": "^1.3",
"tymon/jwt-auth": "^2.0"
},
"require-dev": {
"mockery/mockery": "^1.1",
"phpunit/phpunit": ">=6.0",
"phpunit/phpunit": ">=9.0",
"squizlabs/php_codesniffer": "^3.0"
},
"minimum-stability": "beta",
Expand Down
26 changes: 12 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Saritasa Transformers Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<source>
<include>
<directory suffix=".php">./src/</directory>
<exclude>
<directory>./vendor</directory>
<directory>./tests</directory>
</exclude>
</whitelist>
</filter>
</include>
<exclude>
<directory>./vendor</directory>
<directory>./tests</directory>
</exclude>
</source>
</phpunit>
62 changes: 0 additions & 62 deletions src/Api/BaseApiController.php

This file was deleted.

17 changes: 8 additions & 9 deletions src/Api/ForgotPasswordApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

namespace Saritasa\LaravelControllers\Api;

use Dingo\Api\Http\Request;
use Dingo\Api\Http\Response;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Saritasa\LaravelControllers\Responses\SuccessMessage;
use Saritasa\Transformers\BaseTransformer;

/**
* This controller is responsible for handling password reset emails
*
* Utilize native Laravel password management without UI, in API style
* https://laravel.com/docs/5.4/passwords
*/
class ForgotPasswordApiController extends BaseApiController
class ForgotPasswordApiController extends Controller
{
use SendsPasswordResetEmails;

/**
* Create a new controller instance.
*
* @param BaseTransformer $baseTransformer To use as default controller responses transformer
*/
public function __construct(BaseTransformer $baseTransformer)
public function __construct()
{
parent::__construct($baseTransformer);
parent::__construct();
$this->middleware('guest');
}

Expand All @@ -39,7 +38,7 @@ public function __construct(BaseTransformer $baseTransformer)
*/
protected function sendResetLinkResponse(Request $request, $languageResourceId): Response
{
return $this->json(new SuccessMessage(trans($languageResourceId)));
return new JsonResponse(new SuccessMessage(trans($languageResourceId)));
}

/**
Expand Down
42 changes: 21 additions & 21 deletions src/Api/JWTAuthApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@

namespace Saritasa\LaravelControllers\Api;

use Dingo\Api\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Saritasa\LaravelControllers\Requests\Concerns\ILoginRequest;
use Saritasa\LaravelControllers\Responses\AuthSuccess;
use Saritasa\Transformers\IDataTransformer;
use Saritasa\LaravelControllers\Responses\ResponsesTrait;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\JWTAuth;
use Tymon\JWTAuth\JWTGuard;

/**
* Authenticate API Controller. Uses JWT authentication.
*/
class JWTAuthApiController extends BaseApiController
class JWTAuthApiController extends Controller
{
use ResponsesTrait;

/**
* Jwt auth service.
*
* @var JWTAuth
* @var JWTGuard
*/
protected $jwtAuth;

/**
* Authenticate API Controller. Uses JWT authentication.
*
* @param JWTAuth $jwtAuth Jwt guard
* @param IDataTransformer $transformer Default transformer to apply to handled entity
*/
public function __construct(JWTAuth $jwtAuth, ?IDataTransformer $transformer = null)
public function __construct(JWTGuard $jwtAuth)
{
$this->jwtAuth = $jwtAuth;
parent::__construct($transformer);
}

/**
* Authenticate user by `email` and `password`.
*
* @param ILoginRequest $request HTTP Request
*
* @return Response
* @return JsonResponse
*
* @throws HttpException
*/
public function login(ILoginRequest $request): Response
public function login(ILoginRequest $request): JsonResponse
{
$credentials = $request->only('email', 'password');
try {
if (!$token = $this->jwtAuth->attempt($credentials)) {
$this->response->errorNotFound(trans('controllers::auth.failed'));
return $this->errorNotFound(trans('controllers::auth.failed'));
}

return $this->json(new AuthSuccess($token));
return new JsonResponse(new AuthSuccess($token));
} catch (JWTException $e) {
$this->response->errorInternal(trans('controllers::auth.jwt_error'));
return $this->errorInternal(trans('controllers::auth.jwt_error'));
}
}

Expand All @@ -68,11 +68,11 @@ public function login(ILoginRequest $request): Response
public function logout(): Response
{
try {
$this->jwtAuth->parseToken()->invalidate();
$this->jwtAuth->invalidate();
} catch (JWTException $exception) {
$this->response->errorUnauthorized(trans('controllers::auth.jwt_blacklist_error'));
$this->errorUnauthorized(trans('controllers::auth.jwt_blacklist_error'));
}
return $this->response->noContent();
return $this->responseNoContent();
}

/**
Expand All @@ -82,12 +82,12 @@ public function logout(): Response
*
* @throws HttpException
*/
public function refreshToken(): Response
public function refreshToken(): JsonResponse
{
try {
return $this->json(new AuthSuccess($this->jwtAuth->parseToken()->refresh()));
return $this->json(new AuthSuccess($this->jwtAuth->refresh()));
} catch (JWTException $e) {
$this->response->errorForbidden(trans('controllers::auth.jwt_refresh_error'));
return $this->errorForbidden(trans('controllers::auth.jwt_refresh_error'));
}
}
}
11 changes: 6 additions & 5 deletions src/Api/ResetPasswordApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Saritasa\LaravelControllers\Api;

use Dingo\Api\Http\Request;
use Dingo\Api\Http\Response;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Saritasa\LaravelControllers\Responses\ErrorMessage;
use Saritasa\LaravelControllers\Responses\SuccessMessage;
Expand All @@ -16,7 +17,7 @@
* Utilize native Laravel password management without UI, in API style
* https://laravel.com/docs/passwords
*/
class ResetPasswordApiController extends BaseApiController
class ResetPasswordApiController extends Controller
{
use ResetsPasswords;

Expand All @@ -28,7 +29,7 @@ class ResetPasswordApiController extends BaseApiController
*
* @return Response
*/
protected function sendResetResponse(Request $request, string $response): Response
protected function sendResetResponse(Request $request, string $response): JsonResponse
{
return $this->json(new SuccessMessage(trans($response)));
}
Expand All @@ -41,7 +42,7 @@ protected function sendResetResponse(Request $request, string $response): Respon
*
* @return Response
*/
protected function sendResetFailedResponse(Request $request, $message): Response
protected function sendResetFailedResponse(Request $request, $message): JsonResponse
{
return $this->json(new ErrorMessage(trans($message)));
}
Expand Down
17 changes: 0 additions & 17 deletions src/BaseController.php

This file was deleted.

Loading

0 comments on commit 9182c7d

Please sign in to comment.