You've already forked Php-Template
Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
78d5213892
|
|||
|
56b78f0102
|
|||
|
05d8c5b813
|
|||
|
2d6d0a43f3
|
|||
|
e203975294
|
|||
|
b1e5470a07
|
|||
|
5f9ee939d6
|
|||
|
e9a3a5885f
|
|||
|
f12559d2c3
|
|||
|
b466495ae1
|
|||
|
4eb21ace55
|
|||
|
a3e54ce989
|
|||
|
403cb4c12e
|
|||
|
9f048e408c
|
|||
|
be570a3173
|
|||
|
6353a4f19e
|
|||
|
59bcf4d2a7
|
|||
|
89bc3e75ec
|
|||
|
fb04e9c390
|
|||
|
77d66abbe0
|
|||
|
52aa7abc2d
|
|||
|
b6256a7e1d
|
|||
|
59fa57d643
|
|||
|
199921fb83
|
|||
|
aaf772dc6d
|
|||
|
89fd169067
|
|||
|
a01a0c590b
|
|||
|
dfccf9ca4f
|
|||
|
011a1e77c0
|
|||
|
7c7b538616
|
|||
|
71fa18bc17
|
|||
|
7308f33de4
|
|||
|
d0d18fd603
|
|||
|
f57406f981
|
|||
|
e46bf5a6ec
|
|||
|
02acc9afbe
|
|||
|
cdb2b28cb7
|
|||
|
118e83414d
|
@@ -1,18 +1,17 @@
|
||||
on:
|
||||
create:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
- "v*"
|
||||
|
||||
name: 🏗️✨ Build Workflow
|
||||
|
||||
jobs:
|
||||
Build:
|
||||
name: 🖥️🔨 Build
|
||||
name: 🖥️ 🔨 Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
apt update && apt install -yq ca-certificates curl
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
@@ -27,12 +26,48 @@ jobs:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: 🏗️🔧 Set up Docker Buildx
|
||||
- name: 🏗️ 🔧 Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: 🐳🔨 Build Backend Container
|
||||
- name: 🐳 🔨 Build Container
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64,linux/arm64
|
||||
dockerfile: Dockerfile
|
||||
tags: siteworxpro/template:${{ gitea.ref_name }}
|
||||
|
||||
Build-Migrations:
|
||||
needs:
|
||||
- Build
|
||||
name: 🖥️ 🔨 Build Migrations
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
- name: 🔑 🔐 Login to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: 📖 🔍 Checkout Repository Code
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 1
|
||||
|
||||
- name: 🏗️ 🔧 Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: 🐳 🔨 Build Migrations Container
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
platforms: linux/amd64,linux/arm64
|
||||
sbom: true
|
||||
provenance: true
|
||||
context: .
|
||||
file: migrations.Dockerfile
|
||||
tags: siteworxpro/template:${{ gitea.ref_name }}-migrations
|
||||
@@ -1,20 +1,19 @@
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "*"
|
||||
- "**"
|
||||
|
||||
name: 🧪✨ Tests Workflow
|
||||
|
||||
jobs:
|
||||
|
||||
LicenseCheck:
|
||||
name: License Check
|
||||
DatabaseMigrations:
|
||||
name: 🧪 ✨ Database Migrations
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
apt update && apt install -yq ca-certificates curl
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
@@ -29,15 +28,114 @@ jobs:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Install Composer Libraries
|
||||
- name: 🏎️ 🏁 Start Support Containers
|
||||
run: |
|
||||
echo "Starting Support Containers"
|
||||
docker run --rm \
|
||||
--network "${{ env.JOB_CONTAINER_NAME }}-${{ gitea.job }}-network" \
|
||||
--name ${{ gitea.job }}-${{ gitea.run_id }}-postgres \
|
||||
-e POSTGRES_USER=postgres \
|
||||
-e POSTGRES_PASSWORD=postgres \
|
||||
-e POSTGRES_DB=postgres \
|
||||
-p 5432 \
|
||||
-d postgres:17
|
||||
|
||||
echo "Waiting for Postgres to start"
|
||||
sleep 10
|
||||
|
||||
- name: 💽 ⬆️ Run Migrations
|
||||
run: |
|
||||
docker run \
|
||||
--name ${{ gitea.run_id }}-migrate \
|
||||
--rm \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
--network "${{ env.JOB_CONTAINER_NAME }}-${{ gitea.job }}-network" \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/migrate:v4.18.3 -database "postgres://postgres:postgres@${{ gitea.job }}-${{ gitea.run_id }}-postgres:5432/postgres?sslmode=disable" -path db/migrations up
|
||||
|
||||
- name: 💽 ⬇️ Rollback Migrations
|
||||
run: |
|
||||
docker run \
|
||||
--name ${{ gitea.run_id }}-migrate \
|
||||
--rm \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
--network "${{ env.JOB_CONTAINER_NAME }}-${{ gitea.job }}-network" \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/migrate:v4.18.3 -database "postgres://postgres:postgres@${{ gitea.job }}-${{ gitea.run_id }}-postgres:5432/postgres?sslmode=disable" -path db/migrations down --all
|
||||
|
||||
- name: 🧨 💥 Tear Down Support Containers
|
||||
if: always()
|
||||
run: |
|
||||
docker stop ${{ gitea.job }}-${{ gitea.run_id }}-postgres
|
||||
|
||||
LibraryAudit:
|
||||
name: 🛡️ 🔒 Library Audit
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
- name: 📖 🔍 Checkout Repository Code
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 1
|
||||
|
||||
- name: 🔑 🔐 Login to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: 📖 ✨ Install Composer Libraries
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v composer-cache:/tmp/cache \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/composer \
|
||||
install --ignore-platform-reqs --no-interaction --prefer-dist --optimize-autoloader
|
||||
|
||||
- name: Run License Check
|
||||
- name: Run Library Audit
|
||||
run: |
|
||||
docker run --rm \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/composer \
|
||||
audit
|
||||
|
||||
LicenseCheck:
|
||||
name: 🛡️ 🔒 License Check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
- name: 📖 🔍 Checkout Repository Code
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 1
|
||||
|
||||
- name: 🔑 🔐 Login to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: 📖 ✨ Install Composer Libraries
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v composer-cache:/tmp/cache \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/composer \
|
||||
install --ignore-platform-reqs --no-interaction --prefer-dist --optimize-autoloader
|
||||
|
||||
- name: 🎟️ 🔬 Run License Check
|
||||
run: |
|
||||
docker run --rm \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
@@ -46,12 +144,11 @@ jobs:
|
||||
run tests:license
|
||||
|
||||
CodeLint:
|
||||
name: Code Lint
|
||||
name: 📝 ✨ Code Lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
apt update && apt install -yq ca-certificates curl
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
@@ -66,9 +163,10 @@ jobs:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Install Composer Libraries
|
||||
- name: 📖 ✨ Install Composer Libraries
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v composer-cache:/tmp/cache \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/composer \
|
||||
@@ -83,12 +181,11 @@ jobs:
|
||||
run tests:lint
|
||||
|
||||
CodeSniffer:
|
||||
name: Code Sniffer
|
||||
name: 🐙 🔍 Code Sniffer
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
apt update && apt install -yq ca-certificates curl
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
@@ -103,9 +200,10 @@ jobs:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Install Composer Libraries
|
||||
- name: 📖 ✨ Install Composer Libraries
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v composer-cache:/tmp/cache \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/composer \
|
||||
@@ -120,12 +218,11 @@ jobs:
|
||||
run tests:phpstan
|
||||
|
||||
UnitTests:
|
||||
name: Unit Tests
|
||||
name: 🧪 ✅ Unit Tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
apt update && apt install -yq ca-certificates curl
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
|
||||
@@ -140,9 +237,10 @@ jobs:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Install Composer Libraries
|
||||
- name: 📖 ✨ Install Composer Libraries
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v composer-cache:/tmp/cache \
|
||||
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
|
||||
-w ${{ github.workspace }} \
|
||||
siteworxpro/composer \
|
||||
|
||||
@@ -33,6 +33,7 @@ COPY --from=library /app/vendor /app/vendor
|
||||
ADD src src/
|
||||
ADD server.php .
|
||||
ADD .rr.yaml .
|
||||
ADD config.php .
|
||||
|
||||
EXPOSE 9501
|
||||
|
||||
|
||||
21
README.md
21
README.md
@@ -1,8 +1,6 @@
|
||||
# Template
|
||||
|
||||
[](https://git.siteworxpro.com/rrise/php-template/-/commits/master)
|
||||
[](https://git.siteworxpro.com/rrise/php-template/-/commits/master)
|
||||
[](https://git.siteworxpro.com/rrise/php-template/-/releases)
|
||||

|
||||
|
||||
## Dev Environment
|
||||
|
||||
@@ -10,13 +8,28 @@
|
||||
- Docker
|
||||
- Docker Compose
|
||||
|
||||
### migrations
|
||||
|
||||
create a new migration
|
||||
```shell
|
||||
docker run --rm -v $(PWD):/app siteworxpro/migrate:v4.18.3 create -ext sql -dir /app/db/migrations -seq create_users_table
|
||||
```
|
||||
|
||||
```text
|
||||
postgres://siteworxpro:password@localhost:5432/siteworxpro?sslmode=disable
|
||||
```
|
||||
|
||||
```shell
|
||||
docker run --rm -v $(PWD):/app siteworxpro/migrate:v4.18.3 -database "postgres://siteworxpro:password@localhost:5432/siteworxpro?sslmode=disable" -path /app/db/migrations up
|
||||
```
|
||||
|
||||
### Starting the Runtime
|
||||
```shell
|
||||
docker-compose up -d
|
||||
```
|
||||
### Start the server
|
||||
```shell
|
||||
docker exec -it template-runtime-1 rr serve
|
||||
docker exec -it template-dev-runtime-1 rr serve
|
||||
```
|
||||
|
||||
You can access the api at `http://localhost:9501/`
|
||||
|
||||
@@ -9,21 +9,21 @@
|
||||
},
|
||||
"require": {
|
||||
"php": "^8.4",
|
||||
"league/route": "^6.2",
|
||||
"illuminate/database": "^12.10",
|
||||
"spiral/roadrunner-http": "^3.5",
|
||||
"nyholm/psr7": "^1.8",
|
||||
"league/route": "^6.2.0",
|
||||
"illuminate/database": "^v12.34.0",
|
||||
"spiral/roadrunner-http": "^v3.6.0",
|
||||
"nyholm/psr7": "^1.8.2",
|
||||
"illuminate/support": "^v12.10.2",
|
||||
"roadrunner-php/app-logger": "^1.2",
|
||||
"siteworxpro/config": "^1.1",
|
||||
"predis/predis": "^3.0"
|
||||
"roadrunner-php/app-logger": "^1.2.0",
|
||||
"siteworxpro/config": "^1.1.1",
|
||||
"predis/predis": "^v3.2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^12.1",
|
||||
"phpunit/phpunit": "^12.4",
|
||||
"mockery/mockery": "^1.6",
|
||||
"squizlabs/php_codesniffer": "^3.12",
|
||||
"lendable/composer-license-checker": "^1.2",
|
||||
"phpstan/phpstan": "^2.1"
|
||||
"phpstan/phpstan": "^2.1.31"
|
||||
},
|
||||
"scripts": {
|
||||
"tests:all": [
|
||||
@@ -53,16 +53,10 @@
|
||||
"phpstan analyse --level 4 ./src/ -c phpstan.neon"
|
||||
]
|
||||
},
|
||||
"repositories": {
|
||||
"git.siteworxpro.com/24": {
|
||||
"repositories": [
|
||||
{
|
||||
"type": "composer",
|
||||
"url": "https://git.siteworxpro.com/api/v4/group/24/-/packages/composer/packages.json",
|
||||
"options": {
|
||||
"ssl": {
|
||||
"verify_peer": false,
|
||||
"allow_self_signed": true
|
||||
}
|
||||
}
|
||||
"url": "https://gitea.siteworxpro.com/api/packages/php-packages/composer"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
956
composer.lock
generated
956
composer.lock
generated
File diff suppressed because it is too large
Load Diff
5
migrations.Dockerfile
Normal file
5
migrations.Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM siteworxpro/migrate:v4.18.3
|
||||
|
||||
ADD db/migrations /app/db/migrations
|
||||
|
||||
WORKDIR /app
|
||||
@@ -4,11 +4,11 @@ use Siteworxpro\App\Server;
|
||||
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
// Instantiate the ExternalServer class
|
||||
$server = new Server();
|
||||
|
||||
// Start the server
|
||||
try {
|
||||
// Instantiate the ExternalServer class
|
||||
$server = new Server();
|
||||
|
||||
// Start the server
|
||||
$server->startServer();
|
||||
} catch (JsonException $e) {
|
||||
echo $e->getMessage();
|
||||
|
||||
46
src/Controllers/Controller.php
Normal file
46
src/Controllers/Controller.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Controllers;
|
||||
|
||||
use League\Route\Http\Exception\NotFoundException;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
abstract class Controller implements ControllerInterface
|
||||
{
|
||||
/**
|
||||
* @param ServerRequest $request
|
||||
* @return ResponseInterface
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function get(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
throw new NotFoundException("not found");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function post(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
throw new NotFoundException("not found");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function delete(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
throw new NotFoundException("not found");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function patch(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
throw new NotFoundException("not found");
|
||||
}
|
||||
}
|
||||
43
src/Controllers/ControllerInterface.php
Normal file
43
src/Controllers/ControllerInterface.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Controllers;
|
||||
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
interface ControllerInterface
|
||||
{
|
||||
/**
|
||||
* Handle the request and return a response.
|
||||
*
|
||||
* @param ServerRequest $request The request data.
|
||||
* @return ResponseInterface The response data.
|
||||
*/
|
||||
public function get(ServerRequest $request): ResponseInterface;
|
||||
|
||||
/**
|
||||
* Handle the request and return a response.
|
||||
*
|
||||
* @param ServerRequest $request The request data.
|
||||
* @return ResponseInterface The response data.
|
||||
*/
|
||||
public function post(ServerRequest $request): ResponseInterface;
|
||||
|
||||
/**
|
||||
* Handle the request and return a response.
|
||||
*
|
||||
* @param ServerRequest $request The request data.
|
||||
* @return ResponseInterface The response data.
|
||||
*/
|
||||
public function delete(ServerRequest $request): ResponseInterface;
|
||||
|
||||
/**
|
||||
* Handle the request and return a response.
|
||||
*
|
||||
* @param ServerRequest $request The request data.
|
||||
* @return ResponseInterface The response data.
|
||||
*/
|
||||
public function patch(ServerRequest $request): ResponseInterface;
|
||||
}
|
||||
27
src/Controllers/IndexController.php
Normal file
27
src/Controllers/IndexController.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Controllers;
|
||||
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
|
||||
/**
|
||||
* Class IndexController
|
||||
*
|
||||
* This class handles the index route of the application.
|
||||
*/
|
||||
class IndexController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handles the GET request for the index route.
|
||||
*
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function get(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Facades;
|
||||
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Siteworx\Config\Exception\EmptyDirectoryException;
|
||||
use Siteworx\Config\Exception\FileNotFoundException;
|
||||
use Siteworx\Config\Exception\UnsupportedFormatException;
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
*
|
||||
* This class serves as a facade for the configuration settings of the application.
|
||||
* It extends the Facade class from the Illuminate\Support\Facades namespace.
|
||||
*
|
||||
* @method static bool | string | int get(string $key) Retrieve the configuration value for the given key.
|
||||
*
|
||||
* @package Siteworx\App\Facades
|
||||
*/
|
||||
class Config extends Facade
|
||||
{
|
||||
protected static $cached = false;
|
||||
|
||||
|
||||
/**
|
||||
* @throws UnsupportedFormatException
|
||||
* @throws FileNotFoundException
|
||||
* @throws EmptyDirectoryException
|
||||
*/
|
||||
public static function getFacadeRoot(): \Siteworx\Config\Config
|
||||
{
|
||||
if (self::$resolvedInstance !== null) {
|
||||
try {
|
||||
$config = self::resolveFacadeInstance(self::getFacadeAccessor());
|
||||
if ($config instanceof \Siteworx\Config\Config) {
|
||||
return $config;
|
||||
}
|
||||
} catch (BindingResolutionException) {
|
||||
}
|
||||
}
|
||||
|
||||
return \Siteworx\Config\Config::load(__DIR__ . '/../../config.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string The name of the component.
|
||||
*/
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return \Siteworx\Config\Config::class;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Siteworxpro\App\Facades\Config;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
|
||||
/**
|
||||
* Class CorsMiddleware
|
||||
|
||||
@@ -5,15 +5,20 @@ declare(strict_types=1);
|
||||
namespace Siteworxpro\App;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use League\Route\Http\Exception\MethodNotAllowedException;
|
||||
use League\Route\Http\Exception\NotFoundException;
|
||||
use League\Route\Router;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Siteworxpro\App\Facades\Config;
|
||||
use Siteworxpro\App\Facades\Logger;
|
||||
use Siteworx\Config\Config as SWConfig;
|
||||
use Siteworxpro\App\Controllers\IndexController;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use Siteworxpro\App\Http\Middleware\CorsMiddleware;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\App\Services\Facades\Logger;
|
||||
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
||||
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
||||
use Spiral\RoadRunner\Http\PSR7Worker;
|
||||
use Spiral\RoadRunner\Worker;
|
||||
|
||||
@@ -38,11 +43,17 @@ class Server
|
||||
*/
|
||||
protected PSR7Worker $worker;
|
||||
|
||||
public static array $serviceProviders = [
|
||||
LoggerServiceProvider::class,
|
||||
RedisServiceProvider::class
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
*
|
||||
* Initializes the server by booting the PSR-7 worker and router.
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -57,11 +68,33 @@ class Server
|
||||
* subclasses to ensure proper initialization.
|
||||
*
|
||||
* @return void
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private function boot(): void
|
||||
{
|
||||
$container = new Container();
|
||||
Facade::setFacadeApplication($container);
|
||||
Facade::setFacadeContainer($container);
|
||||
|
||||
// Bind the container to the Config facade first so that it can be used by service providers
|
||||
$container->bind(SWConfig::class, function () {
|
||||
return SWConfig::load(__DIR__ . '/../config.php');
|
||||
});
|
||||
|
||||
foreach (self::$serviceProviders as $serviceProvider) {
|
||||
if (class_exists($serviceProvider)) {
|
||||
$provider = new $serviceProvider($container);
|
||||
if ($provider instanceof ServiceProvider) {
|
||||
$provider->register();
|
||||
} else {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'Service provider %s is not an instance of ServiceProvider.',
|
||||
$serviceProvider
|
||||
));
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException(sprintf('Service provider %s not found.', $serviceProvider));
|
||||
}
|
||||
}
|
||||
|
||||
$this->worker = new PSR7Worker(
|
||||
Worker::create(),
|
||||
@@ -113,10 +146,7 @@ class Server
|
||||
*/
|
||||
protected function registerRoutes(): void
|
||||
{
|
||||
$this->router->get('/', function () {
|
||||
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
||||
});
|
||||
|
||||
$this->router->get('/', IndexController::class . '::get');
|
||||
$this->router->middleware(new CorsMiddleware());
|
||||
}
|
||||
|
||||
|
||||
308
src/Services/Facade.php
Normal file
308
src/Services/Facade.php
Normal file
@@ -0,0 +1,308 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Support\Testing\Fakes\Fake;
|
||||
use Mockery;
|
||||
use Mockery\Expectation;
|
||||
use Mockery\ExpectationInterface;
|
||||
use Mockery\LegacyMockInterface;
|
||||
use Mockery\MockInterface;
|
||||
|
||||
class Facade
|
||||
{
|
||||
/**
|
||||
* The application instance being facaded.
|
||||
*
|
||||
* @var Container | null
|
||||
*/
|
||||
protected static ?Container $container = null;
|
||||
|
||||
/**
|
||||
* The resolved object instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static array $resolvedInstance;
|
||||
|
||||
/**
|
||||
* Indicates if the resolved instance should be cached.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static bool $cached = true;
|
||||
|
||||
/**
|
||||
* Run a Closure when the facade has been resolved.
|
||||
*
|
||||
* @param \Closure $callback
|
||||
* @return void
|
||||
*/
|
||||
public static function resolved(\Closure $callback): void
|
||||
{
|
||||
$accessor = static::getFacadeAccessor();
|
||||
|
||||
if (static::$container->resolved($accessor) === true) {
|
||||
$callback(static::getFacadeRoot(), static::$container);
|
||||
}
|
||||
|
||||
static::$container->afterResolving($accessor, function ($service, $app) use ($callback) {
|
||||
$callback($service, $app);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the facade into a Mockery spy.
|
||||
*
|
||||
* @return MockInterface
|
||||
*/
|
||||
public static function spy(): MockInterface
|
||||
{
|
||||
if (! static::isMock()) {
|
||||
$class = static::getMockableClass();
|
||||
|
||||
return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) {
|
||||
static::swap($spy);
|
||||
});
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Cannot spy on an existing mock instance.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a partial mock on the facade.
|
||||
*
|
||||
* @return MockInterface
|
||||
*/
|
||||
public static function partialMock(): MockInterface
|
||||
{
|
||||
$name = static::getFacadeAccessor();
|
||||
|
||||
$mock = static::isMock()
|
||||
? static::$resolvedInstance[$name]
|
||||
: static::createFreshMockInstance();
|
||||
|
||||
return $mock->makePartial();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a mock expectation on the facade.
|
||||
*
|
||||
* @return Expectation|ExpectationInterface
|
||||
*/
|
||||
public static function shouldReceive(): Mockery\Expectation | Mockery\ExpectationInterface
|
||||
{
|
||||
$name = static::getFacadeAccessor();
|
||||
|
||||
$mock = static::isMock()
|
||||
? static::$resolvedInstance[$name]
|
||||
: static::createFreshMockInstance();
|
||||
|
||||
return $mock->shouldReceive(...func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a mock expectation on the facade.
|
||||
*
|
||||
* @return Expectation|ExpectationInterface
|
||||
*/
|
||||
public static function expects(): Mockery\Expectation | Mockery\ExpectationInterface
|
||||
{
|
||||
$name = static::getFacadeAccessor();
|
||||
|
||||
$mock = static::isMock()
|
||||
? static::$resolvedInstance[$name]
|
||||
: static::createFreshMockInstance();
|
||||
|
||||
return $mock->expects(...func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh mock instance for the given class.
|
||||
*
|
||||
* @return MockInterface|LegacyMockInterface
|
||||
*/
|
||||
protected static function createFreshMockInstance(): MockInterface | LegacyMockInterface
|
||||
{
|
||||
return tap(static::createMock(), function ($mock) {
|
||||
static::swap($mock);
|
||||
|
||||
$mock->shouldAllowMockingProtectedMethods();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh mock instance for the given class.
|
||||
*
|
||||
* @return MockInterface
|
||||
*/
|
||||
protected static function createMock(): MockInterface
|
||||
{
|
||||
$class = static::getMockableClass();
|
||||
|
||||
return $class ? Mockery::mock($class) : Mockery::mock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a mock is set as the instance of the facade.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isMock(): bool
|
||||
{
|
||||
$name = static::getFacadeAccessor();
|
||||
|
||||
return isset(static::$resolvedInstance[$name]) &&
|
||||
static::$resolvedInstance[$name] instanceof LegacyMockInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mockable class for the bound instance.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected static function getMockableClass(): ?string
|
||||
{
|
||||
if ($root = static::getFacadeRoot()) {
|
||||
return get_class($root);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hotswap the underlying instance behind the facade.
|
||||
*
|
||||
* @param mixed $instance
|
||||
* @return void
|
||||
*/
|
||||
public static function swap(mixed $instance): void
|
||||
{
|
||||
static::$resolvedInstance[static::getFacadeAccessor()] = $instance;
|
||||
|
||||
if (isset(static::$container)) {
|
||||
static::$container->instance(static::getFacadeAccessor(), $instance);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a "fake" has been set as the facade instance.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isFake(): bool
|
||||
{
|
||||
$name = static::getFacadeAccessor();
|
||||
|
||||
return isset(static::$resolvedInstance[$name]) &&
|
||||
static::$resolvedInstance[$name] instanceof Fake;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the root object behind the facade.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getFacadeRoot(): mixed
|
||||
{
|
||||
return static::resolveFacadeInstance(static::getFacadeAccessor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
throw new \RuntimeException('Facade does not implement getFacadeAccessor method.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the facade root instance from the container.
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function resolveFacadeInstance(string $name): mixed
|
||||
{
|
||||
if (isset(static::$resolvedInstance[$name])) {
|
||||
return static::$resolvedInstance[$name];
|
||||
}
|
||||
|
||||
if (static::$container) {
|
||||
if (static::$cached) {
|
||||
return static::$resolvedInstance[$name] = static::$container[$name];
|
||||
}
|
||||
|
||||
return static::$container[$name];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear a resolved facade instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public static function clearResolvedInstance(string $name): void
|
||||
{
|
||||
unset(static::$resolvedInstance[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all of the resolved instances.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clearResolvedInstances(): void
|
||||
{
|
||||
static::$resolvedInstance = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the application instance behind the facade.
|
||||
*/
|
||||
public static function getFacadeContainer(): ?Container
|
||||
{
|
||||
return static::$container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the application instance.
|
||||
*
|
||||
* @param Container $container
|
||||
* @return void
|
||||
*/
|
||||
public static function setFacadeContainer(Container $container): void
|
||||
{
|
||||
static::$container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle dynamic, static calls to the object.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public static function __callStatic(string $method, array $args)
|
||||
{
|
||||
$instance = static::getFacadeRoot();
|
||||
|
||||
if (! $instance) {
|
||||
throw new \RuntimeException('A facade root has not been set.');
|
||||
}
|
||||
|
||||
return $instance->$method(...$args);
|
||||
}
|
||||
}
|
||||
31
src/Services/Facades/Config.php
Normal file
31
src/Services/Facades/Config.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services\Facades;
|
||||
|
||||
use Siteworx\Config\Config as SwConfig;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
*
|
||||
* This class serves as a facade for the configuration settings of the application.
|
||||
* It extends the Facade class from the Illuminate\Support\Facades namespace.
|
||||
*
|
||||
* @method static array | bool | string | int | null get(string $key) Retrieve the configuration value for the given key. // @codingStandardsIgnoreStart
|
||||
*
|
||||
* @package Siteworx\App\Facades
|
||||
*/
|
||||
class Config extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string The name of the component.
|
||||
*/
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return SwConfig::class;
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Facades;
|
||||
namespace Siteworxpro\App\Services\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use RoadRunner\Logger\Logger as RRLogger;
|
||||
use Spiral\Goridge\RPC\RPC;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
|
||||
/**
|
||||
* Class Logger
|
||||
@@ -14,6 +13,7 @@ use Spiral\Goridge\RPC\RPC;
|
||||
* This class serves as a facade for the Monolog logger.
|
||||
* It extends the Facade class from the Illuminate\Support\Facades namespace.
|
||||
*
|
||||
* @method static debug(string $message, array $context = []) Log an informational message.
|
||||
* @method static info(string $message, array $context = []) Log an informational message.
|
||||
* @method static error(string $message, array $context = []) Log an error message.
|
||||
* @method static warning(string $message, array $context = []) Log a warning message.
|
||||
@@ -22,22 +22,6 @@ use Spiral\Goridge\RPC\RPC;
|
||||
*/
|
||||
class Logger extends Facade
|
||||
{
|
||||
public static function getFacadeRoot(): RRLogger
|
||||
{
|
||||
if (self::$resolvedInstance !== null) {
|
||||
$logger = self::resolveFacadeInstance(self::getFacadeAccessor());
|
||||
|
||||
if ($logger instanceof RRLogger) {
|
||||
return $logger;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
||||
|
||||
return new RRLogger($rpc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Facades;
|
||||
namespace Siteworxpro\App\Services\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Predis\Client;
|
||||
use Predis\Response\Status;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
|
||||
/**
|
||||
* Facade for the Redis client.
|
||||
@@ -21,25 +21,6 @@ use Predis\Response\Status;
|
||||
*/
|
||||
class Redis extends Facade
|
||||
{
|
||||
public static function getFacadeRoot(): Client
|
||||
{
|
||||
if (self::$resolvedInstance !== null) {
|
||||
$redis = self::resolveFacadeInstance(self::getFacadeAccessor());
|
||||
|
||||
if ($redis instanceof Client) {
|
||||
return $redis;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new Redis client instance if not already resolved
|
||||
return new Client([
|
||||
'scheme' => 'tcp',
|
||||
'host' => Config::get('redis.host'),
|
||||
'port' => Config::get('redis.port'),
|
||||
'database' => Config::get('redis.database'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
20
src/Services/ServiceProviders/LoggerServiceProvider.php
Normal file
20
src/Services/ServiceProviders/LoggerServiceProvider.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services\ServiceProviders;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use RoadRunner\Logger\Logger as RRLogger;
|
||||
use Spiral\Goridge\RPC\RPC;
|
||||
|
||||
class LoggerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(RRLogger::class, function () {
|
||||
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
||||
return new RRLogger($rpc);
|
||||
});
|
||||
}
|
||||
}
|
||||
24
src/Services/ServiceProviders/RedisServiceProvider.php
Normal file
24
src/Services/ServiceProviders/RedisServiceProvider.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services\ServiceProviders;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Predis\Client;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
|
||||
class RedisServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(Client::class, function () {
|
||||
return new Client([
|
||||
'scheme' => 'tcp',
|
||||
'host' => Config::get('redis.host'),
|
||||
'port' => Config::get('redis.port'),
|
||||
'database' => Config::get('redis.database'),
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ namespace Siteworxpro\Tests\Http\Middleware;
|
||||
use Nyholm\Psr7\Response;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Siteworxpro\App\Facades\Config;
|
||||
use Siteworxpro\App\Http\Middleware\CorsMiddleware;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class CorsMiddlewareTest extends Unit
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Siteworxpro\Tests;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Siteworxpro\App\Facades\Config;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
|
||||
abstract class Unit extends TestCase
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user