0

ZF2 に実装したサービスのインスタンスを作成しようとしています。

Module.php

namespace ProductImage;

use Zend\Db\ResultSet\ResultSet; use ProductImage\Model\ProductImageStorage; use ProductImage\Service\ProductImageService; use Zend\ModuleManager\Feature\ServiceProviderInterface; use Zend\Db\TableGateway\TableGateway; use ProductImage\Model\ProductImageTable; use ProductImage\Model\ProductImage;

class Module implements ServiceProviderInterface {
    /* Invoked by Module Manager */
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__  => __DIR__ . '/src/' . __NAMESPACE__,
                    'ImageResizer' => __DIR__ . '/src/ImageResizer'
                ),
            ),
        );
    }

    /* Invoked by Module Manager */
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    /* Configure DB Service Managers */
    public function getServiceConfig()
    {
        return array(
            'factories'  => array(
                'ProductImage\Model\ProductImageTable'     => function($sm)
                {
                    $tableGateway = $sm->get('ProductImageTableGateway');
                    $table = new ProductImageTable($tableGateway);
                    return $table;
                },
                'ProductImageTableGateway'                 => function ($sm)
                {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new ProductImage());
                    return new TableGateway('F3G_IMAGES', $dbAdapter, null, $resultSetPrototype);
                }
            ),
            'invokables' => array(
                'productImageService'=> function ($sm)
                {
                    $productImageTable = $sm->get('ProductImageTable\Model\ProductImageTable');
                    return new ProductImageService($productImageTable);
                }
            ),
        );
    } }

ProductImage\Service\ProductImageService

class ProductImageService implements ServiceLocatorAwareInterface
{
    protected $productImageTable;
    protected $services;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->services = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->services;
    }

    function __construct(ProductImageTable $productImageTable)
    {
        $this->productImageTable = $productImageTable;
    }
    // ...
}

スタンドアロンの php ファイル (functions.inc.php) で DI を使用したサービスのインスタンス化

//initialise a standard loader
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/lib/zendframework/library/');
require_once 'Zend/Loader/AutoloaderFactory.php';
use Zend\Loader\AutoloaderFactory;
Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
            'autoregister_zf' => TRUE,
        )
    )
);
$di = new Zend\Di\Di();
$piService = 
$di->get('ProductImage\Service\ProductImageService'); // (< throws Zend\\Di\\Exception\\ClassNotFoundException)
var_export($piService);
die();

$di->get によってスローされるエラー:

[Wed Oct 02 10:37:41 2013] [error] [client 192.168.6.52] PHP Fatal error:  Uncaught exception 'Zend\\Di\\Exception\\ClassNotFoundException' with message 'Class ProductImage\\Service\\ProductImageService could not be located in provided definitions.' in /usr/lib/zendframework/library/Zend/Di/Di.php:264\nStack trace:\n#0 /usr/lib/zendframework/library/Zend/Di/Di.php(227): Zend\\Di\\Di->newInstance('ProductImage\\Se...', Array, true)\n#1 /home/app/public_html/wp-content/themes/manage-product-images/functions.inc.php(34): Zend\\Di\\Di->get('ProductImage\\Se...')\n#2 /home/app/public_html/wp-content/themes/functions.inc.php(14): require_once('/home...')\n#3 /home/app/public_html/wp-content/themes/functions.php(14): require_once('/home...')\n#4 /home/app/public_html/wp-settings.php(293): include('/home...')\n#5 /home/app/public_html/wp-config.php(90): require_once('/home...')\n#6 /home/app/public_html/w in /usr/lib/zendframework/library/Zend/Di/Di.php on line 264

ドキュメント ( http://framework.zend.com/manual/2.1/en/tutorials/quickstart.di.html ) を見ると、DI を構成するにはモジュールに Bootstrap.php が必要なようです。このファイルがなく、どのように見えるべきかわからないので、これを行う別の方法はありますか?

4

0 に答える 0