-
Notifications
You must be signed in to change notification settings - Fork 3
/
AutoloadCollection.php
76 lines (67 loc) · 1.71 KB
/
AutoloadCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* PHP Autoload Override (https://github.com/adriansuter/php-autoload-override)
*
* @license https://github.com/adriansuter/php-autoload-override/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace AdrianSuter\Autoload\Override;
use function array_keys;
use function file_exists;
use function glob;
use function is_dir;
use function realpath;
/**
* @package AdrianSuter\Autoload\Override
*/
class AutoloadCollection
{
/**
* @var bool[] The keys of this associative array are the file paths.
*/
private $filePaths = [];
/**
* Add a file to the autoload collection.
*
* This method would ignore the file if it could not be found.
*
* @param string $path The path to the file.
*/
public function addFile(string $path): void
{
$realpath = realpath($path);
if ($realpath !== false) {
$this->filePaths[$realpath] = true;
}
}
/**
* Add a directory, i.e. the php files inside a directory.
*
* This method would ignore the directory if it could not be found.
*
* @param string $directory The directory.
*/
public function addDirectory(string $directory): void
{
if (
!file_exists($directory) ||
!is_dir($directory) ||
false === ($directory = realpath($directory))
) {
return;
}
$files = glob($directory . '/*.php');
foreach ($files as $file) {
$this->addFile($file);
}
}
/**
* Get the file paths.
*
* @return string[] The file paths.
*/
public function getFilePaths(): array
{
return array_keys($this->filePaths);
}
}