Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
* text=auto eol=lf

/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs export-ignore
.travis.yml export-ignore
phpcs.xml.dist export-ignore
phpunit.xml.dist export-ignore
/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php-cs-fixer.php export-ignore
phpcs.xml.dist export-ignore
phpunit.xml.dist export-ignore
.phpstan.neon export-ignore
73 changes: 22 additions & 51 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: "testing"

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
qa:
Expand All @@ -13,19 +13,19 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
Expand All @@ -40,63 +40,34 @@ jobs:

strategy:
matrix:
php:
- 7.2
- 7.3
- 7.4
composer-args: [ "" ]
include:
- php: 8.0
composer-args: --ignore-platform-reqs
fail-fast: false
php:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
- 8.4

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Cache PHP dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress ${{ matrix.composer-args }}
run: composer install --prefer-dist --no-progress

- name: Tests
run: composer test

- name: Tests coverage
run: composer coverage

static-analysis:
name: Static Analysis
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4

- name: Cache PHP dependencies
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Static Analysis
run: composer run-script phpstan -- --level=5 src/ tests/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ vendor
composer.lock
coverage
*.cache
.idea
kit
10 changes: 10 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return My\PhpCsFixerConfig::create()
->setFinder(
PhpCsFixer\Finder::create()
->files()
->name('*.php')
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
);
5 changes: 5 additions & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
- tests
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [3.2.0] - 2025-03-21
### Added
- Support for PHP 8.4

## [3.1.1] - 2022-03-13
### Fixed
- Get the session name from options if available and not explicity set [#14]
Expand Down Expand Up @@ -90,6 +94,7 @@ First version
[#9]: https://github.com/middlewares/php-session/issues/9
[#14]: https://github.com/middlewares/php-session/issues/14

[3.2.0]: https://github.com/middlewares/php-session/compare/v3.1.1...v3.2.0
[3.1.1]: https://github.com/middlewares/php-session/compare/v3.1.0...v3.1.1
[3.1.0]: https://github.com/middlewares/php-session/compare/v3.0.1...v3.1.0
[3.0.1]: https://github.com/middlewares/php-session/compare/v3.0.0...v3.0.1
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019
Copyright (c) 2019-2025

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
},
"require": {
"php": "^7.2 || ^8.0",
"psr/http-server-middleware": "^1.0"
"psr/http-server-middleware": "^1"
},
"require-dev": {
"middlewares/utils": "^3.0",
"phpunit/phpunit": "^8|^9",
"friendsofphp/php-cs-fixer": "^2.0",
"squizlabs/php_codesniffer": "^3.0",
"oscarotero/php-cs-fixer-config": "^1.0",
"phpstan/phpstan": "^0.12",
"laminas/laminas-diactoros": "^2.3"
"middlewares/utils": "^2 || ^3 || ^4",
"phpunit/phpunit": "^8 || ^9",
"friendsofphp/php-cs-fixer": "^3",
"squizlabs/php_codesniffer": "^3",
"oscarotero/php-cs-fixer-config": "^2",
"phpstan/phpstan": "^1 || ^2",
"laminas/laminas-diactoros": "^2 || ^3"
},
"autoload": {
"psr-4": {
Expand All @@ -46,4 +46,4 @@
"coverage": "phpunit --coverage-text",
"coverage-html": "phpunit --coverage-html=coverage"
}
}
}
16 changes: 16 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<ruleset name="Middlewares coding standard">
<description>Middlewares coding standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="report" value="full"/>
<arg name="colors"/>

<!-- coding standard -->
<rule ref="PSR2"/>

<!-- Paths to check -->
<file>src</file>
<file>tests</file>
</ruleset>
33 changes: 33 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
failOnWarning="true">
<testsuites>
<testsuite name="PhpSession test suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
18 changes: 12 additions & 6 deletions src/PhpSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PhpSession implements MiddlewareInterface
private $id;

/**
* @var array|null
* @var array<int|bool|string|float>|null
*/
private $options;

Expand Down Expand Up @@ -59,6 +59,7 @@ public function id(string $id): self
/**
* Set the session options.
*
* @param array<int|bool|string|float> $options
* @throws RuntimeException
*/
public function options(array $options): self
Expand Down Expand Up @@ -92,10 +93,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

// Session name
$name = $this->name ?? $this->options['name'] ?? session_name();
session_name($name);
session_name((string) $name);

// Session ID
$id = $this->id ?: self::readSessionCookie($request, $name);
$id = $this->id ?: self::readSessionCookie($request, (string) $name);
if (!empty($id)) {
session_id($id);
}
Expand All @@ -119,8 +120,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if (session_id() !== $id) {
$response = self::writeSessionCookie(
$response,
session_name(),
session_id(),
(string) session_name(),
(string) session_id(),
time(),
session_get_cookie_params()
);
Expand All @@ -132,6 +133,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/**
* Check PHP session settings for compatibility with PSR-7.
*
* @param array<int|bool|string|float> $options
* @throws RuntimeException
*/
private static function checkSessionSettings(array $options): void
Expand Down Expand Up @@ -178,7 +180,7 @@ private static function checkSessionCanStart(): void
/**
* Regenerate the session ID if it's needed.
*/
private static function runIdRegeneration(int $interval = null, string $key = null): void
private static function runIdRegeneration(?int $interval = null, ?string $key = null): void
{
if (empty($interval)) {
return;
Expand All @@ -203,11 +205,14 @@ private static function runIdRegeneration(int $interval = null, string $key = nu
private static function readSessionCookie(ServerRequestInterface $request, string $name): string
{
$cookies = $request->getCookieParams();

return $cookies[$name] ?? '';
}

/**
* Write a session cookie to the PSR-7 response.
*
* @param array<bool|int|string> $params
*/
private static function writeSessionCookie(
ResponseInterface $response,
Expand All @@ -220,6 +225,7 @@ private static function writeSessionCookie(

// if omitted, the cookie will expire at end of the session (ie when the browser closes)
if (!empty($params['lifetime'])) {
// @phpstan-ignore-next-line
$expires = gmdate('D, d M Y H:i:s T', $now + $params['lifetime']);
$cookie .= "; Expires={$expires}; Max-Age={$params['lifetime']}";
}
Expand Down
Loading