| | 7 | /** |
|---|
| | 8 | * Interface for class loaders that load classes from foreign namespaces. |
|---|
| | 9 | * |
|---|
| | 10 | * This interface must reside here because the stubClassLoader uses it as type hint. |
|---|
| | 11 | * |
|---|
| | 12 | * @package stubbles |
|---|
| | 13 | */ |
|---|
| | 14 | interface stubForeignClassLoader |
|---|
| | 15 | { |
|---|
| | 16 | /** |
|---|
| | 17 | * sets the namespace where this classloader is responsible for |
|---|
| | 18 | * |
|---|
| | 19 | * @param string $namespace |
|---|
| | 20 | */ |
|---|
| | 21 | public function setNamespace($namespace); |
|---|
| | 22 | |
|---|
| | 23 | /** |
|---|
| | 24 | * returns the namespace where this classloader is responsible for |
|---|
| | 25 | * |
|---|
| | 26 | * @return string |
|---|
| | 27 | */ |
|---|
| | 28 | public function getNamespace(); |
|---|
| | 29 | |
|---|
| | 30 | /** |
|---|
| | 31 | * loads the given class |
|---|
| | 32 | * |
|---|
| | 33 | * @param string $fqClassName the full qualified class name of the class to load |
|---|
| | 34 | * @throws Exception |
|---|
| | 35 | */ |
|---|
| | 36 | public function load($fqClassName); |
|---|
| | 37 | } |
|---|
| | 158 | * registers a foreign class loader |
|---|
| | 159 | * |
|---|
| | 160 | * @param stubForeignClassLoader $foreignClassLoader |
|---|
| | 161 | */ |
|---|
| | 162 | public static function registerForeignClassLoader(stubForeignClassLoader $foreignClassLoader) |
|---|
| | 163 | { |
|---|
| | 164 | self::$foreignClassLoaders[$foreignClassLoader->getNamespace()] = $foreignClassLoader; |
|---|
| | 165 | } |
|---|
| | 166 | |
|---|
| | 167 | /** |
|---|
| | 168 | * checks whether a class resides in a foreign namespace |
|---|
| | 169 | * |
|---|
| | 170 | * @param string $fqClassName the class to check if it is in a foreign namespace |
|---|
| | 171 | * @return string the foreign namespace, null if class is not in any one |
|---|
| | 172 | */ |
|---|
| | 173 | private static function getForeignNamespace($fqClassName) |
|---|
| | 174 | { |
|---|
| | 175 | foreach (self::$foreignClassLoaders as $foreignNamespace => $foreignClassLoader) { |
|---|
| | 176 | if (substr($fqClassName, 0, strlen($foreignNamespace)) == $foreignNamespace) { |
|---|
| | 177 | return $foreignNamespace; |
|---|
| | 178 | } |
|---|
| | 179 | } |
|---|
| | 180 | |
|---|
| | 181 | return null; |
|---|
| | 182 | } |
|---|
| | 183 | |
|---|
| | 184 | /** |
|---|