Skip to content

Commit 14c068c

Browse files
committed
Rewrite, added abstract method APIs
1 parent bb00af1 commit 14c068c

21 files changed

+766
-498
lines changed

.editorconfig

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
9+
[*.php]
10+
indent_style = tab
11+
indent_size = 4
12+
13+
[*.json]
14+
indent_style = tab
15+
indent_size = 4
16+
17+
[*.md]
18+
indent_style = space
19+
indent_size = 4
20+
21+
[*.yml]
22+
indent_style = space
23+
indent_size = 2

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,20 @@ An object-oriented path manipulation library in PHP.
44
The API and implementation is largely based on the [rust `std::path` implementation](https://github.com/rust-lang/rust/blob/d8bdb3fdcbd88eb16e1a6669236122c41ed2aed3/src/libstd/path.rs).
55

66
(I was listening to Tchaikovsky's Pathetique when I wrote this)
7+
8+
## Platform behaviour
9+
Paths can be constructed for a specific `Platform` (either Windows or Unix)
10+
such that Windows paths can be interpreted correctly on Unix systems
11+
(although, apparently, they cannot be used to interact with the filesystem).
12+
13+
All methods that interact with the filesystem would throw a `PlatformMismatchException`
14+
if a path for a different platform is used.
15+
This will never happen if all paths are costructed with specifying a platform.
16+
17+
Some relative paths may be converted across platforms using `$path->toCurrentPlatform()`.
18+
19+
## Serialization
20+
The path can be serialized using the PHP `serialize()` function.
21+
The serialized data include the platform that the path is constructed for,
22+
so paths can be deserialized on a different platform
23+
and still retain some correct behaviour.

src/Component.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
namespace SOFe\Pathetique;
66

7-
final class Component {
8-
public const PREFIX = 1;
9-
public const ROOT_DIR = 2;
10-
public const CURRENT_DIR = 3;
11-
public const PARENT_DIR = 4;
12-
public const NORMAL = 5;
7+
interface Component {
8+
/**
9+
* Returns the directory-separator-free string for this component.
10+
*
11+
* Note that the leading `\` in `\\server\share` on DOS is not considered a directory separator,
12+
* while the leading `/` in a Unix absolute path is considered a directory separator.
13+
*
14+
* Consequently, joining `toString()` of all components with `Platform->getDirectorySeparator()`
15+
* yields the original path.
16+
*/
17+
public function toString() : string;
1318

14-
private function __construct() {
15-
}
19+
public function __toString() : string;
1620
}

src/CurrentDirectoryComponent.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace SOFe\Pathetique;
6+
7+
/**
8+
* A `.` component.
9+
*/
10+
final class CurrentDirectoryComponent implements Component {
11+
public function toString() : string {
12+
return ".";
13+
}
14+
15+
public function __toString() : string {
16+
return ".";
17+
}
18+
}

src/DeviceNsPrefix.php

+8-22
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,15 @@
44

55
namespace SOFe\Pathetique;
66

7-
use function strlen;
8-
9-
final class DeviceNsPrefix extends Prefix {
10-
/** @var string */
11-
private $device;
12-
13-
public function __construct(string $device) {
14-
$this->device = $device;
15-
}
16-
17-
public function getDevice() : string {
18-
return $this->device;
19-
}
20-
21-
public function getFullPrefix() : string {
22-
return "\\\\.\\{$this->device}";
23-
}
24-
25-
public function getLength() : int {
26-
return 4 + strlen($this->device);
7+
/**
8+
* A Windows device namespace path prefix, e.g. `\\.\COM42`.
9+
*/
10+
final class DeviceNsPrefix implements Prefix {
11+
public function toString() : string {
12+
// TODO unimplemented
2713
}
2814

29-
public function isVerbatim() : bool {
30-
return false;
15+
public function __toString() : string {
16+
// TODO unimplemented
3117
}
3218
}

src/DiskPrefix.php

+8-20
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,15 @@
44

55
namespace SOFe\Pathetique;
66

7-
final class DiskPrefix extends Prefix {
8-
/** @var string */
9-
private $drive;
10-
11-
public function __construct(string $drive) {
12-
$this->drive = $drive;
13-
}
14-
15-
public function getDrive() : string {
16-
return $this->drive;
17-
}
18-
19-
public function getFullPrefix() : string {
20-
return "{$this->drive}:";
21-
}
22-
23-
public function getLength() : int {
24-
return 1 + 1;
7+
/**
8+
* A normal Windows path prefix with a disk drive, e.g. `C:\`.
9+
*/
10+
final class DiskPrefix implements Prefix {
11+
public function toString() : string {
12+
// TODO unimplemented
2513
}
2614

27-
public function isVerbatim() : bool {
28-
return true;
15+
public function __toString() : string {
16+
// TODO unimplemented
2917
}
3018
}

src/IOException.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace SOFe\Pathetique;
6+
7+
use Exception;
8+
9+
class IOException extends Exception {
10+
}

src/NormalComponent.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace SOFe\Pathetique;
6+
7+
/**
8+
* A normal path component.
9+
*/
10+
final class NormalComponent implements Component {
11+
public function toString() : string {
12+
// TODO unimplemented
13+
}
14+
15+
public function __toString() : string {
16+
// TODO unimplemented
17+
}
18+
}

src/ParentDirectoryComponent.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace SOFe\Pathetique;
6+
7+
/**
8+
* A `..` component.
9+
*/
10+
final class ParentDirectoryComponent implements Component {
11+
public function toString() : string {
12+
return "..";
13+
}
14+
15+
public function __toString() : string {
16+
return "..";
17+
}
18+
}

0 commit comments

Comments
 (0)