Skip to content

Commit c68eb4f

Browse files
committed
Issue 139: Expand the README.md for 'ock/class-files-iterator'.
1 parent 3ce7701 commit c68eb4f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,55 @@ Main concepts:
1111
- [`ClassNamesIA*`](src/ClassNamesIA/ClassNamesIAInterface.php)\
1212
IteratorAggregate that lists class names by numeric keys.\
1313
This can be used if the file path is not relevant.
14+
15+
## Benefits
16+
17+
There are other packages which attempt to solve the same problem in different ways.
18+
19+
The benefits of this package are:
20+
- Predictable (alphabetic) order of class files.\
21+
This is thanks to internal usage of `scandir()` with sort flag, instead of directory iterators.
22+
- The "iterators" are stateless/immutable, and can be passed around as value objects.\
23+
This is thanks to them extending `IteratorAggregate` instead of `Iterator`.
24+
- Code that needs a list of classes or class files can simply depend on the interfaces, and does not need to deal with directories.
25+
26+
## Limitations
27+
28+
Iterators for reflection classes are not part of this package.
29+
30+
## Usage
31+
32+
```php
33+
use Ock\ClassFilesIterator\ClassFilesIA\ClassFilesIAInterface;
34+
use Ock\ClassFilesIterator\ClassNamesIA\ClassNamesIAInterface;
35+
use Ock\ClassFilesIterator\NamespaceDirectory;
36+
use Ock\ClassFilesIterator\Tests\Fixtures\Acme\Plant\VenusFlyTrap;
37+
38+
// Basic creation for a PSR-4 directory.
39+
$basic_namespace_dir = NamespaceDirectory::create($basedir . '/src', 'Acme\\Foo');
40+
41+
// Convenient creation with a "seed" class.
42+
// The directory is determined automatically with ReflectionClass::getFileName().
43+
$namespace_dir = NamespaceDirectory::fromKnownClass(VenusFlyTrap::class);
44+
45+
// Iterate class files.
46+
function foo(ClassFilesIAInterface $classFilesIA): void {
47+
foreach ($classFilesIA as $file => $class) {
48+
$rc = new \ReflectionClass($class);
49+
// Call realpath() to be sure.
50+
assert($rc->getFileName() === realpath($file));
51+
}
52+
// Or do this to skip the realpath() call:
53+
foreach ($classFilesIA->withRealpathRoot() as $file => $class) {
54+
$rc = new \ReflectionClass($class);
55+
assert($rc->getFileName() === $file);
56+
}
57+
}
58+
59+
// Iterate class names.
60+
function foo(ClassNamesIAInterface $classNamesIA): void {
61+
foreach ($classNamesIA as $class) {
62+
assert(class_exists($class));
63+
}
64+
}
65+
```

0 commit comments

Comments
 (0)