0

我正在尝试运行一个开源 Web 应用程序。它来自这里 - https://sourceforge.net/projects/labshare-sahara/ 它使用 Zend PHP 框架。

我的问题在于以下代码。它无法找到文件,因此返回 false,并且消息:

“2012-08-20T00:01:08+02:00 DEBUG (7): Unable to find auth class SAHARANS_Auth_Type_Database in include path”

输出到日志文件。


   private function _loadClass($name)
       {
            /* Find the class to load. */
            $file = implode('/', explode('_', $name)) . '.php';
            foreach (explode(PATH_SEPARATOR, get_include_path()) as $path)
            {
                $this->_logger->debug("does $path/$file exist?"); //I added this
                if (file_exists($path . PATH_SEPARATOR . $file))
                {
                    return new $name();
                }
            }

            $this->_logger->debug("Unable to find auth class $name in include path.");
            return false;
        }

为了提供更多上下文,我让它在日志中打印更多信息 - 所以它每次在 for 循环中写入日志。这是输出:

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application/../library/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\library/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application\models/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\institution/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does ./SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\php\PEAR/SAHARANS/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): Unable to find auth class SAHARANS_Auth_Type_Database in include path

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application/../library/Sahara/Auth/Type/Database.php exist?

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\library/Sahara/Auth/Type/Database.php exist

2012-08-20T00:47:07+02:00 DEBUG (7): does C:\xampp\htdocs\Sahara-WI\WI\application\models/Sahara/Auth/Type/Database.php exist?

注意:Database.php 确实存在于 models\Sahara\Auth\Type 目录中!

立即看起来很奇怪的是路径中的“/”和“\”之间的互换,但试图强制反弹(我使用的是 Windows 机器)似乎没有任何影响。

提前致谢!

4

3 回答 3

1

PATH_SEPARATOR 是;在 Windows 上,并且 : 在其他操作系统上。它用于拆分多个路径,而不是(子)目录和文件名。

改变:

if (file_exists($path . PATH_SEPARATOR . $file));

至:

if (file_exists($path . '/' . $file));

这将在任何地方工作。

于 2012-08-19T23:15:16.093 回答
1

那么2件事:

  1. DIRECTORY_SEPARATOR而不用PATH_SEPARATOR
  2. 用于realpath()修复任何路径异常。

如果有人感兴趣,我的自动加载器看起来像这样(虽然不是 ZF):

<?php

namespace Application\Common;
/**
 * Autoloader class
 * This class will be responsible for autoloading of classes.
 */
class Autoloader {

    /** @var string $namespace  Namespace prefix for this instance */
    private $namespace;
    /** @var string $path       Path prefix for this instance */
    private $path;

    /**
     * @param string $namespace
     * @param string $path
     */
    public function __construct($namespace, $path) {
        $this->namespace = ltrim($namespace, "\\");
        $this->path      = rtrim($path, "/\\");
    }

    /**
     * Attempts to load a class.
     *
     * @param string $class The class name, including namespace.
     *
     * @return bool Success or fail based on class existence.
     */
    public function load($class) {
        $class = ltrim($class, "\\");
        //Check to see if class is from correct namespace defined by Autoloader instance.
        if (strpos($class, $this->namespace) === 0) {
            //Explode by namespace parts
            $namespace_parts = explode("\\", $class);
            //Class would be the last element, take it out and return it
            $class             = array_pop($namespace_parts);
            $namespace_parts[] = '';
            /* Build the directory path:
                The base path as defined in Autoloader
                The namespace parts, each a folder
                Parts of the class, separated by an underscore, become folders as well
            */
            $path = $this->path . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $namespace_parts);
            $path .= str_replace("_", DIRECTORY_SEPARATOR, $class) . ".php";
            $path = realpath($path);
            if (file_exists($path)) {
                require $path;
                return true;
            }
        }
        return false;
    }

    /**
     * Register the function.
     */
    public function register() {
        spl_autoload_register(array($this, "load"));
    }

    /**
     * Unregisters the function.
     */
    public function unregister() {
        spl_autoload_unregister(array($this), "load");
    }
}

利用:

//Application being the common namespace prefix.
//__DIR__."Application" being the base directory.
$autoloader = new Application\Common\Autoloader("Application", __DIR__ . "Application");
$autoloader->register();
于 2012-08-19T23:18:36.097 回答
0

你的自动加载器需要在类中实际需要,所以你可能想要:

if (file_exists($path . PATH_SEPARATOR . $file))
{
    require $path . PATH_SEPARATOR . $file;
    return new $name();
}
于 2012-08-20T08:06:38.970 回答