Fix autoloader not ignoring vendor prefixed class names

refs #4639
This commit is contained in:
Johannes Meyer 2014-04-09 14:20:05 +02:00
parent 3a29cc34c9
commit f9324032cb

View File

@ -1,30 +1,5 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Application;
@ -40,6 +15,7 @@ class Loader
/**
* List of namespaces
*
* @var array
*/
private $namespaces = array();
@ -54,9 +30,11 @@ class Loader
/**
* Register new namespace for directory
*
* @param string $namespace
* @param string $directory
* @throws \Icinga\Exception\ProgrammingError
*
* @throws ProgrammingError
*/
public function registerNamespace($namespace, $directory)
{
@ -69,7 +47,9 @@ class Loader
/**
* Test if a namespace exists
*
* @param string $namespace
*
* @return bool
*/
public function hasNamespace($namespace)
@ -80,9 +60,10 @@ class Loader
/**
* Class loader
*
* Ignores all but classes in the Icinga namespace.
* Ignores all but classes in registered namespaces.
*
* @param string $class
*
* @return boolean
*/
public function loadClass($class)
@ -91,7 +72,6 @@ class Loader
if ($namespace) {
$file = $this->namespaces[$namespace] . preg_replace('/^' . preg_quote($namespace) . '/', '', $class);
$file = str_replace(self::NAMESPACE_SEPARATOR, '/', $file) . '.php';
if (@file_exists($file)) {
@ -109,6 +89,7 @@ class Loader
* Return is the longest match in the array found
*
* @param string $className
*
* @return bool|string
*/
private function getNamespaceForClass($className)
@ -116,8 +97,10 @@ class Loader
$testNamespace = '';
$testLength = 0;
foreach ($this->namespaces as $namespace => $directory) {
$stub = preg_replace('/^'. preg_quote($namespace). '/', '', $className);
foreach (array_keys($this->namespaces) as $namespace) {
$stub = preg_replace(
'/^' . preg_quote($namespace) . '(' . preg_quote(self::NAMESPACE_SEPARATOR) . '|$)/', '', $className
);
$length = strlen($className) - strlen($stub);
if ($length > $testLength) {
$testLength = $length;