Elgg  Version 4.x
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, \ElggCache $cache) {
43  $this->loader = $loader;
44 
45  if (!$config->AutoloaderManager_skip_storage) {
46  $this->setCache($cache);
47  $this->loadCache();
48  }
49  }
50 
61  public function addClasses($dir) {
62  if (!in_array($dir, $this->scannedDirs)) {
63  $map = $this->loader->getClassMap();
64  $map->mergeMap($this->scanClassesDir($dir));
65  $this->scannedDirs[] = $dir;
66  $this->altered = true;
67  }
68  $this->loader->addFallback($dir);
69  return $this;
70  }
71 
81  protected function scanClassesDir($dir) {
82  if (!is_dir($dir)) {
83  return [];
84  }
85 
86  $dir = new \DirectoryIterator($dir);
87  $map = [];
88 
89  foreach ($dir as $file) {
90  /* @var \SplFileInfo $file */
91  if (!$file->isFile() || !$file->isReadable()) {
92  continue;
93  }
94 
95  $path = $file->getRealPath();
96 
97  if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
98  continue;
99  }
100 
101  $class = $file->getBasename('.php');
102  $map[$class] = $path;
103  }
104  return $map;
105  }
106 
112  public function saveCache() {
113  if (!$this->cache) {
114  return $this;
115  }
116 
117  $map = $this->loader->getClassMap();
118 
119  if ($this->altered || $map->getAltered()) {
120  $classes = $map->getMap();
121  $scanned_dirs = $this->scannedDirs;
122 
123  if (empty($classes) && empty($scanned_dirs)) {
124  // if there is nothing to cache do not save it
125  return $this;
126  }
127 
128  $this->cache->save(self::FILENAME, [
129  self::KEY_CLASSES => $classes,
130  self::KEY_SCANNED_DIRS => $scanned_dirs,
131  ]);
132  }
133 
134  return $this;
135  }
136 
142  public function loadCache() {
143  $cache = $this->getCacheFileContents();
144  if ($cache) {
145  // the cached class map will have the full scanned core classes, so
146  // don't consider the earlier mappings as "altering" the map
147  $this->loader->getClassMap()
148  ->setMap($cache[self::KEY_CLASSES])
149  ->setAltered(false);
150  $this->scannedDirs = $cache[self::KEY_SCANNED_DIRS];
151  return true;
152  }
153  $this->altered = true;
154  return false;
155  }
156 
162  protected function getCacheFileContents() {
163  if (!$this->cache) {
164  return false;
165  }
166 
167  $spec = $this->cache->load(self::FILENAME);
168  if (isset($spec[self::KEY_CLASSES])) {
169  return $spec;
170  }
171 
172  return false;
173  }
174 
180  public function deleteCache() {
181  if ($this->cache) {
182  $this->cache->delete(self::FILENAME);
183  }
184 
185  $this->loader->getClassMap()->setMap([])->setAltered(true);
186  $this->scannedDirs = [];
187  $this->altered = true;
188 
189  return $this;
190  }
191 
197  public function getLoader() {
198  return $this->loader;
199  }
200 }
saveCache()
If necessary, save necessary state details.
setCache(ElggCache $cache)
Set cache.
Definition: Cacheable.php:27
loadCache()
Set the state of the manager from the cache.
$classes
Definition: users.php:21
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 ...
__construct(\Elgg\ClassLoader $loader,\Elgg\Config $config,\ElggCache $cache)
Constructor.
$class
Definition: summary.php:44
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$path
Definition: details.php:68
getLoader()
Get the class loader.
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.
trait Cacheable
Utility trait for injecting cache.
Definition: Cacheable.php:13