Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
AutoloadManager.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
12 
13  const FILENAME = 'autoload_data.php';
14  const KEY_CLASSES = 'classes';
15  const KEY_SCANNED_DIRS = 'scannedDirs';
16 
20  protected $loader;
21 
25  protected $scannedDirs = array();
26 
30  protected $altered = false;
31 
35  protected $storage = null;
36 
42  public function __construct(\Elgg\ClassLoader $loader) {
43  $this->loader = $loader;
44  }
45 
56  public function addClasses($dir) {
57  if (!in_array($dir, $this->scannedDirs)) {
58  $map = $this->loader->getClassMap();
59  $map->mergeMap($this->scanClassesDir($dir));
60  $this->scannedDirs[] = $dir;
61  $this->altered = true;
62  }
63  $this->loader->addFallback($dir);
64  return $this;
65  }
66 
76  protected function scanClassesDir($dir) {
77  $dir = new \DirectoryIterator($dir);
78  $map = array();
79 
80  foreach ($dir as $file) {
81  /* @var \SplFileInfo $file */
82  if (!$file->isFile() || !$file->isReadable()) {
83  continue;
84  }
85 
86  $path = $file->getRealPath();
87 
88  if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
89  continue;
90  }
91 
92  $class = $file->getBasename('.php');
93  $map[$class] = $path;
94  }
95  return $map;
96  }
97 
105  public function setClassPath($class, $path) {
106  $this->loader->getClassMap()->setPath($class, $path);
107  return $this;
108  }
109 
115  public function saveCache() {
116  if ($this->storage) {
117  $map = $this->loader->getClassMap();
118  if ($this->altered || $map->getAltered()) {
119  $spec[self::KEY_CLASSES] = $map->getMap();
120  $spec[self::KEY_SCANNED_DIRS] = $this->scannedDirs;
121  $this->storage->save(self::FILENAME, serialize($spec));
122  }
123  }
124  return $this;
125  }
126 
132  public function loadCache() {
133  $spec = $this->getSpec();
134  if ($spec) {
135  // the cached class map will have the full scanned core classes, so
136  // don't consider the earlier mappings as "altering" the map
137  $this->loader->getClassMap()
138  ->setMap($spec[self::KEY_CLASSES])
139  ->setAltered(false);
140  $this->scannedDirs = $spec[self::KEY_SCANNED_DIRS];
141  return true;
142  }
143  $this->altered = true;
144  return false;
145  }
146 
153  protected function getSpec() {
154  if ($this->storage) {
155  $serialization = $this->storage->load(self::FILENAME);
156  if ($serialization) {
157  $spec = unserialize($serialization);
158  if (isset($spec[self::KEY_CLASSES])) {
159  return $spec;
160  }
161  }
162  }
163  return false;
164  }
165 
171  public function deleteCache() {
172  if ($this->storage) {
173  $this->storage->delete(self::FILENAME);
174  }
175  return $this;
176  }
177 
183  public function getLoader() {
184  return $this->loader;
185  }
186 
193  public function setStorage(\ElggCache $storage) {
194  $this->storage = $storage;
195  }
196 
202  public function __destruct() {
203  $this->saveCache();
204  }
205 }
206 
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.
$path
Definition: details.php:88
scanClassesDir($dir)
Scan (non-recursively) a /classes directory for PHP files to map directly to classes.
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
addClasses($dir)
Add classes found in this directory to the class map and allow classes in subdirectories to be found ...
getLoader()
Get the class loader.
__construct(\Elgg\ClassLoader $loader)
Constructor.
setClassPath($class, $path)
Register the location of a class on the class map.
__destruct()
Save the cache on object destruction.
getSpec()
Some method that does something.
deleteCache()
Delete the cache file.
setStorage(\ElggCache $storage)
Set the cache storage object.
$loader
Definition: ajax_loader.php:32