14 const FILENAME =
'autoload_data.php';
15 const KEY_CLASSES =
'classes';
16 const KEY_SCANNED_DIRS =
'scannedDirs';
21 protected array $scannedDirs = [];
26 protected bool $altered =
false;
50 if (!in_array($dir, $this->scannedDirs)) {
51 $map = $this->loader->getClassMap();
52 $map->mergeMap($this->scanClassesDir($dir));
53 $this->scannedDirs[] = $dir;
54 $this->altered =
true;
57 $this->loader->addFallback($dir);
77 $dir = new \DirectoryIterator($dir);
80 foreach ($dir as $file) {
82 if (!$file->isFile() || !$file->isReadable()) {
86 $path = $file->getRealPath();
88 if (pathinfo(
$path, PATHINFO_EXTENSION) !==
'php') {
92 $class = $file->getBasename(
'.php');
105 $map = $this->loader->getClassMap();
107 if ($this->altered || $map->getAltered()) {
109 $scanned_dirs = $this->scannedDirs;
111 if (empty(
$classes) && empty($scanned_dirs)) {
116 $this->cache->save(self::FILENAME, [
118 self::KEY_SCANNED_DIRS => $scanned_dirs,
131 $cache = $this->getCacheFileContents();
135 $this->loader->getClassMap()
136 ->setMap($cache[self::KEY_CLASSES])
138 $this->scannedDirs = $cache[self::KEY_SCANNED_DIRS];
143 $this->altered =
true;
154 $spec = $this->cache->load(self::FILENAME);
156 return isset($spec[self::KEY_CLASSES]) ? $spec : null;
165 $this->cache->delete(self::FILENAME);
166 $this->loader->getClassMap()->setMap([])->setAltered(
true);
167 $this->scannedDirs = [];
168 $this->altered =
true;
saveCache()
If necessary, save necessary state details.
loadCache()
Set the state of the manager from the cache.
A class/interface/trait autoloader for PHP.
addClasses(string $dir)
Add classes found in this directory to the class map and allow classes in subdirectories to be found ...
Manages core autoloading and caching of class maps.
getCacheFileContents()
Tries to read the contents of the cache file and if valid returns the content.
deleteCache()
Delete the cache file.
scanClassesDir(string $dir)
Scan (non-recursively) a /classes directory for PHP files to map directly to classes.
__construct(protected ClassLoader $loader, protected AutoloadCache $cache)
Constructor.