Elgg  Version 3.0
AutoloadManager.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
14 
15  use Cacheable;
16 
17  const FILENAME = 'autoload_data.php';
18  const KEY_CLASSES = 'classes';
19  const KEY_SCANNED_DIRS = 'scannedDirs';
20 
24  protected $loader;
25 
29  protected $scannedDirs = [];
30 
34  protected $altered = false;
35 
41  public function __construct(\Elgg\ClassLoader $loader) {
42  $this->loader = $loader;
43  }
44 
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  $this->loader->addFallback($dir);
63  return $this;
64  }
65 
75  protected function scanClassesDir($dir) {
76  if (!is_dir($dir)) {
77  return [];
78  }
79 
80  $dir = new \DirectoryIterator($dir);
81  $map = [];
82 
83  foreach ($dir as $file) {
84  /* @var \SplFileInfo $file */
85  if (!$file->isFile() || !$file->isReadable()) {
86  continue;
87  }
88 
89  $path = $file->getRealPath();
90 
91  if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
92  continue;
93  }
94 
95  $class = $file->getBasename('.php');
96  $map[$class] = $path;
97  }
98  return $map;
99  }
100 
106  public function saveCache() {
107  if (!$this->cache) {
108  return $this;
109  }
110 
111  $map = $this->loader->getClassMap();
112  if ($this->altered || $map->getAltered()) {
113  $spec = [
114  self::KEY_CLASSES => $map->getMap(),
115  self::KEY_SCANNED_DIRS => $this->scannedDirs,
116  ];
117 
118  $this->cache->save(self::FILENAME, $spec);
119  }
120 
121  return $this;
122  }
123 
129  public function loadCache() {
130  $cache = $this->getCacheFileContents();
131  if ($cache) {
132  // the cached class map will have the full scanned core classes, so
133  // don't consider the earlier mappings as "altering" the map
134  $this->loader->getClassMap()
135  ->setMap($cache[self::KEY_CLASSES])
136  ->setAltered(false);
137  $this->scannedDirs = $cache[self::KEY_SCANNED_DIRS];
138  return true;
139  }
140  $this->altered = true;
141  return false;
142  }
143 
149  protected function getCacheFileContents() {
150  if (!$this->cache) {
151  return false;
152  }
153 
154  $spec = $this->cache->load(self::FILENAME);
155  if (isset($spec[self::KEY_CLASSES])) {
156  return $spec;
157  }
158 
159  return false;
160  }
161 
167  public function deleteCache() {
168  if ($this->cache) {
169  $this->cache->delete(self::FILENAME);
170  }
171 
172  $this->loader->getClassMap()->setMap([])->setAltered(true);
173  $this->scannedDirs = [];
174  $this->altered = true;
175 
176  return $this;
177  }
178 
184  public function getLoader() {
185  return $this->loader;
186  }
187 }
saveCache()
If necessary, save necessary state details.
if(!array_key_exists($filename, $text_files)) $file
loadCache()
Set the state of the manager from the cache.
A class/interface/trait autoloader for PHP.
Definition: ClassLoader.php:50
$path
Definition: details.php:89
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 ...
Configuration exception.
$class
Definition: field.php:29
getLoader()
Get the class loader.
__construct(\Elgg\ClassLoader $loader)
Constructor.
trait Cacheable
Utility trait for injecting cache.
Definition: Cacheable.php:11
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.
$loader
Definition: ajax_loader.php:27