Skip to content

Commit c1574f2

Browse files
committed
Adjust syntax and features to support PHP 5.6 and above
1 parent fb138ba commit c1574f2

File tree

9 files changed

+473
-408
lines changed

9 files changed

+473
-408
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,31 @@ permissions:
88
jobs:
99
CI:
1010
runs-on: ubuntu-latest
11+
1112
strategy:
1213
matrix:
1314
php:
14-
- '8.1'
15-
- '8.2'
16-
- '8.3'
15+
- '5.6'
16+
- '7.1'
17+
- '7.2'
18+
- '7.3'
19+
- '7.4'
20+
- '8.0'
21+
1722
steps:
18-
- uses: shivammathur/setup-php@2.30.4
19-
with:
20-
php-version: ${{ matrix.php }}
2123
- uses: actions/checkout@v4
2224
with:
23-
fetch-depth: 2 # faster checkout
24-
- run: composer update --no-interaction --prefer-dist
25-
- run: composer run test
25+
fetch-depth: 2
26+
27+
- name: Setup PHP
28+
uses: shivammathur/setup-php@2.30.4
29+
with:
30+
php-version: ${{ matrix.php }}
31+
extensions: mbstring
32+
coverage: none
33+
34+
- name: Install Dependencies
35+
run: composer update --no-interaction --prefer-dist
36+
37+
- name: Run Tests
38+
run: composer run test

README.md

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
# `mindplay\php-vite`
1+
# php-vite
22

3-
[![PHP Version](https://img.shields.io/badge/php-8.1%2B-blue.svg)](https://packagist.org/packages/mindplay/php-vite)
4-
[![Build Status](https://github.com/mindplay-dk/php-vite/actions/workflows/ci.yml/badge.svg)](https://github.com/mindplay-dk/php-vite/actions/workflows/ci.yml)
3+
[![PHP Version](https://img.shields.io/badge/php-5.6%2B-blue.svg)](https://packagist.org/packages/ngekoding/php-vite)
4+
[![Build Status](https://github.com/mindplay-dk/php-vite/actions/workflows/ci.yml/badge.svg)](https://github.com/ngekoding/php-vite/actions/workflows/ci.yml)
55
[![License](https://img.shields.io/badge/license-MPL--2.0-green)](https://opensource.org/license/mpl-2-0)
66

7-
This library provides a lightweight [backend integration](https://vitejs.dev/guide/backend-integration.html)
8-
for your PHP-based MPA, SPA, or PWA based on [Vite](https://vitejs.dev/).
7+
This library provides a lightweight [backend integration](https://vitejs.dev/guide/backend-integration.html) for your PHP-based MPA, SPA, or PWA based on [Vite](https://vitejs.dev/).
98

10-
It parses the [build manifest](https://vitejs.dev/config/build-options#build-manifest) (the `.vite/manifest.json` file)
11-
and produces the required `<script>` and `<link>` tags to load (and preload) scripts, CSS files, and other assets.
9+
It parses the [build manifest](https://vitejs.dev/config/build-options#build-manifest) (the `.vite/manifest.json` file) and produces the required `<script>` and `<link>` tags to load (and preload) scripts, CSS files, and other assets.
10+
11+
## PHP 5.6 Compatible Version
12+
13+
> ⚠️ This is a customized fork of the original [`mindplay-dk/php-vite`](https://github.com/mindplay-dk/php-vite) package.
14+
15+
This version has been adapted to support **PHP 5.6 and above**, making it suitable for legacy projects that are still running on older PHP versions.
16+
17+
### Key differences from the original:
18+
19+
* Rewritten to remove syntax and language features that require PHP 8.1+
20+
* Compatible with PHP 5.6, 7.x, and newer
21+
* Namespaces changed to `Ngekoding\Vite` to distinguish from the original package
22+
* Test suite migrated to use PHPUnit
23+
24+
If you're working on modern PHP (8.1+), you are encouraged to use the original package:
25+
👉 [mindplay-dk/php-vite](https://github.com/mindplay-dk/php-vite)
1226

1327
## Basic Usage
1428

@@ -20,20 +34,20 @@ In the following steps, we'll cover usage of the library API only.
2034
#### 1. Load the `manifest.json` file created by Vite:
2135

2236
```php
23-
use mindplay\vite\Manifest;
37+
use Ngekoding\Vite\Manifest;
2438

2539
$vite = new Manifest(
26-
manifest_path: $your_root_dir . '/public/dist/.vite/manifest.json',
27-
base_path: '/dist/',
28-
dev: false
40+
false, // dev mode (true = development, false = production)
41+
'/path/to/manifest.json', // path to manifest.json
42+
'/dist/' // public base path
2943
);
3044
```
3145

32-
The `manifest_path` points to the Vite `manifest.json` file created for the production build.
33-
34-
In this example, `dev` is `false`, so we'll be creating tags for the production assets.
46+
The constructor accepts three parameters:
3547

36-
The `base_path` is relative to your public web root - it is the root folder from which Vite's production assets are served, and/or the root folder from which Vite serves assets dynamically in development mode.
48+
1. `dev` – set to `true` for development mode, or `false` for production mode.
49+
2. `manifestPath` – points to the Vite `manifest.json` file created for the production build.
50+
3. `basePath` – is relative to your public web root, it is the root folder from which Vite's production assets are served, and/or the root folder from which Vite serves assets dynamically in development mode.
3751

3852
Note that, in development mode (when `dev` is set to `true`) the `manifest.json` file is unused, and not required.
3953

@@ -83,23 +97,20 @@ For example:
8397

8498
The service preloads any statically imported scripts and CSS files by default.
8599

86-
In addition, you can configure it to preload other statically imported assets as well -
87-
for convenience, there are two methods to automatically configure preloading of all
88-
common image and font asset types:
100+
In addition, you can configure it to preload other statically imported assets as well - for convenience, there are two methods to automatically configure preloading of all common image and font asset types:
89101

90102
```php
91103
$manifest->preloadImages();
92104
$manifest->preloadFonts();
93105
```
94106

95-
You can also configure it to preload any other asset types - for example, to configure
96-
preloading of `.json` assets, you could add the following:
107+
You can also configure it to preload any other asset types - for example, to configure preloading of `.json` assets, you could add the following:
97108

98109
```php
99110
$manifest->preload(
100-
ext: "json",
101-
mime_type: "application/json",
102-
preload_as: "fetch"
111+
"json", // file extension (without dot)
112+
"application/json", // MIME type
113+
"fetch" // preload `as` attribute
103114
);
104115
```
105116

@@ -110,7 +121,7 @@ Then create your tags as covered in the documentation above.
110121
For advanced use cases, you can also directly get the URL for an asset published by Vite:
111122

112123
```php
113-
$my_url = $manifest->getURL("consent-banner.ts");
124+
$url = $manifest->getURL("consent-banner.ts");
114125
```
115126

116127
You can use this feature to, for example:

composer.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
{
2-
"name": "mindplay/php-vite",
2+
"name": "ngekoding/php-vite",
33
"type": "library",
44
"license": "MPL-2.0",
55
"description": "a lightweight PHP-backend integration for the Vite frontend build tool",
66
"scripts": {
7-
"test": "XDEBUG_MODE=coverage php test/test.php"
7+
"test": "./vendor/bin/phpunit"
88
},
99
"autoload": {
1010
"psr-4": {
11-
"mindplay\\vite\\": "src/"
11+
"Ngekoding\\Vite\\": "src/"
12+
}
13+
},
14+
"autoload-dev": {
15+
"psr-4": {
16+
"Ngekoding\\Vite\\": "test/"
1217
}
1318
},
1419
"require": {
15-
"php": ">=8.1"
20+
"php": ">=5.6"
1621
},
1722
"require-dev": {
18-
"mindplay/testies": "^1.1",
19-
"phpunit/php-code-coverage": "^10"
23+
"phpunit/phpunit": "^5.7.0"
2024
}
2125
}

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit>
3+
<testsuites>
4+
<testsuite name="default">
5+
<directory>test</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

0 commit comments

Comments
 (0)