Skip to content

Commit 5425321

Browse files
committed
v2.0.x updates
1 parent 9d07d64 commit 5425321

26 files changed

+425
-1598
lines changed

.github/workflows/tests.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
tags: [ 'v*' ]
7+
pull_request:
8+
branches: [ main, master ]
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
php-version: ['8.0', '8.1', '8.2', '8.3']
17+
18+
name: PHP ${{ matrix.php-version }}
19+
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php-version }}
27+
extensions: mbstring
28+
coverage: none
29+
30+
- name: Validate composer.json
31+
run: composer validate --strict --no-check-lock
32+
33+
- name: Cache Composer packages
34+
id: composer-cache
35+
uses: actions/cache@v3
36+
with:
37+
path: vendor
38+
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}
39+
restore-keys: |
40+
${{ runner.os }}-php-${{ matrix.php-version }}-
41+
42+
- name: Install dependencies
43+
run: composer install --prefer-dist --no-progress
44+
45+
- name: Run test suite
46+
run: vendor/bin/phpunit

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
vendor
33
.phpunit.result.cache
44
/phpunit.xml
5+
composer.lock
6+
.DS_Store

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

Browser.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Browser.php v2.x Backward Compatibility Shim
4+
*
5+
* This file provides backward compatibility for users who include Browser.php directly
6+
* without using Composer's autoloader.
7+
*
8+
* For new projects, please use the namespaced class:
9+
* use cbschuld\Browser;
10+
* $browser = new Browser();
11+
*
12+
* Legacy usage (deprecated but still works):
13+
* require_once 'Browser.php';
14+
* $browser = new Browser();
15+
*/
16+
17+
// Include the actual Browser class
18+
require_once __DIR__ . '/src/Browser.php';
19+
20+
// Create global alias for backward compatibility
21+
if (!class_exists('Browser', false)) {
22+
class_alias('cbschuld\\Browser', 'Browser');
23+
}
24+
25+
// Optional: Trigger deprecation notice (uncomment if desired)
26+
// trigger_error(
27+
// 'Using the global Browser class is deprecated. Please use cbschuld\\Browser instead.',
28+
// E_USER_DEPRECATED
29+
// );

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@ changes when it socially makes sense.
77

88
## [Unreleased]
99

10+
## [2.0.0] - 2025-08-31
11+
### Added
12+
- PSR-4 autoloading with `cbschuld` namespace
13+
- Return type declarations on key methods (setVersion, checkBrowserEdge, etc.)
14+
- `compareVersion()` method for PHP 8+ compatible version comparisons
15+
- Enhanced Edge browser detection (supports Edge/, Edg/, EdgA/, EdgiOS/ patterns)
16+
- Root-level Browser.php shim for non-Composer backward compatibility
17+
- GitHub Actions CI pipeline (replaces Travis CI)
18+
- Support metadata in composer.json
19+
- Comprehensive upgrade documentation (UPGRADING.md)
20+
21+
### Changed
22+
- **BREAKING**: Minimum PHP version is now 8.0 (was 7.2)
23+
- **BREAKING**: Added return type declarations to key methods (may affect extenders)
24+
- **BREAKING**: Class is now namespaced as `cbschuld\Browser`
25+
- Updated PHPUnit to 9.6 for broad PHP 8.x compatibility
26+
- Updated phpunit.xml.dist to PHPUnit 9.6 schema
27+
- Fixed fgetcsv escape parameter for PHP 8.1+ compatibility
28+
- Removed composer.lock from repository (library best practice)
29+
30+
### Removed
31+
- Support for PHP 7.x
32+
- Travis CI configuration (.travis.yml)
33+
- Composer branch alias
34+
35+
### Migration
36+
- See UPGRADING.md for detailed migration instructions
37+
- Backward compatibility maintained via automatic class aliasing
38+
- Non-Composer users can still use direct includes
39+
1040
## [1.9.4] - 2019-07-09
1141
### Added
1242
- Added better support for Firefox Mobile

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2013 - 2019 Chris Schuld <[email protected]>
1+
Copyright 2013 - 2025 Chris Schuld <[email protected]>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

README.md

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
11
# cbschuld/browser.php
22

3-
[![Build Status](https://travis-ci.org/cbschuld/Browser.php.png?branch=master)](https://travis-ci.org/cbschuld/Browser.php)
3+
[![Tests](https://github.com/cbschuld/Browser.php/actions/workflows/tests.yml/badge.svg)](https://github.com/cbschuld/Browser.php/actions/workflows/tests.yml)
44

55
Helps detect the user's browser and platform at the PHP level via the user agent
66

77

88
## Installation
99

10-
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
10+
### Via Composer (Recommended)
1111

12-
composer require cbschuld/browser.php
12+
```bash
13+
composer require cbschuld/browser.php
14+
```
15+
16+
For development only:
17+
```bash
18+
composer require --dev cbschuld/browser.php
19+
```
20+
21+
### Direct Download
22+
23+
Download `Browser.php` and include it in your project:
24+
```php
25+
require_once 'Browser.php';
26+
```
1327

14-
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
28+
## Usage
1529

16-
composer require --dev cbschuld/browser.php
30+
### Modern Usage (v2.0+, Recommended)
31+
32+
```php
33+
use cbschuld\Browser;
1734

35+
$browser = new Browser();
36+
if ($browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->compareVersion('10', '>=')) {
37+
echo 'You have Firefox version 10 or greater';
38+
}
39+
```
1840

19-
## Typical Usage:
41+
### Legacy Usage (Still Supported)
2042

2143
```php
44+
// Works via automatic aliasing - no namespace needed
2245
$browser = new Browser();
23-
if( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >=10 ) {
24-
echo 'You have FireFox version 10 or greater';
46+
if ($browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 10) {
47+
echo 'You have Firefox version 10 or greater';
48+
}
49+
```
50+
51+
### Version Comparison (PHP 8+ Compatible)
52+
53+
```php
54+
use cbschuld\Browser;
55+
56+
$browser = new Browser();
57+
58+
// Recommended approach for version comparison
59+
if ($browser->compareVersion('15.0', '>=')) {
60+
echo 'Modern browser detected';
61+
}
62+
63+
// Multiple comparisons
64+
if ($browser->getBrowser() == Browser::BROWSER_CHROME &&
65+
$browser->compareVersion('90', '>=')) {
66+
echo 'Modern Chrome detected';
2567
}
2668
```
2769

@@ -53,11 +95,11 @@ This solution identifies the following Browsers and does a best-guess on the ver
5395
* Playstation (`Browser::BROWSER_PLAYSTATION`)
5496
* iPhone (`Browser::BROWSER_IPHONE`)
5597
* iPod (`Browser::BROWSER_IPOD`)
56-
* Google.s Android(`Browser::BROWSER_ANDROID`)
57-
* Google.s Chrome(`Browser::BROWSER_CHROME`)
98+
* Google's Android(`Browser::BROWSER_ANDROID`)
99+
* Google's Chrome(`Browser::BROWSER_CHROME`)
58100
* GoogleBot(`Browser::BROWSER_GOOGLEBOT`)
59-
* Yahoo!.s Slurp(`Browser::BROWSER_SLURP`)
60-
* W3C.s Validator(`Browser::BROWSER_W3CVALIDATOR`)
101+
* Yahoo!'s Slurp(`Browser::BROWSER_SLURP`)
102+
* W3C's Validator(`Browser::BROWSER_W3CVALIDATOR`)
61103
* BlackBerry(`Browser::BROWSER_BLACKBERRY`)
62104

63105
## Operating System Detection
@@ -87,7 +129,7 @@ Detecting the user's browser type and version is helpful in web applications tha
87129

88130
In an active project of mine we have a pretty graphically intensive and visually appealing user interface which leverages a lot of transparent PNG files. Because we all know how great IE6 supports PNG files it was necessary for us to tell our users the lack of power their browser has in a kind way.
89131

90-
Searching for a way to do this at the PHP layer and not at the client layer was more of a challenge than I would have guessed; the only script available was written by Gary White and Gary no longer maintains this script because of reliability. I do agree 100% with Gary about the readability; however, there are realistic reasons to desire the user.s browser and browser version and if your visitor is not echoing a false user agent we can take an educated guess.
132+
Searching for a way to do this at the PHP layer and not at the client layer was more of a challenge than I would have guessed; the only script available was written by Gary White and Gary no longer maintains this script because of reliability. I do agree 100% with Gary about the readability; however, there are realistic reasons to desire the user's browser and browser version and if your visitor is not echoing a false user agent we can take an educated guess.
91133

92134
I based this solution off of Gary White's original work but have since replaced all of his original code. Either way, thank you to Gary. Sadly, I never was able to get in touch with him regarding this solution.
93135

@@ -106,7 +148,17 @@ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Ge
106148
Tests can be run by phpunit:
107149

108150
```bash
109-
vendor/phpunit/phpunit/phpunit
151+
vendor/bin/phpunit
110152
```
111153

154+
## Upgrading from v1.x
155+
156+
See [UPGRADING.md](UPGRADING.md) for detailed migration instructions.
157+
158+
**Quick Summary:**
159+
- v2.0 requires PHP 8.0+
160+
- Class is now namespaced: `cbschuld\Browser`
161+
- Backward compatibility maintained via automatic aliasing
162+
- Use `compareVersion()` for reliable version comparisons
163+
112164

UPGRADING.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Upgrading from v1.x to v2.0
2+
3+
## Breaking Changes
4+
5+
### PHP Version Requirement
6+
- **Minimum PHP version**: 8.0+ (was 7.2+)
7+
- **Reason**: Enables modern PHP features and improved performance
8+
9+
### Namespace Introduction
10+
- **New namespace**: `cbschuld\Browser`
11+
- **Migration path**: See usage examples below
12+
13+
### Return Type Declarations
14+
- Added return type hints to protected methods
15+
- **Impact**: If you extend the `Browser` class and override protected methods, you'll need to add matching return types
16+
- **Example**: `protected function checkBrowserEdge(): bool` (was `protected function checkBrowserEdge()`)
17+
18+
## Migration Guide
19+
20+
### For Composer Users
21+
22+
#### Option 1: Use Namespaced Class (Recommended)
23+
```php
24+
// Old (v1.x)
25+
$browser = new Browser();
26+
27+
// New (v2.x) - Recommended
28+
use cbschuld\Browser;
29+
$browser = new Browser();
30+
```
31+
32+
#### Option 2: Keep Using Global Class (Backward Compatible)
33+
```php
34+
// Still works in v2.x due to automatic aliasing
35+
$browser = new Browser();
36+
```
37+
38+
### For Non-Composer Users
39+
40+
#### Direct Include (Still Works)
41+
```php
42+
// Include the root Browser.php file
43+
require_once '/path/to/Browser.php';
44+
$browser = new Browser(); // Works via automatic aliasing
45+
```
46+
47+
#### Manual Namespace Include
48+
```php
49+
// Include the namespaced file directly
50+
require_once '/path/to/src/Browser.php';
51+
use cbschuld\Browser;
52+
$browser = new Browser();
53+
```
54+
55+
### For Class Extenders
56+
57+
If you extend the Browser class, update your method signatures:
58+
59+
```php
60+
// Old (v1.x)
61+
class MyBrowser extends Browser {
62+
protected function checkBrowserEdge() {
63+
// your implementation
64+
}
65+
}
66+
67+
// New (v2.x)
68+
use cbschuld\Browser;
69+
70+
class MyBrowser extends Browser {
71+
protected function checkBrowserEdge(): bool {
72+
// your implementation
73+
}
74+
}
75+
```
76+
77+
## New Features in v2.0
78+
79+
### Improved Version Comparison
80+
Use the new `compareVersion()` method for PHP 8+ compatible version comparisons:
81+
82+
```php
83+
$browser = new Browser();
84+
// Instead of: $browser->getVersion() >= '10.0'
85+
// Use: $browser->compareVersion('10.0', '>=')
86+
```
87+
88+
### Enhanced Edge Detection
89+
- Better support for modern Edge user agents
90+
- Handles Edge/, Edg/, EdgA/, and EdgiOS/ patterns
91+
92+
## Troubleshooting
93+
94+
### Tests Failing After Upgrade
95+
- Ensure you're using PHP 8.0+
96+
- Run `composer install` to get compatible dependencies
97+
- Update any custom test classes that extend Browser
98+
99+
### Class Not Found Errors
100+
- For Composer users: Run `composer dump-autoload`
101+
- For direct includes: Ensure you're including the root `Browser.php` file
102+
103+
### Version Comparison Issues
104+
- Replace direct version comparisons with `compareVersion()` method
105+
- This handles PHP 8's stricter type comparison rules
106+
107+
## Need Help?
108+
109+
- [GitHub Issues](https://github.com/cbschuld/Browser.php/issues)
110+
- [Documentation](https://github.com/cbschuld/Browser.php)

0 commit comments

Comments
 (0)