Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / class-loader / ClassLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ClassLoader;
13
14 @trigger_error('The '.__NAMESPACE__.'\ClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.', E_USER_DEPRECATED);
15
16 /**
17  * ClassLoader implements an PSR-0 class loader.
18  *
19  * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
20  *
21  *     $loader = new ClassLoader();
22  *
23  *     // register classes with namespaces
24  *     $loader->addPrefix('Symfony\Component', __DIR__.'/component');
25  *     $loader->addPrefix('Symfony',           __DIR__.'/framework');
26  *
27  *     // activate the autoloader
28  *     $loader->register();
29  *
30  *     // to enable searching the include path (e.g. for PEAR packages)
31  *     $loader->setUseIncludePath(true);
32  *
33  * In this example, if you try to use a class in the Symfony\Component
34  * namespace or one of its children (Symfony\Component\Console for instance),
35  * the autoloader will first look for the class under the component/
36  * directory, and it will then fallback to the framework/ directory if not
37  * found before giving up.
38  *
39  * @author Fabien Potencier <fabien@symfony.com>
40  * @author Jordi Boggiano <j.boggiano@seld.be>
41  *
42  * @deprecated since version 3.3, to be removed in 4.0.
43  */
44 class ClassLoader
45 {
46     private $prefixes = array();
47     private $fallbackDirs = array();
48     private $useIncludePath = false;
49
50     /**
51      * Returns prefixes.
52      *
53      * @return array
54      */
55     public function getPrefixes()
56     {
57         return $this->prefixes;
58     }
59
60     /**
61      * Returns fallback directories.
62      *
63      * @return array
64      */
65     public function getFallbackDirs()
66     {
67         return $this->fallbackDirs;
68     }
69
70     /**
71      * Adds prefixes.
72      *
73      * @param array $prefixes Prefixes to add
74      */
75     public function addPrefixes(array $prefixes)
76     {
77         foreach ($prefixes as $prefix => $path) {
78             $this->addPrefix($prefix, $path);
79         }
80     }
81
82     /**
83      * Registers a set of classes.
84      *
85      * @param string       $prefix The classes prefix
86      * @param array|string $paths  The location(s) of the classes
87      */
88     public function addPrefix($prefix, $paths)
89     {
90         if (!$prefix) {
91             foreach ((array) $paths as $path) {
92                 $this->fallbackDirs[] = $path;
93             }
94
95             return;
96         }
97         if (isset($this->prefixes[$prefix])) {
98             if (is_array($paths)) {
99                 $this->prefixes[$prefix] = array_unique(array_merge(
100                     $this->prefixes[$prefix],
101                     $paths
102                 ));
103             } elseif (!in_array($paths, $this->prefixes[$prefix])) {
104                 $this->prefixes[$prefix][] = $paths;
105             }
106         } else {
107             $this->prefixes[$prefix] = array_unique((array) $paths);
108         }
109     }
110
111     /**
112      * Turns on searching the include for class files.
113      *
114      * @param bool $useIncludePath
115      */
116     public function setUseIncludePath($useIncludePath)
117     {
118         $this->useIncludePath = (bool) $useIncludePath;
119     }
120
121     /**
122      * Can be used to check if the autoloader uses the include path to check
123      * for classes.
124      *
125      * @return bool
126      */
127     public function getUseIncludePath()
128     {
129         return $this->useIncludePath;
130     }
131
132     /**
133      * Registers this instance as an autoloader.
134      *
135      * @param bool $prepend Whether to prepend the autoloader or not
136      */
137     public function register($prepend = false)
138     {
139         spl_autoload_register(array($this, 'loadClass'), true, $prepend);
140     }
141
142     /**
143      * Unregisters this instance as an autoloader.
144      */
145     public function unregister()
146     {
147         spl_autoload_unregister(array($this, 'loadClass'));
148     }
149
150     /**
151      * Loads the given class or interface.
152      *
153      * @param string $class The name of the class
154      *
155      * @return bool|null True, if loaded
156      */
157     public function loadClass($class)
158     {
159         if ($file = $this->findFile($class)) {
160             require $file;
161
162             return true;
163         }
164     }
165
166     /**
167      * Finds the path to the file where the class is defined.
168      *
169      * @param string $class The name of the class
170      *
171      * @return string|null The path, if found
172      */
173     public function findFile($class)
174     {
175         if (false !== $pos = strrpos($class, '\\')) {
176             // namespaced class name
177             $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)).DIRECTORY_SEPARATOR;
178             $className = substr($class, $pos + 1);
179         } else {
180             // PEAR-like class name
181             $classPath = null;
182             $className = $class;
183         }
184
185         $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
186
187         foreach ($this->prefixes as $prefix => $dirs) {
188             if ($class === strstr($class, $prefix)) {
189                 foreach ($dirs as $dir) {
190                     if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
191                         return $dir.DIRECTORY_SEPARATOR.$classPath;
192                     }
193                 }
194             }
195         }
196
197         foreach ($this->fallbackDirs as $dir) {
198             if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
199                 return $dir.DIRECTORY_SEPARATOR.$classPath;
200             }
201         }
202
203         if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {
204             return $file;
205         }
206     }
207 }