1212
1313<br />
1414
15-
15+ A simple, type-safe PHP library for working with file system paths.
16+ It handles path normalization, manipulation, and cross-platform compatibility (Windows and Unix) so you don't have to worry about slashes and edge cases.
1617
1718## Installation
1819
@@ -27,3 +28,125 @@ composer require internal/path
2728
2829## Usage
2930
31+ ### Creating paths
32+
33+ ``` php
34+ use Internal\Path;
35+
36+ // Create from string
37+ $path = Path::create('/var/www/app');
38+ $path = Path::create('src/helpers/utils.php');
39+ ```
40+
41+ ### Joining paths
42+
43+ ``` php
44+ $base = Path::create('/var/www');
45+ $full = $base->join('app', 'src', 'Controller.php');
46+ // Result: /var/www/app/src/Controller.php
47+
48+ // Works with Path objects too
49+ $subdir = Path::create('logs');
50+ $logFile = $base->join($subdir, 'app.log');
51+ ```
52+
53+ ### Working with path components
54+
55+ ``` php
56+ $file = Path::create('/home/user/documents/report.pdf');
57+
58+ $file->name(); // 'report.pdf'
59+ $file->stem(); // 'report'
60+ $file->extension(); // 'pdf'
61+ $file->parent(); // Path('/home/user/documents')
62+ ```
63+
64+ ### Path checks
65+
66+ ``` php
67+ $path = Path::create('config/app.php');
68+
69+ $path->isAbsolute(); // false
70+ $path->isRelative(); // true
71+ $path->exists(); // checks if file/directory exists
72+ $path->isFile(); // checks if it's a file
73+ $path->isDir(); // checks if it's a directory
74+ $path->isWriteable(); // checks if writable
75+ ```
76+
77+ ### Converting paths
78+
79+ ``` php
80+ $relative = Path::create('src/Path.php');
81+ $absolute = $relative->absolute();
82+ // Result: /current/working/directory/src/Path.php
83+
84+ // Use as string
85+ echo $path; // Path implements Stringable
86+ ```
87+
88+ ### Pattern matching
89+
90+ ``` php
91+ $path = Path::create('/var/www/app/Controller.php');
92+ $path->match('*.php'); // true
93+ $path->match('/var/www/*/Con*'); // true
94+
95+ // Supports wildcards
96+ $path->match('file?.txt'); // matches file1.txt, fileA.txt, etc.
97+ $path->match('file[123].txt'); // matches file1.txt, file2.txt, file3.txt
98+ $path->match('test/*/*.php'); // matches test/any/file.php
99+ ```
100+
101+ ## Edge cases and special handling
102+
103+ The library handles common edge cases automatically:
104+
105+ ### Hidden files and multiple extensions
106+
107+ ``` php
108+ // Hidden files (Unix-style)
109+ $hidden = Path::create('.gitignore');
110+ $hidden->stem(); // '.gitignore'
111+ $hidden->extension(); // 'gitignore'
112+
113+ // Files with multiple dots
114+ $config = Path::create('app.config.json');
115+ $config->stem(); // 'app.config'
116+ $config->extension(); // 'json' (only the last extension)
117+ ```
118+
119+ ### Windows paths
120+
121+ ``` php
122+ // Automatically normalizes Windows backslashes
123+ $winPath = Path::create('C:\Users\Admin\Documents');
124+ echo $winPath; // 'C:/Users/Admin/Documents'
125+
126+ // Windows drive letters are recognized as absolute
127+ Path::create('C:/Program Files')->isAbsolute(); // true
128+ ```
129+
130+ ### Path normalization
131+
132+ ``` php
133+ // Removes redundant separators
134+ Path::create('path//to///file.txt'); // 'path/to/file.txt'
135+
136+ // Resolves . and .. segments
137+ Path::create('path/./to/../file.txt'); // 'path/file.txt'
138+
139+ // Empty path becomes current directory
140+ Path::create(''); // '.'
141+ ```
142+
143+ ### Safety checks
144+
145+ ``` php
146+ // Cannot join absolute paths (prevents common mistakes)
147+ $base = Path::create('/var/www');
148+ $base->join('/etc/config'); // throws LogicException
149+
150+ // Cannot navigate above root in absolute paths
151+ Path::create('/var/../../root'); // throws LogicException
152+ ```
0 commit comments