1
<?php
require_once BLOCKS_ROOT_ENGINE . "Zend/Loader/StandardAutoloader.php";
//require_once BLOCKS_ROOT_ENGINE."library/Zend/Config/Reader/Xml.php";

class Engine {
    static public function start() {
        $autoLoader = new Zend\Loader\StandardAutoloader(array(
            'fallback_autoloader' => true,
        ));
        // register our StandardAutoloader with the SPL autoloader
        $autoLoader->register();


        $config = new Engine_Zend_Config_Reader_Xml();
        //echo $config->config->layout->default->head;
        $root = new Core_Root_Blocks_Root();
    }
}

以上位于Engine/Engine.php

Zend 库位于Engine/Zend/*自动加载器成功找到 Xml 类。但由于某种奇怪的原因,它找不到它实现的接口。我究竟做错了什么。

目录结构

在此处输入图像描述

XML.php

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Config\Reader;

use XMLReader;
use Zend\Config\Exception;

/**
 * XML config reader.
 */
class Xml implements ReaderInterface
{
    /**
     * XML Reader instance.
     *
     * @var XMLReader
     */
    protected $reader;

    /**
     * Directory of the file to process.
     *
     * @var string
     */
    protected $directory;

    /**
     * Nodes to handle as plain text.
     *
     * @var array
     */
    protected $textNodes = array(
        XMLReader::TEXT,
        XMLReader::CDATA,
        XMLReader::WHITESPACE,
        XMLReader::SIGNIFICANT_WHITESPACE
    );

    /**
     * fromFile(): defined by Reader interface.
     *
     * @see    ReaderInterface::fromFile()
     * @param  string $filename
     * @return array
     * @throws Exception\RuntimeException
     */
    public function fromFile($filename)
    {
        if (!is_file($filename) || !is_readable($filename)) {
            throw new Exception\RuntimeException(sprintf(
                "File '%s' doesn't exist or not readable",
                $filename
            ));
        }

        $this->reader = new XMLReader();
        $this->reader->open($filename, null, LIBXML_XINCLUDE);

        $this->directory = dirname($filename);

        set_error_handler(
            function ($error, $message = '', $file = '', $line = 0) use ($filename) {
                throw new Exception\RuntimeException(sprintf(
                    'Error reading XML file "%s": %s',
                    $filename, $message
                ), $error);
            }, E_WARNING
        );
        $return = $this->process();
        restore_error_handler();

        return $return;
    }

    /**
     * fromString(): defined by Reader interface.
     *
     * @see    ReaderInterface::fromString()
     * @param  string $string
     * @return array|bool
     * @throws Exception\RuntimeException
     */
    public function fromString($string)
    {
        if (empty($string)) {
            return array();
        }
        $this->reader = new XMLReader();

        $this->reader->xml($string, null, LIBXML_XINCLUDE);

        $this->directory = null;

        set_error_handler(
            function ($error, $message = '', $file = '', $line = 0) {
                throw new Exception\RuntimeException(sprintf(
                    'Error reading XML string: %s',
                    $message
                ), $error);
            }, E_WARNING
        );
        $return = $this->process();
        restore_error_handler();

        return $return;
    }

    /**
     * Process data from the created XMLReader.
     *
     * @return array
     */
    protected function process()
    {
        return $this->processNextElement();
    }

    /**
     * Process the next inner element.
     *
     * @return mixed
     */
    protected function processNextElement()
    {
        $children = array();
        $text     = '';

        while ($this->reader->read()) {
            if ($this->reader->nodeType === XMLReader::ELEMENT) {
                if ($this->reader->depth === 0) {
                    return $this->processNextElement();
                }

                $attributes = $this->getAttributes();
                $name       = $this->reader->name;

                if ($this->reader->isEmptyElement) {
                    $child = array();
                } else {
                    $child = $this->processNextElement();
                }

                if ($attributes) {
                    if (!is_array($child)) {
                        $child = array();
                    }

                    $child = array_merge($child, $attributes);
                }

                if (isset($children[$name])) {
                    if (!is_array($children[$name]) || !array_key_exists(0, $children[$name])) {
                        $children[$name] = array($children[$name]);
                    }

                    $children[$name][] = $child;
                } else {
                    $children[$name] = $child;
                }
            } elseif ($this->reader->nodeType === XMLReader::END_ELEMENT) {
                break;
            } elseif (in_array($this->reader->nodeType, $this->textNodes)) {
                $text .= $this->reader->value;
            }
        }

        return $children ?: $text;
    }

    /**
     * Get all attributes on the current node.
     *
     * @return array
     */
    protected function getAttributes()
    {
        $attributes = array();

        if ($this->reader->hasAttributes) {
            while ($this->reader->moveToNextAttribute()) {
                $attributes[$this->reader->localName] = $this->reader->value;
            }

            $this->reader->moveToElement();
        }

        return $attributes;
    }
}

错误

在此处输入图像描述

4

3 回答 3

1

我认为您错过了在index.php文件中设置 Zend Framework 库的包含路径。

自动加载器类加载是因为您require_once使用自动加载器类定义添加到确切的文件中。据我了解BLOCKS_ROOT_ENGINE,常量在您的index.php.

另外我猜你添加了某种/opt/local/apache2/htdocs/blocks路径来包含路径index.php- 那是因为自动加载器能够加载Engine_Zend_Config_Reader_Xml类。

有必要BLOCKS_ROOT_ENGINE在您index.php的包含路径列表中添加以允许自动加载器查找Zend\Config\Reader\ReaderInterface

于 2013-06-18T21:47:55.870 回答
0

不确定这是否是你的情况,但有一次我在允许的情况下与我的学生遇到了奇怪的问题。一名学生(具有 root 权限)使用文件夹权限做了一些事情,我们无法找出是什么 - 但该文件夹中的文件无法再被 php 访问。

甚至file_exists()返回 false。我们最终通过复制整个文件夹、删除原始文件夹并用原始名称重命名复制的文件夹来解决问题。

请检查您的权限是否正常。

于 2013-06-19T07:43:27.537 回答
0

好吧,这个问题与我设置了我的自动加载器有关。我下载了skeleton ZF2,发现这个文件对设置很有帮助

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

/**
 * This autoloading setup is really more complicated than it needs to be for most
 * applications. The added complexity is simply to reduce the time it takes for
 * new developers to be productive with a fresh skeleton. It allows autoloading
 * to be correctly configured, regardless of the installation method and keeps
 * the use of composer completely optional. This setup should work fine for
 * most users, however, feel free to configure autoloading however you'd like.
 */

// Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';
}

$zf2Path = false;

if (is_dir('vendor/ZF2/library')) {
    $zf2Path = 'vendor/ZF2/library';
} elseif (getenv('ZF2_PATH')) {      // Support for ZF2_PATH environment variable or git submodule
    $zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value
    $zf2Path = get_cfg_var('zf2_path');
}

if ($zf2Path) {
    if (isset($loader)) {
        $loader->add('Zend', $zf2Path);
    } else {
        include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        Zend\Loader\AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true
            )
        ));
    }
}

if (!class_exists('Zend\Loader\AutoloaderFactory')) {
    throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}
于 2013-06-28T14:30:04.083 回答