Skip to content

Commit 125661e

Browse files
committed
feat: add configurable cache directory option for session caching
- Added `cache_dir` option to allow custom cache directory configuration - Updated SessionCache to accept and prioritize custom cache directory over platform defaults - Added getCacheDir() method to Options class to retrieve custom cache directory - Updated README with comprehensive documentation on cache directory configuration and examples for different hosting environments (shared hosting, cross-platform, project-specific) - Documente
1 parent cae0ded commit 125661e

File tree

7 files changed

+214
-6
lines changed

7 files changed

+214
-6
lines changed

CHANGELOG.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,58 @@ Alle wichtigen Änderungen an diesem Projekt werden in dieser Datei dokumentiert
55
Das Format basiert auf [Keep a Changelog](https://keepachangelog.com/de/1.0.0/),
66
und dieses Projekt folgt [Semantic Versioning](https://semver.org/lang/de/).
77

8+
## [2.2.0] - 2025-11-15
9+
10+
### Hinzugefügt
11+
12+
- **Eigene Cache-Verzeichnisse**: Neue Option `cache_dir` ermöglicht die Angabe eines eigenen Cache-Verzeichnisses für Session-Dateien
13+
- Löst Probleme mit `open_basedir`-Einschränkungen in Shared-Hosting-Umgebungen (z.B. Plesk)
14+
- Unterstützt beliebige Verzeichnisse: `/tmp/`, `sys_get_temp_dir()`, oder projektspezifische Pfade
15+
- Vollständig rückwärtskompatibel - bestehender Code funktioniert ohne Änderungen
16+
- **Dokumentation**: Umfassende Dokumentation der `cache_dir`-Option im README
17+
- Beispiele für verschiedene Hosting-Umgebungen
18+
- Cache-Verzeichnis-Priorität erklärt
19+
- Empfohlene Konfigurationen für Plesk und andere Shared-Hosting-Anbieter
20+
- **Beispiele**: Neue Datei `example-custom-cache.php` mit 5 verschiedenen Anwendungsbeispielen
21+
- **Technische Dokumentation**: `CACHE_DIR_FIX.md` mit detaillierter Implementierungsdokumentation
22+
23+
### Geändert
24+
25+
- `SessionCache::__construct()` akzeptiert jetzt optionalen `$customCacheDir`-Parameter
26+
- `SessionCache::getCacheDir()` priorisiert eigenes Cache-Verzeichnis über plattformspezifische Defaults
27+
- `Options::getCacheDir()` neue Methode zum Abrufen der `cache_dir`-Option
28+
- `HttpClient` übergibt `cache_dir` aus Optionen an `SessionCache`
29+
- README aktualisiert mit `cache_dir`-Dokumentation und Anwendungsbeispielen
30+
31+
### Behoben
32+
33+
- Session-Cache funktioniert jetzt in Umgebungen mit `open_basedir`-Einschränkungen
34+
- Keine PHP-Warnungen mehr bei eingeschränkten Dateisystem-Zugriffen
35+
36+
### Technische Details
37+
38+
**Cache-Verzeichnis-Priorität:**
39+
40+
1. Eigenes Verzeichnis (wenn `cache_dir` Option gesetzt)
41+
2. Plattformspezifische Standard-Verzeichnisse
42+
3. Fallback auf `/tmp/` oder `sys_get_temp_dir()`
43+
44+
**Verwendungsbeispiel:**
45+
46+
```php
47+
$pxrest = new Client(
48+
'https://myserver.ch:999',
49+
'DEMO',
50+
'USR',
51+
'password',
52+
'ADR,STU',
53+
[
54+
'enable_session_caching' => true,
55+
'cache_dir' => sys_get_temp_dir() . '/proffix-cache'
56+
]
57+
);
58+
```
59+
860
## [2.1.0] - 2025-11-12
961

1062
### Hinzugefügt

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Optionen sind **fakultativ** und werden in der Regel nicht benötigt:
6969
| timeout | `15` | Timeout für Curl in Sekunden; Standard = 15 |
7070
| follow_redirects | `true` | Weiterleitungen der API folgen; Standard = false |
7171
| enable_session_caching | `true` | Session-Caching aktivieren; Standard = true |
72+
| cache_dir | `/tmp/proffix-cache` | Eigenes Cache-Verzeichnis; Standard = plattformspezifisch |
7273

7374
### Session-Caching
7475

@@ -83,22 +84,62 @@ Der Wrapper unterstützt automatisches Session-Caching, um die Performance zu ve
8384

8485
**Cache-Speicherort:**
8586

87+
Der Cache-Speicherort kann über die Option `cache_dir` angepasst werden. Dies ist besonders nützlich bei Hosting-Umgebungen mit `open_basedir`-Einschränkungen.
88+
89+
**Standard-Speicherorte** (wenn `cache_dir` nicht angegeben):
90+
8691
- **Windows:** `%APPDATA%/php-wrapper-proffix-restapi/`
8792
- **Linux/Mac:** `~/.cache/php-wrapper-proffix-restapi/` oder `/tmp/php-wrapper-proffix-restapi/`
8893

8994
Der Dateiname wird aus Benutzername, Datenbank und URL generiert, um Konflikte bei mehreren Clients zu vermeiden.
9095

96+
**Eigenes Cache-Verzeichnis verwenden:**
97+
98+
```php
99+
$pxrest = new Client(
100+
'https://myserver.ch:999',
101+
'DEMO',
102+
'USR',
103+
'b62cce2fe18f7a156a9c719c57bebf0478a3d50f0d7bd18d9e8a40be2e663017',
104+
'ADR,STU',
105+
[
106+
'enable_session_caching' => true,
107+
'cache_dir' => '/tmp/proffix-cache' // Eigenes Verzeichnis
108+
]
109+
);
110+
```
111+
112+
**Empfohlene Cache-Verzeichnisse für verschiedene Umgebungen:**
113+
114+
```php
115+
// Shared Hosting (z.B. Plesk) mit open_basedir-Einschränkungen
116+
'cache_dir' => '/tmp/proffix-cache'
117+
118+
// Cross-Platform (empfohlen)
119+
'cache_dir' => sys_get_temp_dir() . '/proffix-cache'
120+
121+
// Innerhalb des Projekts
122+
'cache_dir' => __DIR__ . '/cache'
123+
```
124+
91125
**Technische Details:**
92126

93127
Die Session-Verwaltung erfolgt über die `SessionCache`-Klasse (`src/RestAPIWrapperProffix/HttpClient/SessionCache.php`), die folgende Funktionen bietet:
94128

95129
- `load()`: Lädt eine gespeicherte Session-ID aus dem Cache
96130
- `save($sessionId)`: Speichert eine Session-ID im Cache
97131
- `clear()`: Löscht die gespeicherte Session-ID
132+
- Unterstützung für eigene Cache-Verzeichnisse (Option `cache_dir`)
98133
- Automatische Erkennung des plattformspezifischen Cache-Verzeichnisses
99134
- Thread-sichere Dateioperationen mit `LOCK_EX`
100135
- Kollisionsvermeidung durch Base64-URL-kodierte Dateinamen
101136

137+
**Cache-Verzeichnis-Priorität:**
138+
139+
1. Eigenes Verzeichnis (wenn `cache_dir` Option gesetzt)
140+
2. Plattformspezifische Standard-Verzeichnisse
141+
3. Fallback auf `/tmp/` oder `sys_get_temp_dir()`
142+
102143
**Session-Caching deaktivieren:**
103144

104145
```php

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.2.0

example-custom-cache.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Example: Using custom cache directory to avoid open_basedir restrictions
4+
*
5+
* This example shows how to specify a custom cache directory that complies
6+
* with your hosting environment's open_basedir restrictions.
7+
*/
8+
9+
require_once __DIR__ . '/vendor/autoload.php';
10+
11+
use Pitwch\RestAPIWrapperProffix\Client;
12+
13+
// Example 1: Using /tmp/ directory (commonly allowed by open_basedir)
14+
$options1 = [
15+
'enable_session_caching' => true,
16+
'cache_dir' => '/tmp/proffix-cache'
17+
];
18+
19+
$client1 = new Client(
20+
'https://work.pitw.ch',
21+
'DEMODB',
22+
'TM',
23+
'your-password',
24+
'',
25+
$options1
26+
);
27+
28+
// Example 2: Using sys_get_temp_dir() (cross-platform)
29+
$options2 = [
30+
'enable_session_caching' => true,
31+
'cache_dir' => sys_get_temp_dir() . '/proffix-cache'
32+
];
33+
34+
$client2 = new Client(
35+
'https://work.pitw.ch',
36+
'DEMODB',
37+
'TM',
38+
'your-password',
39+
'',
40+
$options2
41+
);
42+
43+
// Example 3: Using a custom directory within your web root
44+
$options3 = [
45+
'enable_session_caching' => true,
46+
'cache_dir' => __DIR__ . '/cache'
47+
];
48+
49+
$client3 = new Client(
50+
'https://work.pitw.ch',
51+
'DEMODB',
52+
'TM',
53+
'your-password',
54+
'',
55+
$options3
56+
);
57+
58+
// Example 4: Backward compatibility - no custom cache_dir (uses default behavior)
59+
$options4 = [
60+
'enable_session_caching' => true
61+
];
62+
63+
$client4 = new Client(
64+
'https://work.pitw.ch',
65+
'DEMODB',
66+
'TM',
67+
'your-password',
68+
'',
69+
$options4
70+
);
71+
72+
// Example 5: Disable session caching entirely
73+
$options5 = [
74+
'enable_session_caching' => false
75+
];
76+
77+
$client5 = new Client(
78+
'https://work.pitw.ch',
79+
'DEMODB',
80+
'TM',
81+
'your-password',
82+
'',
83+
$options5
84+
);
85+
86+
echo "✅ All client configurations created successfully!\n";
87+
echo "Cache directories:\n";
88+
echo " - Client 1: /tmp/proffix-cache\n";
89+
echo " - Client 2: " . sys_get_temp_dir() . "/proffix-cache\n";
90+
echo " - Client 3: " . __DIR__ . "/cache\n";
91+
echo " - Client 4: (default platform-specific)\n";
92+
echo " - Client 5: (caching disabled)\n";

src/RestAPIWrapperProffix/HttpClient/HttpClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public function __construct($url, $apiDatabase, $apiUser, $apiPassword, $apiModu
4545

4646
// Initialize session cache if enabled
4747
if ($this->options->isSessionCachingEnabled()) {
48-
$sessionCache = new SessionCache($apiUser, $apiDatabase, $url);
48+
$customCacheDir = $this->options->getCacheDir();
49+
$sessionCache = new SessionCache($apiUser, $apiDatabase, $url, $customCacheDir);
4950
$this->options->setSessionCache($sessionCache);
5051
}
5152
}

src/RestAPIWrapperProffix/HttpClient/Options.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,14 @@ public function getSessionCache()
144144
{
145145
return $this->sessionCache;
146146
}
147+
148+
/**
149+
* Get custom cache directory
150+
*
151+
* @return string|null
152+
*/
153+
public function getCacheDir(): ?string
154+
{
155+
return $this->options['cache_dir'] ?? null;
156+
}
147157
}

src/RestAPIWrapperProffix/HttpClient/SessionCache.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
/**
66
* File-backed cache for storing PxSessionId.
77
*
8-
* Chooses a per-user cache directory:
9-
* - On Windows: %APPDATA%/php-wrapper-proffix-restapi
10-
* - On Linux/Mac: ~/.cache/php-wrapper-proffix-restapi or /tmp/php-wrapper-proffix-restapi
8+
* Cache directory priority:
9+
* 1. Custom directory provided via constructor (if specified)
10+
* 2. Platform-specific defaults:
11+
* - On Windows: %APPDATA%/php-wrapper-proffix-restapi
12+
* - On Linux/Mac: ~/.cache/php-wrapper-proffix-restapi or /tmp/php-wrapper-proffix-restapi
1113
*
1214
* The filename is derived from username, database, and restURL to avoid collisions
1315
* when multiple clients are used.
@@ -20,19 +22,22 @@ class SessionCache
2022
private string $database;
2123
private string $restURL;
2224
private ?string $cacheDir = null;
25+
private ?string $customCacheDir = null;
2326

2427
/**
2528
* SessionCache constructor.
2629
*
2730
* @param string $username
2831
* @param string $database
2932
* @param string $restURL
33+
* @param string|null $customCacheDir Optional custom cache directory path
3034
*/
31-
public function __construct(string $username, string $database, string $restURL)
35+
public function __construct(string $username, string $database, string $restURL, ?string $customCacheDir = null)
3236
{
3337
$this->username = $username;
3438
$this->database = $database;
3539
$this->restURL = $restURL;
40+
$this->customCacheDir = $customCacheDir;
3641
}
3742

3843
/**
@@ -126,6 +131,13 @@ private function getCacheDir(): string
126131
return $this->cacheDir;
127132
}
128133

134+
// Priority 1: Use custom cache directory if provided
135+
if ($this->customCacheDir !== null && !empty($this->customCacheDir)) {
136+
$this->cacheDir = rtrim($this->customCacheDir, DIRECTORY_SEPARATOR);
137+
return $this->cacheDir;
138+
}
139+
140+
// Priority 2: Use platform-specific defaults
129141
if (PHP_OS_FAMILY === 'Windows') {
130142
$appData = getenv('APPDATA');
131143
if ($appData && !empty($appData)) {

0 commit comments

Comments
 (0)