Elgg  Version 1.9
AutoloadManager.php
Go to the documentation of this file.
1 <?php
11 
12  const FILENAME = 'autoload_data.php';
13  const KEY_CLASSES = 'classes';
14  const KEY_SCANNED_DIRS = 'scannedDirs';
15 
19  protected $loader;
20 
24  protected $scannedDirs = array();
25 
29  protected $altered = false;
30 
34  protected $storage = null;
35 
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  $dir = new DirectoryIterator($dir);
77  $map = array();
78 
79  foreach ($dir as $file) {
80  /* @var SplFileInfo $file */
81  if (!$file->isFile() || !$file->isReadable()) {
82  continue;
83  }
84 
85  $path = $file->getRealPath();
86 
87  if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
88  continue;
89  }
90 
91  $class = $file->getBasename('.php');
92  $map[$class] = $path;
93  }
94  return $map;
95  }
96 
104  public function setClassPath($class, $path) {
105  $this->loader->getClassMap()->setPath($class, $path);
106  return $this;
107  }
108 
114  public function saveCache() {
115  if ($this->storage) {
116  $map = $this->loader->getClassMap();
117  if ($this->altered || $map->getAltered()) {
118  $spec[self::KEY_CLASSES] = $map->getMap();
119  $spec[self::KEY_SCANNED_DIRS] = $this->scannedDirs;
120  $this->storage->save(self::FILENAME, serialize($spec));
121  }
122  }
123  return $this;
124  }
125 
131  public function loadCache() {
132  $spec = $this->getSpec();
133  if ($spec) {
134  // the cached class map will have the full scanned core classes, so
135  // don't consider the earlier mappings as "altering" the map
136  $this->loader->getClassMap()
137  ->setMap($spec[self::KEY_CLASSES])
138  ->setAltered(false);
139  $this->scannedDirs = $spec[self::KEY_SCANNED_DIRS];
140  return true;
141  }
142  $this->altered = true;
143  return false;
144  }
145 
152  protected function getSpec() {
153  if ($this->storage) {
154  $serialization = $this->storage->load(self::FILENAME);
155  if ($serialization) {
156  $spec = unserialize($serialization);
157  if (isset($spec[self::KEY_CLASSES])) {
158  return $spec;
159  }
160  }
161  }
162  return false;
163  }
164 
170  public function deleteCache() {
171  if ($this->storage) {
172  $this->storage->delete(self::FILENAME);
173  }
174  return $this;
175  }
176 
182  public function getLoader() {
183  return $this->loader;
184  }
185 
192  public function setStorage(ElggCache $storage) {
193  $this->storage = $storage;
194  }
195 }
loadCache()
Set the state of the manager from the cache.
addClasses($dir)
Add classes found in this directory to the class map and allow classes in subdirectories to be found ...
scanClassesDir($dir)
Scan (non-recursively) a /classes directory for PHP files to map directly to classes.
__construct(Elgg_ClassLoader $loader)
Constructor.
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
setClassPath($class, $path)
Register the location of a class on the class map.
setStorage(ElggCache $storage)
Set the cache storage object.
deleteCache()
Delete the cache file.
saveCache()
If necessary, save necessary state details.
getSpec()
Some method that does something.
getLoader()
Get the class loader.
$path
Definition: invalid.php:17
elgg ajax loader
Definition: admin.php:1315