Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.

Commit e86f310

Browse files
committed
Document Windows compatibility
1 parent 0abda9f commit e86f310

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,56 @@ bin/regenerate_composer.json.php
103103

104104
This will merge all the package-specific dependencies and the autoload rules into
105105
the root composer.json file.
106+
107+
### Windows compatibility
108+
109+
Windows compatibility is achieved on a few different fronts:
110+
111+
#### Newlines
112+
113+
This repository comes with a `.gitattributes` file to ensure that the unit test
114+
files and fixtures are normalized to `\n` on checkout. It's important, because
115+
Windows uses `\r\n` for newlines in text files. Unix-based systems use `\n`.
116+
Without the `.gitattributes`, git on Windows would replace all the `\n` with `\r\n`
117+
on checkout.
118+
119+
The strings produced by the library uses `\n` for newlines where it can make
120+
that choice. For example, the `WXRWriter` class will separate XML tags with
121+
`\n` newlines to make sure the generated XML is consistent across platforms.
122+
123+
#### Paths
124+
125+
The `Filesystem` components makes a point of using Unix-style forward slashes
126+
as directory separators, even on Windows.
127+
128+
As a library consumer, ensure all the local paths you pass to the library are
129+
using Unix-style forward slashes as directory separators. A simple str_replace
130+
will do the trick:
131+
132+
```php
133+
if (DIRECTORY_SEPARATOR === '\\') {
134+
$path = str_replace('\\', '/', $path);
135+
}
136+
```
137+
138+
The reason for using Unix-style forward slashes is care for data integrity.
139+
Windows understands both forward slashes and backslashes, so the replacement
140+
operation is safe there. On Unix, however, a backslash can be used as a part
141+
of a filename so it cannot be safely translated.
142+
143+
Importantly, do not just run this str_replace() on every possible path.
144+
`C:\my-dir\my-file.txt` is both, a valid Windows absolute path and a valid Unix
145+
filename and a relative path. Furthermore, `Filesystem` supports more filesystems
146+
than just local disk.
147+
148+
Anytime you're handling paths, consider:
149+
150+
- Which filesystem is this path related to? Local? Remote? Git?
151+
- Which OS are you on? Windows? Unix?
152+
153+
If the answers are "local" and "Windows", you may need to apply the `str_replace()`
154+
slash normalization. Otherwise, just keep the path as it is.
155+
156+
The takeaway from this section is: **paths are difficult**.
157+
158+
For a fun read on the topic, check out this article: [Windows File Paths](https://www.fileside.app/blog/2023-03-17_windows-file-paths/).

0 commit comments

Comments
 (0)