Completed
Push — master ( 450a89...be9ae4 )
by Robin
299:22 queued 287:36
created
lib/autoloader.php 2 patches
Indentation   +145 added lines, -145 removed lines patch added patch discarded remove patch
@@ -35,149 +35,149 @@
 block discarded – undo
35 35
 use \OCP\AutoloadNotAllowedException;
36 36
 
37 37
 class Autoloader {
38
-	/** @var bool */
39
-	private $useGlobalClassPath = true;
40
-	/** @var array */
41
-	private $validRoots = [];
42
-
43
-	/**
44
-	 * Optional low-latency memory cache for class to path mapping.
45
-	 *
46
-	 * @var \OC\Memcache\Cache
47
-	 */
48
-	protected $memoryCache;
49
-
50
-	/**
51
-	 * Autoloader constructor.
52
-	 *
53
-	 * @param string[] $validRoots
54
-	 */
55
-	public function __construct(array $validRoots) {
56
-		foreach ($validRoots as $root) {
57
-			$this->validRoots[$root] = true;
58
-		}
59
-	}
60
-
61
-	/**
62
-	 * Add a path to the list of valid php roots for auto loading
63
-	 *
64
-	 * @param string $root
65
-	 */
66
-	public function addValidRoot($root) {
67
-		$root = stream_resolve_include_path($root);
68
-		$this->validRoots[$root] = true;
69
-	}
70
-
71
-	/**
72
-	 * disable the usage of the global classpath \OC::$CLASSPATH
73
-	 */
74
-	public function disableGlobalClassPath() {
75
-		$this->useGlobalClassPath = false;
76
-	}
77
-
78
-	/**
79
-	 * enable the usage of the global classpath \OC::$CLASSPATH
80
-	 */
81
-	public function enableGlobalClassPath() {
82
-		$this->useGlobalClassPath = true;
83
-	}
84
-
85
-	/**
86
-	 * get the possible paths for a class
87
-	 *
88
-	 * @param string $class
89
-	 * @return array|bool an array of possible paths or false if the class is not part of ownCloud
90
-	 */
91
-	public function findClass($class) {
92
-		$class = trim($class, '\\');
93
-
94
-		$paths = array();
95
-		if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) {
96
-			$paths[] = \OC::$CLASSPATH[$class];
97
-			/**
98
-			 * @TODO: Remove this when necessary
99
-			 * Remove "apps/" from inclusion path for smooth migration to multi app dir
100
-			 */
101
-			if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
102
-				\OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', \OCP\Util::DEBUG);
103
-				$paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
104
-			}
105
-		} elseif (strpos($class, 'OC_') === 0) {
106
-			$paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
107
-		} elseif (strpos($class, 'OCA\\') === 0) {
108
-			list(, $app, $rest) = explode('\\', $class, 3);
109
-			$app = strtolower($app);
110
-			$appPath = \OC_App::getAppPath($app);
111
-			if ($appPath && stream_resolve_include_path($appPath)) {
112
-				$paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
113
-				// If not found in the root of the app directory, insert '/lib' after app id and try again.
114
-				$paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
115
-			}
116
-		} elseif ($class === 'Test\\TestCase') {
117
-			// This File is considered public API, so we make sure that the class
118
-			// can still be loaded, although the PSR-4 paths have not been loaded.
119
-			$paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
120
-		}
121
-		return $paths;
122
-	}
123
-
124
-	/**
125
-	 * @param string $fullPath
126
-	 * @return bool
127
-	 */
128
-	protected function isValidPath($fullPath) {
129
-		foreach ($this->validRoots as $root => $true) {
130
-			if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
131
-				return true;
132
-			}
133
-		}
134
-		throw new AutoloadNotAllowedException($fullPath);
135
-	}
136
-
137
-	/**
138
-	 * Load the specified class
139
-	 *
140
-	 * @param string $class
141
-	 * @return bool
142
-	 */
143
-	public function load($class) {
144
-		$pathsToRequire = null;
145
-		if ($this->memoryCache) {
146
-			$pathsToRequire = $this->memoryCache->get($class);
147
-		}
148
-
149
-		if(class_exists($class, false)) {
150
-			return false;
151
-		}
152
-
153
-		if (!is_array($pathsToRequire)) {
154
-			// No cache or cache miss
155
-			$pathsToRequire = array();
156
-			foreach ($this->findClass($class) as $path) {
157
-				$fullPath = stream_resolve_include_path($path);
158
-				if ($fullPath && $this->isValidPath($fullPath)) {
159
-					$pathsToRequire[] = $fullPath;
160
-				}
161
-			}
162
-
163
-			if ($this->memoryCache) {
164
-				$this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
165
-			}
166
-		}
167
-
168
-		foreach ($pathsToRequire as $fullPath) {
169
-			require_once $fullPath;
170
-		}
171
-
172
-		return false;
173
-	}
174
-
175
-	/**
176
-	 * Sets the optional low-latency cache for class to path mapping.
177
-	 *
178
-	 * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
179
-	 */
180
-	public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
181
-		$this->memoryCache = $memoryCache;
182
-	}
38
+    /** @var bool */
39
+    private $useGlobalClassPath = true;
40
+    /** @var array */
41
+    private $validRoots = [];
42
+
43
+    /**
44
+     * Optional low-latency memory cache for class to path mapping.
45
+     *
46
+     * @var \OC\Memcache\Cache
47
+     */
48
+    protected $memoryCache;
49
+
50
+    /**
51
+     * Autoloader constructor.
52
+     *
53
+     * @param string[] $validRoots
54
+     */
55
+    public function __construct(array $validRoots) {
56
+        foreach ($validRoots as $root) {
57
+            $this->validRoots[$root] = true;
58
+        }
59
+    }
60
+
61
+    /**
62
+     * Add a path to the list of valid php roots for auto loading
63
+     *
64
+     * @param string $root
65
+     */
66
+    public function addValidRoot($root) {
67
+        $root = stream_resolve_include_path($root);
68
+        $this->validRoots[$root] = true;
69
+    }
70
+
71
+    /**
72
+     * disable the usage of the global classpath \OC::$CLASSPATH
73
+     */
74
+    public function disableGlobalClassPath() {
75
+        $this->useGlobalClassPath = false;
76
+    }
77
+
78
+    /**
79
+     * enable the usage of the global classpath \OC::$CLASSPATH
80
+     */
81
+    public function enableGlobalClassPath() {
82
+        $this->useGlobalClassPath = true;
83
+    }
84
+
85
+    /**
86
+     * get the possible paths for a class
87
+     *
88
+     * @param string $class
89
+     * @return array|bool an array of possible paths or false if the class is not part of ownCloud
90
+     */
91
+    public function findClass($class) {
92
+        $class = trim($class, '\\');
93
+
94
+        $paths = array();
95
+        if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) {
96
+            $paths[] = \OC::$CLASSPATH[$class];
97
+            /**
98
+             * @TODO: Remove this when necessary
99
+             * Remove "apps/" from inclusion path for smooth migration to multi app dir
100
+             */
101
+            if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
102
+                \OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', \OCP\Util::DEBUG);
103
+                $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
104
+            }
105
+        } elseif (strpos($class, 'OC_') === 0) {
106
+            $paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
107
+        } elseif (strpos($class, 'OCA\\') === 0) {
108
+            list(, $app, $rest) = explode('\\', $class, 3);
109
+            $app = strtolower($app);
110
+            $appPath = \OC_App::getAppPath($app);
111
+            if ($appPath && stream_resolve_include_path($appPath)) {
112
+                $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
113
+                // If not found in the root of the app directory, insert '/lib' after app id and try again.
114
+                $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
115
+            }
116
+        } elseif ($class === 'Test\\TestCase') {
117
+            // This File is considered public API, so we make sure that the class
118
+            // can still be loaded, although the PSR-4 paths have not been loaded.
119
+            $paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
120
+        }
121
+        return $paths;
122
+    }
123
+
124
+    /**
125
+     * @param string $fullPath
126
+     * @return bool
127
+     */
128
+    protected function isValidPath($fullPath) {
129
+        foreach ($this->validRoots as $root => $true) {
130
+            if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
131
+                return true;
132
+            }
133
+        }
134
+        throw new AutoloadNotAllowedException($fullPath);
135
+    }
136
+
137
+    /**
138
+     * Load the specified class
139
+     *
140
+     * @param string $class
141
+     * @return bool
142
+     */
143
+    public function load($class) {
144
+        $pathsToRequire = null;
145
+        if ($this->memoryCache) {
146
+            $pathsToRequire = $this->memoryCache->get($class);
147
+        }
148
+
149
+        if(class_exists($class, false)) {
150
+            return false;
151
+        }
152
+
153
+        if (!is_array($pathsToRequire)) {
154
+            // No cache or cache miss
155
+            $pathsToRequire = array();
156
+            foreach ($this->findClass($class) as $path) {
157
+                $fullPath = stream_resolve_include_path($path);
158
+                if ($fullPath && $this->isValidPath($fullPath)) {
159
+                    $pathsToRequire[] = $fullPath;
160
+                }
161
+            }
162
+
163
+            if ($this->memoryCache) {
164
+                $this->memoryCache->set($class, $pathsToRequire, 60); // cache 60 sec
165
+            }
166
+        }
167
+
168
+        foreach ($pathsToRequire as $fullPath) {
169
+            require_once $fullPath;
170
+        }
171
+
172
+        return false;
173
+    }
174
+
175
+    /**
176
+     * Sets the optional low-latency cache for class to path mapping.
177
+     *
178
+     * @param \OC\Memcache\Cache $memoryCache Instance of memory cache.
179
+     */
180
+    public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) {
181
+        $this->memoryCache = $memoryCache;
182
+    }
183 183
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -99,24 +99,24 @@  discard block
 block discarded – undo
99 99
 			 * Remove "apps/" from inclusion path for smooth migration to multi app dir
100 100
 			 */
101 101
 			if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
102
-				\OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', \OCP\Util::DEBUG);
102
+				\OCP\Util::writeLog('core', 'include path for class "'.$class.'" starts with "apps/"', \OCP\Util::DEBUG);
103 103
 				$paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
104 104
 			}
105 105
 		} elseif (strpos($class, 'OC_') === 0) {
106
-			$paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
106
+			$paths[] = \OC::$SERVERROOT.'/lib/private/legacy/'.strtolower(str_replace('_', '/', substr($class, 3)).'.php');
107 107
 		} elseif (strpos($class, 'OCA\\') === 0) {
108 108
 			list(, $app, $rest) = explode('\\', $class, 3);
109 109
 			$app = strtolower($app);
110 110
 			$appPath = \OC_App::getAppPath($app);
111 111
 			if ($appPath && stream_resolve_include_path($appPath)) {
112
-				$paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php');
112
+				$paths[] = $appPath.'/'.strtolower(str_replace('\\', '/', $rest).'.php');
113 113
 				// If not found in the root of the app directory, insert '/lib' after app id and try again.
114
-				$paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php');
114
+				$paths[] = $appPath.'/lib/'.strtolower(str_replace('\\', '/', $rest).'.php');
115 115
 			}
116 116
 		} elseif ($class === 'Test\\TestCase') {
117 117
 			// This File is considered public API, so we make sure that the class
118 118
 			// can still be loaded, although the PSR-4 paths have not been loaded.
119
-			$paths[] = \OC::$SERVERROOT . '/tests/lib/TestCase.php';
119
+			$paths[] = \OC::$SERVERROOT.'/tests/lib/TestCase.php';
120 120
 		}
121 121
 		return $paths;
122 122
 	}
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
 	 */
128 128
 	protected function isValidPath($fullPath) {
129 129
 		foreach ($this->validRoots as $root => $true) {
130
-			if (substr($fullPath, 0, strlen($root) + 1) === $root . '/') {
130
+			if (substr($fullPath, 0, strlen($root) + 1) === $root.'/') {
131 131
 				return true;
132 132
 			}
133 133
 		}
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 			$pathsToRequire = $this->memoryCache->get($class);
147 147
 		}
148 148
 
149
-		if(class_exists($class, false)) {
149
+		if (class_exists($class, false)) {
150 150
 			return false;
151 151
 		}
152 152
 
Please login to merge, or discard this patch.