You've already forked Php-Template
Compare commits
2 Commits
master
...
e0ba77556d
| Author | SHA1 | Date | |
|---|---|---|---|
|
e0ba77556d
|
|||
|
f8b988ca0d
|
@@ -1,83 +0,0 @@
|
|||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIEFzCCA52gAwIBAgIURfvF11Q9R3Ue38Tr0BzIoUe0TKQwCgYIKoZIzj0EAwMw
|
|
||||||
MDEuMCwGA1UEAxMlU2l0ZXdvcnggSW50ZXJtZWRpYXRlIEVDMzg0IEF1dGhvcml0
|
|
||||||
eTAeFw0yNTEyMDQxNjM1NTFaFw0yNjEyMDQxNjM2MjFaMHExCzAJBgNVBAYTAlVT
|
|
||||||
MREwDwYDVQQIEwhWaXJnaW5pYTEVMBMGA1UEBxMMUHVyY2VsbHZpbGxlMSQwIgYD
|
|
||||||
VQQKExtTaXRld29yeCBQcm9mZXNzaW9uYWxzLCBMTEMxEjAQBgNVBAMTCWxvY2Fs
|
|
||||||
aG9zdDB2MBAGByqGSM49AgEGBSuBBAAiA2IABM+jXangYCOi01IMblAXJ6iFZE4v
|
|
||||||
SBBOZKNQCwGz8kKi5jyXtVwz6U26DMlBSK+InhhOFQlCRcP9ow8LtlQdaY2XnGKr
|
|
||||||
3X3zxdUZJVhLi/wog+I4igU3+xuyn1E/BgEZx6OCAjUwggIxMA4GA1UdDwEB/wQE
|
|
||||||
AwIHgDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMB0GA1UdDgQW
|
|
||||||
BBRAtanjiWMYAdpCCz0rEkyqf691bzAfBgNVHSMEGDAWgBQYBC15lPoGGGxbmwqY
|
|
||||||
MWL7jjI6azBJBggrBgEFBQcBAQQ9MDswOQYIKwYBBQUHMAGGLWh0dHBzOi8vdmF1
|
|
||||||
bHQuc2l0ZXdvcnhwcm8uY29tL3YxL3N3eF9pbnQvb2NzcDAaBgNVHREEEzARggls
|
|
||||||
b2NhbGhvc3SHBH8AAAEwHAYDVR0gBBUwEzAIBgZngQwBAgIwBwYFZ4EMAQEwggE1
|
|
||||||
BgNVHR8EggEsMIIBKDBioGCgXoZcaHR0cHM6Ly92YXVsdC5zaXRld29yeHByby5j
|
|
||||||
b20vdjEvc3d4X2ludC9pc3N1ZXIvMjVmMWRiNTAtZDQxOS1kZWQ3LTZiZjktZWNh
|
|
||||||
Y2E4NGEwMmY0L2NybC9wZW0wXqBcoFqGWGh0dHBzOi8vdmF1bHQuc2l0ZXdvcnhw
|
|
||||||
cm8uY29tL3YxL3N3eF9pbnQvaXNzdWVyLzI1ZjFkYjUwLWQ0MTktZGVkNy02YmY5
|
|
||||||
LWVjYWNhODRhMDJmNC9jcmwwYqBgoF6GXGh0dHBzOi8vdmF1bHQuc2l0ZXdvcnhw
|
|
||||||
cm8uY29tL3YxL3N3eF9pbnQvaXNzdWVyLzI1ZjFkYjUwLWQ0MTktZGVkNy02YmY5
|
|
||||||
LWVjYWNhODRhMDJmNC9jcmwvZGVyMAoGCCqGSM49BAMDA2gAMGUCMGxgZmKITQFu
|
|
||||||
H6j3j/t9MOTxhVsfOuoD0q3pMlp9d1u4Lg0THKUOzN06BVuXwC1eagIxAL2I/2a1
|
|
||||||
MMJmhky2EavzOsYt37Ae+1KGyELiwcWe5f/lActlw97pqRajpmqEmdo7PA==
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIEETCCAfmgAwIBAgIUIRpRFzFBITweYJETytgbPBgwbWgwDQYJKoZIhvcNAQEL
|
|
||||||
BQAwgZ0xCzAJBgNVBAYTAlVTMREwDwYDVQQIDAhWaXJnaW5pYTEVMBMGA1UEBwwM
|
|
||||||
UHVyY2VsbHZpbGxlMSQwIgYDVQQKDBtTaXRld29yeCBQcm9mZXNzaW9uYWxzLCBM
|
|
||||||
TEMxFDASBgNVBAMMC1NXWCBSb290IENBMSgwJgYJKoZIhvcNAQkBFhl3ZWJtYXN0
|
|
||||||
ZXJAc2l0ZXdvcnhwcm8uY29tMB4XDTIzMDMyMTE2MzAxNVoXDTMzMDMxODE2MzA0
|
|
||||||
NVowMDEuMCwGA1UEAxMlU2l0ZXdvcnggSW50ZXJtZWRpYXRlIEVDMzg0IEF1dGhv
|
|
||||||
cml0eTB2MBAGByqGSM49AgEGBSuBBAAiA2IABIhlP1W1O1WjoDFGFi5XbE0zVy90
|
|
||||||
76pQQ8VmSYtaZI9Jz5pAZTOQ073t/QkTWge8uhDaJ2J2uBhjQJGr5BPttvBcLJFI
|
|
||||||
52X7hJuck4oL0aukXiHYA5gZbC5LhKVvCyZcWqNjMGEwDgYDVR0PAQH/BAQDAgEG
|
|
||||||
MA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBgELXmU+gYYbFubCpgxYvuOMjpr
|
|
||||||
MB8GA1UdIwQYMBaAFHWCysIFrdsWYZJSjBO1pSQPETkTMA0GCSqGSIb3DQEBCwUA
|
|
||||||
A4ICAQBbw5roegt0tUc+gu0IcHDt56cUoqChmIZXzla8gTgg820ww/+Wm+vNAl8W
|
|
||||||
r3Y67LzK19CygoujD2o7M25syaByRiw9JdIfNGvBzklOOM+sus9DDmwSUBMCuljS
|
|
||||||
KLBhWzIrXDZwemzklGEbj+RL4o2ZiL01nx8xygDF55eaudNS0VzRzd2Hv0C+rm2i
|
|
||||||
nnwRNoKsL14YXc41rFBWwb5ViRuD2Wp0c9CivEOd4UNKgOnGyNxcNhjzNlY05t3c
|
|
||||||
NEeskEXiz21sj0vnrwM7olKyXPXDFUCCKGb21Sn9sWKldicumU1i1HdDGA1w50uh
|
|
||||||
NS4G4wqGQ8iZCq3h6JkpBMGPJPG3Dq6yuzrh8fmh56IqtKY4MxdKHb91MtFHnkw5
|
|
||||||
jCrxqpTKShRyqcBSx8QmXRXpec5FEB88NQ3aKhtFlNqXYphNRAI9bLIyGkdxUF/r
|
|
||||||
PCkZkKBhbsRvXT8Ii/K1PQHzliQqJxXhrrJEsIg2jiSQItBg52ZySzuw+Y6++h11
|
|
||||||
73XMKJ53oOeLcxvp2qJRwMkNTwVfNxDmKC0tIRdI+KoJYbYeN0Ev/pEdPdYl+hjY
|
|
||||||
uQhKMt1KtpUyYwPzTGPKGMnklKj/T3Qu7fmpsWxtAOuK7yLLMayBwXBlVBD23md+
|
|
||||||
UAfPR3FfVX+aRqqsvT7WI+SnlycJuYXs41ZPxBjLq2aB7fhAwQ==
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIGIjCCBAqgAwIBAgIJAKU+Idu5bncNMA0GCSqGSIb3DQEBCwUAMIGdMQswCQYD
|
|
||||||
VQQGEwJVUzERMA8GA1UECAwIVmlyZ2luaWExFTATBgNVBAcMDFB1cmNlbGx2aWxs
|
|
||||||
ZTEkMCIGA1UECgwbU2l0ZXdvcnggUHJvZmVzc2lvbmFscywgTExDMRQwEgYDVQQD
|
|
||||||
DAtTV1ggUm9vdCBDQTEoMCYGCSqGSIb3DQEJARYZd2VibWFzdGVyQHNpdGV3b3J4
|
|
||||||
cHJvLmNvbTAeFw0yMDA5MDgxMjU3NTJaFw00MDA5MDMxMjU3NTJaMIGdMQswCQYD
|
|
||||||
VQQGEwJVUzERMA8GA1UECAwIVmlyZ2luaWExFTATBgNVBAcMDFB1cmNlbGx2aWxs
|
|
||||||
ZTEkMCIGA1UECgwbU2l0ZXdvcnggUHJvZmVzc2lvbmFscywgTExDMRQwEgYDVQQD
|
|
||||||
DAtTV1ggUm9vdCBDQTEoMCYGCSqGSIb3DQEJARYZd2VibWFzdGVyQHNpdGV3b3J4
|
|
||||||
cHJvLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAN9JNyWot7VX
|
|
||||||
ODvru8S5/o6gdFuynA1l5T0uXSzWhROMYHndmY+n7pwQCwf1R8iLL3aat9sDRxqM
|
|
||||||
RRScD3nNW6UzC5xNz12wiuemf2KT82cTjmUBU3CvtjstbgkrQ/SrpR/Arvu2YwUe
|
|
||||||
tmL9ft/xaoGvZXx8LKpyRMrHA1FlS2st+RFWBC0yXTU/nL4/7YQKVEcbc3YZvgCT
|
|
||||||
P4/8pxH9u8W7kgnufQHHKEIZR9lxIUhQ7yvc61B3zMntbJsZV1N+0c7j5DXY5cfT
|
|
||||||
6zXlfG2hSX1dbhM56y8O8KiCFaWaDRZ9mwkfZGM0W58gkhXUPXOrIOwewLmvl2Z4
|
|
||||||
Vu43UkLfKhtQApxk6zodHRq1e2rNWSpBCGznT9XyoeO/spJ7yggNkleTa+SnnlmV
|
|
||||||
rHJS/YUp3/jAvJY2bCHQKFu/mguMY3Ub2X6eEBsVZOmUqDMbya1TPP6GCVqh4gUu
|
|
||||||
yip6qS9UksaTF6IN3IcrGhwtTyvp8BFqwVA0tMhgraf1rv6ZoXjY/NDuGjE1xXJg
|
|
||||||
Hg+gg2pIIRcXjcsG1tXFXTgxDqoh127ADg/gtq9cIyarMx4LdNTjnR+CnhjqvRkT
|
|
||||||
uiUBB1bwDc9pbX0ulfnR+VuIZtQ6PSuWwChnMdNBKmCgQT1J1AHWpQqnFTjg42NV
|
|
||||||
5QAdFOQxAnsq2DxkurVFEz2J3euZx1ZdAgMBAAGjYzBhMB0GA1UdDgQWBBR1gsrC
|
|
||||||
Ba3bFmGSUowTtaUkDxE5EzAfBgNVHSMEGDAWgBR1gsrCBa3bFmGSUowTtaUkDxE5
|
|
||||||
EzAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjANBgkqhkiG9w0BAQsF
|
|
||||||
AAOCAgEAZDjIlaAoMfRGdb3/i40s6nN18iWN2Chttd8dLXgV+/SZ8GrAU89JNJrK
|
|
||||||
ODaLZT1wHeWVz0LP3miByuvfrnH4qzPEOI2L6zEy/FJr8SCivjm7aUExyb5kTSXp
|
|
||||||
LkwVcOI9UfQb6lCy9Gs/rUEcWQjs5KS3dy6ZwBMaywq6sRj7MeXmhqXhj7aAyWFA
|
|
||||||
psnQsuP2XweWa9OX6Z+u78sebfoiJlOEUvV9VRNHQYpLUd75p6sti1Dm9blWkZEO
|
|
||||||
hyssi3kOJMH+g5pc9xNbD9gS+/pFUWxEVAhHOc0xdEIcHfV5oiiOUDD5EOIPi3xv
|
|
||||||
/NYTV7o7pv2/QlH09vO2PHdsy07lhsg7NoM3U+zYq609Ox78/b4PNd+TkdtYKebO
|
|
||||||
VumZ0xXab0lWbTVuno52k473ODQRA/v9YWHtuovW0Lzf5fDcBhVXTDeW21SmMJIx
|
|
||||||
B+dgJDh7ql7ruZqjMj+kePjM9Mm+M5pDZ6vrEtgiR2yQj/IE+LoQh/bxFHpFkIK8
|
|
||||||
I6AWoxABAvLZB+KHl1ufR5yOauJG2+SQRuzHNZvkAcdjmwpgfxcsB2mY7o0RbGmZ
|
|
||||||
VWm97P4P9iJhje/W4C0cGwVY5wRAMAg6SI1BpcW7YghB14UrKaxpEzHCdZIeeT94
|
|
||||||
GYzN2XNSSGW3s1anFedd5PQyRM7PlJIcloLYrqyWW6M7OwWnMXA=
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
-----BEGIN EC PRIVATE KEY-----
|
|
||||||
MIGkAgEBBDBrpJYaCMqgu490fpZoIphGVspE33v3JwyD9B55HwSX/jykySs9NTOv
|
|
||||||
68YndzE9LNCgBwYFK4EEACKhZANiAATPo12p4GAjotNSDG5QFyeohWROL0gQTmSj
|
|
||||||
UAsBs/JCouY8l7VcM+lNugzJQUiviJ4YThUJQkXD/aMPC7ZUHWmNl5xiq91988XV
|
|
||||||
GSVYS4v8KIPiOIoFN/sbsp9RPwYBGcc=
|
|
||||||
-----END EC PRIVATE KEY-----
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
tls:
|
|
||||||
stores:
|
|
||||||
default:
|
|
||||||
defaultCertificate:
|
|
||||||
certFile: /etc/ssl/localhost.crt
|
|
||||||
keyFile: /etc/ssl/localhost.key
|
|
||||||
|
|
||||||
options:
|
|
||||||
default:
|
|
||||||
minVersion: VersionTLS13
|
|
||||||
preferServerCipherSuites: true
|
|
||||||
|
|
||||||
mintls13:
|
|
||||||
minVersion: VersionTLS13
|
|
||||||
@@ -26,12 +26,6 @@ jobs:
|
|||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
- name: Write Version File
|
|
||||||
run: |
|
|
||||||
echo $GITEA_REF_NAME > VERSION
|
|
||||||
sed -i "s/dev-version/${GITEA_REF_NAME}/g" src/Helpers/Version.php
|
|
||||||
|
|
||||||
|
|
||||||
- name: 🏗️ 🔧 Set up Docker Buildx
|
- name: 🏗️ 🔧 Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ jobs:
|
|||||||
-e POSTGRES_PASSWORD=postgres \
|
-e POSTGRES_PASSWORD=postgres \
|
||||||
-e POSTGRES_DB=postgres \
|
-e POSTGRES_DB=postgres \
|
||||||
-p 5432 \
|
-p 5432 \
|
||||||
-d postgres:18
|
-d postgres:17
|
||||||
|
|
||||||
echo "Waiting for Postgres to start"
|
echo "Waiting for Postgres to start"
|
||||||
sleep 10
|
sleep 10
|
||||||
|
|||||||
11
.rr.yaml
11
.rr.yaml
@@ -6,17 +6,6 @@ server:
|
|||||||
rpc:
|
rpc:
|
||||||
listen: tcp://127.0.0.1:6001
|
listen: tcp://127.0.0.1:6001
|
||||||
|
|
||||||
grpc:
|
|
||||||
listen: "tcp://0.0.0.0:9001"
|
|
||||||
pool:
|
|
||||||
command: "php grpc-worker.php"
|
|
||||||
num_workers: ${GRPC_WORKERS:-4}
|
|
||||||
debug: ${DEBUG:-false}
|
|
||||||
reflection: ${GRPC_REFLECTION:-true}
|
|
||||||
destroy_timeout: 5s
|
|
||||||
proto:
|
|
||||||
- "protos/example.proto"
|
|
||||||
|
|
||||||
http:
|
http:
|
||||||
pool:
|
pool:
|
||||||
allocate_timeout: 5s
|
allocate_timeout: 5s
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name=".dev: Compose Deployment" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
|
|
||||||
<deployment type="docker-compose.yml">
|
|
||||||
<settings>
|
|
||||||
<option name="sourceFilePath" value=".dev/docker-compose.yml" />
|
|
||||||
</settings>
|
|
||||||
</deployment>
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="All" type="ComposerRunConfigurationType" factoryName="Composer Script">
|
|
||||||
<option name="commandLineParameters" value="" />
|
|
||||||
<option name="pathToComposerJson" value="$PROJECT_DIR$/composer.json" />
|
|
||||||
<option name="script" value="tests:all" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="Lint:fix" type="ComposerRunConfigurationType" factoryName="Composer Script">
|
|
||||||
<option name="commandLineParameters" value="" />
|
|
||||||
<option name="pathToComposerJson" value="$PROJECT_DIR$/composer.json" />
|
|
||||||
<option name="script" value="tests:lint:fix" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="Main" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
|
|
||||||
<CommandLine>
|
|
||||||
<PhpTestInterpreterSettings>
|
|
||||||
<option name="interpreterName" value="composer-runtime" />
|
|
||||||
</PhpTestInterpreterSettings>
|
|
||||||
</CommandLine>
|
|
||||||
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml" coverage_engine="PCov" scope="XML" use_alternative_configuration_file="true" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
25
Dockerfile
25
Dockerfile
@@ -1,5 +1,5 @@
|
|||||||
# Use the RoadRunner image as a base for the first stage
|
# Use the RoadRunner image as a base for the first stage
|
||||||
FROM ghcr.io/roadrunner-server/roadrunner:2025.1.6 AS roadrunner
|
FROM ghcr.io/roadrunner-server/roadrunner:2025.1.4 AS roadrunner
|
||||||
|
|
||||||
# Use the official Composer image as the base for the library stage
|
# Use the official Composer image as the base for the library stage
|
||||||
FROM siteworxpro/composer AS library
|
FROM siteworxpro/composer AS library
|
||||||
@@ -12,11 +12,9 @@ RUN composer install --optimize-autoloader --ignore-platform-reqs --no-dev
|
|||||||
|
|
||||||
|
|
||||||
# Use the official PHP CLI image with Alpine Linux for the second stage
|
# Use the official PHP CLI image with Alpine Linux for the second stage
|
||||||
FROM siteworxpro/php:8.5.1-cli-alpine AS php
|
FROM php:8.4.14-alpine AS php
|
||||||
|
|
||||||
ARG KAFKA_ENABLED=0
|
ARG KAFKA_ENABLED=0
|
||||||
ARG USER=appuser
|
|
||||||
ARG UID=1000
|
|
||||||
|
|
||||||
# Move the production PHP configuration file to the default location
|
# Move the production PHP configuration file to the default location
|
||||||
RUN mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
|
RUN mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
|
||||||
@@ -28,7 +26,6 @@ RUN if [ "$KAFKA_ENABLED" -eq 1 ] ; then \
|
|||||||
echo "Kafka support enabled" ; \
|
echo "Kafka support enabled" ; \
|
||||||
apk add autoconf g++ librdkafka-dev make --no-cache ; \
|
apk add autoconf g++ librdkafka-dev make --no-cache ; \
|
||||||
pecl install rdkafka && docker-php-ext-enable rdkafka ; \
|
pecl install rdkafka && docker-php-ext-enable rdkafka ; \
|
||||||
apk del autoconf g++ make ; \
|
|
||||||
else \
|
else \
|
||||||
echo "Kafka support disabled" ; \
|
echo "Kafka support disabled" ; \
|
||||||
exit 0 ; \
|
exit 0 ; \
|
||||||
@@ -45,19 +42,11 @@ COPY --from=library /app/vendor /app/vendor
|
|||||||
|
|
||||||
# Copy the RoadRunner configuration file and source
|
# Copy the RoadRunner configuration file and source
|
||||||
ADD src src/
|
ADD src src/
|
||||||
ADD generated generated/
|
ADD server.php .
|
||||||
ADD protos protos/
|
ADD .rr.yaml .
|
||||||
ADD server.php cli.php grpc-worker.php .rr.yaml config.php ./
|
ADD config.php .
|
||||||
|
|
||||||
EXPOSE 9501 9001
|
EXPOSE 9501
|
||||||
|
|
||||||
# Create a non-root user and set ownership of the /app directory
|
|
||||||
RUN if [ ! $UID -eq 0 ] ; then addgroup -g $UID $USER && adduser -D -u $UID -G $USER $USER && chown -R $USER:$USER /app ; fi
|
|
||||||
USER $USER
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \
|
|
||||||
CMD curl -f http://localhost:9501/healthz || exit 1
|
|
||||||
|
|
||||||
# Entrypoint command to run the RoadRunner server with the specified configuration
|
# Entrypoint command to run the RoadRunner server with the specified configuration
|
||||||
ENTRYPOINT ["rr", "serve"]
|
ENTRYPOINT ["rr", "serve", "-c", ".rr.yaml", "-s"]
|
||||||
CMD ["-c", ".rr.yaml", "-s"]
|
|
||||||
139
README.md
139
README.md
@@ -2,132 +2,55 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
This is a PHP project template that provides a structured development environment using Docker Compose and Make.
|
|
||||||
It includes tools for code quality, testing, dependency management, and gRPC support.
|
|
||||||
|
|
||||||
## Dev Environment
|
## Dev Environment
|
||||||
|
|
||||||
This project uses Docker Compose and Make to manage the development environment. The `makefile` provides convenient
|
### Prerequisites
|
||||||
commands for common development tasks.
|
- Docker
|
||||||
|
- Docker Compose
|
||||||
|
|
||||||
## Prerequisites
|
### migrations
|
||||||
|
|
||||||
- Docker and Docker Compose
|
create a new migration
|
||||||
- Make
|
```shell
|
||||||
- protoc (Protocol Buffers compiler) - for gRPC code generation
|
docker run --rm -v $(PWD):/app siteworxpro/migrate:v4.18.3 create -ext sql -dir /app/db/migrations -seq create_users_table
|
||||||
|
|
||||||
## Quick Start
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Install PHP dependencies
|
|
||||||
make composer-install
|
|
||||||
|
|
||||||
# Start the development container
|
|
||||||
make start
|
|
||||||
|
|
||||||
# Run the application server
|
|
||||||
make run
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Available Commands
|
```text
|
||||||
|
postgres://siteworxpro:password@localhost:5432/siteworxpro?sslmode=disable
|
||||||
### Container Management
|
|
||||||
|
|
||||||
- `make start` - Start the development runtime container
|
|
||||||
- `make stop` - Stop and remove all containers
|
|
||||||
- `make restart` - Restart the development container
|
|
||||||
- `make rebuild` - Rebuild containers (use after Dockerfile changes)
|
|
||||||
- `make sh` - Open a shell in the development container
|
|
||||||
- `make ps` - Show running containers
|
|
||||||
|
|
||||||
### Application
|
|
||||||
|
|
||||||
- `make run` - Run the application server (RoadRunner)
|
|
||||||
- `make migrate` - Run database migrations
|
|
||||||
|
|
||||||
### Composer & Dependencies
|
|
||||||
|
|
||||||
- `make composer-install` - Install PHP dependencies
|
|
||||||
- `make composer-update` - Update PHP dependencies
|
|
||||||
- `make composer-require package=vendor/package` - Add a new dependency
|
|
||||||
- `make composer-require-dev package=vendor/package` - Add a new dev dependency
|
|
||||||
|
|
||||||
### Code Quality
|
|
||||||
|
|
||||||
- `make lint` - Run linters (phpcs and phpstan)
|
|
||||||
- `make fmt` - Format code with php-cs-fixer
|
|
||||||
- `make test` - Run the test suite (phpunit)
|
|
||||||
- `make license-check` - Check license headers in source files
|
|
||||||
|
|
||||||
### Debugging & Coverage
|
|
||||||
|
|
||||||
- `make enable-debug` - Enable Xdebug for debugging
|
|
||||||
- `make enable-coverage` - Enable PCOV for code coverage
|
|
||||||
|
|
||||||
### gRPC
|
|
||||||
|
|
||||||
- `make protoc` - Generate PHP gRPC code from `.proto` files
|
|
||||||
|
|
||||||
### CI Workflow
|
|
||||||
|
|
||||||
- `make ci` - Run the full CI pipeline locally (install, license check, lint, test)
|
|
||||||
|
|
||||||
### Help
|
|
||||||
|
|
||||||
- `make help` - Show all available commands with descriptions
|
|
||||||
|
|
||||||
## Common Workflows
|
|
||||||
|
|
||||||
### Starting Development
|
|
||||||
|
|
||||||
```bash
|
|
||||||
make composer-install
|
|
||||||
make start
|
|
||||||
make run
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Adding a New Package
|
```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
|
||||||
```bash
|
|
||||||
make composer-require package=vendor/package-name
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Running Tests
|
### Starting the Runtime
|
||||||
|
```shell
|
||||||
```bash
|
docker-compose up -d
|
||||||
make test
|
```
|
||||||
|
### Start the server
|
||||||
|
```shell
|
||||||
|
docker exec -it template-dev-runtime-1 rr serve
|
||||||
```
|
```
|
||||||
|
|
||||||
### Code Quality Check
|
You can access the api at `http://localhost:9501/`
|
||||||
|
|
||||||
```bash
|
### Xdebug
|
||||||
make lint
|
|
||||||
make fmt
|
xdebug needs to be built into the container before it will work
|
||||||
|
```shell
|
||||||
|
docker exec -it php-template-composer-runtime-1 bin/xdebug.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
### Debugging
|
### Install the dependencies
|
||||||
|
```shell
|
||||||
```bash
|
docker run --rm -v $(PWD):/app siteworxpro/composer install --ignore-platform-reqs
|
||||||
make enable-debug
|
|
||||||
make run
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Notes
|
### Running all tests
|
||||||
|
```shell
|
||||||
|
docker run --rm -v $(PWD):/app siteworxpro/composer run tests:all
|
||||||
|
```
|
||||||
|
|
||||||
- All commands run inside Docker containers, ensuring a consistent environment
|
|
||||||
- The development runtime uses RoadRunner as the application server
|
|
||||||
- Composer commands run in a separate `composer-runtime` container
|
|
||||||
- Database migrations run in a dedicated `migration-container`
|
|
||||||
|
|
||||||
## Additional Information
|
|
||||||
|
|
||||||
### Accessing Services
|
|
||||||
|
|
||||||
- You can access the api at [https://localhost](https://localhost)
|
|
||||||
- Traefik dashboard is at [https://127.0.0.1/dashboard/](https://127.0.0.1/dashboard/)
|
|
||||||
- the grpc server is at [tcp://localhost:9001](tcp://localhost:9001)
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ echo "Installing xDebug"
|
|||||||
|
|
||||||
apk add make gcc linux-headers autoconf alpine-sdk
|
apk add make gcc linux-headers autoconf alpine-sdk
|
||||||
|
|
||||||
curl -sL https://github.com/xdebug/xdebug/archive/3.5.0alpha3.tar.gz -o 3.5.0alpha3.tar.gz
|
curl -sL https://github.com/xdebug/xdebug/archive/3.4.0.tar.gz -o 3.4.0.tar.gz
|
||||||
tar -xvf 3.5.0alpha3.tar.gz
|
tar -xvf 3.4.0.tar.gz
|
||||||
cd xdebug-3.5.0alpha3 || exit
|
cd xdebug-3.4.0 || exit
|
||||||
phpize
|
phpize
|
||||||
./configure --enable-xdebug
|
./configure --enable-xdebug
|
||||||
make
|
make
|
||||||
@@ -20,5 +20,5 @@ xdebug.client_host = host.docker.internal
|
|||||||
" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
|
" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
|
||||||
|
|
||||||
cd ..
|
cd ..
|
||||||
rm -rf xdebug-3.5.0alpha3
|
rm -rf xdebug-3.4.0
|
||||||
rm -rf 3.5.0alpha3.tar.gz
|
rm -rf 3.4.0.tar.gz
|
||||||
|
|||||||
@@ -4,12 +4,11 @@
|
|||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Siteworxpro\\App\\": "src/",
|
"Siteworxpro\\App\\": "src/",
|
||||||
"Siteworxpro\\Tests\\": "tests/",
|
"Siteworxpro\\Tests\\": "tests/"
|
||||||
"GRPC\\": "generated/GRPC"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.5",
|
"php": "^8.4",
|
||||||
"league/route": "^6.2.0",
|
"league/route": "^6.2.0",
|
||||||
"illuminate/database": "^v12.34.0",
|
"illuminate/database": "^v12.34.0",
|
||||||
"spiral/roadrunner-http": "^v3.6.0",
|
"spiral/roadrunner-http": "^v3.6.0",
|
||||||
@@ -22,18 +21,12 @@
|
|||||||
"lcobucci/jwt": "^5.6",
|
"lcobucci/jwt": "^5.6",
|
||||||
"adhocore/cli": "^1.9",
|
"adhocore/cli": "^1.9",
|
||||||
"robinvdvleuten/ulid": "^5.0",
|
"robinvdvleuten/ulid": "^5.0",
|
||||||
"monolog/monolog": "^3.9",
|
"monolog/monolog": "^3.9"
|
||||||
"react/promise": "^3",
|
|
||||||
"react/async": "^4",
|
|
||||||
"guzzlehttp/guzzle": "^7.10",
|
|
||||||
"zircote/swagger-php": "^5.7",
|
|
||||||
"spiral/roadrunner-grpc": "^3.5",
|
|
||||||
"league/tactician": "^1.1"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^12.4",
|
"phpunit/phpunit": "^12.4",
|
||||||
"mockery/mockery": "^1.6",
|
"mockery/mockery": "^1.6",
|
||||||
"squizlabs/php_codesniffer": "^4.0",
|
"squizlabs/php_codesniffer": "^3.12",
|
||||||
"lendable/composer-license-checker": "^1.2",
|
"lendable/composer-license-checker": "^1.2",
|
||||||
"phpstan/phpstan": "^2.1.31",
|
"phpstan/phpstan": "^2.1.31",
|
||||||
"kwn/php-rdkafka-stubs": "^2.2"
|
"kwn/php-rdkafka-stubs": "^2.2"
|
||||||
|
|||||||
1482
composer.lock
generated
1482
composer.lock
generated
File diff suppressed because it is too large
Load Diff
11
config.php
11
config.php
@@ -4,16 +4,12 @@ use Siteworxpro\App\Helpers\Env;
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
'app' => [
|
|
||||||
'log_level' => Env::get('LOG_LEVEL', 'debug'),
|
|
||||||
'dev_mode' => Env::get('DEV_MODE', false, 'bool'),
|
|
||||||
],
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The server configuration.
|
* The server configuration.
|
||||||
*/
|
*/
|
||||||
'server' => [
|
'server' => [
|
||||||
'port' => Env::get('HTTP_PORT', 9501, 'int'),
|
'port' => Env::get('HTTP_PORT', 9501, 'int'),
|
||||||
|
'dev_mode' => Env::get('DEV_MODE', false, 'bool'),
|
||||||
],
|
],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,11 +47,11 @@ return [
|
|||||||
'signing_key' => Env::get('JWT_SIGNING_KEY', 'a_super_secret_key'),
|
'signing_key' => Env::get('JWT_SIGNING_KEY', 'a_super_secret_key'),
|
||||||
'audience' => Env::get('JWT_AUDIENCE', 'my_audience'),
|
'audience' => Env::get('JWT_AUDIENCE', 'my_audience'),
|
||||||
'issuer' => Env::get('JWT_ISSUER', 'my_issuer'),
|
'issuer' => Env::get('JWT_ISSUER', 'my_issuer'),
|
||||||
'strict_validation' => Env::get('JWT_STRICT_VALIDATION', false, 'bool'),
|
'strict_validation' => Env::get('JWT_STRICT_VALIDATION', true, 'bool'),
|
||||||
],
|
],
|
||||||
|
|
||||||
'queue' => [
|
'queue' => [
|
||||||
'broker' => Env::get('QUEUE_BROKER', 'redis'),
|
'broker' => Env::get('QUEUE_BROKER', 'kafka'),
|
||||||
|
|
||||||
'broker_config' => [
|
'broker_config' => [
|
||||||
|
|
||||||
@@ -65,7 +61,6 @@ return [
|
|||||||
|
|
||||||
'kafka' => [
|
'kafka' => [
|
||||||
'brokers' => Env::get('QUEUE_KAFKA_BROKERS', 'kafka:9092'),
|
'brokers' => Env::get('QUEUE_KAFKA_BROKERS', 'kafka:9092'),
|
||||||
'consumerGroup' => Env::get('QUEUE_KAFKA_CONSUMER_GROUP', 'default_group'),
|
|
||||||
],
|
],
|
||||||
|
|
||||||
'rabbitmq' => [
|
'rabbitmq' => [
|
||||||
|
|||||||
@@ -5,12 +5,6 @@ volumes:
|
|||||||
services:
|
services:
|
||||||
|
|
||||||
traefik:
|
traefik:
|
||||||
labels:
|
|
||||||
- "traefik.enable=true"
|
|
||||||
- "traefik.http.routers.traefik.entrypoints=web-secure"
|
|
||||||
- "traefik.http.routers.traefik.rule=Host(`127.0.0.1`) && (PathPrefix(`/dashboard`) || PathPrefix(`/api`))"
|
|
||||||
- "traefik.http.routers.traefik.tls=true"
|
|
||||||
- "traefik.http.routers.traefik.service=api@internal"
|
|
||||||
image: traefik:latest
|
image: traefik:latest
|
||||||
container_name: traefik
|
container_name: traefik
|
||||||
healthcheck:
|
healthcheck:
|
||||||
@@ -21,20 +15,15 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
- "443:443"
|
- "443:443"
|
||||||
- "9001:9001"
|
|
||||||
volumes:
|
volumes:
|
||||||
- "/var/run/docker.sock:/var/run/docker.sock"
|
- "/var/run/docker.sock:/var/run/docker.sock"
|
||||||
- "./ssl:/etc/ssl"
|
|
||||||
restart: always
|
restart: always
|
||||||
command:
|
command:
|
||||||
- "--providers.docker=true"
|
- "--providers.docker=true"
|
||||||
- "--api.insecure=true"
|
|
||||||
- "--ping"
|
- "--ping"
|
||||||
- "--providers.file.filename=/etc/ssl/traefik.yml"
|
|
||||||
- "--providers.docker.exposedByDefault=false"
|
- "--providers.docker.exposedByDefault=false"
|
||||||
- "--entrypoints.web.address=:80"
|
- "--entrypoints.web.address=:80"
|
||||||
- "--entrypoints.web-secure.address=:443"
|
- "--entrypoints.web-secure.address=:443"
|
||||||
- "--entrypoints.grpc.address=:9001"
|
|
||||||
- "--accesslog=true"
|
- "--accesslog=true"
|
||||||
- "--entrypoints.web.http.redirections.entryPoint.to=web-secure"
|
- "--entrypoints.web.http.redirections.entryPoint.to=web-secure"
|
||||||
- "--entrypoints.web.http.redirections.entryPoint.scheme=https"
|
- "--entrypoints.web.http.redirections.entryPoint.scheme=https"
|
||||||
@@ -42,30 +31,16 @@ services:
|
|||||||
|
|
||||||
composer-runtime:
|
composer-runtime:
|
||||||
volumes:
|
volumes:
|
||||||
- ..:/app
|
- .:/app
|
||||||
image: siteworxpro/composer
|
image: siteworxpro/composer
|
||||||
entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
||||||
environment:
|
environment:
|
||||||
PHP_IDE_CONFIG: serverName=localhost
|
PHP_IDE_CONFIG: serverName=localhost
|
||||||
|
|
||||||
swagger-ui:
|
|
||||||
labels:
|
|
||||||
- "traefik.enable=true"
|
|
||||||
- "traefik.http.routers.swagger-ui.entrypoints=web-secure"
|
|
||||||
- "traefik.http.routers.swagger-ui.rule=Host(`localhost`) && PathPrefix(`/docs`)"
|
|
||||||
- "traefik.http.routers.swagger-ui.tls=true"
|
|
||||||
- "traefik.http.routers.swagger-ui.service=swagger-ui"
|
|
||||||
- "traefik.http.services.swagger-ui.loadbalancer.server.port=8080"
|
|
||||||
image: swaggerapi/swagger-ui:latest
|
|
||||||
container_name: swagger-ui
|
|
||||||
environment:
|
|
||||||
BASE_URL: /docs
|
|
||||||
URL: /.well-known/swagger.yaml
|
|
||||||
|
|
||||||
migration-container:
|
migration-container:
|
||||||
volumes:
|
volumes:
|
||||||
- ../db/migrations:/app/db/migrations
|
- ./db/migrations:/app/db/migrations
|
||||||
- ../bin:/app/bin
|
- ./bin:/app/bin
|
||||||
image: siteworxpro/migrate:v4.18.3
|
image: siteworxpro/migrate:v4.18.3
|
||||||
working_dir: /app
|
working_dir: /app
|
||||||
# entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
# entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
||||||
@@ -90,21 +65,12 @@ services:
|
|||||||
- "traefik.http.services.api.loadbalancer.healthcheck.path=/healthz"
|
- "traefik.http.services.api.loadbalancer.healthcheck.path=/healthz"
|
||||||
- "traefik.http.services.api.loadbalancer.healthcheck.interval=5s"
|
- "traefik.http.services.api.loadbalancer.healthcheck.interval=5s"
|
||||||
- "traefik.http.services.api.loadbalancer.healthcheck.timeout=60s"
|
- "traefik.http.services.api.loadbalancer.healthcheck.timeout=60s"
|
||||||
- "traefik.tcp.services.api.loadbalancer.server.port=9001"
|
|
||||||
- "traefik.http.services.api.loadbalancer.server.port=9501"
|
|
||||||
- "traefik.tcp.routers.grpc.entrypoints=grpc"
|
|
||||||
- "traefik.tcp.routers.grpc.rule=HostSNI(`localhost`) || HostSNI(`127.0.0.1`)"
|
|
||||||
- "traefik.tcp.routers.grpc.tls=true"
|
|
||||||
- "traefik.tcp.routers.grpc.service=api"
|
|
||||||
container_name: dev-runtime
|
|
||||||
volumes:
|
volumes:
|
||||||
- ..:/app
|
- .:/app
|
||||||
build:
|
build:
|
||||||
args:
|
args:
|
||||||
KAFKA_ENABLED: "0"
|
KAFKA_ENABLED: "1"
|
||||||
UID: 0
|
context: .
|
||||||
USER: root
|
|
||||||
context: ..
|
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -117,17 +83,12 @@ services:
|
|||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
environment:
|
environment:
|
||||||
JWT_ISSUER: https://auth.siteworxpro.com/application/o/postman/
|
|
||||||
JWT_AUDIENCE: 1RWyqJFlyA4hmsDzq6kSxs0LXvk7UgEAfgmBCpQ9
|
|
||||||
JWT_SIGNING_KEY: https://auth.siteworxpro.com/application/o/postman/.well-known/openid-configuration
|
|
||||||
QUEUE_BROKER: redis
|
|
||||||
PHP_IDE_CONFIG: serverName=localhost
|
PHP_IDE_CONFIG: serverName=localhost
|
||||||
WORKERS: 1
|
WORKERS: 1
|
||||||
GRPC_WORKERS: 1
|
|
||||||
DEBUG: 1
|
DEBUG: 1
|
||||||
REDIS_HOST: redis
|
REDIS_HOST: redis
|
||||||
DB_HOST: postgres
|
DB_HOST: postgres
|
||||||
DEV_MODE: 1
|
JWT_SIGNING_KEY: a-string-secret-at-least-256-bits-long
|
||||||
|
|
||||||
## Kafka and Zookeeper for local development
|
## Kafka and Zookeeper for local development
|
||||||
kafka-ui:
|
kafka-ui:
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
||||||
# NO CHECKED-IN PROTOBUF GENCODE
|
|
||||||
# source: protos/example.proto
|
|
||||||
|
|
||||||
namespace GRPC\GPBMetadata;
|
|
||||||
|
|
||||||
class Example
|
|
||||||
{
|
|
||||||
public static $is_initialized = false;
|
|
||||||
|
|
||||||
public static function initOnce() {
|
|
||||||
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
|
|
||||||
|
|
||||||
if (static::$is_initialized == true) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$pool->internalAddGeneratedFile(
|
|
||||||
"\x0A\xE5\x01\x0A\x14protos/example.proto\x12\x0Ahelloworld\"\x1C\x0A\x0CHelloRequest\x12\x0C\x0A\x04name\x18\x01 \x01(\x09\"\x1D\x0A\x0AHelloReply\x12\x0F\x0A\x07message\x18\x01 \x01(\x092I\x0A\x07Greeter\x12>\x0A\x08SayHello\x12\x18.helloworld.HelloRequest\x1A\x16.helloworld.HelloReply\"\x00B1Z\x0Dproto/greeter\xCA\x02\x0CGRPC\\Greeter\xE2\x02\x10GRPC\\GPBMetadatab\x06proto3"
|
|
||||||
, true);
|
|
||||||
|
|
||||||
static::$is_initialized = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
# Generated by the protocol buffer compiler (roadrunner-server/grpc). DO NOT EDIT!
|
|
||||||
# source: protos/example.proto
|
|
||||||
|
|
||||||
namespace GRPC\Greeter;
|
|
||||||
|
|
||||||
use Spiral\RoadRunner\GRPC;
|
|
||||||
|
|
||||||
interface GreeterInterface extends GRPC\ServiceInterface
|
|
||||||
{
|
|
||||||
// GRPC specific service name.
|
|
||||||
public const NAME = "helloworld.Greeter";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param GRPC\ContextInterface $ctx
|
|
||||||
* @param HelloRequest $in
|
|
||||||
* @return HelloReply
|
|
||||||
*
|
|
||||||
* @throws GRPC\Exception\InvokeException
|
|
||||||
*/
|
|
||||||
public function SayHello(GRPC\ContextInterface $ctx, HelloRequest $in): HelloReply;
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
<?php
|
|
||||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
||||||
# NO CHECKED-IN PROTOBUF GENCODE
|
|
||||||
# source: protos/example.proto
|
|
||||||
|
|
||||||
namespace GRPC\Greeter;
|
|
||||||
|
|
||||||
use Google\Protobuf\Internal\GPBType;
|
|
||||||
use Google\Protobuf\Internal\GPBUtil;
|
|
||||||
use Google\Protobuf\RepeatedField;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The response message containing the greetings
|
|
||||||
*
|
|
||||||
* Generated from protobuf message <code>helloworld.HelloReply</code>
|
|
||||||
*/
|
|
||||||
class HelloReply extends \Google\Protobuf\Internal\Message
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Generated from protobuf field <code>string message = 1;</code>
|
|
||||||
*/
|
|
||||||
protected $message = '';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param array $data {
|
|
||||||
* Optional. Data for populating the Message object.
|
|
||||||
*
|
|
||||||
* @type string $message
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
public function __construct($data = NULL) {
|
|
||||||
\GRPC\GPBMetadata\Example::initOnce();
|
|
||||||
parent::__construct($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generated from protobuf field <code>string message = 1;</code>
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getMessage()
|
|
||||||
{
|
|
||||||
return $this->message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generated from protobuf field <code>string message = 1;</code>
|
|
||||||
* @param string $var
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setMessage($var)
|
|
||||||
{
|
|
||||||
GPBUtil::checkString($var, True);
|
|
||||||
$this->message = $var;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
<?php
|
|
||||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
||||||
# NO CHECKED-IN PROTOBUF GENCODE
|
|
||||||
# source: protos/example.proto
|
|
||||||
|
|
||||||
namespace GRPC\Greeter;
|
|
||||||
|
|
||||||
use Google\Protobuf\Internal\GPBType;
|
|
||||||
use Google\Protobuf\Internal\GPBUtil;
|
|
||||||
use Google\Protobuf\RepeatedField;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The request message containing the user's name.
|
|
||||||
*
|
|
||||||
* Generated from protobuf message <code>helloworld.HelloRequest</code>
|
|
||||||
*/
|
|
||||||
class HelloRequest extends \Google\Protobuf\Internal\Message
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Generated from protobuf field <code>string name = 1;</code>
|
|
||||||
*/
|
|
||||||
protected $name = '';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param array $data {
|
|
||||||
* Optional. Data for populating the Message object.
|
|
||||||
*
|
|
||||||
* @type string $name
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
public function __construct($data = NULL) {
|
|
||||||
\GRPC\GPBMetadata\Example::initOnce();
|
|
||||||
parent::__construct($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generated from protobuf field <code>string name = 1;</code>
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getName()
|
|
||||||
{
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generated from protobuf field <code>string name = 1;</code>
|
|
||||||
* @param string $var
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setName($var)
|
|
||||||
{
|
|
||||||
GPBUtil::checkString($var, True);
|
|
||||||
$this->name = $var;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
### Note to Developers
|
|
||||||
Only generated files are allowed in this directory.
|
|
||||||
Please do not add any other files here manually.
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Siteworxpro\App\Grpc;
|
|
||||||
|
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
|
||||||
|
|
||||||
try {
|
|
||||||
$server = new Grpc();
|
|
||||||
exit($server->start());
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
echo $e->getMessage();
|
|
||||||
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
187
makefile
187
makefile
@@ -1,187 +0,0 @@
|
|||||||
# Makefile (enhanced)
|
|
||||||
SHELL := /bin/sh
|
|
||||||
.DEFAULT_GOAL := help
|
|
||||||
|
|
||||||
# Docker Compose file
|
|
||||||
COMPOSE_FILE := -f .dev/docker-compose.yml
|
|
||||||
|
|
||||||
# Reusable vars
|
|
||||||
DOCKER := docker compose $(COMPOSE_FILE)
|
|
||||||
COMPOSER_RUNTIME := composer-runtime
|
|
||||||
DEV_RUNTIME := dev-runtime
|
|
||||||
MIGRATION_CONTAINER := migration-container
|
|
||||||
|
|
||||||
PROTOC_GEN_DIR := ./protoc-gen-php-grpc-2025.1.5-darwin-arm64
|
|
||||||
PROTOC_GEN := $(PROTOC_GEN_DIR)/protoc-gen-php-grpc
|
|
||||||
PROTOC_URL := https://github.com/roadrunner-server/roadrunner/releases/download/v2025.1.5/protoc-gen-php-grpc-2025.1.5-darwin-arm64.tar.gz
|
|
||||||
|
|
||||||
DEV := $(DOCKER) exec $(DEV_RUNTIME) sh -c
|
|
||||||
COMPOSER := $(DOCKER) exec $(COMPOSER_RUNTIME) sh -c
|
|
||||||
|
|
||||||
# Colors
|
|
||||||
GREEN := \033[32m
|
|
||||||
YELLOW := \033[33m
|
|
||||||
RESET := \033[0m
|
|
||||||
|
|
||||||
# Fancy emoji
|
|
||||||
SPARK := ✨
|
|
||||||
ROCKET := 🚀
|
|
||||||
WARN := ⚠️
|
|
||||||
MAGNIFY := 🔍
|
|
||||||
BUG := 🐞
|
|
||||||
COMPOSE := 🐳
|
|
||||||
TRASH := 🧹
|
|
||||||
PROTO := 🧩
|
|
||||||
CHECK := ✅
|
|
||||||
CROSS := ❌
|
|
||||||
|
|
||||||
# Align width for help display
|
|
||||||
HELP_COL_WIDTH := 26
|
|
||||||
|
|
||||||
# Help: auto-generate from targets with "##" comments
|
|
||||||
help: ## Show this help
|
|
||||||
@echo "$(SPARK) Available commands:"
|
|
||||||
@awk -F':|##' '/^[a-zA-Z0-9._-]+:.*##/ {printf " %-$(HELP_COL_WIDTH)s - %s\n", $$1, $$3}' $(MAKEFILE_LIST) | sort
|
|
||||||
|
|
||||||
docker-build: ## Build Docker image
|
|
||||||
@push_flag=""; \
|
|
||||||
if [ -n "$(push)" ]; then \
|
|
||||||
push="--push"; \
|
|
||||||
fi
|
|
||||||
@if [ -z "$(image)" ]; then \
|
|
||||||
echo "image variable is required: make docker-build image=your-image-name tag=your"; \
|
|
||||||
exit 1; \
|
|
||||||
fi
|
|
||||||
@if [ -z "$(tag)" ]; then \
|
|
||||||
echo "tag variable is required: make docker-build image=your-image-name tag=your"; \
|
|
||||||
exit 1; \
|
|
||||||
fi
|
|
||||||
@platform_flag=""; \
|
|
||||||
if [ -n "$(platform)" ]; then \
|
|
||||||
platform_flag="--platform=$(platform)"; \
|
|
||||||
fi; \
|
|
||||||
printf "$(YELLOW)$(SPARK) Building Docker image: $(image):$(tag)$(RESET)\n"; \
|
|
||||||
docker buildx build $$platform_flag --provenance=true --sbom=true $$push_flag --tag $(image):$(tag) .
|
|
||||||
|
|
||||||
start: ## Start the development runtime container
|
|
||||||
@printf "$(GREEN)$(ROCKET) Starting $(DEV_RUNTIME)$(RESET)\n"
|
|
||||||
$(DOCKER) up $(DEV_RUNTIME) -d --no-recreate
|
|
||||||
|
|
||||||
sh: ## Open a shell in the development runtime container
|
|
||||||
@$(MAKE) start
|
|
||||||
$(DOCKER) exec $(DEV_RUNTIME) sh
|
|
||||||
|
|
||||||
run: ## Run the application server in the development runtime container
|
|
||||||
@$(MAKE) start
|
|
||||||
$(DEV) "rr serve"
|
|
||||||
|
|
||||||
stop: ## Stop and remove the development runtime container
|
|
||||||
@printf "$(YELLOW)$(WARN) Stopping all containers$(RESET)\n"
|
|
||||||
$(DOCKER) down
|
|
||||||
|
|
||||||
restart: ## Restart dev container (stop + start)
|
|
||||||
@$(MAKE) stop
|
|
||||||
@$(MAKE) start
|
|
||||||
|
|
||||||
rebuild: ## Rebuild containers (useful after Dockerfile changes)
|
|
||||||
@printf "$(YELLOW)$(SPARK) Rebuilding containers$(RESET)\n"
|
|
||||||
@$(MAKE) stop
|
|
||||||
@printf "$(YELLOW)$(TRASH) Deleting all Docker resources$(RESET)\n"
|
|
||||||
docker system prune --all --volumes --force
|
|
||||||
@$(MAKE) start
|
|
||||||
|
|
||||||
ps: ## Show docker compose ps
|
|
||||||
$(DOCKER) ps
|
|
||||||
|
|
||||||
migrate: ## Run database migrations in the migration container
|
|
||||||
$(DOCKER) up $(MIGRATION_CONTAINER)
|
|
||||||
|
|
||||||
# Composer helpers
|
|
||||||
composer-install: ## Install PHP dependencies in the composer runtime container
|
|
||||||
@printf "$(COMPOSE) $(GREEN)Installing PHP dependencies in $(COMPOSER_RUNTIME)$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer install --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs"
|
|
||||||
|
|
||||||
composer-install-no-dev: ## Install PHP dependencies without dev packages in the composer runtime container
|
|
||||||
@printf "$(COMPOSE) $(GREEN)Installing PHP dependencies (no-dev) in $(COMPOSER_RUNTIME)$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs"
|
|
||||||
|
|
||||||
composer-require: ## Require a PHP package in the composer runtime container (usage: make composer-require package=vendor/package)
|
|
||||||
ifndef package
|
|
||||||
$(error package variable is required: make composer-require package=vendor/package)
|
|
||||||
endif
|
|
||||||
@printf "$(COMPOSE) $(MAGNIFY) Requiring package $(package) in $(COMPOSER_RUNTIME)$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer require $(package) --ignore-platform-reqs"
|
|
||||||
|
|
||||||
composer-require-dev: ## Require a PHP package as dev in the composer runtime container (usage: make composer-require-dev package=vendor/package)
|
|
||||||
ifndef package
|
|
||||||
$(error package variable is required: make composer-require-dev package=vendor/package)
|
|
||||||
endif
|
|
||||||
@printf "$(COMPOSE) $(MAGNIFY) Requiring dev package $(package) in $(COMPOSER_RUNTIME)$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer require --dev $(package) --ignore-platform-reqs"
|
|
||||||
|
|
||||||
composer-update: ## Update PHP dependencies in the composer runtime container
|
|
||||||
@printf "$(COMPOSE) $(MAGNIFY) Updating PHP dependencies$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer update --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs"
|
|
||||||
|
|
||||||
enable-debug: ## Enable Xdebug in the development runtime container
|
|
||||||
@$(DOCKER) up $(DEV_RUNTIME) -d --no-recreate
|
|
||||||
@printf "$(GREEN)$(BUG) Enabling Xdebug in $(DEV_RUNTIME)$(RESET)\n"
|
|
||||||
$(DEV) "bin/xdebug.sh"
|
|
||||||
|
|
||||||
enable-coverage: ## Enable PCOV code coverage in the composer runtime container
|
|
||||||
@printf "$(GREEN)$(MAGNIFY) Enabling PCOV in $(COMPOSER_RUNTIME)$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "bin/pcov.sh"
|
|
||||||
|
|
||||||
protoc: ## Generate PHP gRPC code from .proto files
|
|
||||||
@printf "$(PROTO) $(GREEN)Setting up protoc-gen-php-grpc plugin$(RESET)\n"
|
|
||||||
@curl -LOs $(PROTOC_URL)
|
|
||||||
@tar -xzf protoc-gen-php-grpc-2025.1.5-darwin-arm64.tar.gz
|
|
||||||
@printf "$(PROTO) $(GREEN)Generating PHP gRPC code from .proto files$(RESET)\n"
|
|
||||||
@protoc --plugin=./protoc-gen-php-grpc-2025.1.5-darwin-arm64/protoc-gen-php-grpc \
|
|
||||||
--php_out=./generated \
|
|
||||||
--php-grpc_out=./generated \
|
|
||||||
protos/example.proto
|
|
||||||
@printf "$(TRASH) $(GREEN)Cleaning up protoc-gen-php-grpc plugin files$(RESET)\n"
|
|
||||||
@rm -rf $(PROTOC_GEN_DIR) protoc-gen-php-grpc-2025.1.5-darwin-arm64.tar.gz
|
|
||||||
|
|
||||||
license-check: ## Check license headers in source files
|
|
||||||
@printf "$(MAGNIFY) $(GREEN)Checking license headers$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer run-script tests:license || true"
|
|
||||||
|
|
||||||
# Developer tasks
|
|
||||||
lint: ## Run linting (phpcs/phpstan) in composer runtime
|
|
||||||
@printf "$(MAGNIFY) $(GREEN)Running linters$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer run-script tests:lint || true"
|
|
||||||
$(COMPOSER) "composer run-script tests:phpstan || true"
|
|
||||||
|
|
||||||
fmt: ## Format code (php-cs-fixer)
|
|
||||||
@printf "$(MAGNIFY) $(GREEN)Formatting code$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
|
|
||||||
$(COMPOSER) "composer run-script tests:lint:fix"
|
|
||||||
|
|
||||||
test: ## Run test suite (phpunit)
|
|
||||||
@printf "$(CHECK) $(GREEN)Running unit tests$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d
|
|
||||||
$(COMPOSER) "composer run-script tests:unit || true"
|
|
||||||
|
|
||||||
test-coverage: ## Run test suite with coverage report
|
|
||||||
@printf "$(CHECK) $(GREEN)Running unit tests with coverage report$(RESET)\n"
|
|
||||||
@$(DOCKER) up $(COMPOSER_RUNTIME) -d
|
|
||||||
@$(MAKE) enable-coverage
|
|
||||||
$(COMPOSER) "composer run-script tests:unit:coverage || true"
|
|
||||||
|
|
||||||
# Convenience aliases
|
|
||||||
dev: run ## Alias for start
|
|
||||||
ci: composer-install migrate license-check lint test ## CI-like local flow
|
|
||||||
down: stop ## Alias for stop
|
|
||||||
up: start ## Alias for start
|
|
||||||
|
|
||||||
.PHONY: help start sh run stop docker-build restart rebuild ps migrate composer-install composer-require composer-require-dev composer-update enable-debug enable-coverage protoc license-check lint fmt test test-coverage dev ci down up
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
syntax = "proto3";
|
|
||||||
|
|
||||||
option go_package = "proto/greeter";
|
|
||||||
option php_namespace = "GRPC\\Greeter";
|
|
||||||
option php_metadata_namespace = "GRPC\\GPBMetadata";
|
|
||||||
|
|
||||||
package helloworld;
|
|
||||||
|
|
||||||
// The greeting service definition.
|
|
||||||
service Greeter {
|
|
||||||
// Sends a greeting
|
|
||||||
rpc SayHello (HelloRequest) returns (HelloReply) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The request message containing the user's name.
|
|
||||||
message HelloRequest {
|
|
||||||
string name = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The response message containing the greetings
|
|
||||||
message HelloReply {
|
|
||||||
string message = 1;
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,10 @@ use Siteworxpro\App\Api;
|
|||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Instantiate the ExternalServer class
|
||||||
$server = new Api();
|
$server = new Api();
|
||||||
|
|
||||||
|
// Start the server
|
||||||
$server->startServer();
|
$server->startServer();
|
||||||
} catch (JsonException $e) {
|
} catch (JsonException $e) {
|
||||||
echo $e->getMessage();
|
echo $e->getMessage();
|
||||||
|
|||||||
24
src/Annotations/Async/HandlesMessage.php
Normal file
24
src/Annotations/Async/HandlesMessage.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Siteworxpro\App\Annotations\Async;
|
||||||
|
|
||||||
|
use Attribute;
|
||||||
|
|
||||||
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
||||||
|
readonly class HandlesMessage
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $messageClass,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMessageClass(): string
|
||||||
|
{
|
||||||
|
return $this->messageClass;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/Annotations/Events/ListensFor.php
Normal file
15
src/Annotations/Events/ListensFor.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Siteworxpro\App\Annotations\Events;
|
||||||
|
|
||||||
|
use Attribute;
|
||||||
|
|
||||||
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
||||||
|
readonly class ListensFor
|
||||||
|
{
|
||||||
|
public function __construct(public string $eventClass)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/Annotations/Guards/Jwt.php
Normal file
44
src/Annotations/Guards/Jwt.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Siteworxpro\App\Annotations\Guards;
|
||||||
|
|
||||||
|
use Attribute;
|
||||||
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
||||||
|
readonly class Jwt
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private string $issuer = '',
|
||||||
|
private string $audience = '',
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRequiredAudience(): string
|
||||||
|
{
|
||||||
|
return Config::get('jwt.audience') ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAudience(): string
|
||||||
|
{
|
||||||
|
if ($this->audience === '') {
|
||||||
|
return Config::get('jwt.audience') ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->audience;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIssuer(): string
|
||||||
|
{
|
||||||
|
if ($this->issuer === '') {
|
||||||
|
return Config::get('jwt.issuer') ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->issuer;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/Annotations/Guards/Scope.php
Normal file
21
src/Annotations/Guards/Scope.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Siteworxpro\App\Annotations\Guards;
|
||||||
|
|
||||||
|
use Attribute;
|
||||||
|
|
||||||
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
||||||
|
readonly class Scope
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private array $scopes = []
|
||||||
|
) {}
|
||||||
|
|
||||||
|
|
||||||
|
public function getScopes(): array
|
||||||
|
{
|
||||||
|
return $this->scopes;
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/Api.php
33
src/Api.php
@@ -6,20 +6,17 @@ namespace Siteworxpro\App;
|
|||||||
|
|
||||||
use League\Route\Http\Exception\MethodNotAllowedException;
|
use League\Route\Http\Exception\MethodNotAllowedException;
|
||||||
use League\Route\Http\Exception\NotFoundException;
|
use League\Route\Http\Exception\NotFoundException;
|
||||||
use League\Route\RouteGroup;
|
|
||||||
use League\Route\Router;
|
use League\Route\Router;
|
||||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||||
use Siteworxpro\App\Controllers\HealthcheckController;
|
use Siteworxpro\App\Controllers\HealthcheckController;
|
||||||
use Siteworxpro\App\Controllers\IndexController;
|
use Siteworxpro\App\Controllers\IndexController;
|
||||||
use Siteworxpro\App\Controllers\OpenApiController;
|
|
||||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||||
use Siteworxpro\App\Http\Middleware\CorsMiddleware;
|
use Siteworxpro\App\Http\Middleware\CorsMiddleware;
|
||||||
use Siteworxpro\App\Http\Middleware\JwtMiddleware;
|
use Siteworxpro\App\Http\Middleware\JwtMiddleware;
|
||||||
use Siteworxpro\App\Http\Middleware\ScopeMiddleware;
|
use Siteworxpro\App\Http\Middleware\ScopeMiddleware;
|
||||||
use Siteworxpro\App\Http\Responses\NotFoundResponse;
|
|
||||||
use Siteworxpro\App\Http\Responses\ServerErrorResponse;
|
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
use Siteworxpro\App\Services\Facades\Logger;
|
||||||
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
use Spiral\RoadRunner\Http\PSR7Worker;
|
use Spiral\RoadRunner\Http\PSR7Worker;
|
||||||
use Spiral\RoadRunner\Worker;
|
use Spiral\RoadRunner\Worker;
|
||||||
|
|
||||||
@@ -72,14 +69,8 @@ class Api
|
|||||||
|
|
||||||
$this->router = new Router();
|
$this->router = new Router();
|
||||||
$this->router->get('/', IndexController::class . '::get');
|
$this->router->get('/', IndexController::class . '::get');
|
||||||
$this->router->post('/', IndexController::class . '::post');
|
|
||||||
$this->router->get('/healthz', HealthcheckController::class . '::get');
|
$this->router->get('/healthz', HealthcheckController::class . '::get');
|
||||||
|
|
||||||
$this->router->group('/.well-known', function (RouteGroup $router) {
|
|
||||||
$router->get('/swagger.yaml', OpenApiController::class . '::get');
|
|
||||||
$router->get('/swagger.json', OpenApiController::class . '::get');
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->router->middleware(new CorsMiddleware());
|
$this->router->middleware(new CorsMiddleware());
|
||||||
$this->router->middleware(new JwtMiddleware());
|
$this->router->middleware(new JwtMiddleware());
|
||||||
$this->router->middleware(new ScopeMiddleware());
|
$this->router->middleware(new ScopeMiddleware());
|
||||||
@@ -113,20 +104,28 @@ class Api
|
|||||||
$response = $this->router->handle($request);
|
$response = $this->router->handle($request);
|
||||||
$this->worker->respond($response);
|
$this->worker->respond($response);
|
||||||
} catch (MethodNotAllowedException | NotFoundException) {
|
} catch (MethodNotAllowedException | NotFoundException) {
|
||||||
$uri = '';
|
|
||||||
if (isset($request)) {
|
|
||||||
$uri = $request->getUri()->getPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->worker->respond(
|
$this->worker->respond(
|
||||||
JsonResponseFactory::createJsonResponse(new NotFoundResponse($uri))
|
JsonResponseFactory::createJsonResponse(
|
||||||
|
['status_code' => 404, 'reason_phrase' => 'Not Found'],
|
||||||
|
CodesEnum::NOT_FOUND
|
||||||
|
)
|
||||||
);
|
);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
Logger::error($e->getMessage());
|
Logger::error($e->getMessage());
|
||||||
Logger::error($e->getTraceAsString());
|
Logger::error($e->getTraceAsString());
|
||||||
|
|
||||||
|
$json = ['status_code' => 500, 'reason_phrase' => 'Server Error'];
|
||||||
|
if (Config::get("server.dev_mode")) {
|
||||||
|
$json = [
|
||||||
|
'status_code' => 500,
|
||||||
|
'reason_phrase' => 'Server Error',
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
'trace' => $e->getTraceAsString(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$this->worker->respond(
|
$this->worker->respond(
|
||||||
JsonResponseFactory::createJsonResponse(new ServerErrorResponse($e))
|
JsonResponseFactory::createJsonResponse($json, CodesEnum::INTERNAL_SERVER_ERROR)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,12 +24,12 @@ class Kafka extends Broker
|
|||||||
|
|
||||||
$conf = new Conf();
|
$conf = new Conf();
|
||||||
$conf->set('bootstrap.servers', $config['brokers'] ?? 'localhost:9092');
|
$conf->set('bootstrap.servers', $config['brokers'] ?? 'localhost:9092');
|
||||||
|
|
||||||
$this->producer = new Producer($conf);
|
$this->producer = new Producer($conf);
|
||||||
$this->producer->addBrokers($config['brokers'] ?? 'localhost:9092');
|
$this->producer->addBrokers($config['brokers'] ?? 'localhost:9092');
|
||||||
|
|
||||||
$conf->set('group.id', $config['consumerGroup'] ?? 'default');
|
$conf->set('group.id', $config['consumerGroup'] ?? 'default');
|
||||||
$conf->set('auto.offset.reset', 'earliest');
|
$conf->set('auto.offset.reset', 'earliest');
|
||||||
|
|
||||||
$this->consumer = new KafkaConsumer($conf);
|
$this->consumer = new KafkaConsumer($conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,14 +60,6 @@ class Kafka extends Broker
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($kafkaMessage->err === RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART) {
|
|
||||||
throw new \RuntimeException(
|
|
||||||
"Topic '{$queue->queueName()}' or partition does not exist. Kafka does not auto-create topics" .
|
|
||||||
" unless configured to do so."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @var string | null $messageData */
|
|
||||||
$messageData = $kafkaMessage->payload;
|
$messageData = $kafkaMessage->payload;
|
||||||
if ($messageData !== null) {
|
if ($messageData !== null) {
|
||||||
/** @var Message $message */
|
/** @var Message $message */
|
||||||
|
|||||||
@@ -4,97 +4,88 @@ declare(ticks=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Async;
|
namespace Siteworxpro\App\Async;
|
||||||
|
|
||||||
use Siteworxpro\App\Attributes\Async\HandlesMessage;
|
use Siteworxpro\App\Annotations\Async\HandlesMessage;
|
||||||
use Siteworxpro\App\Async\Messages\Message;
|
|
||||||
use Siteworxpro\App\Async\Queues\Queue;
|
use Siteworxpro\App\Async\Queues\Queue;
|
||||||
use Siteworxpro\App\Services\Facades\Broker;
|
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
use Siteworxpro\App\Services\Facades\Logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Long-running process that listens to queues, pops messages, and dispatches them to handlers.
|
|
||||||
*/
|
|
||||||
class Consumer
|
class Consumer
|
||||||
{
|
{
|
||||||
private static bool $shutDown = false;
|
private static bool $shutDown = false;
|
||||||
|
|
||||||
/** @var array<string,string> */
|
|
||||||
private const array QUEUES = [
|
private const array QUEUES = [
|
||||||
'default' => Queues\DefaultQueue::class,
|
'default' => Queues\DefaultQueue::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var Queue[] */
|
|
||||||
private array $queues = [];
|
private array $queues = [];
|
||||||
|
|
||||||
/** @var array<string, string[]> message FQCN => handler FQCNs */
|
|
||||||
private array $handlers = [];
|
private array $handlers = [];
|
||||||
|
|
||||||
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\';
|
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\';
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string[] $queues Optional list of queue names (keys from self::QUEUES)
|
|
||||||
*/
|
|
||||||
public function __construct(array $queues = [])
|
public function __construct(array $queues = [])
|
||||||
{
|
{
|
||||||
$queueClasses = $queues === []
|
if ($queues === []) {
|
||||||
? array_values(self::QUEUES)
|
$queues = self::QUEUES;
|
||||||
: array_map(
|
} else {
|
||||||
static function (string $name): string {
|
$mappedQueues = [];
|
||||||
if (!isset(self::QUEUES[$name])) {
|
foreach ($queues as $queueName) {
|
||||||
throw new \InvalidArgumentException("Queue '$name' is not defined.");
|
if (isset(self::QUEUES[$queueName])) {
|
||||||
|
$mappedQueues[] = self::QUEUES[$queueName];
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException("Queue '$queueName' is not defined.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$queues = $mappedQueues;
|
||||||
}
|
}
|
||||||
return self::QUEUES[$name];
|
|
||||||
},
|
|
||||||
$queues
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($queueClasses as $class) {
|
|
||||||
$this->queues[] = new $class();
|
foreach ($queues as $queueClass) {
|
||||||
|
$this->queues[] = new $queueClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->registerHandlers();
|
$this->registerHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Discover handler classes under `Handlers` and register them via HandlesMessage attributes.
|
|
||||||
*/
|
|
||||||
private function registerHandlers(): void
|
private function registerHandlers(): void
|
||||||
{
|
{
|
||||||
$it = new \RecursiveIteratorIterator(
|
$recursiveIterator = new \RecursiveIteratorIterator(
|
||||||
new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/')
|
new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/')
|
||||||
);
|
);
|
||||||
|
|
||||||
/** @var \SplFileInfo $file */
|
foreach ($recursiveIterator as $file) {
|
||||||
foreach ($it as $file) {
|
if ($file->isFile() && $file->getExtension() === 'php') {
|
||||||
if (!$file->isFile() || $file->getExtension() !== 'php') {
|
$relativePath = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname());
|
||||||
continue;
|
$className = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relativePath, 0, -4));
|
||||||
|
|
||||||
|
|
||||||
|
if (class_exists($className)) {
|
||||||
|
$reflection = new \ReflectionClass($className);
|
||||||
|
$attributes = $reflection->getAttributes(HandlesMessage::class);
|
||||||
|
foreach ($attributes as $attribute) {
|
||||||
|
$instance = $attribute->newInstance();
|
||||||
|
$messageClass = $instance->getMessageClass();
|
||||||
|
$this->handlers[$messageClass][] = $className;
|
||||||
}
|
}
|
||||||
|
|
||||||
$relative = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname());
|
|
||||||
$class = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relative, 0, -4));
|
|
||||||
|
|
||||||
if (!class_exists($class)) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$ref = new \ReflectionClass($class);
|
|
||||||
foreach ($ref->getAttributes(HandlesMessage::class) as $attr) {
|
|
||||||
$messageClass = $attr->newInstance()->getMessageClass();
|
|
||||||
$this->handlers[$messageClass][] = $class;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signal handler used to initiate graceful or immediate shutdown.
|
* @param $signal
|
||||||
*/
|
*/
|
||||||
public static function handleSignal(int $signal): void
|
public static function handleSignal($signal): void
|
||||||
{
|
{
|
||||||
switch ($signal) {
|
switch ($signal) {
|
||||||
|
// Graceful
|
||||||
case SIGINT:
|
case SIGINT:
|
||||||
case SIGTERM:
|
case SIGTERM:
|
||||||
case SIGHUP:
|
case SIGHUP:
|
||||||
self::$shutDown = true;
|
self::$shutDown = true;
|
||||||
return;
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Not Graceful
|
||||||
case SIGKILL:
|
case SIGKILL:
|
||||||
exit(9);
|
exit(9);
|
||||||
}
|
}
|
||||||
@@ -105,9 +96,6 @@ class Consumer
|
|||||||
return self::$shutDown;
|
return self::$shutDown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Start the consumer main loop.
|
|
||||||
*/
|
|
||||||
public function start(): void
|
public function start(): void
|
||||||
{
|
{
|
||||||
if (!\function_exists('pcntl_signal')) {
|
if (!\function_exists('pcntl_signal')) {
|
||||||
@@ -115,11 +103,10 @@ class Consumer
|
|||||||
}
|
}
|
||||||
|
|
||||||
Logger::info('Starting queue consumer...');
|
Logger::info('Starting queue consumer...');
|
||||||
Logger::info('Using Broker: ' . Broker::getFacadeRoot()::class);
|
|
||||||
|
|
||||||
foreach ([SIGINT, SIGTERM, SIGHUP] as $sig) {
|
\pcntl_signal(SIGINT, [self::class, 'handleSignal']);
|
||||||
\pcntl_signal($sig, [self::class, 'handleSignal']);
|
\pcntl_signal(SIGTERM, [self::class, 'handleSignal']);
|
||||||
}
|
\pcntl_signal(SIGHUP, [self::class, 'handleSignal']);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if ($this->shouldShutDown()) {
|
if ($this->shouldShutDown()) {
|
||||||
@@ -131,43 +118,40 @@ class Consumer
|
|||||||
foreach ($this->queues as $queue) {
|
foreach ($this->queues as $queue) {
|
||||||
Logger::info('Listening to queue: ' . $queue->queueName());
|
Logger::info('Listening to queue: ' . $queue->queueName());
|
||||||
$message = $queue->pop();
|
$message = $queue->pop();
|
||||||
if (!$message) {
|
if ($message) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::info('Processing message of type: ' . get_class($message));
|
Logger::info('Processing message of type: ' . get_class($message));
|
||||||
|
|
||||||
foreach ($this->getHandlersForMessage($message) as $handler) {
|
$handlers = $this->getHandlerForMessage($message);
|
||||||
|
|
||||||
|
foreach ($handlers as $handler) {
|
||||||
$handler($message);
|
$handler($message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Continue polling from the top of the loop after processing a message.
|
|
||||||
continue 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid busy-looping when no messages are available.
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getHandlerForMessage($message): array
|
||||||
* @return callable[] Handler instances invokable with the message
|
|
||||||
*/
|
|
||||||
private function getHandlersForMessage(Message $message): array
|
|
||||||
{
|
{
|
||||||
$messageClass = get_class($message);
|
|
||||||
|
|
||||||
if (!isset($this->handlers[$messageClass])) {
|
|
||||||
throw new \RuntimeException("No handler found for message class: $messageClass");
|
|
||||||
}
|
|
||||||
|
|
||||||
$callables = [];
|
$callables = [];
|
||||||
foreach ($this->handlers[$messageClass] as $handlerClass) {
|
|
||||||
|
$messageClass = get_class($message);
|
||||||
|
if (isset($this->handlers[$messageClass])) {
|
||||||
|
$handlerClasses = $this->handlers[$messageClass];
|
||||||
|
|
||||||
|
foreach ($handlerClasses as $handlerClass) {
|
||||||
if (class_exists($handlerClass)) {
|
if (class_exists($handlerClass)) {
|
||||||
$callables[] = new $handlerClass();
|
$handlerInstance = new $handlerClass();
|
||||||
|
|
||||||
|
$callables[] = $handlerInstance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $callables;
|
return $callables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new \RuntimeException("No handler found for message class: $messageClass");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Async\Handlers;
|
namespace Siteworxpro\App\Async\Handlers;
|
||||||
|
|
||||||
use Siteworxpro\App\Attributes\Async\HandlesMessage;
|
use Siteworxpro\App\Annotations\Async\HandlesMessage;
|
||||||
use Siteworxpro\App\Async\Messages\Message;
|
use Siteworxpro\App\Async\Messages\Message;
|
||||||
use Siteworxpro\App\Async\Messages\SayHelloMessage;
|
use Siteworxpro\App\Async\Messages\SayHelloMessage;
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
use Siteworxpro\App\Services\Facades\Logger;
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Attributes\Async;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute to mark a class as a handler for a specific message class in an async workflow.
|
|
||||||
*
|
|
||||||
* Repeatable: attach multiple times to handle multiple message classes.
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
|
||||||
readonly class HandlesMessage
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Create a new HandlesMessage attribute.
|
|
||||||
*
|
|
||||||
* @param class-string $messageClass Fully-qualified class name of the message handled.
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
public string $messageClass,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the fully-qualified message class this handler processes.
|
|
||||||
*
|
|
||||||
* @return class-string
|
|
||||||
*/
|
|
||||||
public function getMessageClass(): string
|
|
||||||
{
|
|
||||||
return $this->messageClass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Attributes\CommandBus;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class HandlesCommand
|
|
||||||
* @package Siteworxpro\App\Attributes\CommandBus
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS)]
|
|
||||||
readonly class HandlesCommand
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param class-string $commandClass
|
|
||||||
*/
|
|
||||||
public function __construct(public string $commandClass)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Attributes\Events;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute to mark a class as an event listener for a specific event class.
|
|
||||||
*
|
|
||||||
* Apply this attribute to classes that subscribe to domain or application events.
|
|
||||||
* Repeatable: can be attached multiple times to the same class to listen for multiple events.
|
|
||||||
*
|
|
||||||
* Targets: class only.
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
|
||||||
readonly class ListensFor
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Initialize the ListensFor attribute.
|
|
||||||
*
|
|
||||||
* @param class-string $eventClass Fully-qualified class name of the event to listen for.
|
|
||||||
*/
|
|
||||||
public function __construct(public string $eventClass)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Attributes\Guards;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute to guard classes or methods with JWT claim requirements.
|
|
||||||
*
|
|
||||||
* Apply this attribute to a class or method to declare the expected JWT issuer and/or audience.
|
|
||||||
* If either the issuer or audience is an empty string, the value will be resolved from configuration:
|
|
||||||
* - `jwt.issuer`
|
|
||||||
* - `jwt.audience`
|
|
||||||
*
|
|
||||||
* Targets: class or method.
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
|
||||||
readonly class Jwt
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Initialize the Jwt attribute with optional overrides for expected JWT claims.
|
|
||||||
*
|
|
||||||
* @param string $issuer Optional expected JWT issuer (`iss`). Empty string uses `Config::get('jwt.issuer')`.
|
|
||||||
* @param string $audience Optional expected JWT audience (`aud`). Empty string uses `Config::get('jwt.audience')`.
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
private string $issuer = '',
|
|
||||||
private string $audience = '',
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the expected audience for validation.
|
|
||||||
*
|
|
||||||
* Returns the constructor-provided audience when non-empty; otherwise falls back to `jwt.audience` config.
|
|
||||||
*
|
|
||||||
* @return string The audience value to enforce.
|
|
||||||
*/
|
|
||||||
public function getAudience(): string
|
|
||||||
{
|
|
||||||
if ($this->audience === '') {
|
|
||||||
return Config::get('jwt.audience') ?? '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->audience;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the expected issuer for validation.
|
|
||||||
*
|
|
||||||
* Returns the constructor-provided issuer when non-empty; otherwise falls back to `jwt.issuer` config.
|
|
||||||
*
|
|
||||||
* @return string The issuer value to enforce.
|
|
||||||
*/
|
|
||||||
public function getIssuer(): string
|
|
||||||
{
|
|
||||||
if ($this->issuer === '') {
|
|
||||||
return Config::get('jwt.issuer') ?? '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->issuer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Attributes\Guards;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
|
||||||
readonly class RequireAllScopes
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Attributes\Guards;
|
|
||||||
|
|
||||||
use Attribute;
|
|
||||||
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
|
||||||
readonly class Scope
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param array<int, string> $scopes the required scopes
|
|
||||||
* @param string $claim the claim to check for scopes
|
|
||||||
* @param string $separator the separator used to split scopes in the claim
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
private array $scopes = [],
|
|
||||||
private string $claim = 'scope',
|
|
||||||
private string $separator = ' '
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getScopes(): array
|
|
||||||
{
|
|
||||||
return $this->scopes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getClaim(): string
|
|
||||||
{
|
|
||||||
return $this->claim;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSeparator(): string
|
|
||||||
{
|
|
||||||
return $this->separator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,8 +7,6 @@ namespace Siteworxpro\App\Cli;
|
|||||||
use Ahc\Cli\Application;
|
use Ahc\Cli\Application;
|
||||||
use Siteworxpro\App\Cli\Commands\DemoCommand;
|
use Siteworxpro\App\Cli\Commands\DemoCommand;
|
||||||
use Siteworxpro\App\Cli\Commands\Queue\Start;
|
use Siteworxpro\App\Cli\Commands\Queue\Start;
|
||||||
use Siteworxpro\App\Cli\Commands\Queue\TestJob;
|
|
||||||
use Siteworxpro\App\Helpers\Version;
|
|
||||||
use Siteworxpro\App\Kernel;
|
use Siteworxpro\App\Kernel;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
@@ -22,11 +20,10 @@ class App
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
Kernel::boot();
|
Kernel::boot();
|
||||||
$this->app = new Application('Php-Template', Version::VERSION);
|
$this->app = new Application('Php-Template', Config::get('app.version') ?? 'dev-master');
|
||||||
|
|
||||||
$this->app->add(new DemoCommand());
|
$this->app->add(new DemoCommand());
|
||||||
$this->app->add(new Start());
|
$this->app->add(new Start());
|
||||||
$this->app->add(new TestJob());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(): int
|
public function run(): int
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace Siteworxpro\App\Cli\Commands;
|
namespace Siteworxpro\App\Cli\Commands;
|
||||||
|
|
||||||
use Ahc\Cli\Input\Command;
|
use Ahc\Cli\Input\Command;
|
||||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
|
||||||
use Siteworxpro\App\Services\Facades\CommandBus;
|
|
||||||
|
|
||||||
class DemoCommand extends Command implements CommandInterface
|
class DemoCommand extends Command implements CommandInterface
|
||||||
{
|
{
|
||||||
@@ -30,14 +28,18 @@ class DemoCommand extends Command implements CommandInterface
|
|||||||
$pb->finish();
|
$pb->finish();
|
||||||
|
|
||||||
$this->writer()->boldBlue("Demo Command Executed!\n");
|
$this->writer()->boldBlue("Demo Command Executed!\n");
|
||||||
|
|
||||||
|
if ($this->values()['name']) {
|
||||||
$name = $this->values()['name'];
|
$name = $this->values()['name'];
|
||||||
$greet = $this->values()['greet'] ?? false;
|
$greet = $this->values()['greet'] ?? false;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if ($greet) {
|
if ($greet) {
|
||||||
$this->writer()->green("Hello, $name! Welcome to the CLI demo.\n");
|
$this->writer()->green("Hello, $name! Welcome to the CLI demo.\n");
|
||||||
} else {
|
} else {
|
||||||
$exampleCommand = new ExampleCommand($name);
|
$this->writer()->yellow("Name provided: {$name}\n");
|
||||||
$this->writer()->yellow(CommandBus::handle($exampleCommand));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ class Start extends Command implements CommandInterface
|
|||||||
$queues = explode(',', $this->values()['queues']);
|
$queues = explode(',', $this->values()['queues']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SayHelloMessage::dispatch("hello from queue consumer!");
|
||||||
|
|
||||||
$consumer = new Consumer($queues);
|
$consumer = new Consumer($queues);
|
||||||
$consumer->start();
|
$consumer->start();
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Cli\Commands\Queue;
|
|
||||||
|
|
||||||
use Ahc\Cli\Input\Command;
|
|
||||||
use Siteworxpro\App\Async\Messages\SayHelloMessage;
|
|
||||||
use Siteworxpro\App\Cli\Commands\CommandInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class TestJob
|
|
||||||
*
|
|
||||||
* A CLI command to schedule a demo job that dispatches a SayHelloMessage.
|
|
||||||
*/
|
|
||||||
class TestJob extends Command implements CommandInterface
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct('queue:demo', 'Schedule a demo job.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the command to dispatch a SayHelloMessage.
|
|
||||||
*
|
|
||||||
* @return int Exit code
|
|
||||||
*/
|
|
||||||
public function execute(): int
|
|
||||||
{
|
|
||||||
SayHelloMessage::dispatch('World from TestJob Command!');
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\CommandBus;
|
|
||||||
|
|
||||||
use League\Tactician\Exception\CanNotInvokeHandlerException;
|
|
||||||
use League\Tactician\Handler\Locator\HandlerLocator;
|
|
||||||
use Siteworxpro\App\Attributes\CommandBus\HandlesCommand;
|
|
||||||
|
|
||||||
class AttributeLocator implements HandlerLocator
|
|
||||||
{
|
|
||||||
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\CommandBus\\Handlers\\';
|
|
||||||
|
|
||||||
private array $handlers;
|
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$directory = __DIR__ . '/Handlers';
|
|
||||||
$files = scandir($directory);
|
|
||||||
foreach ($files as $file) {
|
|
||||||
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
|
|
||||||
$className = pathinfo($file, PATHINFO_FILENAME);
|
|
||||||
$fullClassName = self::HANDLER_NAMESPACE . $className;
|
|
||||||
|
|
||||||
if (class_exists($fullClassName)) {
|
|
||||||
$reflectionClass = new \ReflectionClass($fullClassName);
|
|
||||||
$attributes = $reflectionClass->getAttributes(HandlesCommand::class);
|
|
||||||
|
|
||||||
foreach ($attributes as $attribute) {
|
|
||||||
$instance = $attribute->newInstance();
|
|
||||||
$commandClass = $instance->commandClass;
|
|
||||||
$this->handlers[$commandClass] = $fullClassName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getHandlerForCommand($commandName)
|
|
||||||
{
|
|
||||||
if (isset($this->handlers[$commandName])) {
|
|
||||||
$handlerClass = $this->handlers[$commandName];
|
|
||||||
return new $handlerClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new CanNotInvokeHandlerException("No handler found for command: " . $commandName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\CommandBus\Commands;
|
|
||||||
|
|
||||||
readonly abstract class Command
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\CommandBus\Commands;
|
|
||||||
|
|
||||||
readonly class ExampleCommand extends Command
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private string $name
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getName(): string
|
|
||||||
{
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\CommandBus\Handlers;
|
|
||||||
|
|
||||||
abstract class CommandHandler implements CommandHandlerInterface
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\CommandBus\Handlers;
|
|
||||||
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\Command;
|
|
||||||
|
|
||||||
interface CommandHandlerInterface
|
|
||||||
{
|
|
||||||
public function __invoke(Command $command): mixed;
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\CommandBus\Handlers;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Attributes\CommandBus\HandlesCommand;
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\Command;
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
|
||||||
|
|
||||||
#[HandlesCommand(ExampleCommand::class)]
|
|
||||||
class ExampleHandler extends CommandHandler
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param Command|ExampleCommand $command
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function __invoke(Command|ExampleCommand $command): string
|
|
||||||
{
|
|
||||||
if (!method_exists($command, 'getName')) {
|
|
||||||
throw new \TypeError('Invalid command type provided to ExampleHandler.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$name = $command->getName();
|
|
||||||
Logger::info('Handling ExampleCommand for name: ' . $name);
|
|
||||||
|
|
||||||
return 'Hello, ' . $name . '!';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,29 +6,8 @@ namespace Siteworxpro\App\Controllers;
|
|||||||
|
|
||||||
use League\Route\Http\Exception\NotFoundException;
|
use League\Route\Http\Exception\NotFoundException;
|
||||||
use Nyholm\Psr7\ServerRequest;
|
use Nyholm\Psr7\ServerRequest;
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Siteworxpro\App\Helpers\Version;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Controller
|
|
||||||
*
|
|
||||||
* An abstract base controller providing default implementations for HTTP methods.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Controllers
|
|
||||||
*/
|
|
||||||
#[OA\Info(
|
|
||||||
version: Version::VERSION,
|
|
||||||
description: "This is a template API built using Siteworxpro framework.",
|
|
||||||
title: "Siteworxpro Template API",
|
|
||||||
contact: new OA\Contact(
|
|
||||||
name: "Siteworxpro",
|
|
||||||
url: "https://www.siteworxpro.com",
|
|
||||||
email: "support@siteworxpro.com"
|
|
||||||
),
|
|
||||||
license: new OA\License('MIT', 'https://opensource.org/licenses/MIT')
|
|
||||||
)]
|
|
||||||
#[OA\Server(url: "https://localhost", description: "Local Server")]
|
|
||||||
abstract class Controller implements ControllerInterface
|
abstract class Controller implements ControllerInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,11 +7,6 @@ namespace Siteworxpro\App\Controllers;
|
|||||||
use Nyholm\Psr7\ServerRequest;
|
use Nyholm\Psr7\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface ControllerInterface
|
|
||||||
*
|
|
||||||
* Defines the contract for handling HTTP requests in a controller.
|
|
||||||
*/
|
|
||||||
interface ControllerInterface
|
interface ControllerInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,31 +8,15 @@ use Illuminate\Database\PostgresConnection;
|
|||||||
use Nyholm\Psr7\ServerRequest;
|
use Nyholm\Psr7\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||||
use Siteworxpro\App\Http\Responses\GenericResponse;
|
|
||||||
use Siteworxpro\App\Http\Responses\ServerErrorResponse;
|
|
||||||
use Siteworxpro\App\Models\Model;
|
use Siteworxpro\App\Models\Model;
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
|
||||||
use Siteworxpro\App\Services\Facades\Redis;
|
use Siteworxpro\App\Services\Facades\Redis;
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class HealthcheckController
|
|
||||||
*
|
|
||||||
* Handles health check requests to verify database and cache connectivity.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Controllers
|
|
||||||
*/
|
|
||||||
class HealthcheckController extends Controller
|
class HealthcheckController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Handles the GET request for health check.
|
|
||||||
*
|
|
||||||
* @throws \JsonException
|
* @throws \JsonException
|
||||||
*/
|
*/
|
||||||
#[OA\Get(path: '/healthz', tags: ['Healthcheck'])]
|
|
||||||
#[OA\Response(response: '200', description: 'Healthcheck OK')]
|
|
||||||
#[OA\Response(response: '503', description: 'Healthcheck Failed')]
|
|
||||||
public function get(ServerRequest $request): ResponseInterface
|
public function get(ServerRequest $request): ResponseInterface
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@@ -45,19 +29,18 @@ class HealthcheckController extends Controller
|
|||||||
throw new \Exception('Redis ping failed');
|
throw new \Exception('Redis ping failed');
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Logger::emergency(
|
|
||||||
'Healthcheck failed: ' . $e->getMessage(),
|
|
||||||
['exception' => $e]
|
|
||||||
);
|
|
||||||
|
|
||||||
return JsonResponseFactory::createJsonResponse(
|
return JsonResponseFactory::createJsonResponse(
|
||||||
new ServerErrorResponse($e),
|
[
|
||||||
|
'status_code' => CodesEnum::SERVICE_UNAVAILABLE->value,
|
||||||
|
'message' => 'Healthcheck Failed',
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
],
|
||||||
CodesEnum::SERVICE_UNAVAILABLE
|
CodesEnum::SERVICE_UNAVAILABLE
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonResponseFactory::createJsonResponse(
|
return JsonResponseFactory::createJsonResponse(
|
||||||
new GenericResponse('Healthcheck OK')
|
['status_code' => 200, 'message' => 'Healthcheck OK']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,8 @@ namespace Siteworxpro\App\Controllers;
|
|||||||
|
|
||||||
use Nyholm\Psr7\ServerRequest;
|
use Nyholm\Psr7\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Siteworxpro\App\Attributes\Guards;
|
use Siteworxpro\App\Annotations\Guards;
|
||||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
|
||||||
use Siteworxpro\App\Docs\TokenSecurity;
|
|
||||||
use Siteworxpro\App\Docs\UnauthorizedResponse;
|
|
||||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
use Siteworxpro\App\Http\Responses\GenericResponse;
|
|
||||||
use Siteworxpro\App\Services\Facades\CommandBus;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class IndexController
|
* Class IndexController
|
||||||
@@ -28,39 +22,19 @@ class IndexController extends Controller
|
|||||||
* @throws \JsonException
|
* @throws \JsonException
|
||||||
*/
|
*/
|
||||||
#[Guards\Jwt]
|
#[Guards\Jwt]
|
||||||
#[Guards\Scope(['get.index', 'status.check'])]
|
#[Guards\Scope(['get.index'])]
|
||||||
#[Guards\RequireAllScopes]
|
|
||||||
#[OA\Get(path: '/', security: [new TokenSecurity()], tags: ['Examples'])]
|
|
||||||
#[OA\Response(
|
|
||||||
response: '200',
|
|
||||||
description: 'An Example Response',
|
|
||||||
content: new OA\JsonContent(ref: '#/components/schemas/GenericResponse')
|
|
||||||
)]
|
|
||||||
#[UnauthorizedResponse]
|
|
||||||
public function get(ServerRequest $request): ResponseInterface
|
public function get(ServerRequest $request): ResponseInterface
|
||||||
{
|
{
|
||||||
$command = new ExampleCommand($request->getQueryParams()['name'] ?? 'Guest');
|
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
||||||
$greeting = CommandBus::handle($command);
|
|
||||||
|
|
||||||
return JsonResponseFactory::createJsonResponse(new GenericResponse('Server is running. ' . $greeting));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the POST request for the index route.
|
|
||||||
*
|
|
||||||
* @throws \JsonException
|
* @throws \JsonException
|
||||||
*/
|
*/
|
||||||
#[Guards\Jwt]
|
#[Guards\Jwt]
|
||||||
#[Guards\Scope(['post.index'])]
|
#[Guards\Scope(['post.index'])]
|
||||||
#[OA\Post(path: '/', security: [new TokenSecurity()], tags: ['Examples'])]
|
|
||||||
#[OA\Response(
|
|
||||||
response: '200',
|
|
||||||
description: 'An Example Response',
|
|
||||||
content: new OA\JsonContent(ref: '#/components/schemas/GenericResponse')
|
|
||||||
)]
|
|
||||||
#[UnauthorizedResponse]
|
|
||||||
public function post(ServerRequest $request): ResponseInterface
|
public function post(ServerRequest $request): ResponseInterface
|
||||||
{
|
{
|
||||||
return JsonResponseFactory::createJsonResponse(new GenericResponse('POST request received'));
|
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Controllers;
|
|
||||||
|
|
||||||
use Nyholm\Psr7\Response;
|
|
||||||
use Nyholm\Psr7\ServerRequest;
|
|
||||||
use OpenApi\Generator;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
class OpenApiController extends Controller
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Handles the GET request to generate and return the OpenAPI specification.
|
|
||||||
*
|
|
||||||
* @param ServerRequest $request
|
|
||||||
* @return ResponseInterface
|
|
||||||
*/
|
|
||||||
public function get(ServerRequest $request): ResponseInterface
|
|
||||||
{
|
|
||||||
$openapi = new Generator()->generate([
|
|
||||||
__DIR__ . '/../Controllers',
|
|
||||||
__DIR__ . '/../Models',
|
|
||||||
__DIR__ . '/../Http/Responses',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response = new Response();
|
|
||||||
|
|
||||||
if (
|
|
||||||
$request->getHeaderLine('Accept') === 'application/json' ||
|
|
||||||
str_contains($request->getUri()->getPath(), '.json')
|
|
||||||
) {
|
|
||||||
$response->getBody()->write($openapi->toJson());
|
|
||||||
return $response->withHeader('Content-Type', 'application/json');
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->getBody()->write($openapi->toYaml());
|
|
||||||
return $response->withHeader('Content-Type', 'application/x-yaml');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Docs;
|
|
||||||
|
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
|
|
||||||
class TokenSecurity extends OA\SecurityScheme
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct(
|
|
||||||
securityScheme: 'bearerAuth',
|
|
||||||
type: 'http',
|
|
||||||
description: 'JWT based authentication using Bearer tokens.',
|
|
||||||
bearerFormat: 'JWT',
|
|
||||||
scheme: 'bearer'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Docs;
|
|
||||||
|
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
|
|
||||||
#[\Attribute]
|
|
||||||
class UnauthorizedResponse extends OA\Response
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct(
|
|
||||||
response: '401',
|
|
||||||
description: 'Unauthorized - Authentication is required and has failed or has not yet been provided.',
|
|
||||||
content: new OA\MediaType(
|
|
||||||
mediaType: 'application/json',
|
|
||||||
schema: new OA\Schema(
|
|
||||||
properties: [
|
|
||||||
new OA\Property(property: 'message', type: 'string', example: 'Unauthorized'),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,10 +7,7 @@ namespace Siteworxpro\App\Events;
|
|||||||
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
use Illuminate\Contracts\Support\Arrayable;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Siteworxpro\App\Attributes\Events\ListensFor;
|
use Siteworxpro\App\Annotations\Events\ListensFor;
|
||||||
|
|
||||||
use function React\Async\await;
|
|
||||||
use function React\Async\coroutine;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Dispatcher
|
* Class Dispatcher
|
||||||
@@ -32,8 +29,6 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
*/
|
*/
|
||||||
private Collection $pushed;
|
private Collection $pushed;
|
||||||
|
|
||||||
private array $subscribers = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string LISTENERS_NAMESPACE The namespace where listeners are located
|
* @var string LISTENERS_NAMESPACE The namespace where listeners are located
|
||||||
*/
|
*/
|
||||||
@@ -45,16 +40,6 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
$this->registerListeners();
|
$this->registerListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
|
||||||
public function __destruct()
|
|
||||||
{
|
|
||||||
foreach ($this->pushed as $event => $payload) {
|
|
||||||
$this->dispatch($event, $payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register event listeners based on the ListensFor attribute.
|
* Register event listeners based on the ListensFor attribute.
|
||||||
*
|
*
|
||||||
@@ -114,7 +99,7 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
*/
|
*/
|
||||||
public function subscribe($subscriber): void
|
public function subscribe($subscriber): void
|
||||||
{
|
{
|
||||||
$this->subscribers[] = $subscriber;
|
$this->listeners = array_merge($this->listeners, (array) $subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,7 +108,6 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
* @param $event
|
* @param $event
|
||||||
* @param array $payload
|
* @param array $payload
|
||||||
* @return array|null
|
* @return array|null
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
*/
|
||||||
public function until($event, $payload = []): array|null
|
public function until($event, $payload = []): array|null
|
||||||
{
|
{
|
||||||
@@ -137,7 +121,6 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
* @param array $payload
|
* @param array $payload
|
||||||
* @param bool $halt
|
* @param bool $halt
|
||||||
* @return array|null
|
* @return array|null
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
*/
|
||||||
public function dispatch($event, $payload = [], $halt = false): array|null
|
public function dispatch($event, $payload = [], $halt = false): array|null
|
||||||
{
|
{
|
||||||
@@ -147,46 +130,23 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
$eventClass = $event;
|
$eventClass = $event;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle subscribers as a coroutine
|
|
||||||
$promise = coroutine(function () use ($event, $payload, $halt, $eventClass, &$responses) {
|
|
||||||
foreach ($this->subscribers as $subscriber) {
|
|
||||||
if (method_exists($subscriber, 'handle')) {
|
|
||||||
$response = $subscriber->handle($event, $payload);
|
|
||||||
$responses[$eventClass] = $response;
|
|
||||||
|
|
||||||
if ($halt && $response !== null) {
|
|
||||||
return $responses;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
$listeners = $this->listeners[$eventClass] ?? null;
|
$listeners = $this->listeners[$eventClass] ?? null;
|
||||||
|
|
||||||
// If no listeners, just await the subscriber promise
|
|
||||||
if ($listeners === null) {
|
if ($listeners === null) {
|
||||||
return await($promise);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$responses = [];
|
$responses = [];
|
||||||
|
|
||||||
foreach ($listeners as $listener) {
|
foreach ($listeners as $listener) {
|
||||||
$response = $listener($event, $payload);
|
$response = $listener($event, $payload);
|
||||||
$responses[$eventClass] = $response;
|
$responses[] = $response;
|
||||||
|
|
||||||
if ($halt && $response !== null) {
|
if ($halt && $response !== null) {
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Await the subscriber promise and merge responses
|
|
||||||
$promiseResponses = await($promise);
|
|
||||||
|
|
||||||
if (is_array($promiseResponses)) {
|
|
||||||
$responses = array_merge($responses, $promiseResponses);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $responses;
|
return $responses;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,7 +167,6 @@ class Dispatcher implements DispatcherContract, Arrayable
|
|||||||
*
|
*
|
||||||
* @param $event
|
* @param $event
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
*/
|
||||||
public function flush($event): void
|
public function flush($event): void
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,27 +6,20 @@ namespace Siteworxpro\App\Events\Listeners\Database;
|
|||||||
|
|
||||||
use Illuminate\Database\Events\ConnectionEstablished;
|
use Illuminate\Database\Events\ConnectionEstablished;
|
||||||
use Illuminate\Database\Events\ConnectionEvent;
|
use Illuminate\Database\Events\ConnectionEvent;
|
||||||
use Siteworxpro\App\Attributes\Events\ListensFor;
|
use Siteworxpro\App\Annotations\Events\ListensFor;
|
||||||
use Siteworxpro\App\Events\Listeners\Listener;
|
use Siteworxpro\App\Events\Listeners\Listener;
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
use Siteworxpro\App\Services\Facades\Logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Connected
|
|
||||||
* @package Siteworxpro\App\Events\Listeners\Database
|
|
||||||
*/
|
|
||||||
#[ListensFor(ConnectionEstablished::class)]
|
#[ListensFor(ConnectionEstablished::class)]
|
||||||
class Connected extends Listener
|
class Connected extends Listener
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param mixed $event
|
* @param ConnectionEvent $event
|
||||||
* @param array $payload
|
* @param array $payload
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
public function __invoke(mixed $event, array $payload = []): null
|
public function __invoke($event, array $payload = []): null
|
||||||
{
|
{
|
||||||
if (!($event instanceof ConnectionEvent)) {
|
|
||||||
throw new \TypeError("Invalid event type passed to listener " . static::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::info("Database connection event", [get_class($event), $event->connectionName]);
|
Logger::info("Database connection event", [get_class($event), $event->connectionName]);
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Events\Listeners;
|
namespace Siteworxpro\App\Events\Listeners;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Listener
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Events\Listeners
|
|
||||||
*/
|
|
||||||
abstract class Listener implements ListenerInterface
|
abstract class Listener implements ListenerInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Events\Listeners;
|
namespace Siteworxpro\App\Events\Listeners;
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface ListenerInterface
|
|
||||||
* @package Siteworxpro\App\Events\Listeners
|
|
||||||
*/
|
|
||||||
interface ListenerInterface
|
interface ListenerInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @param mixed $event
|
|
||||||
* @param array $payload
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function __invoke(mixed $event, array $payload = []): mixed;
|
public function __invoke(mixed $event, array $payload = []): mixed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Events\Subscribers;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
|
||||||
|
|
||||||
abstract class Subscriber implements SubscriberInterface, Arrayable
|
|
||||||
{
|
|
||||||
public function toArray(): array
|
|
||||||
{
|
|
||||||
return get_object_vars($this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Events\Subscribers;
|
|
||||||
|
|
||||||
interface SubscriberInterface
|
|
||||||
{
|
|
||||||
public function handle(string $eventName, mixed $payload): mixed;
|
|
||||||
}
|
|
||||||
54
src/Grpc.php
54
src/Grpc.php
@@ -1,54 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App;
|
|
||||||
|
|
||||||
use GRPC\Greeter\GreeterInterface;
|
|
||||||
use Siteworxpro\App\GrpcHandlers\GreeterHandler;
|
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
|
||||||
use Spiral\RoadRunner\GRPC\Invoker;
|
|
||||||
use Spiral\RoadRunner\GRPC\Server;
|
|
||||||
use Spiral\RoadRunner\Worker;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Grpc
|
|
||||||
*
|
|
||||||
* starts a gRPC server using RoadRunner
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App
|
|
||||||
*/
|
|
||||||
class Grpc
|
|
||||||
{
|
|
||||||
private const array SERVICES = [
|
|
||||||
GreeterInterface::class => GreeterHandler::class,
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \ReflectionException
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
Kernel::boot();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts the gRPC server
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function start(): int
|
|
||||||
{
|
|
||||||
$server = new Server(new Invoker(), [
|
|
||||||
'debug' => Config::get('app.dev_mode'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
foreach (self::SERVICES as $interface => $handler) {
|
|
||||||
$server->registerService($interface, new $handler());
|
|
||||||
}
|
|
||||||
|
|
||||||
$server->serve(Worker::create());
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\GrpcHandlers;
|
|
||||||
|
|
||||||
use GRPC\Greeter\GreeterInterface;
|
|
||||||
use GRPC\Greeter\HelloReply;
|
|
||||||
use GRPC\Greeter\HelloRequest;
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
|
||||||
use Siteworxpro\App\Services\Facades\CommandBus;
|
|
||||||
use Spiral\RoadRunner\GRPC;
|
|
||||||
|
|
||||||
class GreeterHandler implements GreeterInterface
|
|
||||||
{
|
|
||||||
public function SayHello(GRPC\ContextInterface $ctx, HelloRequest $in): HelloReply // phpcs:ignore
|
|
||||||
{
|
|
||||||
$command = new ExampleCommand($in->getName());
|
|
||||||
|
|
||||||
$reply = new HelloReply();
|
|
||||||
$reply->setMessage(CommandBus::handle($command));
|
|
||||||
|
|
||||||
return $reply;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,10 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Helpers;
|
namespace Siteworxpro\App\Helpers;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Env
|
|
||||||
* @package Siteworxpro\App\Helpers
|
|
||||||
*/
|
|
||||||
abstract class Env
|
abstract class Env
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,17 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Helpers;
|
namespace Siteworxpro\App\Helpers;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Ulid
|
|
||||||
* @package Siteworxpro\App\Helpers
|
|
||||||
*/
|
|
||||||
class Ulid
|
class Ulid
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Generate a ULID string
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function generate(): string
|
public static function generate(): string
|
||||||
{
|
{
|
||||||
return \Ulid\Ulid::generate()->getRandomness();
|
return \Ulid\Ulid::generate()->getRandomness();
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Helpers;
|
|
||||||
|
|
||||||
class Version
|
|
||||||
{
|
|
||||||
public const string VERSION = 'dev-master';
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Http;
|
namespace Siteworxpro\App\Http;
|
||||||
|
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
|
||||||
use Nyholm\Psr7\Response;
|
use Nyholm\Psr7\Response;
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
|
|
||||||
@@ -18,19 +17,13 @@ class JsonResponseFactory
|
|||||||
/**
|
/**
|
||||||
* Create a JSON response with the given data and status code.
|
* Create a JSON response with the given data and status code.
|
||||||
*
|
*
|
||||||
* @param array|Arrayable $data The data to include in the response.
|
* @param array $data The data to include in the response.
|
||||||
* @param CodesEnum $statusCode The HTTP status code for the response.
|
* @param CodesEnum $statusCode The HTTP status code for the response.
|
||||||
* @return Response The JSON response.
|
* @return Response The JSON response.
|
||||||
* @throws \JsonException
|
* @throws \JsonException
|
||||||
*/
|
*/
|
||||||
public static function createJsonResponse(
|
public static function createJsonResponse(array $data, CodesEnum $statusCode = CodesEnum::OK): Response
|
||||||
array|Arrayable $data,
|
{
|
||||||
CodesEnum $statusCode = CodesEnum::OK
|
|
||||||
): Response {
|
|
||||||
if ($data instanceof Arrayable) {
|
|
||||||
$data = $data->toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
status: $statusCode->value,
|
status: $statusCode->value,
|
||||||
headers: [
|
headers: [
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ namespace Siteworxpro\App\Http\Middleware;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Carbon\WrapperClock;
|
use Carbon\WrapperClock;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
|
||||||
use Lcobucci\JWT\JwtFacade;
|
use Lcobucci\JWT\JwtFacade;
|
||||||
use Lcobucci\JWT\Signer\Hmac\Sha256 as Hmac256;
|
use Lcobucci\JWT\Signer\Hmac\Sha256 as Hmac256;
|
||||||
use Lcobucci\JWT\Signer\Key;
|
|
||||||
use Lcobucci\JWT\Signer\Key\InMemory;
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
||||||
use Lcobucci\JWT\Signer\Rsa\Sha256;
|
use Lcobucci\JWT\Signer\Rsa\Sha256;
|
||||||
use Lcobucci\JWT\Token\InvalidTokenStructure;
|
use Lcobucci\JWT\Token\InvalidTokenStructure;
|
||||||
@@ -23,52 +21,24 @@ use League\Route\Dispatcher;
|
|||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Siteworxpro\App\Attributes\Guards\Jwt;
|
use Siteworxpro\App\Annotations\Guards\Jwt;
|
||||||
use Siteworxpro\App\Controllers\Controller;
|
use Siteworxpro\App\Controllers\Controller;
|
||||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
use Siteworxpro\App\Services\Facades\Guzzle;
|
|
||||||
use Siteworxpro\App\Services\Facades\Redis;
|
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
|
|
||||||
/**
|
|
||||||
* JWT authorization middleware.
|
|
||||||
*
|
|
||||||
* Applies JWT validation to controller actions annotated with `Jwt` attribute.
|
|
||||||
* Flow:
|
|
||||||
* - Resolve the targeted controller and method for the current route.
|
|
||||||
* - If the method has `Jwt`, read the `Authorization` header and parse the Bearer token.
|
|
||||||
* - Validate signature, time constraints, issuer\(\) and audience\(\) based on attribute and config.
|
|
||||||
* - On success, attach all token claims to the request as attributes.
|
|
||||||
* - On failure, return a 401 JSON response with validation errors.
|
|
||||||
*
|
|
||||||
* Configuration:
|
|
||||||
* - `jwt.signing_key`: key material or `file://` path to key.
|
|
||||||
* - `jwt.strict_validation`: bool toggling strict vs loose time validation.
|
|
||||||
*/
|
|
||||||
class JwtMiddleware extends Middleware
|
class JwtMiddleware extends Middleware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Process the incoming request.
|
* @throws \JsonException
|
||||||
*
|
* @throws \Exception
|
||||||
* If the matched controller method is annotated with `Jwt`, validates the token and
|
|
||||||
* augments the request with claims on success. Otherwise, just delegates to the next handler.
|
|
||||||
*
|
|
||||||
* @param ServerRequestInterface $request PSR-7 request instance.
|
|
||||||
* @param RequestHandlerInterface|Dispatcher $handler Next middleware or route dispatcher.
|
|
||||||
*
|
|
||||||
* @return ResponseInterface Response produced by the next handler or a 401 JSON response.
|
|
||||||
*
|
|
||||||
* @throws \JsonException On JSON error response encoding issues.
|
|
||||||
* @throws \Exception On unexpected reflection or JWT parsing issues.
|
|
||||||
*/
|
*/
|
||||||
public function process(
|
public function process(
|
||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
RequestHandlerInterface|Dispatcher $handler
|
RequestHandlerInterface|Dispatcher $handler
|
||||||
): ResponseInterface {
|
): ResponseInterface {
|
||||||
|
|
||||||
// Resolve the callable \[Controller, method] for the current route.
|
$callable = $this->extractRouteCallable($request, $handler);
|
||||||
$callable = $this->extractRouteCallable($handler);
|
|
||||||
if ($callable === null) {
|
if ($callable === null) {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
@@ -81,15 +51,12 @@ class JwtMiddleware extends Middleware
|
|||||||
|
|
||||||
if ($reflectionClass->hasMethod($method)) {
|
if ($reflectionClass->hasMethod($method)) {
|
||||||
$reflectionMethod = $reflectionClass->getMethod($method);
|
$reflectionMethod = $reflectionClass->getMethod($method);
|
||||||
// Read `Jwt` attribute on the controller method.
|
|
||||||
$attributes = $reflectionMethod->getAttributes(Jwt::class);
|
$attributes = $reflectionMethod->getAttributes(Jwt::class);
|
||||||
|
|
||||||
// If no `Jwt` attribute, do not enforce auth here.
|
|
||||||
if (empty($attributes)) {
|
if (empty($attributes)) {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract Bearer token from Authorization header.
|
|
||||||
$token = str_replace('Bearer ', '', $request->getHeaderLine('Authorization'));
|
$token = str_replace('Bearer ', '', $request->getHeaderLine('Authorization'));
|
||||||
|
|
||||||
if (empty($token)) {
|
if (empty($token)) {
|
||||||
@@ -99,7 +66,6 @@ class JwtMiddleware extends Middleware
|
|||||||
], CodesEnum::UNAUTHORIZED);
|
], CodesEnum::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aggregate required issuers and audience from attributes.
|
|
||||||
$requiredIssuers = [];
|
$requiredIssuers = [];
|
||||||
$requiredAudience = '';
|
$requiredAudience = '';
|
||||||
|
|
||||||
@@ -107,7 +73,7 @@ class JwtMiddleware extends Middleware
|
|||||||
/** @var Jwt $jwtInstance */
|
/** @var Jwt $jwtInstance */
|
||||||
$jwtInstance = $attribute->newInstance();
|
$jwtInstance = $attribute->newInstance();
|
||||||
|
|
||||||
if ($jwtInstance->getAudience() !== '') {
|
if ($jwtInstance->getRequiredAudience() !== '') {
|
||||||
$requiredAudience = $jwtInstance->getAudience();
|
$requiredAudience = $jwtInstance->getAudience();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,10 +81,9 @@ class JwtMiddleware extends Middleware
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse and validate the token with signature, time, issuer and audience constraints.
|
|
||||||
$jwt = new JwtFacade()->parse(
|
$jwt = new JwtFacade()->parse(
|
||||||
$token,
|
$token,
|
||||||
$this->getSignedWith($token),
|
$this->getSignedWith(),
|
||||||
Config::get('jwt.strict_validation') ?
|
Config::get('jwt.strict_validation') ?
|
||||||
new StrictValidAt(new WrapperClock(Carbon::now())) :
|
new StrictValidAt(new WrapperClock(Carbon::now())) :
|
||||||
new LooseValidAt(new WrapperClock(Carbon::now())),
|
new LooseValidAt(new WrapperClock(Carbon::now())),
|
||||||
@@ -126,31 +91,23 @@ class JwtMiddleware extends Middleware
|
|||||||
new PermittedFor($requiredAudience)
|
new PermittedFor($requiredAudience)
|
||||||
);
|
);
|
||||||
} catch (RequiredConstraintsViolated $exception) {
|
} catch (RequiredConstraintsViolated $exception) {
|
||||||
// Collect human-readable violations to return to the client.
|
|
||||||
$violations = [];
|
$violations = [];
|
||||||
foreach ($exception->violations() as $violation) {
|
foreach ($exception->violations() as $violation) {
|
||||||
$violations[] = $violation->getMessage();
|
$violations[] = $violation->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonResponseFactory::createJsonResponse([
|
return JsonResponseFactory::createJsonResponse([
|
||||||
'status_code' => CodesEnum::UNAUTHORIZED->value,
|
'status_code' => 401,
|
||||||
'message' => 'Unauthorized: Invalid token',
|
'message' => 'Unauthorized: Invalid token',
|
||||||
'errors' => $violations
|
'errors' => $violations
|
||||||
], CodesEnum::UNAUTHORIZED);
|
], CodesEnum::UNAUTHORIZED);
|
||||||
} catch (InvalidTokenStructure) {
|
} catch (InvalidTokenStructure) {
|
||||||
// Token could not be parsed due to malformed structure.
|
|
||||||
return JsonResponseFactory::createJsonResponse([
|
return JsonResponseFactory::createJsonResponse([
|
||||||
'status_code' => CodesEnum::UNAUTHORIZED->value,
|
'status_code' => 401,
|
||||||
'message' => 'Unauthorized: Invalid token',
|
'message' => 'Unauthorized: Invalid token',
|
||||||
], CodesEnum::UNAUTHORIZED);
|
], CodesEnum::UNAUTHORIZED);
|
||||||
} catch (GuzzleException | \RuntimeException) {
|
|
||||||
return JsonResponseFactory::createJsonResponse([
|
|
||||||
'status_code' => CodesEnum::INTERNAL_SERVER_ERROR->value,
|
|
||||||
'message' => 'Token validation service unavailable or unknown error',
|
|
||||||
], CodesEnum::INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose all token claims as request attributes for downstream consumers.
|
|
||||||
foreach ($jwt->claims()->all() as $item => $value) {
|
foreach ($jwt->claims()->all() as $item => $value) {
|
||||||
$request = $request->withAttribute($item, $value);
|
$request = $request->withAttribute($item, $value);
|
||||||
}
|
}
|
||||||
@@ -160,163 +117,24 @@ class JwtMiddleware extends Middleware
|
|||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getSignedWith(): SignedWith
|
||||||
* Build the signature validation constraint from configured key.
|
|
||||||
*
|
|
||||||
* - If the configured key content includes the string `PUBLIC KEY`, use RSA SHA-256.
|
|
||||||
* - Otherwise assume an HMAC SHA-256 shared secret.
|
|
||||||
* - Supports raw key strings or `file://` paths.
|
|
||||||
*
|
|
||||||
* @return SignedWith Signature constraint used during JWT parsing.
|
|
||||||
*
|
|
||||||
* @throws \RuntimeException When no signing key is configured.
|
|
||||||
* @throws \JsonException
|
|
||||||
*/
|
|
||||||
private function getSignedWith(string $token): SignedWith
|
|
||||||
{
|
{
|
||||||
$keyConfig = Config::get('jwt.signing_key');
|
$key = Config::get('jwt.signing_key');
|
||||||
|
|
||||||
if ($keyConfig === null) {
|
if ($key === null) {
|
||||||
throw new \RuntimeException('JWT signing key is not configured.');
|
throw new \RuntimeException('JWT signing key is not configured.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// file:// path to key
|
if (str_starts_with($key, 'file://')) {
|
||||||
if (str_starts_with($keyConfig, 'file://')) {
|
$key = InMemory::file(substr($key, 7));
|
||||||
$key = InMemory::file(substr($keyConfig, 7));
|
|
||||||
// openid jwks url
|
|
||||||
} elseif (str_contains($keyConfig, '.well-known/')) {
|
|
||||||
$jwt = explode('.', $token);
|
|
||||||
if (count($jwt) !== 3) {
|
|
||||||
throw new InvalidTokenStructure('Invalid JWT structure for JWKS key retrieval.');
|
|
||||||
}
|
|
||||||
$header = json_decode(base64_decode($jwt[0]), true, 512, JSON_THROW_ON_ERROR);
|
|
||||||
$keyId = $header['kid'] ?? '0'; // Default to '0' if no kid present
|
|
||||||
$key = $this->getJwksKey($keyConfig, $keyId);
|
|
||||||
} else {
|
} else {
|
||||||
$key = InMemory::plainText($keyConfig);
|
$key = InMemory::plainText($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Heuristic: if PEM public key content is detected, use RSA; otherwise use HMAC.
|
|
||||||
if (str_contains($key->contents(), 'PUBLIC KEY')) {
|
if (str_contains($key->contents(), 'PUBLIC KEY')) {
|
||||||
return new SignedWith(new Sha256(), $key);
|
return new SignedWith(new Sha256(), $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SignedWith(new Hmac256(), $key);
|
return new SignedWith(new Hmac256(), $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getJwksKey(string $url, string $keyId): Key
|
|
||||||
{
|
|
||||||
$cached = Redis::get('jwks_key_' . $keyId);
|
|
||||||
if ($cached !== null) {
|
|
||||||
return InMemory::plainText($cached);
|
|
||||||
}
|
|
||||||
|
|
||||||
$openIdConfig = Guzzle::get($url);
|
|
||||||
$body = json_decode($openIdConfig->getBody()->getContents(), true, JSON_THROW_ON_ERROR);
|
|
||||||
$jwksUri = $body['jwks_uri'] ?? '';
|
|
||||||
if (empty($jwksUri)) {
|
|
||||||
throw new \RuntimeException('JWKS URI not found in OpenID configuration.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$jwksResponse = Guzzle::get($jwksUri);
|
|
||||||
$jwksBody = json_decode(
|
|
||||||
$jwksResponse->getBody()->getContents(),
|
|
||||||
true,
|
|
||||||
JSON_THROW_ON_ERROR
|
|
||||||
);
|
|
||||||
|
|
||||||
// For simplicity, we take the first key in the JWKS.
|
|
||||||
$firstKey = array_filter(
|
|
||||||
$jwksBody['keys'],
|
|
||||||
fn($key) => $key['kid'] === $keyId
|
|
||||||
)[0] ?? $jwksBody['keys'][0] ?? null;
|
|
||||||
|
|
||||||
if (empty($firstKey)) {
|
|
||||||
throw new \RuntimeException('No matching key found in JWKS for key ID: ' . $keyId);
|
|
||||||
}
|
|
||||||
|
|
||||||
$n = $firstKey['n'];
|
|
||||||
$e = $firstKey['e'];
|
|
||||||
$publicKeyPem = "-----BEGIN PUBLIC KEY-----\n" .
|
|
||||||
chunk_split(base64_encode($this->convertJwkToPem($n, $e)), 64) .
|
|
||||||
"-----END PUBLIC KEY-----\n";
|
|
||||||
|
|
||||||
Redis::set('jwks_key_' . $keyId, $publicKeyPem, 'EX', 3600);
|
|
||||||
|
|
||||||
return InMemory::plainText($publicKeyPem);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build a DER-encoded SubjectPublicKeyInfo from JWK 'n' and 'e'.
|
|
||||||
* Returns raw DER bytes; caller base64-encodes and wraps with PEM headers.
|
|
||||||
*/
|
|
||||||
private function convertJwkToPem(string $n, string $e): string
|
|
||||||
{
|
|
||||||
$modulus = $this->base64UrlDecode($n);
|
|
||||||
$exponent = $this->base64UrlDecode($e);
|
|
||||||
|
|
||||||
$derN = $this->derEncodeInteger($modulus);
|
|
||||||
$derE = $this->derEncodeInteger($exponent);
|
|
||||||
|
|
||||||
// RSAPublicKey (PKCS#1): SEQUENCE { n INTEGER, e INTEGER }
|
|
||||||
$rsaPublicKey = $this->derEncodeSequence($derN . $derE);
|
|
||||||
|
|
||||||
// AlgorithmIdentifier for rsaEncryption: 1.2.840.113549.1.1.1 with NULL
|
|
||||||
$algId = hex2bin('300d06092a864886f70d0101010500');
|
|
||||||
|
|
||||||
// SubjectPublicKey (SPKI) BIT STRING, 0 unused bits + RSAPublicKey
|
|
||||||
$subjectPublicKey = $this->derEncodeBitString($rsaPublicKey);
|
|
||||||
|
|
||||||
// SubjectPublicKeyInfo: SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }
|
|
||||||
return $this->derEncodeSequence($algId . $subjectPublicKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function base64UrlDecode(string $data): string
|
|
||||||
{
|
|
||||||
$data = strtr($data, '-_', '+/');
|
|
||||||
$pad = strlen($data) % 4;
|
|
||||||
if ($pad) {
|
|
||||||
$data .= str_repeat('=', 4 - $pad);
|
|
||||||
}
|
|
||||||
return base64_decode($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function derEncodeLength(int $len): string
|
|
||||||
{
|
|
||||||
if ($len < 0x80) {
|
|
||||||
return chr($len);
|
|
||||||
}
|
|
||||||
$bytes = '';
|
|
||||||
while ($len > 0) {
|
|
||||||
$bytes = chr($len & 0xFF) . $bytes;
|
|
||||||
$len >>= 8;
|
|
||||||
}
|
|
||||||
return chr(0x80 | strlen($bytes)) . $bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function derEncodeInteger(string $bytes): string
|
|
||||||
{
|
|
||||||
// Remove leading zeroes
|
|
||||||
$bytes = ltrim($bytes, "\x00");
|
|
||||||
if ($bytes === '') {
|
|
||||||
$bytes = "\x00";
|
|
||||||
}
|
|
||||||
// Ensure positive INTEGER (prepend 0x00 if MSB set)
|
|
||||||
if ((ord($bytes[0]) & 0x80) !== 0) {
|
|
||||||
$bytes = "\x00" . $bytes;
|
|
||||||
}
|
|
||||||
return "\x02" . $this->derEncodeLength(strlen($bytes)) . $bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function derEncodeSequence(string $bytes): string
|
|
||||||
{
|
|
||||||
return "\x30" . $this->derEncodeLength(strlen($bytes)) . $bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function derEncodeBitString(string $bytes): string
|
|
||||||
{
|
|
||||||
// 0 unused bits + data
|
|
||||||
$payload = "\x00" . $bytes;
|
|
||||||
return "\x03" . $this->derEncodeLength(strlen($payload)) . $payload;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,60 +9,30 @@ use League\Route\Route;
|
|||||||
use Psr\Http\Server\MiddlewareInterface;
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
* Base middleware helper for extracting route callables.
|
|
||||||
*
|
|
||||||
* This abstract middleware provides a utility method to inspect a League\Route
|
|
||||||
* dispatcher and obtain the underlying route callable as a [class, method] tuple.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Http\Middleware
|
|
||||||
*/
|
|
||||||
abstract class Middleware implements MiddlewareInterface
|
abstract class Middleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Extract the route callable [class, method] from a League\Route dispatcher.
|
protected function extractRouteCallable($request, RequestHandlerInterface | Dispatcher $handler): array|null
|
||||||
*
|
{
|
||||||
* When the provided handler is a League\Route\Dispatcher, this inspects its
|
|
||||||
* middleware stack, looks at the last segment (the resolved Route), and
|
|
||||||
* attempts to normalize its callable into a [class, method] pair.
|
|
||||||
*
|
|
||||||
* Supported callable forms:
|
|
||||||
* - array callable: [object|class-string, method-string]
|
|
||||||
* - string callable: "ClassName::methodName"
|
|
||||||
*
|
|
||||||
* Returns null when the handler is not a Dispatcher, the stack is empty,
|
|
||||||
* or the callable cannot be parsed.
|
|
||||||
*
|
|
||||||
* @param RequestHandlerInterface|Dispatcher $handler The downstream handler or dispatcher.
|
|
||||||
*
|
|
||||||
* @return array{0: class-string|object|null, 1: string|null}|null Tuple of [class|object, method] or null.
|
|
||||||
*/
|
|
||||||
protected function extractRouteCallable(
|
|
||||||
RequestHandlerInterface|Dispatcher $handler
|
|
||||||
): array|null {
|
|
||||||
// Only proceed if this is a League\Route dispatcher.
|
|
||||||
if (!$handler instanceof Dispatcher) {
|
if (!$handler instanceof Dispatcher) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Route | null $lastSegment */
|
/** @var Route | null $lastSegment */
|
||||||
// Retrieve the last middleware in the stack, which should be the Route.
|
|
||||||
$lastSegment = array_last($handler->getMiddlewareStack());
|
$lastSegment = array_last($handler->getMiddlewareStack());
|
||||||
|
|
||||||
if ($lastSegment === null) {
|
if ($lastSegment === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtain the callable associated with the route.
|
|
||||||
$callable = $lastSegment->getCallable();
|
$callable = $lastSegment->getCallable();
|
||||||
$class = null;
|
$class = null;
|
||||||
$method = null;
|
$method = null;
|
||||||
|
|
||||||
// Handle array callable: [object|class-string, 'method']
|
|
||||||
if (is_array($callable) && count($callable) === 2) {
|
if (is_array($callable) && count($callable) === 2) {
|
||||||
[$class, $method] = $callable;
|
[$class, $method] = $callable;
|
||||||
} elseif (is_string($callable)) {
|
} elseif (is_string($callable)) {
|
||||||
// Handle string callable: 'ClassName::methodName'
|
// Handle the case where the callable is a string (e.g., 'ClassName::methodName')
|
||||||
[$class, $method] = explode('::', $callable);
|
[$class, $method] = explode('::', $callable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,99 +8,46 @@ use League\Route\Dispatcher;
|
|||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
use Siteworxpro\App\Attributes\Guards\RequireAllScopes;
|
use Siteworxpro\App\Annotations\Guards\Scope;
|
||||||
use Siteworxpro\App\Attributes\Guards\Scope;
|
|
||||||
use Siteworxpro\App\Controllers\Controller;
|
use Siteworxpro\App\Controllers\Controller;
|
||||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
|
|
||||||
/**
|
|
||||||
* Middleware that enforces scope-based access control on controller actions.
|
|
||||||
*
|
|
||||||
* It inspects PHP 8 attributes of type \`Scope\` applied to the resolved controller method,
|
|
||||||
* compares the required scopes with the user scopes provided on the request attribute \`scopes\`,
|
|
||||||
* and returns a 403 JSON response when any required scope is missing.
|
|
||||||
*
|
|
||||||
* If the route callable cannot be resolved, or no scope is required, the request is passed through.
|
|
||||||
*
|
|
||||||
* @see Scope
|
|
||||||
*/
|
|
||||||
class ScopeMiddleware extends Middleware
|
class ScopeMiddleware extends Middleware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Resolve the route callable, read any \`Scope\` attributes, and enforce required scopes.
|
* @throws \JsonException
|
||||||
*
|
|
||||||
* Expected user scopes are provided on the request under the attribute name \`scopes\`
|
|
||||||
* as an array of strings.
|
|
||||||
*
|
|
||||||
* @param ServerRequestInterface $request Incoming PSR-7 request (expects \`scopes\` attribute).
|
|
||||||
* @param RequestHandlerInterface|Dispatcher $handler Next handler or League\Route dispatcher.
|
|
||||||
*
|
|
||||||
* @return ResponseInterface A 403 JSON response when scopes are insufficient; otherwise the handler response.
|
|
||||||
*
|
|
||||||
* @throws \JsonException If encoding the JSON error response fails.
|
|
||||||
* @throws \ReflectionException If reflection on the controller or method fails.
|
|
||||||
*/
|
*/
|
||||||
public function process(
|
public function process(
|
||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
RequestHandlerInterface | Dispatcher $handler
|
RequestHandlerInterface | Dispatcher $handler
|
||||||
): ResponseInterface {
|
): ResponseInterface {
|
||||||
// Attempt to resolve the route's callable [Controller instance, method name].
|
$callable = $this->extractRouteCallable($request, $handler);
|
||||||
$callable = $this->extractRouteCallable($handler);
|
|
||||||
if ($callable === null) {
|
if ($callable === null) {
|
||||||
// If no callable is available, delegate to the next handler.
|
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Controller $class Controller instance resolved from the route. */
|
/** @var Controller $class */
|
||||||
[$class, $method] = $callable;
|
[$class, $method] = $callable;
|
||||||
|
|
||||||
// Ensure the controller exists and the method is defined before reflecting.
|
|
||||||
if (class_exists($class::class)) {
|
if (class_exists($class::class)) {
|
||||||
$reflectionClass = new \ReflectionClass($class);
|
$reflectionClass = new \ReflectionClass($class);
|
||||||
|
|
||||||
if ($reflectionClass->hasMethod($method)) {
|
if ($reflectionClass->hasMethod($method)) {
|
||||||
$reflectionMethod = $reflectionClass->getMethod($method);
|
$reflectionMethod = $reflectionClass->getMethod($method);
|
||||||
|
|
||||||
// Fetch all Scope attributes declared on the method.
|
|
||||||
$attributes = $reflectionMethod->getAttributes(Scope::class);
|
$attributes = $reflectionMethod->getAttributes(Scope::class);
|
||||||
$requireAllAttributes = $reflectionMethod->getAttributes(RequireAllScopes::class);
|
|
||||||
|
|
||||||
if (empty($attributes)) {
|
|
||||||
// No scope attributes; delegate to the next handler.
|
|
||||||
return $handler->handle($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
$requiredScopes = [];
|
|
||||||
$userScopes = [];
|
|
||||||
$requireAll = false;
|
|
||||||
|
|
||||||
foreach ($attributes as $attribute) {
|
foreach ($attributes as $attribute) {
|
||||||
/** @var Scope $scopeInstance Concrete Scope attribute instance. */
|
/** @var Scope $scopeInstance */
|
||||||
$scopeInstance = $attribute->newInstance();
|
$scopeInstance = $attribute->newInstance();
|
||||||
$requiredScopes = array_merge($requiredScopes, $scopeInstance->getScopes());
|
$requiredScopes = $scopeInstance->getScopes();
|
||||||
|
|
||||||
// If any attribute requires all scopes, set the flag.
|
$userScopes = $request->getAttribute('scopes', []);
|
||||||
$requireAll = $requireAll || !empty($requireAllAttributes);
|
|
||||||
|
|
||||||
$scopes = $request->getAttribute($scopeInstance->getClaim());
|
|
||||||
if (!is_array($scopes)) {
|
|
||||||
// If user scopes are not an array, treat as no scopes provided.
|
|
||||||
$scopes = explode($scopeInstance->getSeparator(), (string) $scopes);
|
|
||||||
}
|
|
||||||
|
|
||||||
$userScopes = array_merge(
|
|
||||||
$userScopes,
|
|
||||||
$scopes
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$userScopes = array_unique($userScopes);
|
|
||||||
|
|
||||||
// Deny if any required scope is missing from the user's scopes.
|
|
||||||
if (
|
if (
|
||||||
(!$requireAll && array_intersect($userScopes, $requiredScopes) === []) ||
|
array_any(
|
||||||
($requireAll && array_diff($requiredScopes, $userScopes) !== [])
|
$requiredScopes,
|
||||||
|
fn($requiredScope) => !in_array($requiredScope, $userScopes, true)
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
return JsonResponseFactory::createJsonResponse([
|
return JsonResponseFactory::createJsonResponse([
|
||||||
'error' => 'insufficient_scope',
|
'error' => 'insufficient_scope',
|
||||||
@@ -110,8 +57,8 @@ class ScopeMiddleware extends Middleware
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// All checks passed; continue down the middleware pipeline.
|
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Http\Responses;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
|
|
||||||
#[OA\Schema(
|
|
||||||
schema: 'GenericResponse',
|
|
||||||
properties: [
|
|
||||||
new OA\Property(property: 'message', type: 'string', example: 'Operation completed successfully.'),
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
readonly class GenericResponse implements Arrayable
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private string $message = '',
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function toArray(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'message' => $this->message,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Http\Responses;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
|
|
||||||
#[OA\Schema(
|
|
||||||
schema: 'NotFoundResponse',
|
|
||||||
properties: [
|
|
||||||
new OA\Property(
|
|
||||||
property: 'message',
|
|
||||||
type: 'string',
|
|
||||||
example: 'The requested resource /api/resource was not found.'
|
|
||||||
),
|
|
||||||
new OA\Property(
|
|
||||||
property: 'context',
|
|
||||||
description: 'Additional context about the not found error.',
|
|
||||||
type: 'object',
|
|
||||||
example: '{}'
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
readonly class NotFoundResponse implements Arrayable
|
|
||||||
{
|
|
||||||
public function __construct(private string $uri, private array $context = [])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function toArray(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'message' => 'The requested resource ' . $this->uri . ' was not found.',
|
|
||||||
'context' => $this->context,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Http\Responses;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Support\Arrayable;
|
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
|
|
||||||
#[OA\Schema(
|
|
||||||
schema: 'ServerErrorResponse',
|
|
||||||
properties: array(
|
|
||||||
new OA\Property(property: 'message', type: 'string', example: 'An internal server error occurred.'),
|
|
||||||
new OA\Property(property: 'code', type: 'integer', example: 500),
|
|
||||||
new OA\Property(
|
|
||||||
property: 'file',
|
|
||||||
type: 'string',
|
|
||||||
example: '/var/www/html/app/Http/Controllers/ExampleController.php'
|
|
||||||
),
|
|
||||||
new OA\Property(property: 'line', type: 'integer', example: 42),
|
|
||||||
new OA\Property(
|
|
||||||
property: 'trace',
|
|
||||||
type: 'array',
|
|
||||||
items: new OA\Items(type: 'string'),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)]
|
|
||||||
readonly class ServerErrorResponse implements Arrayable
|
|
||||||
{
|
|
||||||
public function __construct(private \Throwable $e, private array $context = [])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function toArray(): array
|
|
||||||
{
|
|
||||||
if (Config::get('app.dev_mode')) {
|
|
||||||
return [
|
|
||||||
'code' => $this->e->getCode() != 0 ?
|
|
||||||
$this->e->getCode() :
|
|
||||||
CodesEnum::INTERNAL_SERVER_ERROR->value,
|
|
||||||
'message' => $this->e->getMessage(),
|
|
||||||
'file' => $this->e->getFile(),
|
|
||||||
'line' => $this->e->getLine(),
|
|
||||||
'trace' => $this->e->getTrace(),
|
|
||||||
'context' => $this->context,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
'code' => $this->e->getCode() != 0 ?
|
|
||||||
$this->e->getCode() :
|
|
||||||
CodesEnum::INTERNAL_SERVER_ERROR->value,
|
|
||||||
'message' => 'An internal server error occurred.',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,32 +10,17 @@ use Siteworxpro\App\Services\Facade;
|
|||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
use Siteworxpro\App\Services\Facades\Dispatcher;
|
use Siteworxpro\App\Services\Facades\Dispatcher;
|
||||||
use Siteworxpro\App\Services\ServiceProviders\BrokerServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\BrokerServiceProvider;
|
||||||
use Siteworxpro\App\Services\ServiceProviders\CommandBusProvider;
|
|
||||||
use Siteworxpro\App\Services\ServiceProviders\DispatcherServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\DispatcherServiceProvider;
|
||||||
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
||||||
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Kernel
|
|
||||||
*
|
|
||||||
* The Kernel class is responsible for bootstrapping the application by
|
|
||||||
* initializing service providers and setting up the database connection.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App
|
|
||||||
*/
|
|
||||||
class Kernel
|
class Kernel
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* List of service providers to be registered during bootstrapping.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private static array $serviceProviders = [
|
private static array $serviceProviders = [
|
||||||
LoggerServiceProvider::class,
|
LoggerServiceProvider::class,
|
||||||
RedisServiceProvider::class,
|
RedisServiceProvider::class,
|
||||||
DispatcherServiceProvider::class,
|
DispatcherServiceProvider::class,
|
||||||
BrokerServiceProvider::class,
|
BrokerServiceProvider::class
|
||||||
CommandBusProvider::class,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,51 +6,17 @@ namespace Siteworxpro\App\Log;
|
|||||||
|
|
||||||
use Monolog\Formatter\JsonFormatter;
|
use Monolog\Formatter\JsonFormatter;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Psr\Log\LogLevel;
|
use Psr\Log\LogLevel;
|
||||||
use RoadRunner\Logger\Logger as RRLogger;
|
use RoadRunner\Logger\Logger as RRLogger;
|
||||||
use Siteworxpro\App\Services\Facades\RoadRunnerLogger;
|
use Spiral\Goridge\RPC\RPC;
|
||||||
|
|
||||||
/**
|
|
||||||
* Logger implementation that conforms to PSR-3 (`Psr\Log\LoggerInterface`).
|
|
||||||
*
|
|
||||||
* Behavior:
|
|
||||||
* - If environment indicates RoadRunner RPC (`$_SERVER['RR_RPC']`), logs are forwarded
|
|
||||||
* to a RoadRunner RPC logger (`RoadRunner\Logger\Logger`) created via Goridge RPC.
|
|
||||||
* - Otherwise, logs are written to `php://stdout` using Monolog with a JSON formatter.
|
|
||||||
* - Messages below the configured threshold are ignored (level filtering).
|
|
||||||
*
|
|
||||||
* Supported PSR-3 levels are mapped to an internal numeric ordering in `$levels`.
|
|
||||||
* When using the RPC logger, levels are translated to the respective RPC methods
|
|
||||||
* (debug, info, warning, error). When using Monolog, the numeric mapping is used
|
|
||||||
* as the numeric level passed to Monolog's `log` method.
|
|
||||||
*/
|
|
||||||
class Logger implements LoggerInterface
|
class Logger implements LoggerInterface
|
||||||
{
|
{
|
||||||
/**
|
private ?RRLogger $rpcLogger = null;
|
||||||
* RoadRunner RPC logger instance when running under RoadRunner.
|
|
||||||
*
|
|
||||||
* @var RRLogger | LoggerInterface | null
|
|
||||||
*/
|
|
||||||
private RRLogger | LoggerInterface | null $rpcLogger = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Monolog logger used as a fallback to write JSON-formatted logs to stdout.
|
|
||||||
*
|
|
||||||
* @var \Monolog\Logger
|
|
||||||
*/
|
|
||||||
private \Monolog\Logger $monologLogger;
|
private \Monolog\Logger $monologLogger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Numeric ordering for PSR-3 log levels.
|
|
||||||
*
|
|
||||||
* Lower numbers represent higher severity. This mapping is used for filtering
|
|
||||||
* messages according to the configured minimum level and for Monolog numeric level.
|
|
||||||
*
|
|
||||||
* @var array<string,int>
|
|
||||||
*/
|
|
||||||
private array $levels = [
|
private array $levels = [
|
||||||
LogLevel::EMERGENCY => 0,
|
LogLevel::EMERGENCY => 0,
|
||||||
LogLevel::ALERT => 1,
|
LogLevel::ALERT => 1,
|
||||||
@@ -62,146 +28,61 @@ class Logger implements LoggerInterface
|
|||||||
LogLevel::DEBUG => 7,
|
LogLevel::DEBUG => 7,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
public function __construct(private readonly string $level = LogLevel::DEBUG)
|
||||||
* Create a new Logger.
|
{
|
||||||
*
|
|
||||||
* @param string $level Minimum level to log (PSR-3 level string). Messages with
|
|
||||||
* a higher numeric value in `$levels` will be ignored.
|
|
||||||
*
|
|
||||||
* @param resource | null $streamOutput Optional stream handler for Monolog.
|
|
||||||
*
|
|
||||||
* The default is `LogLevel::DEBUG` (log everything).
|
|
||||||
*
|
|
||||||
* If `$_SERVER['RR_RPC']` is set, an RPC connection will be attempted at
|
|
||||||
* $_SERVER['RR_RPC'] and a RoadRunner RPC logger will be used.
|
|
||||||
*
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
private readonly string $level = LogLevel::DEBUG,
|
|
||||||
$streamOutput = null,
|
|
||||||
) {
|
|
||||||
if (isset($_SERVER['RR_RPC'])) {
|
if (isset($_SERVER['RR_RPC'])) {
|
||||||
$this->rpcLogger = RoadRunnerLogger::getFacadeRoot();
|
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
||||||
|
$this->rpcLogger = new RRLogger($rpc);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->monologLogger = new \Monolog\Logger('app_logger');
|
$this->monologLogger = new \Monolog\Logger('app_logger');
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$stream = $streamOutput ?? 'php://stdout';
|
$this->monologLogger->pushHandler(new StreamHandler('php://stdout')->setFormatter($formatter));
|
||||||
$this->monologLogger->pushHandler(new StreamHandler($stream)->setFormatter($formatter));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* System is unusable.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function emergency(\Stringable|string $message, array $context = []): void
|
public function emergency(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::EMERGENCY, $message, $context);
|
$this->log(LogLevel::EMERGENCY, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Action must be taken immediately.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function alert(\Stringable|string $message, array $context = []): void
|
public function alert(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::ALERT, $message, $context);
|
$this->log(LogLevel::ALERT, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Critical conditions.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function critical(\Stringable|string $message, array $context = []): void
|
public function critical(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::CRITICAL, $message, $context);
|
$this->log(LogLevel::CRITICAL, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Runtime errors that do not require immediate action but should typically be logged and monitored.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function error(\Stringable|string $message, array $context = []): void
|
public function error(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::ERROR, $message, $context);
|
$this->log(LogLevel::ERROR, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Exceptional occurrences that are not errors.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function warning(\Stringable|string $message, array $context = []): void
|
public function warning(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::WARNING, $message, $context);
|
$this->log(LogLevel::WARNING, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Normal but significant events.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function notice(\Stringable|string $message, array $context = []): void
|
public function notice(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::NOTICE, $message, $context);
|
$this->log(LogLevel::NOTICE, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Interesting events.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function info(\Stringable|string $message, array $context = []): void
|
public function info(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::INFO, $message, $context);
|
$this->log(LogLevel::INFO, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Detailed debug information.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function debug(\Stringable|string $message, array $context = []): void
|
public function debug(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::DEBUG, $message, $context);
|
$this->log(LogLevel::DEBUG, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs with an arbitrary level.
|
|
||||||
*
|
|
||||||
* Behavior details:
|
|
||||||
* - If the provided `$level` maps to a numeric value greater than the configured
|
|
||||||
* minimum level, the message is discarded (filtered).
|
|
||||||
* - If an RPC logger is available, the message is forwarded to the RPC logger
|
|
||||||
* using a method chosen by level (debug, info, warning, error).
|
|
||||||
* - Otherwise, the message is written to Monolog using the numeric mapping.
|
|
||||||
*
|
|
||||||
* Notes:
|
|
||||||
* - `$level` should be a PSR-3 level string (values defined in `Psr\Log\LogLevel`).
|
|
||||||
* - If an unknown level string is passed, accessing `$this->levels[$level]` may
|
|
||||||
* trigger a PHP notice or undefined index. Ensure callers use valid PSR-3 levels.
|
|
||||||
*
|
|
||||||
* @param mixed $level PSR-3 log level (string)
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function log($level, \Stringable|string $message, array $context = []): void
|
public function log($level, \Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
if (isset($this->levels[$level]) && $this->levels[$level] > $this->levels[$this->level]) {
|
if ($this->levels[$level] > $this->levels[$this->level]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +105,7 @@ class Logger implements LoggerInterface
|
|||||||
$this->rpcLogger->error((string)$message, $context);
|
$this->rpcLogger->error((string)$message, $context);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->rpcLogger->log($level, (string)$message, $context);
|
$this->rpcLogger->log((string)$message, $context);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ declare(strict_types=1);
|
|||||||
namespace Siteworxpro\App\Models;
|
namespace Siteworxpro\App\Models;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use OpenApi\Attributes as OA;
|
|
||||||
use Siteworxpro\App\Helpers\Ulid;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class User
|
* Class User
|
||||||
*
|
*
|
||||||
* @property-read string $id
|
* @property string $id
|
||||||
* @property string $first_name
|
* @property string $first_name
|
||||||
* @property string $last_name
|
* @property string $last_name
|
||||||
* @property string $email
|
* @property string $email
|
||||||
@@ -21,23 +19,6 @@ use Siteworxpro\App\Helpers\Ulid;
|
|||||||
* @property-read string $full_name
|
* @property-read string $full_name
|
||||||
* @property-read string $formatted_email
|
* @property-read string $formatted_email
|
||||||
*/
|
*/
|
||||||
#[OA\Schema(
|
|
||||||
schema: "User",
|
|
||||||
properties: [
|
|
||||||
new OA\Property(
|
|
||||||
property: "id",
|
|
||||||
description: "Unique identifier for the user",
|
|
||||||
type: "string",
|
|
||||||
format: "ulid",
|
|
||||||
readOnly: true,
|
|
||||||
example: '01KBD5WPZKYD77BYM2QD9NKG99'
|
|
||||||
),
|
|
||||||
new OA\Property(property: "first_name", type: "string"),
|
|
||||||
new OA\Property(property: "last_name", type: "string"),
|
|
||||||
new OA\Property(property: "email", type: "string", format: "email"),
|
|
||||||
new OA\Property(property: "created_at", type: "string", format: "date-time"),
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
class User extends Model
|
class User extends Model
|
||||||
{
|
{
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
@@ -55,12 +36,6 @@ class User extends Model
|
|||||||
'password',
|
'password',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(array $attributes = [])
|
|
||||||
{
|
|
||||||
parent::__construct($attributes);
|
|
||||||
$this->attributes['id'] = $this->attributes['id'] ?? Ulid::generate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getFullNameAttribute(): string
|
public function getFullNameAttribute(): string
|
||||||
{
|
{
|
||||||
return "$this->first_name $this->last_name";
|
return "$this->first_name $this->last_name";
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace Siteworxpro\App\Services;
|
namespace Siteworxpro\App\Services;
|
||||||
|
|
||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
use Illuminate\Support\HigherOrderTapProxy;
|
|
||||||
use Illuminate\Support\Testing\Fakes\Fake;
|
use Illuminate\Support\Testing\Fakes\Fake;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use Mockery\Expectation;
|
use Mockery\Expectation;
|
||||||
@@ -55,6 +54,24 @@ class Facade
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Initiate a partial mock on the facade.
|
||||||
*
|
*
|
||||||
@@ -171,6 +188,19 @@ class Facade
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Get the root object behind the facade.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Services\Facades;
|
|
||||||
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\Command;
|
|
||||||
use Siteworxpro\App\Services\Facade;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Broker Facade
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\Facades
|
|
||||||
* @method static mixed handle(Command $command)
|
|
||||||
*/
|
|
||||||
class CommandBus extends Facade
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the registered name of the component.
|
|
||||||
*
|
|
||||||
* @return string The name of the component.
|
|
||||||
*/
|
|
||||||
protected static function getFacadeAccessor(): string
|
|
||||||
{
|
|
||||||
return \League\Tactician\CommandBus::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,7 +14,6 @@ use Siteworxpro\App\Services\Facade;
|
|||||||
* It extends the Facade class from the Illuminate\Support\Facades namespace.
|
* 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
|
* @method static array | bool | string | int | null get(string $key) Retrieve the configuration value for the given key. // @codingStandardsIgnoreStart
|
||||||
* @method static void set(string $key, mixed $value) Set the configuration value for the given key. // @codingStandardsIgnoreEnd
|
|
||||||
*
|
*
|
||||||
* @package Siteworx\App\Facades
|
* @package Siteworx\App\Facades
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Services\Facades;
|
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
|
||||||
use GuzzleHttp\Promise\PromiseInterface;
|
|
||||||
use GuzzleHttp\Psr7\Response;
|
|
||||||
use Siteworxpro\App\Services\Facade;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @method static Response get(string $uri, array $options = [])
|
|
||||||
* @method static Response post(string $uri, array $options = [])
|
|
||||||
* @method static Response put(string $uri, array $options = [])
|
|
||||||
* @method static Response delete(string $uri, array $options = [])
|
|
||||||
* @method static Response patch(string $uri, array $options = [])
|
|
||||||
* @method static Response head(string $uri, array $options = [])
|
|
||||||
* @method static PromiseInterface sendAsync(\Psr\Http\Message\RequestInterface $request, array $options = [])
|
|
||||||
* @method static PromiseInterface requestAsync(string $method, string $uri, array $options = [])
|
|
||||||
*/
|
|
||||||
class Guzzle extends Facade
|
|
||||||
{
|
|
||||||
protected static function getFacadeAccessor(): string
|
|
||||||
{
|
|
||||||
return Client::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Services\Facades;
|
|
||||||
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use RoadRunner\Logger\Logger;
|
|
||||||
use Siteworxpro\App\Services\Facade;
|
|
||||||
use Spiral\Goridge\RPC\RPC;
|
|
||||||
|
|
||||||
class RoadRunnerLogger extends Facade
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public static function getFacadeRoot(): mixed
|
|
||||||
{
|
|
||||||
$container = static::getFacadeContainer();
|
|
||||||
if ($container && $container->has(Logger::class) === false) {
|
|
||||||
$rpc = RPC::create($_SERVER['RR_RPC']);
|
|
||||||
$logger = new Logger($rpc);
|
|
||||||
$container->bind(static::getFacadeAccessor(), function () use ($logger) {
|
|
||||||
return $logger;
|
|
||||||
});
|
|
||||||
|
|
||||||
return $logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $container->get(Logger::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function getFacadeAccessor(): string
|
|
||||||
{
|
|
||||||
return Logger::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,36 +6,10 @@ namespace Siteworxpro\App\Services\ServiceProviders;
|
|||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworxpro\App\Async\Brokers\Broker;
|
use Siteworxpro\App\Async\Brokers\Broker;
|
||||||
use Siteworxpro\App\Async\Brokers\Kafka;
|
|
||||||
use Siteworxpro\App\Async\Brokers\RabbitMQ;
|
|
||||||
use Siteworxpro\App\Async\Brokers\Redis;
|
|
||||||
use Siteworxpro\App\Async\Brokers\Sqs;
|
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class BrokerServiceProvider
|
|
||||||
*
|
|
||||||
* This service provider is responsible for binding the Broker implementation
|
|
||||||
* to the Laravel service container based on configuration settings.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class BrokerServiceProvider extends ServiceProvider
|
class BrokerServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function provides(): array
|
|
||||||
{
|
|
||||||
return [Kafka::class, RabbitMQ::class, Redis::class, Sqs::class];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register services.
|
|
||||||
*
|
|
||||||
* This method binds the Broker interface to a specific implementation
|
|
||||||
* based on the configuration defined in 'queue.broker' and 'queue.broker_config'.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
* @throws \RuntimeException if the specified broker class does not exist.
|
|
||||||
*/
|
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(Broker::class, function (): Broker {
|
$this->app->singleton(Broker::class, function (): Broker {
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Services\ServiceProviders;
|
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
|
||||||
use League\Tactician\CommandBus;
|
|
||||||
use League\Tactician\Handler\CommandHandlerMiddleware;
|
|
||||||
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
|
|
||||||
use League\Tactician\Handler\MethodNameInflector\InvokeInflector;
|
|
||||||
use Siteworxpro\App\CommandBus\AttributeLocator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class CommandBusProvider
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class CommandBusProvider extends ServiceProvider
|
|
||||||
{
|
|
||||||
public function provides(): array
|
|
||||||
{
|
|
||||||
return [CommandBus::class];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function register(): void
|
|
||||||
{
|
|
||||||
$this->app->singleton(CommandBus::class, function () {
|
|
||||||
return new CommandBus([
|
|
||||||
new CommandHandlerMiddleware(
|
|
||||||
new ClassNameExtractor(),
|
|
||||||
new AttributeLocator(),
|
|
||||||
new InvokeInflector()
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,18 +7,8 @@ namespace Siteworxpro\App\Services\ServiceProviders;
|
|||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworxpro\App\Events\Dispatcher;
|
use Siteworxpro\App\Events\Dispatcher;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class DispatcherServiceProvider
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class DispatcherServiceProvider extends ServiceProvider
|
class DispatcherServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function provides(): array
|
|
||||||
{
|
|
||||||
return [Dispatcher::class];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(Dispatcher::class, function () {
|
$this->app->singleton(Dispatcher::class, function () {
|
||||||
|
|||||||
@@ -6,24 +6,13 @@ namespace Siteworxpro\App\Services\ServiceProviders;
|
|||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworxpro\App\Log\Logger;
|
use Siteworxpro\App\Log\Logger;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class LoggerServiceProvider
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class LoggerServiceProvider extends ServiceProvider
|
class LoggerServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function provides(): array
|
|
||||||
{
|
|
||||||
return [Logger::class];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(Logger::class, function () {
|
$this->app->singleton(Logger::class, function () {
|
||||||
return new Logger(Config::get('app.log_level'));
|
return new Logger();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,20 +8,8 @@ use Illuminate\Support\ServiceProvider;
|
|||||||
use Predis\Client;
|
use Predis\Client;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class RedisServiceProvider
|
|
||||||
*
|
|
||||||
* This service provider registers a Redis client as a singleton in the application container.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class RedisServiceProvider extends ServiceProvider
|
class RedisServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function provides(): array
|
|
||||||
{
|
|
||||||
return [Client::class];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(Client::class, function () {
|
$this->app->singleton(Client::class, function () {
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Attributes\Guards;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Attributes\Guards\Jwt;
|
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class JwtTest extends Unit
|
|
||||||
{
|
|
||||||
public function testGetsClassFromConfig(): void
|
|
||||||
{
|
|
||||||
Config::set('jwt.issuer', 'default-issuer');
|
|
||||||
Config::set('jwt.audience', 'default-audience');
|
|
||||||
|
|
||||||
$reflection = new \ReflectionClass(TestClass::class);
|
|
||||||
$attributes = $reflection->getAttributes(Jwt::class);
|
|
||||||
$this->assertCount(1, $attributes);
|
|
||||||
|
|
||||||
/** @var Jwt $instance */
|
|
||||||
$instance = $attributes[0]->newInstance();
|
|
||||||
$this->assertEquals('default-audience', $instance->getAudience());
|
|
||||||
$this->assertEquals('default-issuer', $instance->getIssuer());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetsClassFromCustom(): void
|
|
||||||
{
|
|
||||||
$reflection = new \ReflectionClass(TestClassSpecific::class);
|
|
||||||
$attributes = $reflection->getAttributes(Jwt::class);
|
|
||||||
$this->assertCount(1, $attributes);
|
|
||||||
|
|
||||||
/** @var Jwt $instance */
|
|
||||||
$instance = $attributes[0]->newInstance();
|
|
||||||
$this->assertEquals('custom-audience', $instance->getAudience());
|
|
||||||
$this->assertEquals('custom-issuer', $instance->getIssuer());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Jwt]
|
|
||||||
class TestClass // @codingStandardsIgnoreLine
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Jwt('custom-issuer', 'custom-audience')]
|
|
||||||
class TestClassSpecific // @codingStandardsIgnoreLine
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Attributes\Guards;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Attributes\Guards\Scope;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class ScopeTest extends Unit
|
|
||||||
{
|
|
||||||
public function testGetsClassSingle(): void
|
|
||||||
{
|
|
||||||
$reflection = new \ReflectionClass(TestClassSingle::class);
|
|
||||||
$attributes = $reflection->getAttributes(Scope::class);
|
|
||||||
$this->assertCount(1, $attributes);
|
|
||||||
|
|
||||||
/** @var Scope $instance */
|
|
||||||
$instance = $attributes[0]->newInstance();
|
|
||||||
$this->assertEquals(['read:users'], $instance->getScopes());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetsClassFromCustom(): void
|
|
||||||
{
|
|
||||||
$reflection = new \ReflectionClass(TestClassMultiple::class);
|
|
||||||
$attributes = $reflection->getAttributes(Scope::class);
|
|
||||||
$this->assertCount(1, $attributes);
|
|
||||||
|
|
||||||
/** @var Scope $instance */
|
|
||||||
$instance = $attributes[0]->newInstance();
|
|
||||||
$this->assertEquals(['read:users', 'write:users'], $instance->getScopes());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Scope(['read:users', 'write:users'])]
|
|
||||||
class TestClassMultiple // @codingStandardsIgnoreLine
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Scope(['read:users'])]
|
|
||||||
class TestClassSingle // @codingStandardsIgnoreLine
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Attributes;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Attributes\Async\HandlesMessage;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class HandlesMessageTest extends Unit
|
|
||||||
{
|
|
||||||
public function testGetsClass(): void
|
|
||||||
{
|
|
||||||
$class = new #[HandlesMessage('Siteworxpro\Tests\Attributes\TestClass')] class {
|
|
||||||
};
|
|
||||||
|
|
||||||
$reflection = new \ReflectionClass($class);
|
|
||||||
$attributes = $reflection->getAttributes(HandlesMessage::class);
|
|
||||||
$this->assertCount(1, $attributes);
|
|
||||||
|
|
||||||
/** @var HandlesMessage $instance */
|
|
||||||
$instance = $attributes[0]->newInstance();
|
|
||||||
$this->assertEquals('Siteworxpro\Tests\Attributes\TestClass', $instance->getMessageClass());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\CommandBus;
|
|
||||||
|
|
||||||
use League\Tactician\Exception\CanNotInvokeHandlerException;
|
|
||||||
use Siteworxpro\App\CommandBus\AttributeLocator;
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
|
||||||
use Siteworxpro\App\CommandBus\Handlers\ExampleHandler;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class AttributeLocatorTest extends Unit
|
|
||||||
{
|
|
||||||
private const array HANDLERS = [
|
|
||||||
ExampleCommand::class => ExampleHandler::class,
|
|
||||||
];
|
|
||||||
|
|
||||||
public function testResolvesFiles(): void
|
|
||||||
{
|
|
||||||
$attributeLocator = new AttributeLocator();
|
|
||||||
|
|
||||||
foreach (self::HANDLERS as $command => $handler) {
|
|
||||||
$class = $attributeLocator->getHandlerForCommand($command);
|
|
||||||
$this->assertInstanceOf($handler, $class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testThrowsOnCannotResolve(): void
|
|
||||||
{
|
|
||||||
$attributeLocator = new AttributeLocator();
|
|
||||||
|
|
||||||
$this->expectException(CanNotInvokeHandlerException::class);
|
|
||||||
$attributeLocator->getHandlerForCommand('NonExistentCommand');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\CommandBus\Handlers;
|
|
||||||
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\Command;
|
|
||||||
use Siteworxpro\App\CommandBus\Commands\ExampleCommand;
|
|
||||||
use Siteworxpro\App\CommandBus\Handlers\ExampleHandler;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class ExampleHandlerTest extends Unit
|
|
||||||
{
|
|
||||||
public function testExampleCommand(): void
|
|
||||||
{
|
|
||||||
$command = new ExampleCommand('test payload');
|
|
||||||
$this->assertEquals('test payload', $command->getName());
|
|
||||||
|
|
||||||
$handler = new ExampleHandler();
|
|
||||||
$result = $handler($command);
|
|
||||||
$this->assertEquals('Hello, test payload!', $result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testThrowsException(): void
|
|
||||||
{
|
|
||||||
$class = new readonly class extends Command
|
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
$this->expectException(\TypeError::class);
|
|
||||||
$handler = new ExampleHandler();
|
|
||||||
$handler($class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,45 +4,22 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\Tests\Controllers;
|
namespace Siteworxpro\Tests\Controllers;
|
||||||
|
|
||||||
use League\Tactician\CommandBus;
|
|
||||||
use Siteworxpro\App\Controllers\IndexController;
|
use Siteworxpro\App\Controllers\IndexController;
|
||||||
|
|
||||||
class IndexControllerTest extends AbstractController
|
class IndexControllerTest extends AbstractController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @throws \JsonException|\ReflectionException
|
* @throws \JsonException
|
||||||
*/
|
*/
|
||||||
public function testGet(): void
|
public function testGet(): void
|
||||||
{
|
{
|
||||||
$this->assertTrue(true);
|
$this->assertTrue(true);
|
||||||
|
|
||||||
$this->getContainer()->bind(CommandBus::class, function () {
|
|
||||||
return \Mockery::mock(CommandBus::class)
|
|
||||||
->shouldReceive('handle')
|
|
||||||
->andReturn('Hello World')
|
|
||||||
->getMock();
|
|
||||||
});
|
|
||||||
|
|
||||||
$controller = new IndexController();
|
$controller = new IndexController();
|
||||||
|
|
||||||
$response = $controller->get($this->getMockRequest());
|
$response = $controller->get($this->getMockRequest());
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$this->assertEquals('{"message":"Server is running. Hello World"}', (string)$response->getBody());
|
$this->assertEquals('{"status_code":200,"message":"Server is running"}', (string)$response->getBody());
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \JsonException
|
|
||||||
*/
|
|
||||||
public function testPost(): void
|
|
||||||
{
|
|
||||||
$this->assertTrue(true);
|
|
||||||
|
|
||||||
$controller = new IndexController();
|
|
||||||
|
|
||||||
$response = $controller->post($this->getMockRequest());
|
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
|
||||||
$this->assertEquals('{"message":"POST request received"}', (string)$response->getBody());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Controllers;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Controllers\OpenApiController;
|
|
||||||
|
|
||||||
class OpenApiControllerTest extends ControllerTest
|
|
||||||
{
|
|
||||||
public function testBuildsYaml(): void
|
|
||||||
{
|
|
||||||
$request = $this->getMockRequest('/.well-known/openapi.yaml');
|
|
||||||
$controller = new OpenApiController();
|
|
||||||
|
|
||||||
$response = $controller->get($request);
|
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
|
||||||
$this->assertStringContainsString('openapi: 3.0.0', (string)$response->getBody());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBuildsJson(): void
|
|
||||||
{
|
|
||||||
$request = $this->getMockRequest(uri: '/.well-known/openapi.json');
|
|
||||||
$controller = new OpenApiController();
|
|
||||||
|
|
||||||
$response = $controller->get($request);
|
|
||||||
|
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
|
||||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
|
||||||
$this->assertNotFalse(json_decode($response->getBody()->getContents()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Events;
|
|
||||||
|
|
||||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class DispatcherTest extends Unit
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @throws \Throwable
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
*/
|
|
||||||
public function testRegistersListeners(): void
|
|
||||||
{
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$eventFired = false;
|
|
||||||
$dispatcher->listen('TestEvent', function ($event) use (&$eventFired) {
|
|
||||||
$this->assertEquals('TestEvent', $event);
|
|
||||||
$eventFired = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
$dispatcher->dispatch('TestEvent');
|
|
||||||
$this->assertTrue($eventFired, 'The TestEvent listener was not fired.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
*/
|
|
||||||
public function testPushesEvents()
|
|
||||||
{
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$eventsFired = 0;
|
|
||||||
$dispatcher->listen('PushedEvent1', function ($event) use (&$eventsFired) {
|
|
||||||
$eventsFired++;
|
|
||||||
$this->assertEquals('PushedEvent1', $event);
|
|
||||||
});
|
|
||||||
|
|
||||||
$dispatcher->listen('PushedEvent2', function ($event) use (&$eventsFired) {
|
|
||||||
$eventsFired++;
|
|
||||||
$this->assertEquals('PushedEvent2', $event);
|
|
||||||
});
|
|
||||||
|
|
||||||
$dispatcher->push('PushedEvent1');
|
|
||||||
$dispatcher->push('PushedEvent2');
|
|
||||||
|
|
||||||
unset($dispatcher); // Trigger destructor
|
|
||||||
$this->assertEquals(2, $eventsFired);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
|
||||||
public function testFlushEvent(): void
|
|
||||||
{
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$eventFired = false;
|
|
||||||
$dispatcher->listen('FlushEvent', function ($event) use (&$eventFired) {
|
|
||||||
$this->assertEquals('FlushEvent', $event);
|
|
||||||
$eventFired = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
$dispatcher->push('FlushEvent');
|
|
||||||
$dispatcher->flush('FlushEvent');
|
|
||||||
|
|
||||||
$this->assertTrue($eventFired, 'The FlushEvent listener was not fired.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
*/
|
|
||||||
public function testHasListeners(): void
|
|
||||||
{
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$this->assertFalse(
|
|
||||||
$dispatcher->hasListeners(
|
|
||||||
'NonExistentEvent'
|
|
||||||
),
|
|
||||||
'Expected no listeners for NonExistentEvent.'
|
|
||||||
);
|
|
||||||
|
|
||||||
$dispatcher->listen('ExistingEvent', function () {
|
|
||||||
// Listener logic
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assertTrue(
|
|
||||||
$dispatcher->hasListeners(
|
|
||||||
'ExistingEvent'
|
|
||||||
),
|
|
||||||
'Expected listeners for ExistingEvent.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
|
||||||
public function testForgetEvent(): void
|
|
||||||
{
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$eventFired = false;
|
|
||||||
$dispatcher->listen('ForgetEvent', function ($event) use (&$eventFired) {
|
|
||||||
$this->assertEquals('ForgetEvent', $event);
|
|
||||||
$eventFired = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
$dispatcher->push('ForgetEvent');
|
|
||||||
$dispatcher->forget('ForgetEvent');
|
|
||||||
|
|
||||||
unset($dispatcher); // Trigger destructor
|
|
||||||
|
|
||||||
$this->assertFalse($eventFired, 'The ForgetEvent listener was fired but should have been forgotten.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
*/
|
|
||||||
public function testForgetPushed()
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$dispatcher->listen('EventToForget', function () {
|
|
||||||
$this->fail('The EventToForget listener was fired but should have been forgotten.');
|
|
||||||
});
|
|
||||||
|
|
||||||
$dispatcher->push('EventToForget');
|
|
||||||
$dispatcher->forgetPushed();
|
|
||||||
|
|
||||||
unset($dispatcher); // Trigger destructor
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
*/
|
|
||||||
public function testToArray(): void
|
|
||||||
{
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
|
|
||||||
$dispatcher->listen('ArrayEvent', function () {
|
|
||||||
// Listener logic
|
|
||||||
});
|
|
||||||
|
|
||||||
$arrayRepresentation = $dispatcher->toArray();
|
|
||||||
$this->assertArrayHasKey('ArrayEvent', $arrayRepresentation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws BindingResolutionException
|
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
|
||||||
public function testSubscriber()
|
|
||||||
{
|
|
||||||
$subscriber = $this->getMockBuilder('Siteworxpro\App\Events\Subscribers\Subscriber')
|
|
||||||
->onlyMethods(['handle'])
|
|
||||||
->getMock();
|
|
||||||
|
|
||||||
$subscriber->expects($this->once())
|
|
||||||
->method('handle')
|
|
||||||
->with('SubscribedEvent', [])
|
|
||||||
->willReturn(null);
|
|
||||||
|
|
||||||
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
||||||
$dispatcher->subscribe($subscriber);
|
|
||||||
|
|
||||||
$dispatcher->dispatch('SubscribedEvent');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Events\Listeners;
|
|
||||||
|
|
||||||
use Illuminate\Database\Events\ConnectionEstablished;
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use Psr\Log\LogLevel;
|
|
||||||
use Siteworxpro\App\Events\Listeners\Database\Connected;
|
|
||||||
use Siteworxpro\App\Log\Logger;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class ConnectedTest extends Unit
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws \ReflectionException
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
|
||||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()->bind(Logger::class, fn() => $logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testHandlesEvent()
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$connectedEvent = $this->createMock(ConnectionEstablished::class);
|
|
||||||
$listener = new Connected();
|
|
||||||
|
|
||||||
$listener->__invoke($connectedEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testThrowsException()
|
|
||||||
{
|
|
||||||
$this->expectException(\TypeError::class);
|
|
||||||
$listener = new Connected();
|
|
||||||
$listener->__invoke(new \stdClass());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Facades;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Services\Facades\Dispatcher;
|
|
||||||
|
|
||||||
class DispatcherTest extends AbstractFacade
|
|
||||||
{
|
|
||||||
protected function getFacadeClass(): string
|
|
||||||
{
|
|
||||||
return Dispatcher::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getConcrete(): string
|
|
||||||
{
|
|
||||||
return \Siteworxpro\App\Events\Dispatcher::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Facades;
|
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
|
||||||
use Siteworxpro\App\Services\Facades\Guzzle;
|
|
||||||
|
|
||||||
class GuzzleTest extends AbstractFacade
|
|
||||||
{
|
|
||||||
protected function getFacadeClass(): string
|
|
||||||
{
|
|
||||||
return Guzzle::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getConcrete(): string
|
|
||||||
{
|
|
||||||
return Client::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Facades;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
|
||||||
|
|
||||||
class LoggerTest extends AbstractFacade
|
|
||||||
{
|
|
||||||
protected function getFacadeClass(): string
|
|
||||||
{
|
|
||||||
return Logger::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getConcrete(): string
|
|
||||||
{
|
|
||||||
return \Siteworxpro\App\Log\Logger::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user