ZF2 Zend框架没有为专辑教程应用程序调用global.php

时间:2013-01-08 04:44:00

标签: php phpunit zend-framework2

我收到错误:Zend \ ServiceManager \ Exception \ ServiceNotCreatedException:创建“Zend \ Db \ Adapter \ Adapter”时引发异常;没有实例返回。

追踪它,我发现Zend \ Db \ Adapter \ Adapter在global.php中显示。但是,global.php永远不会被调用,或至少在module.php getserviceconfig()之前没有被调用,其中错误是跳闸。 testconfig.php.dist和application.config.php都将global.php呈现给侦听器定义。但是,global.php永远不会执行。

有人可以告诉我为什么吗?定义如下。

global.php

<?php
echo PHP_EOL . "Global.php executed." . PHP_EOL;
return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=zf2tutorial;host=album.techmate.com',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);
?>

testconfig.php.dist

<?php

echo PHP_EOL . "TestConfig.php.dist executed." . PHP_EOL;
return array(
    'modules' => array(
        'Album', // <-- Add this line
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
?>

application.config.php

<?php
echo PHP_EOL . "Application.config.php executed." . PHP_EOL;
return array(
    'modules' => array(
        'Album',                  // <-- Add this line
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
?>

module.php只是为了完整......

<?php

namespace Album;

use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\Db\Adapter\Adapter as DbAdapter;

class Module implements ServiceProviderInterface {

    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    // Add this method:
    public function getServiceConfig() {
        return array(
            'factories' => array(
/*                'Zend\db\Adapter\Adapter' => function($sm) {
                    echo PHP_EOL . "SM db-adapter executed." . PHP_EOL;
                    $config = $sm->get('config');
                    $config = $config['db'];
                    $dbAdapter = new DbAdapter($config);
                    return $dbAdapter;
                },*/
                'Album\Model\AlbumTable' => function($sm) {
                    echo PHP_EOL . "SM AlbumTable executed." . PHP_EOL;
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    echo PHP_EOL . "SM AlbumTableGateway executed." . PHP_EOL;
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Album());
                        return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }

}

?>

错误......

TestConfig.php.dist executed.


Module.config.php executed.
PHPUnit 3.7.10 by Sebastian Bergmann.

Configuration read from D:\PHP\zf2-tutorial\module\Album\test\phpunit.xml.dist

......E
SM AlbumTable executed.

SM AlbumTableGateway executed.


Time: 0 seconds, Memory: 8.50Mb

There was 1 error:

1) AlbumTest\Model\AlbumTableTest::testGetAlbumTableReturnsAnInstanceOfAlbumTable
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter

D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:450
D:\PHP\zf2-tutorial\module\Album\Module.php:50
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:726
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:843
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:487
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:442
D:\PHP\zf2-tutorial\module\Album\Module.php:44
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:726
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:843
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:487
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:442
D:\PHP\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:33
D:\PHP\zf2-tutorial\module\Album\test\AlbumTest\Model\AlbumTableTest.php:149

FAILURES!
Tests: 7, Assertions: 9, Errors: 1.

3 个答案:

答案 0 :(得分:2)

根据PHPUnit bootstrapping instructions,在TestConfig.php.dist中看起来似乎以下行可能有误:

'config/autoload/{,*.}{global,local}.php',

将行更改为:

'../../../config/autoload/{,*.}{global,local}.php',

如果您有一个local.php配置文件,它也可能无法加载。

当然,如果目录结构是非标准的,那么相应地更改一行,但你明白了。

答案 1 :(得分:1)

我认为global.php没有自动加载,因为文件的命名不匹配:

'config/autoload/{,*.}{global,local}.php',

因此,如果您将文件命名为myconfig.global.php,则应将其取出。

答案 2 :(得分:1)

我找到了解决这个问题的方法,但是我非常失望。阅读ZF2手册,我发现了一个注释,即global.php和local.php应该由ModuleManager与module.config.php合并。从我看到的,这实际上应该发生在ModuleManagerFactory但是......

无论如何,我手动将global.php和local.php信息添加到module.config.php中。这有效,但同样,它不应该是必要的。

与往常一样,我很乐意得到比这更好的答案。

为了清楚起见,我提供了手动参考。它来自第700页。请注意,它还包含一个调用文件my.global.config.php和my.local.config.php的错误。我确实尝试过这些名字以防万一,但是没有用。

154.5 Default Configuration Options
The following options are available when using the default services configured by the ServiceManagerConfig
and ViewManager.
These configuration directives can go to the config/autoload/{,*.}{global,local}.php files, or in the
module/<module name>/config/module.config.php configuration files. The merging of these configuration
files is done by the ModuleManager. It first merges each module’s module.config.php file, and then
the files in config/autoload (first the *.global.php and then the *.local.php files). The order of the
merge is relevant so you can override a module’s configuration with your application configuration. If you have both
a config/autoload/my.global.config.php and config/autoload/my.local.config.php, the
local configuration file overrides the global configuration.
相关问题