Elgg  Version 5.1
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 $loader;
24 
28  protected $scannedDirs = [];
29 
33  protected $altered = false;
34 
42  public function __construct(\Elgg\ClassLoader $loader, \Elgg\Config $config, \Elgg\Cache\BaseCache $cache) {
43  $this->loader = $loader;
44 
45  if (!$config->AutoloaderManager_skip_storage) {
46  $this->setCache($cache);
47  $this->loadCache();
48  }
49  }
50 
62  public function addClasses($dir) {
63  if (!in_array($dir, $this->scannedDirs)) {
64  $map = $this->loader->getClassMap();
65  $map->mergeMap($this->scanClassesDir($dir));
66  $this->scannedDirs[] = $dir;
67  $this->altered = true;
68  }
69 
70  $this->loader->addFallback($dir);
71  return $this;
72  }
73 
84  protected function scanClassesDir($dir) {
85  if (!is_dir($dir)) {
86  return [];
87  }
88 
89  $dir = new \DirectoryIterator($dir);
90  $map = [];
91 
92  foreach ($dir as $file) {
93  /* @var \SplFileInfo $file */
94  if (!$file->isFile() || !$file->isReadable()) {
95  continue;
96  }
97 
98  $path = $file->getRealPath();
99 
100  if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
101  continue;
102  }
103 
104  $class = $file->getBasename('.php');
105  $map[$class] = $path;
106  }
107 
108  return $map;
109  }
110 
116  public function saveCache() {
117  if (!$this->cache) {
118  return $this;
119  }
120 
121  $map = $this->loader->getClassMap();
122 
123  if ($this->altered || $map->getAltered()) {
124  $classes = $map->getMap();
125  $scanned_dirs = $this->scannedDirs;
126 
127  if (empty($classes) && empty($scanned_dirs)) {
128  // if there is nothing to cache do not save it
129  return $this;
130  }
131 
132  $this->cache->save(self::FILENAME, [
133  self::KEY_CLASSES => $classes,
134  self::KEY_SCANNED_DIRS => $scanned_dirs,
135  ]);
136  }
137 
138  return $this;
139  }
140 
146  public function loadCache() {
147  $cache = $this->getCacheFileContents();
148  if ($cache) {
149  // the cached class map will have the full scanned core classes, so
150  // don't consider the earlier mappings as "altering" the map
151  $this->loader->getClassMap()
152  ->setMap($cache[self::KEY_CLASSES])
153  ->setAltered(false);
154  $this->scannedDirs = $cache[self::KEY_SCANNED_DIRS];
155  return true;
156  }
157 
158  $this->altered = true;
159  return false;
160  }
161 
167  protected function getCacheFileContents() {
168  if (!$this->cache) {
169  return false;
170  }
171 
172  $spec = $this->cache->load(self::FILENAME);
173  if (isset($spec[self::KEY_CLASSES])) {
174  return $spec;
175  }
176 
177  return false;
178  }
179 
185  public function deleteCache() {
186  $this->cache?->delete(self::FILENAME);
187  $this->loader->getClassMap()->setMap([])->setAltered(true);
188  $this->scannedDirs = [];
189  $this->altered = true;
190 
191  return $this;
192  }
193 
199  public function getLoader() {
200  return $this->loader;
201  }
202 }
__construct(\Elgg\ClassLoader $loader,\Elgg\Config $config,\Elgg\Cache\BaseCache $cache)
Constructor.
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
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