Elgg  Version master
AutoloadManager.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
13 
14  use Cacheable;
15 
16  const FILENAME = 'autoload_data.php';
17  const KEY_CLASSES = 'classes';
18  const KEY_SCANNED_DIRS = 'scannedDirs';
19 
23  protected array $scannedDirs = [];
24 
28  protected bool $altered = false;
29 
37  public function __construct(protected \Elgg\ClassLoader $loader, \Elgg\Config $config, \Elgg\Cache\BaseCache $cache) {
38  if (!$config->AutoloaderManager_skip_storage) {
39  $this->setCache($cache);
40  $this->loadCache();
41  }
42  }
43 
55  public function addClasses($dir) {
56  if (!in_array($dir, $this->scannedDirs)) {
57  $map = $this->loader->getClassMap();
58  $map->mergeMap($this->scanClassesDir($dir));
59  $this->scannedDirs[] = $dir;
60  $this->altered = true;
61  }
62 
63  $this->loader->addFallback($dir);
64  return $this;
65  }
66 
77  protected function scanClassesDir($dir) {
78  if (!is_dir($dir)) {
79  return [];
80  }
81 
82  $dir = new \DirectoryIterator($dir);
83  $map = [];
84 
85  foreach ($dir as $file) {
86  /* @var \SplFileInfo $file */
87  if (!$file->isFile() || !$file->isReadable()) {
88  continue;
89  }
90 
91  $path = $file->getRealPath();
92 
93  if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
94  continue;
95  }
96 
97  $class = $file->getBasename('.php');
98  $map[$class] = $path;
99  }
100 
101  return $map;
102  }
103 
109  public function saveCache() {
110  if (!$this->cache) {
111  return $this;
112  }
113 
114  $map = $this->loader->getClassMap();
115 
116  if ($this->altered || $map->getAltered()) {
117  $classes = $map->getMap();
118  $scanned_dirs = $this->scannedDirs;
119 
120  if (empty($classes) && empty($scanned_dirs)) {
121  // if there is nothing to cache do not save it
122  return $this;
123  }
124 
125  $this->cache->save(self::FILENAME, [
126  self::KEY_CLASSES => $classes,
127  self::KEY_SCANNED_DIRS => $scanned_dirs,
128  ]);
129  }
130 
131  return $this;
132  }
133 
139  public function loadCache() {
140  $cache = $this->getCacheFileContents();
141  if ($cache) {
142  // the cached class map will have the full scanned core classes, so
143  // don't consider the earlier mappings as "altering" the map
144  $this->loader->getClassMap()
145  ->setMap($cache[self::KEY_CLASSES])
146  ->setAltered(false);
147  $this->scannedDirs = $cache[self::KEY_SCANNED_DIRS];
148  return true;
149  }
150 
151  $this->altered = true;
152  return false;
153  }
154 
160  protected function getCacheFileContents() {
161  if (!$this->cache) {
162  return false;
163  }
164 
165  $spec = $this->cache->load(self::FILENAME);
166  if (isset($spec[self::KEY_CLASSES])) {
167  return $spec;
168  }
169 
170  return false;
171  }
172 
178  public function deleteCache() {
179  $this->cache?->delete(self::FILENAME);
180  $this->loader->getClassMap()->setMap([])->setAltered(true);
181  $this->scannedDirs = [];
182  $this->altered = true;
183 
184  return $this;
185  }
186 
192  public function getLoader() {
193  return $this->loader;
194  }
195 }
saveCache()
If necessary, save necessary state details.
loadCache()
Set the state of the manager from the cache.
$classes
Definition: users.php:29
A class/interface/trait autoloader for PHP.
Definition: ClassLoader.php:48
scanClassesDir($dir)
Scan (non-recursively) a /classes directory for PHP files to map directly to classes.
addClasses($dir)
Add classes found in this directory to the class map and allow classes in subdirectories to be found ...
$class
Definition: summary.php:44
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$path
Definition: details.php:70
getLoader()
Get the class loader.
The Elgg cache base class.
Definition: BaseCache.php:9
__construct(protected\Elgg\ClassLoader $loader,\Elgg\Config $config,\Elgg\Cache\BaseCache $cache)
Constructor.
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.
setCache(BaseCache $cache)
Set cache.
Definition: Cacheable.php:27
trait Cacheable
Utility trait for injecting cache.
Definition: Cacheable.php:13